Search in sources :

Example 1 with Sequence

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()]);
}
Also used : Table(com.github.drinkjava2.jdialects.model.Table) ArrayList(java.util.ArrayList) TableGenerator(com.github.drinkjava2.jdialects.model.TableGenerator) Sequence(com.github.drinkjava2.jdialects.model.Sequence) GlobalIdGenerator(com.github.drinkjava2.jdialects.model.GlobalIdGenerator) FKeyConstraint(com.github.drinkjava2.jdialects.model.FKeyConstraint)

Example 2 with Sequence

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()]);
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) DatabaseMetaData(java.sql.DatabaseMetaData) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ColumnModel(com.github.drinkjava2.jdialects.model.ColumnModel) TableModel(com.github.drinkjava2.jdialects.model.TableModel)

Example 3 with Sequence

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));
}
Also used : DialectException(com.github.drinkjava2.jdialects.DialectException) SortedUUIDGenerator(com.github.drinkjava2.jdialects.id.SortedUUIDGenerator)

Example 4 with Sequence

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);
        }
    }
}
Also used : SequenceIdGenerator(com.github.drinkjava2.jdialects.id.SequenceIdGenerator) HashSet(java.util.HashSet)

Example 5 with Sequence

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() + "\"");
    }
}
Also used : SequenceIdGenerator(com.github.drinkjava2.jdialects.id.SequenceIdGenerator) HashSet(java.util.HashSet)

Aggregations

Sequence (com.github.drinkjava2.jdialects.model.Sequence)3 HashSet (java.util.HashSet)3 SequenceIdGenerator (com.github.drinkjava2.jdialects.id.SequenceIdGenerator)2 FKeyConstraint (com.github.drinkjava2.jdialects.model.FKeyConstraint)2 GlobalIdGenerator (com.github.drinkjava2.jdialects.model.GlobalIdGenerator)2 Table (com.github.drinkjava2.jdialects.model.Table)2 TableGenerator (com.github.drinkjava2.jdialects.model.TableGenerator)2 ArrayList (java.util.ArrayList)2 DialectException (com.github.drinkjava2.jdialects.DialectException)1 SortedUUIDGenerator (com.github.drinkjava2.jdialects.id.SortedUUIDGenerator)1 Column (com.github.drinkjava2.jdialects.model.Column)1 ColumnModel (com.github.drinkjava2.jdialects.model.ColumnModel)1 TableModel (com.github.drinkjava2.jdialects.model.TableModel)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1