panda.dao.sql.expert.HsqldbSqlExpert Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of panda-core Show documentation
Show all versions of panda-core Show documentation
Panda Core is the core module of Panda Framework, it contains commonly used utility classes similar to apache-commons.
package panda.dao.sql.expert;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import panda.dao.DB;
import panda.dao.DaoTypes;
import panda.dao.entity.Entity;
import panda.dao.entity.EntityField;
import panda.dao.query.Query;
import panda.dao.sql.Sql;
import panda.io.Streams;
import panda.lang.Strings;
/**
*/
public class HsqldbSqlExpert extends SqlExpert {
@Override
public DB getDatabaseType() {
return DB.HSQLDB;
}
@Override
public String escape(String s) {
return '"' + Strings.upperCase(s) + '"';
}
@Override
public boolean isSupportDropIfExists() {
return true;
}
@Override
public String dropTable(String tableName) {
return "DROP TABLE " + escapeTable(tableName) + " IF EXISTS";
}
@Override
protected void asTableAlias(Sql sql, String alias) {
super.asTableAlias(sql, Strings.upperCase(alias));
}
@Override
public List create(Entity> entity) {
List sqls = new ArrayList();
StringBuilder sb = new StringBuilder("CREATE TABLE " + escapeTable(client.getTableName(entity)) + " (");
for (EntityField ef : entity.getFields()) {
if (ef.isReadonly()) {
continue;
}
sb.append(Streams.EOL);
sb.append(escapeColumn(ef.getColumn())).append(' ').append(evalFieldType(ef));
// unsupported
// if (ef.isUnsigned()) {
// sb.append(" UNSIGNED");
// }
if (ef.isAutoIncrement()) {
sb.append(" GENERATED BY DEFAULT AS IDENTITY(START WITH ").append(ef.getIdStartWith()).append(")");
}
else {
if (ef.hasDefaultValue()) {
sb.append(" DEFAULT '").append(ef.getDefaultValue()).append('\'');
}
if (ef.isNotNull()) {
sb.append(" NOT NULL");
}
}
sb.append(',');
}
// append primary keys
addPrimaryKeys(sb, entity);
sb.setCharAt(sb.length() - 1, ')');
sqls.add(sb.toString());
// add comments & constraints
addComments(sqls, entity);
addIndexes(sqls, entity);
addForeignKeys(sqls, entity);
return sqls;
}
@Override
protected String evalFieldType(EntityField ef) {
if (Strings.isNotEmpty(ef.getNativeType())) {
return super.evalFieldType(ef);
}
int jdbcType = DaoTypes.getType(ef.getJdbcType());
switch (jdbcType) {
case Types.FLOAT:
return "FLOAT";
case Types.DOUBLE:
return "DOUBLE";
default:
return super.evalFieldType(ef);
}
}
/**
* @param sql sql
* @param query query
* @see http://hsqldb.org/doc/guide/ch09.html#select-section
*/
@Override
protected void limit(Sql sql, Query> query, String alias) {
if (query.getStart() > 0 || query.getLimit() > 0) {
sql.append(" LIMIT ").append(query.getLimit() > 0 ? query.getLimit() : Integer.MAX_VALUE);
if (query.getStart() > 0) {
sql.append(" OFFSET ").append(query.getStart());
}
}
}
}