Search in sources :

Example 6 with Dialect

use of com.github.drinkjava2.jdialects.Dialect in project jDialects by drinkjava2.

the class TestBase method printAllDialectsDDLs.

protected static void printAllDialectsDDLs(TableModel... tables) {
    Dialect[] diaList = Dialect.values();
    for (Dialect dialect : diaList) {
        System.out.println("======" + dialect + "=====");
        try {
            String[] ddls = dialect.toDropAndCreateDDL(tables);
            printDDLs(ddls);
        // printDDLs(DDLFormatter.format(ddls));
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exception found: " + e.getMessage());
        }
    }
}
Also used : Dialect(com.github.drinkjava2.jdialects.Dialect)

Example 7 with Dialect

use of com.github.drinkjava2.jdialects.Dialect in project jDialects by drinkjava2.

the class DialectTest method testPagination2.

@Test
public void testPagination2() {
    Dialect[] dialects = Dialect.values();
    for (Dialect dialect : dialects) {
        System.out.println("=========" + dialect + "==========");
        String result = "";
        try {
            result = dialect.pagin(1, 10, sql3);
            System.out.println(result);
        } catch (DialectException e) {
            System.out.println("Error:" + e.getMessage());
        }
        Assert.assertFalse(result.contains("$"));
        Assert.assertTrue(StrUtils.isEmpty(result) || 1 == StrUtils.countMatches(result, '?'));
        try {
            result = dialect.pagin(3, 10, sql4);
            System.out.println(result);
        } catch (DialectException e) {
            System.out.println("Error:" + e.getMessage());
        }
        Assert.assertFalse(result.contains("$"));
        Assert.assertTrue(StrUtils.isEmpty(result) || 1 == StrUtils.countMatches(result, '?'));
        try {
            result = dialect.pagin(1, 10, sql3);
            System.out.println(result);
        } catch (DialectException e) {
            System.out.println("Error:" + e.getMessage());
        }
        Assert.assertFalse(result.contains("$"));
        Assert.assertTrue(StrUtils.isEmpty(result) || 1 == StrUtils.countMatches(result, '?'));
        try {
            result = dialect.pagin(3, 10, sql4);
            System.out.println(result);
        } catch (DialectException e) {
            System.out.println("Error:" + e.getMessage());
        }
        Assert.assertFalse(result.contains("$"));
        Assert.assertTrue(StrUtils.isEmpty(result) || 1 == StrUtils.countMatches(result, '?'));
    }
}
Also used : DialectException(com.github.drinkjava2.jdialects.DialectException) DerbyDialect(com.github.drinkjava2.jdialects.Dialect.DerbyDialect) PostgresPlusDialect(com.github.drinkjava2.jdialects.Dialect.PostgresPlusDialect) DB2Dialect(com.github.drinkjava2.jdialects.Dialect.DB2Dialect) Dialect(com.github.drinkjava2.jdialects.Dialect) PostgreSQL81Dialect(com.github.drinkjava2.jdialects.Dialect.PostgreSQL81Dialect) MySQLInnoDBDialect(com.github.drinkjava2.jdialects.Dialect.MySQLInnoDBDialect) PostgreSQL9Dialect(com.github.drinkjava2.jdialects.Dialect.PostgreSQL9Dialect) Oracle10gDialect(com.github.drinkjava2.jdialects.Dialect.Oracle10gDialect) PostgreSQL82Dialect(com.github.drinkjava2.jdialects.Dialect.PostgreSQL82Dialect) SybaseASE15Dialect(com.github.drinkjava2.jdialects.Dialect.SybaseASE15Dialect) MySQL5InnoDBDialect(com.github.drinkjava2.jdialects.Dialect.MySQL5InnoDBDialect) SybaseAnywhereDialect(com.github.drinkjava2.jdialects.Dialect.SybaseAnywhereDialect) DerbyTenSixDialect(com.github.drinkjava2.jdialects.Dialect.DerbyTenSixDialect) SQLServerDialect(com.github.drinkjava2.jdialects.Dialect.SQLServerDialect) HSQLDialect(com.github.drinkjava2.jdialects.Dialect.HSQLDialect) DerbyTenSevenDialect(com.github.drinkjava2.jdialects.Dialect.DerbyTenSevenDialect) H2Dialect(com.github.drinkjava2.jdialects.Dialect.H2Dialect) Oracle8iDialect(com.github.drinkjava2.jdialects.Dialect.Oracle8iDialect) Oracle9iDialect(com.github.drinkjava2.jdialects.Dialect.Oracle9iDialect) InformixDialect(com.github.drinkjava2.jdialects.Dialect.InformixDialect) SQLiteDialect(com.github.drinkjava2.jdialects.Dialect.SQLiteDialect) DB2400Dialect(com.github.drinkjava2.jdialects.Dialect.DB2400Dialect) IngresDialect(com.github.drinkjava2.jdialects.Dialect.IngresDialect) DerbyTenFiveDialect(com.github.drinkjava2.jdialects.Dialect.DerbyTenFiveDialect) Test(org.junit.Test)

Example 8 with Dialect

use of com.github.drinkjava2.jdialects.Dialect in project jDialects by drinkjava2.

the class DialectTest method testPagination.

@Test
public void testPagination() {
    Dialect[] dialects = Dialect.values();
    for (Dialect dialect : dialects) {
        System.out.println("=========" + dialect + "==========");
        String result = "";
        try {
            System.out.println(dialect);
            result = dialect.pagin(1, 10, sql1);
            System.out.println(result);
        } catch (DialectException e) {
            System.out.println("Error:" + e.getMessage());
        }
        Assert.assertFalse(result.contains("$"));
        try {
            result = dialect.pagin(3, 10, sql1);
            System.out.println(result);
        } catch (DialectException e) {
            System.out.println("Error:" + e.getMessage());
        }
        Assert.assertFalse(result.contains("$"));
        try {
            result = dialect.pagin(1, 10, sql2);
            System.out.println(result);
        } catch (DialectException e) {
            System.out.println("Error:" + e.getMessage());
        }
        Assert.assertFalse(result.contains("$"));
        try {
            result = dialect.pagin(3, 10, sql2);
            System.out.println(result);
        } catch (DialectException e) {
            System.out.println("Error:" + e.getMessage());
        }
        Assert.assertFalse(result.contains("$"));
    }
}
Also used : DialectException(com.github.drinkjava2.jdialects.DialectException) DerbyDialect(com.github.drinkjava2.jdialects.Dialect.DerbyDialect) PostgresPlusDialect(com.github.drinkjava2.jdialects.Dialect.PostgresPlusDialect) DB2Dialect(com.github.drinkjava2.jdialects.Dialect.DB2Dialect) Dialect(com.github.drinkjava2.jdialects.Dialect) PostgreSQL81Dialect(com.github.drinkjava2.jdialects.Dialect.PostgreSQL81Dialect) MySQLInnoDBDialect(com.github.drinkjava2.jdialects.Dialect.MySQLInnoDBDialect) PostgreSQL9Dialect(com.github.drinkjava2.jdialects.Dialect.PostgreSQL9Dialect) Oracle10gDialect(com.github.drinkjava2.jdialects.Dialect.Oracle10gDialect) PostgreSQL82Dialect(com.github.drinkjava2.jdialects.Dialect.PostgreSQL82Dialect) SybaseASE15Dialect(com.github.drinkjava2.jdialects.Dialect.SybaseASE15Dialect) MySQL5InnoDBDialect(com.github.drinkjava2.jdialects.Dialect.MySQL5InnoDBDialect) SybaseAnywhereDialect(com.github.drinkjava2.jdialects.Dialect.SybaseAnywhereDialect) DerbyTenSixDialect(com.github.drinkjava2.jdialects.Dialect.DerbyTenSixDialect) SQLServerDialect(com.github.drinkjava2.jdialects.Dialect.SQLServerDialect) HSQLDialect(com.github.drinkjava2.jdialects.Dialect.HSQLDialect) DerbyTenSevenDialect(com.github.drinkjava2.jdialects.Dialect.DerbyTenSevenDialect) H2Dialect(com.github.drinkjava2.jdialects.Dialect.H2Dialect) Oracle8iDialect(com.github.drinkjava2.jdialects.Dialect.Oracle8iDialect) Oracle9iDialect(com.github.drinkjava2.jdialects.Dialect.Oracle9iDialect) InformixDialect(com.github.drinkjava2.jdialects.Dialect.InformixDialect) SQLiteDialect(com.github.drinkjava2.jdialects.Dialect.SQLiteDialect) DB2400Dialect(com.github.drinkjava2.jdialects.Dialect.DB2400Dialect) IngresDialect(com.github.drinkjava2.jdialects.Dialect.IngresDialect) DerbyTenFiveDialect(com.github.drinkjava2.jdialects.Dialect.DerbyTenFiveDialect) Test(org.junit.Test)

Example 9 with Dialect

use of com.github.drinkjava2.jdialects.Dialect in project jDialects by drinkjava2.

the class DialectTest method testDropTable.

@Test
public void testDropTable() {
    for (Dialect d : Dialect.values()) {
        String dropTable = d.dropTableDDL("testTable");
        System.out.println(dropTable);
        Assert.assertTrue(StrUtils.containsIgnoreCase(dropTable, "drop"));
        Assert.assertTrue(StrUtils.containsIgnoreCase(dropTable, "testTable"));
    }
}
Also used : DerbyDialect(com.github.drinkjava2.jdialects.Dialect.DerbyDialect) PostgresPlusDialect(com.github.drinkjava2.jdialects.Dialect.PostgresPlusDialect) DB2Dialect(com.github.drinkjava2.jdialects.Dialect.DB2Dialect) Dialect(com.github.drinkjava2.jdialects.Dialect) PostgreSQL81Dialect(com.github.drinkjava2.jdialects.Dialect.PostgreSQL81Dialect) MySQLInnoDBDialect(com.github.drinkjava2.jdialects.Dialect.MySQLInnoDBDialect) PostgreSQL9Dialect(com.github.drinkjava2.jdialects.Dialect.PostgreSQL9Dialect) Oracle10gDialect(com.github.drinkjava2.jdialects.Dialect.Oracle10gDialect) PostgreSQL82Dialect(com.github.drinkjava2.jdialects.Dialect.PostgreSQL82Dialect) SybaseASE15Dialect(com.github.drinkjava2.jdialects.Dialect.SybaseASE15Dialect) MySQL5InnoDBDialect(com.github.drinkjava2.jdialects.Dialect.MySQL5InnoDBDialect) SybaseAnywhereDialect(com.github.drinkjava2.jdialects.Dialect.SybaseAnywhereDialect) DerbyTenSixDialect(com.github.drinkjava2.jdialects.Dialect.DerbyTenSixDialect) SQLServerDialect(com.github.drinkjava2.jdialects.Dialect.SQLServerDialect) HSQLDialect(com.github.drinkjava2.jdialects.Dialect.HSQLDialect) DerbyTenSevenDialect(com.github.drinkjava2.jdialects.Dialect.DerbyTenSevenDialect) H2Dialect(com.github.drinkjava2.jdialects.Dialect.H2Dialect) Oracle8iDialect(com.github.drinkjava2.jdialects.Dialect.Oracle8iDialect) Oracle9iDialect(com.github.drinkjava2.jdialects.Dialect.Oracle9iDialect) InformixDialect(com.github.drinkjava2.jdialects.Dialect.InformixDialect) SQLiteDialect(com.github.drinkjava2.jdialects.Dialect.SQLiteDialect) DB2400Dialect(com.github.drinkjava2.jdialects.Dialect.DB2400Dialect) IngresDialect(com.github.drinkjava2.jdialects.Dialect.IngresDialect) DerbyTenFiveDialect(com.github.drinkjava2.jdialects.Dialect.DerbyTenFiveDialect) Test(org.junit.Test)

Example 10 with Dialect

use of com.github.drinkjava2.jdialects.Dialect in project jDialects by drinkjava2.

the class DDLCreateUtils method transferTableToObjectList.

/**
 * Transfer table to a mixed DDL String or TableGen Object list
 */
/**
 * @param dialect
 * @param t
 * @param objectResultList
 */
private static void transferTableToObjectList(Dialect dialect, TableModel t, List<Object> objectResultList) {
    DDLFeatures features = dialect.ddlFeatures;
    StringBuilder buf = new StringBuilder();
    boolean hasPkey = false;
    String pkeys = "";
    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> idexChks = t.getIndexConsts();
    if (idexChks != null && !idexChks.isEmpty())
        for (IndexModel index : idexChks) dialect.checkReservedWords(index.getName());
    // check unique names
    List<UniqueModel> ukChks = t.getUniqueConsts();
    if (ukChks != null && !ukChks.isEmpty())
        for (UniqueModel unique : ukChks) 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 (// check column names
    ColumnModel col : // check column names
    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);
    // check and cache prime keys
    for (ColumnModel col : columns) {
        if (col.getTransientable())
            continue;
        if (col.getPkey()) {
            hasPkey = true;
            if (StrUtils.isEmpty(pkeys))
                pkeys = col.getColumnName();
            else
                pkeys += "," + col.getColumnName();
        }
    }
    // create table
    buf.append(hasPkey ? dialect.ddlFeatures.createTableString : dialect.ddlFeatures.createMultisetTableString).append(" ").append(tableName).append(" ( ");
    for (ColumnModel c : columns) {
        if (c.getTransientable())
            continue;
        if (c.getColumnType() == null)
            DialectException.throwEX("Type not set on column \"" + c.getColumnName() + "\" at table \"" + tableName + "\"");
        // column definition
        buf.append(c.getColumnName()).append(" ");
        // Identity
        if (GenerationType.IDENTITY.equals(c.getIdGenerationType()) && !features.supportsIdentityColumns)
            DialectException.throwEX("Unsupported identity setting for dialect \"" + dialect + "\" on column \"" + c.getColumnName() + "\" at table \"" + tableName + "\"");
        // Column type definition
        if (GenerationType.IDENTITY.equals(c.getIdGenerationType())) {
            if (features.hasDataTypeInIdentityColumn)
                buf.append(dialect.translateToDDLType(c.getColumnType(), c.getLengths()));
            buf.append(' ');
            if (Type.BIGINT.equals(c.getColumnType()))
                buf.append(features.identityColumnStringBigINT);
            else
                buf.append(features.identityColumnString);
        } else {
            buf.append(dialect.translateToDDLType(c.getColumnType(), c.getLengths()));
            // Default
            String defaultValue = c.getDefaultValue();
            if (defaultValue != null) {
                buf.append(" default ").append(defaultValue);
            }
            // Not null
            if (!c.getNullable())
                buf.append(" not null");
            else
                buf.append(features.nullColumnString);
        }
        // Check
        if (!StrUtils.isEmpty(c.getCheck())) {
            if (features.supportsColumnCheck)
                buf.append(" check (").append(c.getCheck()).append(")");
            else
                logger.warn("Ignore unsupported check setting for dialect \"" + dialect + "\" on column \"" + c.getColumnName() + "\" at table \"" + tableName + "\" with value: " + c.getCheck());
        }
        // Comments
        if (c.getComment() != null) {
            if (StrUtils.isEmpty(features.columnComment) && !features.supportsCommentOn)
                logger.warn("Ignore unsupported comment setting for dialect \"" + dialect + "\" on column \"" + c.getColumnName() + "\" at table \"" + tableName + "\" with value: " + c.getComment());
            else
                buf.append(StrUtils.replace(features.columnComment, "_COMMENT", c.getComment()));
        }
        // tail String
        if (!StrUtils.isEmpty(c.getTail()))
            buf.append(c.getTail());
        buf.append(",");
    }
    // PKEY
    if (!StrUtils.isEmpty(pkeys)) {
        buf.append(" primary key (").append(pkeys).append("),");
    }
    // Table Check
    if (!StrUtils.isEmpty(t.getCheck())) {
        if (features.supportsTableCheck)
            buf.append(" check (").append(t.getCheck()).append("),");
        else
            logger.warn("Ignore unsupported table check setting for dialect \"" + dialect + "\" on table \"" + tableName + "\" with value: " + t.getCheck());
    }
    buf.setLength(buf.length() - 1);
    buf.append(")");
    // Engine for MariaDB & MySql only, for example "engine=innoDB"
    String tableTypeString = features.tableTypeString;
    if (!StrUtils.isEmpty(tableTypeString) && !DDLFeatures.NOT_SUPPORT.equals(tableTypeString)) {
        buf.append(tableTypeString);
        // EngineTail, for example:" DEFAULT CHARSET=utf8"
        if (!StrUtils.isEmpty(t.getEngineTail()))
            buf.append(t.getEngineTail());
    }
    objectResultList.add(buf.toString());
    // table comment on
    if (t.getComment() != null) {
        if (features.supportsCommentOn)
            objectResultList.add("comment on table " + t.getTableName() + " is '" + t.getComment() + "'");
        else
            logger.warn("Ignore unsupported table comment setting for dialect \"" + dialect + "\" on table \"" + tableName + "\" with value: " + t.getComment());
    }
    // column comment on
    for (ColumnModel c : columns) {
        if (features.supportsCommentOn && c.getComment() != null && StrUtils.isEmpty(features.columnComment))
            objectResultList.add("comment on column " + tableName + '.' + c.getColumnName() + " is '" + c.getComment() + "'");
    }
    // index
    buildIndexDLL(dialect, objectResultList, t);
    // unique
    buildUniqueDLL(dialect, objectResultList, t);
}
Also used : 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) ColumnModel(com.github.drinkjava2.jdialects.model.ColumnModel) FKeyModel(com.github.drinkjava2.jdialects.model.FKeyModel)

Aggregations

Dialect (com.github.drinkjava2.jdialects.Dialect)12 Test (org.junit.Test)10 DB2400Dialect (com.github.drinkjava2.jdialects.Dialect.DB2400Dialect)9 DB2Dialect (com.github.drinkjava2.jdialects.Dialect.DB2Dialect)9 DerbyDialect (com.github.drinkjava2.jdialects.Dialect.DerbyDialect)9 DerbyTenFiveDialect (com.github.drinkjava2.jdialects.Dialect.DerbyTenFiveDialect)9 DerbyTenSevenDialect (com.github.drinkjava2.jdialects.Dialect.DerbyTenSevenDialect)9 DerbyTenSixDialect (com.github.drinkjava2.jdialects.Dialect.DerbyTenSixDialect)9 H2Dialect (com.github.drinkjava2.jdialects.Dialect.H2Dialect)9 HSQLDialect (com.github.drinkjava2.jdialects.Dialect.HSQLDialect)9 InformixDialect (com.github.drinkjava2.jdialects.Dialect.InformixDialect)9 IngresDialect (com.github.drinkjava2.jdialects.Dialect.IngresDialect)9 Oracle10gDialect (com.github.drinkjava2.jdialects.Dialect.Oracle10gDialect)9 Oracle8iDialect (com.github.drinkjava2.jdialects.Dialect.Oracle8iDialect)9 Oracle9iDialect (com.github.drinkjava2.jdialects.Dialect.Oracle9iDialect)9 PostgreSQL81Dialect (com.github.drinkjava2.jdialects.Dialect.PostgreSQL81Dialect)9 PostgreSQL82Dialect (com.github.drinkjava2.jdialects.Dialect.PostgreSQL82Dialect)9 PostgreSQL9Dialect (com.github.drinkjava2.jdialects.Dialect.PostgreSQL9Dialect)9 PostgresPlusDialect (com.github.drinkjava2.jdialects.Dialect.PostgresPlusDialect)9 SQLServerDialect (com.github.drinkjava2.jdialects.Dialect.SQLServerDialect)9