use of com.github.drinkjava2.jdialects.model.Sequence in project jDialects by drinkjava2.
the class DDLUtils method toCreateDDLwithoutFormat.
/**
* Transfer table to DDL by given dialect and without format it
*/
public static String[] toCreateDDLwithoutFormat(Dialect dialect, Table... tables) {
// resultList store mixed DDL String + TableGenerator + Sequence
List<Object> objectResultList = new ArrayList<>();
for (Table table : tables) transferTableToObjectList(dialect, table, objectResultList);
List<String> stringResultList = new ArrayList<>();
List<TableGenerator> tbGeneratorList = new ArrayList<>();
List<Sequence> sequenceList = new ArrayList<>();
List<GlobalIdGenerator> globalIdGeneratorList = new ArrayList<>();
List<FKeyConstraint> fKeyConstraintList = new ArrayList<>();
for (Object ddl : objectResultList) {
if (!StrUtils.isEmpty(ddl)) {
if (ddl instanceof String)
stringResultList.add((String) ddl);
else if (ddl instanceof TableGenerator)
tbGeneratorList.add((TableGenerator) ddl);
else if (ddl instanceof Sequence)
sequenceList.add((Sequence) ddl);
else if (ddl instanceof GlobalIdGenerator)
globalIdGeneratorList.add((GlobalIdGenerator) ddl);
else if (ddl instanceof FKeyConstraint)
fKeyConstraintList.add((FKeyConstraint) ddl);
}
}
buildSequenceDDL(dialect, stringResultList, sequenceList);
buildTableGeneratorDDL(dialect, stringResultList, tbGeneratorList);
buildGolbalIDGeneratorDDL(dialect, stringResultList, globalIdGeneratorList);
buildFKeyConstraintDDL(dialect, stringResultList, fKeyConstraintList);
return stringResultList.toArray(new String[stringResultList.size()]);
}
use of com.github.drinkjava2.jdialects.model.Sequence in project jDialects by drinkjava2.
the class TableModelUtilsOfDb method db2Model.
/**
* Convert JDBC connected database structure to TableModels, note: <br/>
* 1)This method does not close connection <br/>
* 2)This method does not support sequence, foreign keys, primary keys...,
* but will improve later.
*/
public static TableModel[] db2Model(Connection con, Dialect dialect) {
// NOSONAR
List<String> tableNames = new ArrayList<String>();
List<TableModel> tableModels = new ArrayList<TableModel>();
SQLException sqlException = null;
ResultSet rs = null;
PreparedStatement pst = null;
try {
DatabaseMetaData meta = con.getMetaData();
if (dialect.isOracleFamily()) {
// NOSONAR
pst = con.prepareStatement("SELECT TABLE_NAME FROM USER_TABLES");
rs = pst.executeQuery();
while (rs.next()) tableNames.add(rs.getString(TABLE_NAME));
rs.close();
pst.close();
// } else if (dialect.isSQLServerFamily()) {
// pst = con.prepareStatement("select name from sysobjects where
// xtype='U'");
// rs = pst.executeQuery();
// while (rs.next())
// tableNames.add(rs.getString(TABLE_NAME));
// rs.close();
// pst.close();
} else {
rs = meta.getTables(null, null, null, new String[] { "TABLE" });
while (rs.next()) tableNames.add(rs.getString(TABLE_NAME));
rs.close();
}
for (String dbTableName : tableNames) {
rs = con.getMetaData().getColumns(null, null, dbTableName, null);
TableModel oneTable = new TableModel(dbTableName);
while (rs.next()) {
// NOSONAR
String colName = rs.getString("COLUMN_NAME");
oneTable.column(colName);
ColumnModel col = oneTable.getColumn(colName);
int javaSqlType = rs.getInt("DATA_TYPE");
try {
col.setColumnType(TypeUtils.javaSqlTypeToDialectType(javaSqlType));
} catch (Exception e1) {
throw new DialectException("jDialect does not supported java.sql.types value " + javaSqlType, e1);
}
col.setLength(rs.getInt("COLUMN_SIZE"));
col.setNullable(rs.getInt("NULLABLE") > 0);
col.setPrecision(rs.getInt("DECIMAL_DIGITS"));
try {
if (((Boolean) (true)).equals(rs.getBoolean("IS_AUTOINCREMENT")))
col.identityId();
} catch (Exception e) {
}
try {
if ("YES".equalsIgnoreCase(rs.getString("IS_AUTOINCREMENT")))
col.identityId();
} catch (Exception e) {
}
}
tableModels.add(oneTable);
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
sqlException = e;
} finally {
if (pst != null)
try {
pst.close();
} catch (SQLException e1) {
if (sqlException != null)
sqlException.setNextException(e1);
else
sqlException = e1;
}
try {
if (rs != null)
rs.close();
} catch (SQLException e2) {
if (sqlException != null)
sqlException.setNextException(e2);
else
sqlException = e2;
}
}
if (sqlException != null)
throw new DialectException(sqlException);
return tableModels.toArray(new TableModel[tableModels.size()]);
}
use of com.github.drinkjava2.jdialects.model.Sequence in project jDialects by drinkjava2.
the class TableModel method sortedUUIDGenerator.
/**
* Add a Sequence Generator, note: not all database support sequence
*/
public void sortedUUIDGenerator(String name, Integer sortedLength, Integer uuidLength) {
DialectException.assureNotNull(name);
if (this.getIdGenerator(GenerationType.SORTED_UUID, name) != null)
throw new DialectException("Duplicated sortedUUIDGenerator name '" + name + "'");
idGenerators.add(new SortedUUIDGenerator(name, sortedLength, uuidLength));
}
use of com.github.drinkjava2.jdialects.model.Sequence in project jDialects by drinkjava2.
the class DDLCreateUtils method buildSequenceDDL.
private static void buildSequenceDDL(Dialect dialect, List<String> stringList, List<SequenceIdGenerator> sequenceList) {
Set<SequenceIdGenerator> notRepeatedSequences = new HashSet<SequenceIdGenerator>();
for (SequenceIdGenerator seq : sequenceList) checkAndInsertToNotRepeatSeq(notRepeatedSequences, seq);
DDLFeatures features = dialect.ddlFeatures;
for (SequenceIdGenerator seq : notRepeatedSequences) {
if (!features.supportBasicOrPooledSequence()) {
DialectException.throwEX("Dialect \"" + dialect + "\" does not support sequence setting on sequence \"" + seq.getName() + "\"");
}
if (features.supportsPooledSequences) {
// create sequence _SEQ start with 11 increment by 33
String pooledSequence = StrUtils.replace(features.createPooledSequenceStrings, "_SEQ", seq.getSequenceName());
pooledSequence = StrUtils.replace(pooledSequence, "11", "" + seq.getInitialValue());
pooledSequence = StrUtils.replace(pooledSequence, "33", "" + seq.getAllocationSize());
stringList.add(pooledSequence);
} else {
if (seq.getInitialValue() >= 2 || seq.getAllocationSize() >= 2)
DialectException.throwEX("Dialect \"" + dialect + "\" does not support initialValue and allocationSize setting on sequence \"" + seq.getName() + "\", try set initialValue and allocationSize to 1 to fix");
// "create sequence _SEQ"
String simepleSeq = StrUtils.replace(features.createSequenceStrings, "_SEQ", seq.getSequenceName());
stringList.add(simepleSeq);
}
}
}
use of com.github.drinkjava2.jdialects.model.Sequence in project jDialects by drinkjava2.
the class DDLDropUtils method buildDropSequenceDDL.
private static void buildDropSequenceDDL(Dialect dialect, List<String> stringResultList, List<SequenceIdGenerator> sequenceList) {
Set<SequenceIdGenerator> notRepeatedSequences = new HashSet<SequenceIdGenerator>();
for (SequenceIdGenerator seq : sequenceList) DDLCreateUtils.checkAndInsertToNotRepeatSeq(notRepeatedSequences, seq);
DDLFeatures features = dialect.ddlFeatures;
for (SequenceIdGenerator seq : notRepeatedSequences) {
if (!features.supportBasicOrPooledSequence()) {
DialectException.throwEX("Dialect \"" + dialect + "\" does not support sequence setting on sequence \"" + seq.getName() + "\"");
}
if (!DDLFeatures.NOT_SUPPORT.equals(features.dropSequenceStrings) && !StrUtils.isEmpty(features.dropSequenceStrings)) {
stringResultList.add(0, StrUtils.replace(features.dropSequenceStrings, "_SEQNAME", seq.getSequenceName()));
} else
DialectException.throwEX("Dialect \"" + dialect + "\" does not support drop sequence ddl, on sequence \"" + seq.getName() + "\"");
}
}
Aggregations