Search in sources :

Example 1 with DerbyDialect

use of org.hibernate.dialect.DerbyDialect in project hibernate-orm by hibernate.

the class StandardDialectResolver method resolveDialect.

@Override
public Dialect resolveDialect(DialectResolutionInfo info) {
    final String databaseName = info.getDatabaseName();
    if ("CUBRID".equalsIgnoreCase(databaseName)) {
        return new CUBRIDDialect();
    }
    if ("HSQL Database Engine".equals(databaseName)) {
        return new HSQLDialect();
    }
    if ("H2".equals(databaseName)) {
        return new H2Dialect();
    }
    if ("MySQL".equals(databaseName)) {
        final int majorVersion = info.getDatabaseMajorVersion();
        if (majorVersion >= 5) {
            return new MySQL5Dialect();
        }
        return new MySQLDialect();
    }
    if ("PostgreSQL".equals(databaseName)) {
        final int majorVersion = info.getDatabaseMajorVersion();
        final int minorVersion = info.getDatabaseMinorVersion();
        if (majorVersion == 9) {
            if (minorVersion >= 4) {
                return new PostgreSQL94Dialect();
            } else if (minorVersion >= 2) {
                return new PostgreSQL92Dialect();
            }
            return new PostgreSQL9Dialect();
        }
        if (majorVersion == 8 && minorVersion >= 2) {
            return new PostgreSQL82Dialect();
        }
        return new PostgreSQL81Dialect();
    }
    if ("EnterpriseDB".equals(databaseName)) {
        return new PostgresPlusDialect();
    }
    if ("Apache Derby".equals(databaseName)) {
        final int majorVersion = info.getDatabaseMajorVersion();
        final int minorVersion = info.getDatabaseMinorVersion();
        if (majorVersion > 10 || (majorVersion == 10 && minorVersion >= 7)) {
            return new DerbyTenSevenDialect();
        } else if (majorVersion == 10 && minorVersion == 6) {
            return new DerbyTenSixDialect();
        } else if (majorVersion == 10 && minorVersion == 5) {
            return new DerbyTenFiveDialect();
        } else {
            return new DerbyDialect();
        }
    }
    if ("ingres".equalsIgnoreCase(databaseName)) {
        final int majorVersion = info.getDatabaseMajorVersion();
        final int minorVersion = info.getDatabaseMinorVersion();
        switch(majorVersion) {
            case 9:
                if (minorVersion > 2) {
                    return new Ingres9Dialect();
                }
                return new IngresDialect();
            case 10:
                return new Ingres10Dialect();
            default:
                LOG.unknownIngresVersion(majorVersion);
        }
        return new IngresDialect();
    }
    if (databaseName.startsWith("Microsoft SQL Server")) {
        final int majorVersion = info.getDatabaseMajorVersion();
        switch(majorVersion) {
            case 8:
                {
                    return new SQLServerDialect();
                }
            case 9:
                {
                    return new SQLServer2005Dialect();
                }
            case 10:
                {
                    return new SQLServer2008Dialect();
                }
            case 11:
            case 12:
            case 13:
                {
                    return new SQLServer2012Dialect();
                }
            default:
                {
                    if (majorVersion < 8) {
                        LOG.unknownSqlServerVersion(majorVersion, SQLServerDialect.class);
                        return new SQLServerDialect();
                    } else {
                        // assume `majorVersion > 13`
                        LOG.unknownSqlServerVersion(majorVersion, SQLServer2012Dialect.class);
                        return new SQLServer2012Dialect();
                    }
                }
        }
    }
    if ("Sybase SQL Server".equals(databaseName) || "Adaptive Server Enterprise".equals(databaseName)) {
        return new SybaseASE15Dialect();
    }
    if (databaseName.startsWith("Adaptive Server Anywhere")) {
        return new SybaseAnywhereDialect();
    }
    if ("Informix Dynamic Server".equals(databaseName)) {
        return new InformixDialect();
    }
    if ("DB2 UDB for AS/400".equals(databaseName)) {
        return new DB2400Dialect();
    }
    if (databaseName.startsWith("DB2/")) {
        return new DB2Dialect();
    }
    if ("Oracle".equals(databaseName)) {
        final int majorVersion = info.getDatabaseMajorVersion();
        switch(majorVersion) {
            case 12:
                return new Oracle12cDialect();
            case 11:
            // fall through
            case 10:
                return new Oracle10gDialect();
            case 9:
                return new Oracle9iDialect();
            case 8:
                return new Oracle8iDialect();
            default:
                LOG.unknownOracleVersion(majorVersion);
        }
        return new Oracle8iDialect();
    }
    if ("HDB".equals(databaseName)) {
        // SAP recommends defaulting to column store.
        return new HANAColumnStoreDialect();
    }
    if (databaseName.startsWith("Firebird")) {
        return new FirebirdDialect();
    }
    return null;
}
Also used : MySQL5Dialect(org.hibernate.dialect.MySQL5Dialect) SybaseASE15Dialect(org.hibernate.dialect.SybaseASE15Dialect) H2Dialect(org.hibernate.dialect.H2Dialect) PostgreSQL92Dialect(org.hibernate.dialect.PostgreSQL92Dialect) IngresDialect(org.hibernate.dialect.IngresDialect) DerbyTenSixDialect(org.hibernate.dialect.DerbyTenSixDialect) Oracle10gDialect(org.hibernate.dialect.Oracle10gDialect) PostgreSQL94Dialect(org.hibernate.dialect.PostgreSQL94Dialect) FirebirdDialect(org.hibernate.dialect.FirebirdDialect) PostgreSQL82Dialect(org.hibernate.dialect.PostgreSQL82Dialect) PostgreSQL81Dialect(org.hibernate.dialect.PostgreSQL81Dialect) MySQLDialect(org.hibernate.dialect.MySQLDialect) PostgreSQL9Dialect(org.hibernate.dialect.PostgreSQL9Dialect) SybaseAnywhereDialect(org.hibernate.dialect.SybaseAnywhereDialect) DerbyTenSevenDialect(org.hibernate.dialect.DerbyTenSevenDialect) DB2400Dialect(org.hibernate.dialect.DB2400Dialect) Ingres9Dialect(org.hibernate.dialect.Ingres9Dialect) HSQLDialect(org.hibernate.dialect.HSQLDialect) HANAColumnStoreDialect(org.hibernate.dialect.HANAColumnStoreDialect) PostgresPlusDialect(org.hibernate.dialect.PostgresPlusDialect) DerbyDialect(org.hibernate.dialect.DerbyDialect) SQLServerDialect(org.hibernate.dialect.SQLServerDialect) Oracle9iDialect(org.hibernate.dialect.Oracle9iDialect) DerbyTenFiveDialect(org.hibernate.dialect.DerbyTenFiveDialect) DB2Dialect(org.hibernate.dialect.DB2Dialect) Oracle8iDialect(org.hibernate.dialect.Oracle8iDialect) Ingres10Dialect(org.hibernate.dialect.Ingres10Dialect) SQLServer2012Dialect(org.hibernate.dialect.SQLServer2012Dialect) Oracle12cDialect(org.hibernate.dialect.Oracle12cDialect) SQLServer2005Dialect(org.hibernate.dialect.SQLServer2005Dialect) InformixDialect(org.hibernate.dialect.InformixDialect) CUBRIDDialect(org.hibernate.dialect.CUBRIDDialect) SQLServer2008Dialect(org.hibernate.dialect.SQLServer2008Dialect)

Example 2 with DerbyDialect

use of org.hibernate.dialect.DerbyDialect in project hibernate-orm by hibernate.

the class SequenceValueExtractor method extractSequenceValue.

public long extractSequenceValue(final SessionImplementor sessionImpl) {
    class WorkImpl implements Work {

        private long value;

        public void execute(Connection connection) throws SQLException {
            Session session = (Session) sessionImpl;
            Transaction transaction = session.beginTransaction();
            try {
                final PreparedStatement query = sessionImpl.getJdbcCoordinator().getStatementPreparer().prepareStatement(queryString);
                ResultSet resultSet = sessionImpl.getJdbcCoordinator().getResultSetReturn().extract(query);
                resultSet.next();
                value = resultSet.getLong(1);
                resultSet.close();
                transaction.commit();
            } catch (GenericJDBCException e) {
                transaction.rollback();
                throw e;
            }
            if (dialect instanceof DerbyDialect) {
                value--;
            }
        }
    }
    WorkImpl work = new WorkImpl();
    ((Session) sessionImpl).doWork(work);
    return work.value;
}
Also used : DerbyDialect(org.hibernate.dialect.DerbyDialect) Transaction(org.hibernate.Transaction) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Work(org.hibernate.jdbc.Work) PreparedStatement(java.sql.PreparedStatement) GenericJDBCException(org.hibernate.exception.GenericJDBCException) Session(org.hibernate.Session)

Example 3 with DerbyDialect

use of org.hibernate.dialect.DerbyDialect in project hibernate-orm by hibernate.

the class ASTParserLoadingTest method testStr.

@Test
public void testStr() {
    Session session = openSession();
    Transaction txn = session.beginTransaction();
    Animal an = new Animal();
    an.setBodyWeight(123.45f);
    session.persist(an);
    String str = (String) session.createQuery("select str(an.bodyWeight) from Animal an where str(an.bodyWeight) like '%1%'").uniqueResult();
    if ((getDialect() instanceof DB2Dialect || getDialect() instanceof TeradataDialect) && !(getDialect() instanceof DerbyDialect)) {
        assertTrue(str.startsWith("1.234"));
    } else //noinspection deprecation
    if (getDialect() instanceof SybaseDialect || getDialect() instanceof Sybase11Dialect || getDialect() instanceof SybaseASE15Dialect || getDialect() instanceof SybaseAnywhereDialect || getDialect() instanceof SQLServerDialect) {
        // str(val) on sybase assumes a default of 10 characters with no decimal point or decimal values
        // str(val) on sybase result is right-justified
        assertEquals(str.length(), 10);
        assertTrue(str.endsWith("123"));
        str = (String) session.createQuery("select str(an.bodyWeight, 8, 3) from Animal an where str(an.bodyWeight, 8, 3) like '%1%'").uniqueResult();
        assertEquals(str.length(), 8);
        assertTrue(str.endsWith("123.450"));
    } else {
        assertTrue(str.startsWith("123.4"));
    }
    //noinspection deprecation
    if (!(getDialect() instanceof SybaseDialect) && !(getDialect() instanceof Sybase11Dialect) && !(getDialect() instanceof SybaseASE15Dialect) && !(getDialect() instanceof SybaseAnywhereDialect) && !(getDialect() instanceof SQLServerDialect || getDialect() instanceof TeradataDialect)) {
        // In TransactSQL (the variant spoken by Sybase and SQLServer), the str() function
        // is explicitly intended for numeric values only...
        String dateStr1 = (String) session.createQuery("select str(current_date) from Animal").uniqueResult();
        String dateStr2 = (String) session.createQuery("select str(year(current_date))||'-'||str(month(current_date))||'-'||str(day(current_date)) from Animal").uniqueResult();
        System.out.println(dateStr1 + '=' + dateStr2);
        if (!(getDialect() instanceof Oracle8iDialect)) {
            //Oracle renders the name of the month :(
            String[] dp1 = StringHelper.split("-", dateStr1);
            String[] dp2 = StringHelper.split("-", dateStr2);
            for (int i = 0; i < 3; i++) {
                if (dp1[i].startsWith("0")) {
                    dp1[i] = dp1[i].substring(1);
                }
                assertEquals(dp1[i], dp2[i]);
            }
        }
    }
    session.delete(an);
    txn.commit();
    session.close();
}
Also used : DerbyDialect(org.hibernate.dialect.DerbyDialect) SybaseASE15Dialect(org.hibernate.dialect.SybaseASE15Dialect) SQLServerDialect(org.hibernate.dialect.SQLServerDialect) SybaseDialect(org.hibernate.dialect.SybaseDialect) DB2Dialect(org.hibernate.dialect.DB2Dialect) Oracle8iDialect(org.hibernate.dialect.Oracle8iDialect) SybaseAnywhereDialect(org.hibernate.dialect.SybaseAnywhereDialect) Transaction(org.hibernate.Transaction) TeradataDialect(org.hibernate.dialect.TeradataDialect) Sybase11Dialect(org.hibernate.dialect.Sybase11Dialect) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with DerbyDialect

use of org.hibernate.dialect.DerbyDialect in project hibernate-orm by hibernate.

the class ASTParserLoadingTest method testComponentNullnessChecks.

@Test
public void testComponentNullnessChecks() {
    Session s = openSession();
    s.beginTransaction();
    Human h = new Human();
    h.setName(new Name("Johnny", 'B', "Goode"));
    s.save(h);
    h = new Human();
    h.setName(new Name("Steve", null, "Ebersole"));
    s.save(h);
    h = new Human();
    h.setName(new Name("Bono", null, null));
    s.save(h);
    h = new Human();
    h.setName(new Name(null, null, null));
    s.save(h);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    List results = s.createQuery("from Human where name is null").list();
    assertEquals(1, results.size());
    results = s.createQuery("from Human where name is not null").list();
    assertEquals(3, results.size());
    String query = (getDialect() instanceof DB2Dialect || getDialect() instanceof HSQLDialect) ? "from Human where cast(? as string) is null" : "from Human where ? is null";
    if (getDialect() instanceof DerbyDialect) {
        s.createQuery(query).setParameter(0, "null").list();
    } else {
        s.createQuery(query).setParameter(0, null).list();
    }
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    s.createQuery("delete Human").executeUpdate();
    s.getTransaction().commit();
    s.close();
}
Also used : HSQLDialect(org.hibernate.dialect.HSQLDialect) DerbyDialect(org.hibernate.dialect.DerbyDialect) List(java.util.List) ArrayList(java.util.ArrayList) DB2Dialect(org.hibernate.dialect.DB2Dialect) Session(org.hibernate.Session) Test(org.junit.Test)

Example 5 with DerbyDialect

use of org.hibernate.dialect.DerbyDialect in project hibernate-orm by hibernate.

the class FooBarTest method testMultiColumnQueries.

@Test
public void testMultiColumnQueries() throws Exception {
    Session s = openSession();
    Transaction txn = s.beginTransaction();
    Foo foo = new Foo();
    s.save(foo);
    Foo foo1 = new Foo();
    s.save(foo1);
    foo.setFoo(foo1);
    List l = s.createQuery("select parent, child from Foo parent, Foo child where parent.foo = child").list();
    assertTrue("multi-column find", l.size() == 1);
    Iterator rs = null;
    Object[] row = null;
    //Derby does not support multiple DISTINCT aggregates
    if (!(getDialect() instanceof DerbyDialect)) {
        rs = s.createQuery("select count(distinct child.id), count(distinct parent.id) from Foo parent, Foo child where parent.foo = child").iterate();
        row = (Object[]) rs.next();
        assertTrue("multi-column count", ((Long) row[0]).intValue() == 1);
        assertTrue("multi-column count", ((Long) row[1]).intValue() == 1);
        assertTrue(!rs.hasNext());
    }
    rs = s.createQuery("select child.id, parent.id, child.long from Foo parent, Foo child where parent.foo = child").iterate();
    row = (Object[]) rs.next();
    assertTrue("multi-column id", row[0].equals(foo.getFoo().getKey()));
    assertTrue("multi-column id", row[1].equals(foo.getKey()));
    assertTrue("multi-column property", row[2].equals(foo.getFoo().getLong()));
    assertTrue(!rs.hasNext());
    rs = s.createQuery("select child.id, parent.id, child.long, child, parent.foo from Foo parent, Foo child where parent.foo = child").iterate();
    row = (Object[]) rs.next();
    assertTrue(foo.getFoo().getKey().equals(row[0]) && foo.getKey().equals(row[1]) && foo.getFoo().getLong().equals(row[2]) && row[3] == foo.getFoo() && row[3] == row[4]);
    assertTrue(!rs.hasNext());
    row = (Object[]) l.get(0);
    assertTrue("multi-column find", row[0] == foo && row[1] == foo.getFoo());
    txn.commit();
    s.close();
    s = openSession();
    txn = s.beginTransaction();
    Iterator iter = s.createQuery("select parent, child from Foo parent, Foo child where parent.foo = child and parent.string='a string'").iterate();
    int deletions = 0;
    while (iter.hasNext()) {
        Object[] pnc = (Object[]) iter.next();
        s.delete(pnc[0]);
        s.delete(pnc[1]);
        deletions++;
    }
    assertTrue("multi-column iterate", deletions == 1);
    txn.commit();
    s.close();
}
Also used : DerbyDialect(org.hibernate.dialect.DerbyDialect) Transaction(org.hibernate.Transaction) JoinedIterator(org.hibernate.internal.util.collections.JoinedIterator) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

DerbyDialect (org.hibernate.dialect.DerbyDialect)8 Session (org.hibernate.Session)7 Test (org.junit.Test)6 Transaction (org.hibernate.Transaction)5 DB2Dialect (org.hibernate.dialect.DB2Dialect)5 HSQLDialect (org.hibernate.dialect.HSQLDialect)4 ArrayList (java.util.ArrayList)3 Iterator (java.util.Iterator)3 List (java.util.List)3 MySQLDialect (org.hibernate.dialect.MySQLDialect)3 SQLServerDialect (org.hibernate.dialect.SQLServerDialect)3 SybaseDialect (org.hibernate.dialect.SybaseDialect)3 Query (org.hibernate.Query)2 ScrollableResults (org.hibernate.ScrollableResults)2 InterbaseDialect (org.hibernate.dialect.InterbaseDialect)2 MckoiDialect (org.hibernate.dialect.MckoiDialect)2 Oracle8iDialect (org.hibernate.dialect.Oracle8iDialect)2 SybaseASE15Dialect (org.hibernate.dialect.SybaseASE15Dialect)2 SybaseAnywhereDialect (org.hibernate.dialect.SybaseAnywhereDialect)2 Connection (java.sql.Connection)1