Search in sources :

Example 41 with TableModel

use of com.github.drinkjava2.jdialects.model.TableModel 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 42 with TableModel

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

the class IdgeneratorTest method testSequenceIdGenerator.

@Test
public void testSequenceIdGenerator() {
    if (!guessedDialect.getDdlFeatures().supportBasicOrPooledSequence())
        return;
    TableModel table1 = new TableModel("testTableIdGenerator");
    table1.sequenceGenerator("seq1", "seq1", 1, 10);
    table1.column("id").STRING(30).pkey().idGenerator("seq1");
    table1.column("id2").STRING(30).pkey().sequenceGenerator("seq2", "seq2", 1, 20);
    TableModel table2 = new TableModel("testTableIdGenerator2");
    table2.sequenceGenerator("seq3", "seq3", 1, 10);
    table2.column("id").STRING(30).pkey().idGenerator("seq3");
    table2.column("id2").STRING(30).pkey().sequenceGenerator("seq2", "seq2", 1, 20);
    reBuildDB(table1, table2);
    IdGenerator gen1 = table1.getIdGenerator("seq1");
    IdGenerator gen2 = table1.getIdGenerator("seq2");
    for (int i = 0; i < 3; i++) {
        System.out.println(gen1.getNextID(dbPro, guessedDialect, null));
        System.out.println(gen2.getNextID(dbPro, guessedDialect, null));
    }
    IdGenerator gen3 = table2.getIdGenerator("seq3");
    IdGenerator gen4 = table2.getIdGenerator("seq2");
    for (int i = 0; i < 3; i++) {
        System.out.println(gen3.getNextID(dbPro, guessedDialect, null));
        System.out.println(gen4.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 43 with TableModel

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

the class IdgeneratorTest method testTableIdGenerator.

@Test
public void testTableIdGenerator() {
    TableModel table1 = new TableModel("testTableIdGenerator");
    table1.tableGenerator("tab1", "tb1", "pkCol", "valueColname", "pkColVal", 1, 10);
    table1.column("id").STRING(30).pkey().idGenerator("tab1");
    table1.column("id2").STRING(30).pkey().tableGenerator("tab2", "tb1", "pkCol", "valueColname", "pkColVal", 1, 10);
    TableModel table2 = new TableModel("testTableIdGenerator2");
    table2.tableGenerator("tab3", "tb1", "pkCol", "valueColname", "pkColVal", 1, 10);
    table2.column("id").STRING(30).pkey().idGenerator("tab3");
    table2.column("id2").STRING(30).pkey().tableGenerator("tab2", "tb1", "pkCol", "valueColname", "pkColVal", 1, 10);
    reBuildDB(table1, table2);
    IdGenerator gen1 = table1.getIdGenerator("tab1");
    IdGenerator gen2 = table1.getIdGenerator("tab2");
    for (int i = 0; i < 3; i++) {
        System.out.println(gen1.getNextID(dbPro, guessedDialect, null));
        System.out.println(gen2.getNextID(dbPro, guessedDialect, null));
    }
    IdGenerator gen3 = table2.getIdGenerator("tab3");
    IdGenerator gen4 = table2.getIdGenerator("tab2");
    for (int i = 0; i < 3; i++) {
        System.out.println(gen3.getNextID(dbPro, guessedDialect, null));
        System.out.println(gen4.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 44 with TableModel

use of com.github.drinkjava2.jdialects.model.TableModel 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)

Example 45 with TableModel

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

the class DDLCreateUtils method buildUniqueDLL.

private static void buildUniqueDLL(Dialect dialect, List<Object> objectResultList, TableModel t) {
    List<UniqueModel> l = t.getUniqueConsts();
    if (l == null || l.isEmpty())
        return;
    String dialectName = "" + dialect;
    for (UniqueModel unique : l) {
        boolean nullable = false;
        String[] columns = unique.getColumnList();
        for (String colNames : columns) {
            ColumnModel vc = t.getColumn(colNames.toLowerCase());
            if (vc != null && vc.getNullable())
                nullable = true;
        }
        String uniqueName = unique.getName();
        if (StrUtils.isEmpty(uniqueName))
            uniqueName = "UK_" + t.getTableName() + "_" + StrUtils.arrayToString(unique.getColumnList(), "_");
        String template = "alter table $TABLE add constraint $UKNAME unique ($COLUMNS)";
        if ((// DB2 and
        StrUtils.startsWithIgnoreCase(dialectName, "DB2") || // DERBY
        StrUtils.startsWithIgnoreCase(dialectName, "DERBY")) && nullable)
            template = "create unique index $UKNAME on $TABLE ($COLUMNS)";
        else if (StrUtils.startsWithIgnoreCase(dialectName, "Informix"))
            template = "alter table $TABLE add constraint unique ($COLUMNS) constraint $UKNAME";
        String result = StrUtils.replace(template, "$TABLE", t.getTableName());
        result = StrUtils.replace(result, "$UKNAME", uniqueName);
        result = StrUtils.replace(result, "$COLUMNS", StrUtils.arrayToString(unique.getColumnList()));
        objectResultList.add(result);
    }
}
Also used : ColumnModel(com.github.drinkjava2.jdialects.model.ColumnModel) UniqueModel(com.github.drinkjava2.jdialects.model.UniqueModel)

Aggregations

TableModel (com.github.drinkjava2.jdialects.model.TableModel)36 Test (org.junit.Test)19 ColumnModel (com.github.drinkjava2.jdialects.model.ColumnModel)11 AutoIdGenerator (com.github.drinkjava2.jdialects.id.AutoIdGenerator)9 IdGenerator (com.github.drinkjava2.jdialects.id.IdGenerator)9 SequenceIdGenerator (com.github.drinkjava2.jdialects.id.SequenceIdGenerator)5 TableIdGenerator (com.github.drinkjava2.jdialects.id.TableIdGenerator)5 FKeyModel (com.github.drinkjava2.jdialects.model.FKeyModel)5 IndexModel (com.github.drinkjava2.jdialects.model.IndexModel)3 UniqueModel (com.github.drinkjava2.jdialects.model.UniqueModel)3 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 Dialect (com.github.drinkjava2.jdialects.Dialect)2 Connection (java.sql.Connection)2 Map (java.util.Map)2 SortedUUIDGenerator (com.github.drinkjava2.jdialects.id.SortedUUIDGenerator)1 Table (com.github.drinkjava2.jdialects.model.Table)1 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 BeanInfo (java.beans.BeanInfo)1 PropertyDescriptor (java.beans.PropertyDescriptor)1