org.dbtools.renderer.HSQLDBRenderer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dbtools-gen Show documentation
Show all versions of dbtools-gen Show documentation
DBTools ORM Class Generator.
/*
* SchemaRenderer.java
*
* Created on February 23, 2002
*
* Copyright 2006 Jeff Campbell. All rights reserved. Unauthorized reproduction
* is a violation of applicable law. This material contains certain
* confidential or proprietary information and trade secrets of Jeff Campbell.
*/
/*
* http://hsqldb.org/web/hsqlDocsFrame.html
*/
package org.dbtools.renderer;
import org.dbtools.schema.ForeignKey;
import org.dbtools.schema.schemafile.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author Jeff Campbell
*/
public class HSQLDBRenderer extends SchemaRenderer {
public static final String RENDERER_NAME = "hsqldb";
public HSQLDBRenderer() {
super();
this.setDbVendorName(RENDERER_NAME);
}
@Override
public String generateSchema(SchemaDatabase database, String[] tablesToGenerate, String[] viewsToGenerate, boolean dropTables, boolean createInserts) {
showProgress("Generating SQL schema using HSQLDB renderer ...", true);
StringBuilder schema = new StringBuilder();
List foreignKeysToCreate = new ArrayList();
List uniqueFields = null;
List requestedTables = getTablesToGenerate(database, tablesToGenerate);
List requestedViews = getViewsToGenerate(database, viewsToGenerate);
// drop schema
if (dropTables) {
generateDropSchema(true, true, schema, requestedTables, requestedViews);
}
// create tables
for (SchemaTable table : requestedTables) {
// reset values for new table
List indexFields = new ArrayList();
uniqueFields = new ArrayList();
String tableType = table.getParameter("tableType");
if (tableType == null || tableType.equals("")) {
tableType = "CACHED"; // cached table type saved data to disk
}
// add table header
schema.append("CREATE ").append(tableType).append(" TABLE ");
schema.append(table.getName());
schema.append(" (\n");
// add fields
List fields = table.getFields();
SchemaTableField enumPKField = null;
SchemaTableField enumValueField = null;
for (int j = 0; j < fields.size(); j++) {
SchemaTableField field = fields.get(j);
// add field
// name
schema.append("\t");
schema.append(field.getName());
// datatype
schema.append(" ");
schema.append(getSqlType(field.getJdbcDataType()));
//check for size for datatype
if (field.getSize() > 0) {
schema.append("(");
schema.append(field.getSize());
// check for decimals (ignore decimals)
// if(field.getDecimals() > 0) {
// schema.append(", ").append(field.getDecimals());
// }
schema.append(")");
}
String defaultValue = field.getDefaultValue();
if (defaultValue != null && !defaultValue.equals("")) {
schema.append(" DEFAULT ").append(formatDefaultValue(field));
/*
if (defaultValue.toUpperCase().equals("NULL")) {
schema.append(" DEFAULT NULL");
} else {
schema.append(" DEFAULT '");
schema.append(defaultValue);
schema.append("'");
}
*/
}
// not null
if (field.isNotNull() && !field.isIncrement()) {
schema.append(" NOT NULL");
}
if (field.isIncrement()) {
schema.append(" GENERATED BY DEFAULT AS IDENTITY (START WITH ").append(field.getIncrementInitialValue()).append(")");
}
if (field.isPrimaryKey()) {
schema.append(" PRIMARY KEY");
}
if (field.isUnique()) {
uniqueFields.add(field);
}
if (field.isIndex()) {
indexFields.add(field);
}
// add foreign key
if (!field.getForeignKeyField().equals("")) {
foreignKeysToCreate.add(new ForeignKey(table.getName(), field.getName(), field.getForeignKeyTable(), field.getForeignKeyField()));
}
schema.append("");
// if this is the last one, then don't put a ','
if (j == fields.size() - 1) {
// add unique fields
if (uniqueFields.size() > 0) {
schema.append(",\n\tUNIQUE(");
for (int k = 0; k < uniqueFields.size(); k++) {
SchemaTableField uField = (SchemaTableField) uniqueFields.get(k);
if (k != 0) {
schema.append(", ");
}
schema.append(uField.getName());
}
schema.append(")");
}
// add forengn keys fields
// if (foreignKeyFields.size() > 0) {
// for (int k = 0; k < foreignKeyFields.size(); k++) {
// SchemaField foreignKeyField = (SchemaField) foreignKeyFields.get(k);
// schema.append(",\n\tFOREIGN KEY (");
// schema.append(foreignKeyField.getName()).append(") REFERENCES ").append(foreignKeyField.getForeignKeyTable()).append(" (").append(foreignKeyField.getForeignKeyField()).append(")");
// }
// }
} else {
// more fields to come...
schema.append(",\n");
}
// check for enumFields
if (enumPKField == null && field.isPrimaryKey()) {
enumPKField = field;
}
if (enumValueField == null && field.getJdbcDataType() == SchemaFieldType.VARCHAR) {
enumValueField = field;
}
}
// check for uniqueDeclarations
generateUniqueConstraints(schema, table);
// check for indexDeclarations
generateIndexes(schema, table);
// add table footer
schema.append("\n);");
// create indexes
for (SchemaTableField indexField : indexFields) {
schema.append("\nCREATE INDEX ").append(table.getName()).append(indexField.getName()).append("_IDX ON ").append(table.getName()).append(" (").append(indexField.getName()).append(");");
}
schema.append("\n\n");
generateEnumSchema(schema, table, getAlreadyCreatedEnum(), enumPKField, enumValueField, createInserts);
} // end of tables
// create foreign keys
for (ForeignKey fk : foreignKeysToCreate) {
schema.append("ALTER TABLE ").append(fk.getPrimaryKeyTable()).append("\n");
schema.append("ADD CONSTRAINT ").append(fk.getPrimaryKeyTable()).append("_").append(fk.getPrimaryKeyField()).append("_FK\n");
schema.append("FOREIGN KEY (").append(fk.getPrimaryKeyField()).append(")\n");
schema.append("REFERENCES ").append(fk.getForeignKeyTable()).append(" (").append(fk.getForeignKeyField()).append(");\n");
schema.append("\n");
}
// create views
// for (SchemaView view : requestedViews) {
// // header
// schema.append("CREATE VIEW ").append(view.getName()).append(" AS \n");
//
// // SELECT
// schema.append(" SELECT \n");
//
// Iterator vfItr = view.getViewFields().iterator();
// while (vfItr.hasNext()) {
// SchemaViewField viewField = (SchemaViewField) vfItr.next();
//
// schema.append("\t").append(viewField.getExpression()).append(" ").append(viewField.getName());
//
// if (vfItr.hasNext()) {
// schema.append(",\n");
// } else {
// schema.append("\n");
// }
// }
//
// schema.append(" ").append(view.getViewPostSelectClause()).append(";");
//
// // end
// schema.append("\n\n");
// } // end of views
// return
return schema.toString();
}
@Override
public String formatDefaultValue(SchemaTableField field) {
String defaultValue = field.getDefaultValue();
String newDefaultValue;
Class javaType = field.getJavaClassType();
if (javaType == boolean.class) {
if (defaultValue.toUpperCase().equals("TRUE") || defaultValue.equals("1")) {
newDefaultValue = "'true'";
} else {
newDefaultValue = "'false'";
}
} else {
newDefaultValue = super.formatDefaultValue(field);
}
return newDefaultValue;
}
}