Search in sources :

Example 1 with MySQLDialect

use of org.hibernate.dialect.MySQLDialect 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 MySQLDialect

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

the class ASTParserLoadingTest method testExpressionWithParamInFunction.

@Test
public void testExpressionWithParamInFunction() {
    Session s = openSession();
    s.beginTransaction();
    s.createQuery("from Animal a where abs(a.bodyWeight-:param) < 2.0").setLong("param", 1).list();
    s.createQuery("from Animal a where abs(:param - a.bodyWeight) < 2.0").setLong("param", 1).list();
    if ((getDialect() instanceof HSQLDialect) || (getDialect() instanceof DB2Dialect)) {
        // HSQLDB and DB2 don't like the abs(? - ?) syntax. bit work if at least one parameter is typed...
        s.createQuery("from Animal where abs(cast(:x as long) - :y) < 2.0").setLong("x", 1).setLong("y", 1).list();
        s.createQuery("from Animal where abs(:x - cast(:y as long)) < 2.0").setLong("x", 1).setLong("y", 1).list();
        s.createQuery("from Animal where abs(cast(:x as long) - cast(:y as long)) < 2.0").setLong("x", 1).setLong("y", 1).list();
    } else {
        s.createQuery("from Animal where abs(:x - :y) < 2.0").setLong("x", 1).setLong("y", 1).list();
    }
    if (getDialect() instanceof DB2Dialect) {
        s.createQuery("from Animal where lower(upper(cast(:foo as string))) like 'f%'").setString("foo", "foo").list();
    } else {
        s.createQuery("from Animal where lower(upper(:foo)) like 'f%'").setString("foo", "foo").list();
    }
    s.createQuery("from Animal a where abs(abs(a.bodyWeight - 1.0 + :param) * abs(length('ffobar')-3)) = 3.0").setLong("param", 1).list();
    if (getDialect() instanceof DB2Dialect) {
        s.createQuery("from Animal where lower(upper('foo') || upper(cast(:bar as string))) like 'f%'").setString("bar", "xyz").list();
    } else {
        s.createQuery("from Animal where lower(upper('foo') || upper(:bar)) like 'f%'").setString("bar", "xyz").list();
    }
    if (getDialect() instanceof AbstractHANADialect) {
        s.createQuery("from Animal where abs(cast(1 as double) - cast(:param as double)) = 1.0").setLong("param", 1).list();
    } else if (!(getDialect() instanceof PostgreSQLDialect || getDialect() instanceof PostgreSQL81Dialect || getDialect() instanceof MySQLDialect)) {
        s.createQuery("from Animal where abs(cast(1 as float) - cast(:param as float)) = 1.0").setLong("param", 1).list();
    }
    s.getTransaction().commit();
    s.close();
}
Also used : HSQLDialect(org.hibernate.dialect.HSQLDialect) AbstractHANADialect(org.hibernate.dialect.AbstractHANADialect) MySQLDialect(org.hibernate.dialect.MySQLDialect) PostgreSQLDialect(org.hibernate.dialect.PostgreSQLDialect) DB2Dialect(org.hibernate.dialect.DB2Dialect) Session(org.hibernate.Session) PostgreSQL81Dialect(org.hibernate.dialect.PostgreSQL81Dialect) Test(org.junit.Test)

Example 3 with MySQLDialect

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

the class BulkManipulationTest method testUpdateOnManyToOne.

@Test
@SkipForDialect(value = CUBRIDDialect.class, comment = "As of verion 8.4.1 CUBRID doesn't support temporary tables. This test fails with" + "HibernateException: cannot doAfterTransactionCompletion multi-table deletes using dialect not supporting temp tables")
public void testUpdateOnManyToOne() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    s.createQuery("update Animal a set a.mother = null where a.id = 2").executeUpdate();
    if (!(getDialect() instanceof MySQLDialect)) {
        // MySQL does not support (even un-correlated) subqueries against the update-mutating table
        s.createQuery("update Animal a set a.mother = (from Animal where id = 1) where a.id = 2").executeUpdate();
    }
    t.commit();
    s.close();
}
Also used : MySQLDialect(org.hibernate.dialect.MySQLDialect) Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 4 with MySQLDialect

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

the class BulkManipulationTest method testUpdateOnAnimal.

@Test
public void testUpdateOnAnimal() {
    TestData data = new TestData();
    data.prepare();
    Session s = openSession();
    Transaction t = s.beginTransaction();
    int count = s.createQuery("update Animal set description = description where description = :desc").setString("desc", data.frog.getDescription()).executeUpdate();
    assertEquals("Incorrect entity-updated count", 1, count);
    count = s.createQuery("update Animal set description = :newDesc where description = :desc").setString("desc", data.polliwog.getDescription()).setString("newDesc", "Tadpole").executeUpdate();
    assertEquals("Incorrect entity-updated count", 1, count);
    Animal tadpole = (Animal) s.load(Animal.class, data.polliwog.getId());
    assertEquals("Update did not take effect", "Tadpole", tadpole.getDescription());
    count = s.createQuery("update Animal set bodyWeight = bodyWeight + :w1 + :w2").setDouble("w1", 1).setDouble("w2", 2).executeUpdate();
    assertEquals("incorrect count on 'complex' update assignment", count, 6);
    if (!(getDialect() instanceof MySQLDialect)) {
        // MySQL does not support (even un-correlated) subqueries against the update-mutating table
        s.createQuery("update Animal set bodyWeight = ( select max(bodyWeight) from Animal )").executeUpdate();
    }
    t.commit();
    s.close();
    data.cleanup();
}
Also used : MySQLDialect(org.hibernate.dialect.MySQLDialect) Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 5 with MySQLDialect

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

the class BulkManipulationTest method testUpdateOnMammal.

@Test
public void testUpdateOnMammal() {
    TestData data = new TestData();
    data.prepare();
    Session s = openSession();
    Transaction t = s.beginTransaction();
    int count = s.createQuery("update Mammal set description = description").executeUpdate();
    assertEquals("incorrect update count against 'middle' of joined-subclass hierarchy", 2, count);
    count = s.createQuery("update Mammal set bodyWeight = 25").executeUpdate();
    assertEquals("incorrect update count against 'middle' of joined-subclass hierarchy", 2, count);
    if (!(getDialect() instanceof MySQLDialect)) {
        // MySQL does not support (even un-correlated) subqueries against the update-mutating table
        count = s.createQuery("update Mammal set bodyWeight = ( select max(bodyWeight) from Animal )").executeUpdate();
        assertEquals("incorrect update count against 'middle' of joined-subclass hierarchy", 2, count);
    }
    t.commit();
    s.close();
    data.cleanup();
}
Also used : MySQLDialect(org.hibernate.dialect.MySQLDialect) Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

MySQLDialect (org.hibernate.dialect.MySQLDialect)27 Session (org.hibernate.Session)26 Test (org.junit.Test)26 Transaction (org.hibernate.Transaction)18 HSQLDialect (org.hibernate.dialect.HSQLDialect)15 List (java.util.List)12 ArrayList (java.util.ArrayList)11 Iterator (java.util.Iterator)10 PointbaseDialect (org.hibernate.dialect.PointbaseDialect)7 SAPDBDialect (org.hibernate.dialect.SAPDBDialect)7 Serializable (java.io.Serializable)5 Query (org.hibernate.Query)5 DB2Dialect (org.hibernate.dialect.DB2Dialect)5 MckoiDialect (org.hibernate.dialect.MckoiDialect)5 Date (java.util.Date)4 AbstractHANADialect (org.hibernate.dialect.AbstractHANADialect)4 PostgreSQL81Dialect (org.hibernate.dialect.PostgreSQL81Dialect)4 TimesTenDialect (org.hibernate.dialect.TimesTenDialect)4 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3