Search in sources :

Example 21 with Table

use of com.github.drinkjava2.jdialects.model.Table in project jDialects by drinkjava2.

the class DDLDropUtils method transferTableToObjectList.

/**
 * Transfer table to a mixed DDL String or TableGen Object list
 */
private static void transferTableToObjectList(Dialect dialect, TableModel t, List<Object> objectResultList) {
    StringBuilder buf = new StringBuilder();
    String tableName = t.getTableName();
    List<ColumnModel> columns = t.getColumns();
    // Reserved words check
    dialect.checkNotEmptyReservedWords(tableName, "Table name can not be empty");
    // check index names
    List<IndexModel> l = t.getIndexConsts();
    if (l != null && !l.isEmpty())
        for (IndexModel index : l) dialect.checkReservedWords(index.getName());
    // check unique names
    List<UniqueModel> l2 = t.getUniqueConsts();
    if (l2 != null && !l2.isEmpty())
        for (UniqueModel unique : l2) dialect.checkReservedWords(unique.getName());
    // check Fkey names
    List<FKeyModel> fkeyChks = t.getFkeyConstraints();
    if (fkeyChks != null && !fkeyChks.isEmpty())
        for (FKeyModel fkey : fkeyChks) dialect.checkReservedWords(fkey.getFkeyName());
    for (ColumnModel col : columns) dialect.checkNotEmptyReservedWords(col.getColumnName(), "Column name can not be empty");
    // idGenerator
    for (IdGenerator idGen : t.getIdGenerators()) objectResultList.add(idGen);
    // Foreign key
    for (FKeyModel fkey : t.getFkeyConstraints()) objectResultList.add(fkey);
    // drop table
    buf.append(dialect.dropTableDDL(tableName));
    objectResultList.add(buf.toString());
}
Also used : ColumnModel(com.github.drinkjava2.jdialects.model.ColumnModel) TableIdGenerator(com.github.drinkjava2.jdialects.id.TableIdGenerator) AutoIdGenerator(com.github.drinkjava2.jdialects.id.AutoIdGenerator) SequenceIdGenerator(com.github.drinkjava2.jdialects.id.SequenceIdGenerator) IdGenerator(com.github.drinkjava2.jdialects.id.IdGenerator) IndexModel(com.github.drinkjava2.jdialects.model.IndexModel) UniqueModel(com.github.drinkjava2.jdialects.model.UniqueModel) FKeyModel(com.github.drinkjava2.jdialects.model.FKeyModel)

Example 22 with Table

use of com.github.drinkjava2.jdialects.model.Table in project jDialects by drinkjava2.

the class IdgeneratorTest method testSortedUUIDGenerator.

@Test
public void testSortedUUIDGenerator() {
    TableModel table = new TableModel("testSortedUUIDGenerator");
    table.sortedUUIDGenerator("sorteduuid", 8, 8);
    table.addGenerator(new SortedUUIDGenerator("sorteduuid2", 10, 10));
    table.column("id").STRING(30).pkey().idGenerator("sorteduuid");
    table.column("id2").STRING(30).pkey().idGenerator("sorteduuid2");
    reBuildDB(table);
    IdGenerator gen1 = table.getIdGenerator("sorteduuid");
    for (int i = 0; i < 10; i++) System.out.println(gen1.getNextID(dbPro, guessedDialect, null));
    IdGenerator gen2 = table.getIdGenerator("sorteduuid2");
    for (int i = 0; i < 10; i++) System.out.println(gen2.getNextID(dbPro, guessedDialect, null));
}
Also used : SortedUUIDGenerator(com.github.drinkjava2.jdialects.id.SortedUUIDGenerator) AutoIdGenerator(com.github.drinkjava2.jdialects.id.AutoIdGenerator) IdGenerator(com.github.drinkjava2.jdialects.id.IdGenerator) TableModel(com.github.drinkjava2.jdialects.model.TableModel) Test(org.junit.Test)

Example 23 with Table

use of com.github.drinkjava2.jdialects.model.Table in project jDialects by drinkjava2.

the class IdgeneratorTest method testAutoIdGenerator.

@Test
public void testAutoIdGenerator() {
    TableModel table = new TableModel("testAutoIdGenerator");
    table.column("id").STRING(30).pkey().autoId();
    reBuildDB(table);
    IdGenerator gen = table.getColumn("id").getIdGenerator();
    for (int i = 0; i < 5; i++) System.out.println(gen.getNextID(dbPro, guessedDialect, null));
    gen = AutoIdGenerator.INSTANCE;
    for (int i = 0; i < 5; i++) System.out.println(gen.getNextID(dbPro, guessedDialect, null));
}
Also used : AutoIdGenerator(com.github.drinkjava2.jdialects.id.AutoIdGenerator) IdGenerator(com.github.drinkjava2.jdialects.id.IdGenerator) TableModel(com.github.drinkjava2.jdialects.model.TableModel) Test(org.junit.Test)

Example 24 with Table

use of com.github.drinkjava2.jdialects.model.Table in project jDialects by drinkjava2.

the class IdgeneratorTest method testIdentityGenerator.

@Test
public void testIdentityGenerator() {
    TableModel table = new TableModel("testIdentity");
    table.column("id").INTEGER().identityId();
    table.column("name").STRING(30);
    reBuildDB(table);
    dbPro.nExecute("insert into testIdentity (name) values(?)", "Tom");
    dbPro.nExecute("insert into testIdentity (name) values(?)", "Sam");
    IdGenerator idGen = table.getIdGenerator(GenerationType.IDENTITY);
    System.out.println(idGen.getNextID(dbPro, guessedDialect, Type.INTEGER));
    idGen = table.getColumn("id").getIdGenerator();
    System.out.println(idGen.getNextID(dbPro, guessedDialect, Type.INTEGER));
}
Also used : AutoIdGenerator(com.github.drinkjava2.jdialects.id.AutoIdGenerator) IdGenerator(com.github.drinkjava2.jdialects.id.IdGenerator) TableModel(com.github.drinkjava2.jdialects.model.TableModel) Test(org.junit.Test)

Example 25 with Table

use of com.github.drinkjava2.jdialects.model.Table in project jDialects by drinkjava2.

the class DDLCreateUtils method checkAndInsertToNotRepeatTable.

/**
 * if name not found, add <br/>
 * If name same, but other fields different, throw exception </br>
 * If name same, and other field same, ignore </br>
 */
protected static void checkAndInsertToNotRepeatTable(Set<TableIdGenerator> notRepeatedSeq, TableIdGenerator tab) {
    DialectException.assureNotEmpty(tab.getName(), "TableGen name can not be empty");
    DialectException.assureNotEmpty(tab.getTable(), "TableGen tableName can not be empty of \"" + tab.getName() + "\"");
    DialectException.assureNotEmpty(tab.getPkColumnName(), "TableGen pkColumnName can not be empty of \"" + tab.getName() + "\"");
    DialectException.assureNotEmpty(tab.getPkColumnValue(), "TableGen pkColumnValue can not be empty of \"" + tab.getName() + "\"");
    DialectException.assureNotEmpty(tab.getValueColumnName(), "TableGen valueColumnName can not be empty of \"" + tab.getName() + "\"");
    boolean canAdd = true;
    for (TableIdGenerator not : notRepeatedSeq) {
        if (tab.getName().equals(not.getName())) {
            canAdd = false;
            if (!(tab.getTable().equals(not.getTable()) && tab.getPkColumnName().equals(not.getPkColumnName()) && tab.getPkColumnValue().equals(not.getPkColumnValue()) && tab.getValueColumnName().equals(not.getValueColumnName()) && tab.getInitialValue().equals(not.getInitialValue()) && tab.getAllocationSize().equals(not.getAllocationSize())))
                throw new DialectException("In one or more tableModel, duplicated TableIdGenerator name '" + tab.getName() + "' but different value of table/pKColumnName/pkColumnValue/valueColumnName/initialValue/allocationSize setting");
        }
    }
    if (canAdd)
        notRepeatedSeq.add(tab);
}
Also used : TableIdGenerator(com.github.drinkjava2.jdialects.id.TableIdGenerator)

Aggregations

Table (com.github.drinkjava2.jdialects.model.Table)18 Test (org.junit.Test)10 TableModel (com.github.drinkjava2.jdialects.model.TableModel)9 ColumnModel (com.github.drinkjava2.jdialects.model.ColumnModel)8 AutoIdGenerator (com.github.drinkjava2.jdialects.id.AutoIdGenerator)7 IdGenerator (com.github.drinkjava2.jdialects.id.IdGenerator)7 TableIdGenerator (com.github.drinkjava2.jdialects.id.TableIdGenerator)6 FKeyModel (com.github.drinkjava2.jdialects.model.FKeyModel)6 SequenceIdGenerator (com.github.drinkjava2.jdialects.id.SequenceIdGenerator)4 ArrayList (java.util.ArrayList)4 Dialect (com.github.drinkjava2.jdialects.Dialect)3 FKeyConstraint (com.github.drinkjava2.jdialects.model.FKeyConstraint)3 UniqueModel (com.github.drinkjava2.jdialects.model.UniqueModel)3 DB2400Dialect (com.github.drinkjava2.jdialects.Dialect.DB2400Dialect)2 DB2Dialect (com.github.drinkjava2.jdialects.Dialect.DB2Dialect)2 DerbyDialect (com.github.drinkjava2.jdialects.Dialect.DerbyDialect)2 DerbyTenFiveDialect (com.github.drinkjava2.jdialects.Dialect.DerbyTenFiveDialect)2 DerbyTenSevenDialect (com.github.drinkjava2.jdialects.Dialect.DerbyTenSevenDialect)2 DerbyTenSixDialect (com.github.drinkjava2.jdialects.Dialect.DerbyTenSixDialect)2 H2Dialect (com.github.drinkjava2.jdialects.Dialect.H2Dialect)2