Search in sources :

Example 1 with PostgreSQLDialect

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

the class IlikeExpression method toSqlString.

@Override
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) {
    final Dialect dialect = criteriaQuery.getFactory().getDialect();
    final String[] columns = criteriaQuery.findColumns(propertyName, criteria);
    if (columns.length != 1) {
        throw new HibernateException("ilike may only be used with single-column properties");
    }
    if (dialect instanceof PostgreSQLDialect || dialect instanceof PostgreSQL81Dialect) {
        return columns[0] + " ilike ?";
    } else {
        return dialect.getLowercaseFunction() + '(' + columns[0] + ") like ?";
    }
}
Also used : PostgreSQLDialect(org.hibernate.dialect.PostgreSQLDialect) HibernateException(org.hibernate.HibernateException) PostgreSQL81Dialect(org.hibernate.dialect.PostgreSQL81Dialect) Dialect(org.hibernate.dialect.Dialect) PostgreSQLDialect(org.hibernate.dialect.PostgreSQLDialect) PostgreSQL81Dialect(org.hibernate.dialect.PostgreSQL81Dialect)

Example 2 with PostgreSQLDialect

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

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

the class FooBarTest method testCollectionsInSelect.

@Test
public void testCollectionsInSelect() throws Exception {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Foo[] foos = new Foo[] { null, new Foo() };
    s.save(foos[1]);
    Baz baz = new Baz();
    baz.setDefaults();
    baz.setFooArray(foos);
    s.save(baz);
    Baz baz2 = new Baz();
    baz2.setDefaults();
    s.save(baz2);
    Bar bar = new Bar();
    bar.setBaz(baz);
    s.save(bar);
    List list = s.createQuery("select new Result(foo.string, foo.long, foo.integer) from Foo foo").list();
    assertTrue(list.size() == 2 && (list.get(0) instanceof Result) && (list.get(1) instanceof Result));
    /*list = s.find("select new Result( baz.name, foo.long, count(elements(baz.fooArray)) ) from Baz baz join baz.fooArray foo group by baz.name, foo.long");
		assertTrue( list.size()==1 && ( list.get(0) instanceof Result ) );
		Result r = ((Result) list.get(0) );
		assertEquals( r.getName(), baz.getName() );
		assertEquals( r.getCount(), 1 );
		assertEquals( r.getAmount(), foos[1].getLong().longValue() );*/
    list = s.createQuery("select new Result( baz.name, max(foo.long), count(foo) ) from Baz baz join baz.fooArray foo group by baz.name").list();
    assertTrue(list.size() == 1 && (list.get(0) instanceof Result));
    Result r = ((Result) list.get(0));
    assertEquals(r.getName(), baz.getName());
    assertEquals(r.getCount(), 1);
    assertTrue(r.getAmount() > 696969696969696000l);
    //The following test is disabled for databases with no subselects...also for Interbase (not sure why).
    if (!(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) && /*&& !(dialect instanceof MckoiDialect)*/
    !(getDialect() instanceof SAPDBDialect) && !(getDialect() instanceof PointbaseDialect)) {
        s.createQuery("select count(*) from Baz as baz where 1 in indices(baz.fooArray)").list();
        s.createQuery("select count(*) from Bar as bar where 'abc' in elements(bar.baz.fooArray)").list();
        s.createQuery("select count(*) from Bar as bar where 1 in indices(bar.baz.fooArray)").list();
        if (!(getDialect() instanceof DB2Dialect) && !(getDialect() instanceof Oracle8iDialect) && !(getDialect() instanceof SybaseDialect) && !(getDialect() instanceof Sybase11Dialect) && !(getDialect() instanceof SybaseASE15Dialect) && !(getDialect() instanceof PostgreSQLDialect) && !(getDialect() instanceof PostgreSQL81Dialect) && !(getDialect() instanceof AbstractHANADialect)) {
            // SybaseAnywhereDialect supports implicit conversions from strings to ints
            s.createQuery("select count(*) from Bar as bar, bar.component.glarch.proxyArray as g where g.id in indices(bar.baz.fooArray)").list();
            s.createQuery("select max( elements(bar.baz.fooArray) ) from Bar as bar, bar.component.glarch.proxyArray as g where g.id in indices(bar.baz.fooArray)").list();
        }
        s.createQuery("select count(*) from Bar as bar where '1' in (from bar.component.glarch.proxyArray g where g.name='foo')").list();
        s.createQuery("select count(*) from Bar as bar where '1' in (from bar.component.glarch.proxyArray g where g.name='foo')").list();
        s.createQuery("select count(*) from Bar as bar left outer join bar.component.glarch.proxyArray as pg where '1' in (from bar.component.glarch.proxyArray)").list();
    }
    list = s.createQuery("from Baz baz left join baz.fooToGlarch join fetch baz.fooArray foo left join fetch foo.foo").list();
    assertTrue(list.size() == 1 && ((Object[]) list.get(0)).length == 2);
    s.createQuery("select baz.name from Bar bar inner join bar.baz baz inner join baz.fooSet foo where baz.name = bar.string").list();
    s.createQuery("SELECT baz.name FROM Bar AS bar INNER JOIN bar.baz AS baz INNER JOIN baz.fooSet AS foo WHERE baz.name = bar.string").list();
    if (!(getDialect() instanceof HSQLDialect))
        s.createQuery("select baz.name from Bar bar join bar.baz baz left outer join baz.fooSet foo where baz.name = bar.string").list();
    s.createQuery("select baz.name from Bar bar join bar.baz baz join baz.fooSet foo where baz.name = bar.string").list();
    s.createQuery("SELECT baz.name FROM Bar AS bar JOIN bar.baz AS baz JOIN baz.fooSet AS foo WHERE baz.name = bar.string").list();
    if (!(getDialect() instanceof HSQLDialect)) {
        s.createQuery("select baz.name from Bar bar left join bar.baz baz left join baz.fooSet foo where baz.name = bar.string").list();
        s.createQuery("select foo.string from Bar bar left join bar.baz.fooSet foo where bar.string = foo.string").list();
    }
    s.createQuery("select baz.name from Bar bar left join bar.baz baz left join baz.fooArray foo where baz.name = bar.string").list();
    s.createQuery("select foo.string from Bar bar left join bar.baz.fooArray foo where bar.string = foo.string").list();
    s.createQuery("select bar.string, foo.string from Bar bar inner join bar.baz as baz inner join baz.fooSet as foo where baz.name = 'name'").list();
    s.createQuery("select foo from Bar bar inner join bar.baz as baz inner join baz.fooSet as foo").list();
    s.createQuery("select foo from Bar bar inner join bar.baz.fooSet as foo").list();
    s.createQuery("select bar.string, foo.string from Bar bar join bar.baz as baz join baz.fooSet as foo where baz.name = 'name'").list();
    s.createQuery("select foo from Bar bar join bar.baz as baz join baz.fooSet as foo").list();
    s.createQuery("select foo from Bar bar join bar.baz.fooSet as foo").list();
    assertTrue(s.createQuery("from Bar bar join bar.baz.fooArray foo").list().size() == 1);
    assertTrue(s.createQuery("from Bar bar join bar.baz.fooSet foo").list().size() == 0);
    assertTrue(s.createQuery("from Bar bar join bar.baz.fooArray foo").list().size() == 1);
    s.delete(bar);
    if (getDialect() instanceof DB2Dialect || getDialect() instanceof PostgreSQLDialect || getDialect() instanceof PostgreSQL81Dialect) {
        s.createQuery("select one from One one join one.manies many group by one order by count(many)").iterate();
        s.createQuery("select one from One one join one.manies many group by one having count(many) < 5").iterate();
    }
    s.createQuery("from One one join one.manies many where one.id = 1 and many.id = 1").list();
    s.createQuery("select one.id, elements(one.manies) from One one").iterate();
    s.createQuery("select max( elements(one.manies) ) from One one").iterate();
    s.createQuery("select one, elements(one.manies) from One one").list();
    Iterator iter = s.createQuery("select elements(baz.fooArray) from Baz baz where baz.id=?").setParameter(0, baz.getCode(), StandardBasicTypes.STRING).iterate();
    assertTrue(iter.next() == foos[1] && !iter.hasNext());
    list = s.createQuery("select elements(baz.fooArray) from Baz baz where baz.id=?").setParameter(0, baz.getCode(), StandardBasicTypes.STRING).list();
    assertEquals(1, list.size());
    iter = s.createQuery("select indices(baz.fooArray) from Baz baz where baz.id=?").setParameter(0, baz.getCode(), StandardBasicTypes.STRING).iterate();
    assertTrue(iter.next().equals(new Integer(1)) && !iter.hasNext());
    iter = s.createQuery("select size(baz.stringSet) from Baz baz where baz.id=?").setParameter(0, baz.getCode(), StandardBasicTypes.STRING).iterate();
    assertEquals(new Integer(3), iter.next());
    s.createQuery("from Foo foo where foo.component.glarch.id is not null").list();
    iter = s.createQuery("select baz, size(baz.stringSet), count( distinct elements(baz.stringSet) ), max( elements(baz.stringSet) ) from Baz baz group by baz").iterate();
    while (iter.hasNext()) {
        Object[] arr = (Object[]) iter.next();
        log.info(arr[0] + " " + arr[1] + " " + arr[2] + " " + arr[3]);
    }
    s.delete(baz);
    s.delete(baz2);
    s.delete(foos[1]);
    t.commit();
    s.close();
}
Also used : HSQLDialect(org.hibernate.dialect.HSQLDialect) SybaseASE15Dialect(org.hibernate.dialect.SybaseASE15Dialect) SybaseDialect(org.hibernate.dialect.SybaseDialect) PointbaseDialect(org.hibernate.dialect.PointbaseDialect) DB2Dialect(org.hibernate.dialect.DB2Dialect) Oracle8iDialect(org.hibernate.dialect.Oracle8iDialect) PostgreSQL81Dialect(org.hibernate.dialect.PostgreSQL81Dialect) SAPDBDialect(org.hibernate.dialect.SAPDBDialect) AbstractHANADialect(org.hibernate.dialect.AbstractHANADialect) MySQLDialect(org.hibernate.dialect.MySQLDialect) PostgreSQLDialect(org.hibernate.dialect.PostgreSQLDialect) Transaction(org.hibernate.Transaction) JoinedIterator(org.hibernate.internal.util.collections.JoinedIterator) Iterator(java.util.Iterator) Sybase11Dialect(org.hibernate.dialect.Sybase11Dialect) List(java.util.List) ArrayList(java.util.ArrayList) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with PostgreSQLDialect

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

the class LikeTest method testLike.

@Test
public void testLike() {
    Session session = openSession();
    Transaction tx = session.beginTransaction();
    TestObject obj = new TestObject();
    String uniq = "uniq" + System.currentTimeMillis();
    obj.setText("XyZ " + uniq + " blablabla");
    session.save(obj);
    session.flush();
    tx.commit();
    session.close();
    String pattern = "XyZ " + uniq + "%";
    // retrieve object - case sensitive - works ok
    session = openSession();
    tx = session.beginTransaction();
    List objects = session.createCriteria(TestObject.class).add(Restrictions.like("text", pattern)).list();
    assertEquals(1, objects.size());
    session.clear();
    // retrieve object - case insensitive - works ok
    objects = session.createCriteria(TestObject.class).add(Restrictions.like("text", pattern).ignoreCase()).list();
    assertEquals(1, objects.size());
    session.clear();
    if (!(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof PostgreSQLDialect) && !(getDialect() instanceof PostgreSQL81Dialect)) {
        // retrieve object - case insensitive via custom expression - works
        // ok
        objects = session.createCriteria(TestObject.class).add(StringExpression.stringExpression("text", pattern, true)).list();
        assertEquals(1, objects.size());
        session.clear();
        // retrieve object - case sensitive via custom expression - not
        // working
        objects = session.createCriteria(TestObject.class).add(StringExpression.stringExpression("text", pattern, false)).list();
        assertEquals(1, objects.size());
    }
    tx.rollback();
    session.close();
}
Also used : MySQLDialect(org.hibernate.dialect.MySQLDialect) PostgreSQLDialect(org.hibernate.dialect.PostgreSQLDialect) Transaction(org.hibernate.Transaction) List(java.util.List) Session(org.hibernate.Session) PostgreSQL81Dialect(org.hibernate.dialect.PostgreSQL81Dialect) Test(org.junit.Test)

Example 5 with PostgreSQLDialect

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

the class FormulaJoinTest method testFormulaJoin.

@Test
public void testFormulaJoin() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Master master = new Master();
    master.setName("master 1");
    Detail current = new Detail();
    current.setCurrentVersion(true);
    current.setVersion(2);
    current.setDetails("details of master 1 blah blah");
    current.setMaster(master);
    master.setDetail(current);
    Detail past = new Detail();
    past.setCurrentVersion(false);
    past.setVersion(1);
    past.setDetails("old details of master 1 yada yada");
    past.setMaster(master);
    s.persist(master);
    s.persist(past);
    s.persist(current);
    tx.commit();
    s.close();
    if (getDialect() instanceof PostgreSQLDialect || getDialect() instanceof PostgreSQL81Dialect)
        return;
    s = openSession();
    tx = s.beginTransaction();
    List l = s.createQuery("from Master m left join m.detail d").list();
    assertEquals(l.size(), 1);
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    l = s.createQuery("from Master m left join fetch m.detail").list();
    assertEquals(l.size(), 1);
    Master m = (Master) l.get(0);
    assertEquals("master 1", m.getDetail().getMaster().getName());
    assertTrue(m == m.getDetail().getMaster());
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    l = s.createQuery("from Master m join fetch m.detail").list();
    assertEquals(l.size(), 1);
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    l = s.createQuery("from Detail d join fetch d.currentMaster.master").list();
    assertEquals(l.size(), 2);
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    l = s.createQuery("from Detail d join fetch d.currentMaster.master m join fetch m.detail").list();
    assertEquals(l.size(), 2);
    s.createQuery("delete from Detail").executeUpdate();
    s.createQuery("delete from Master").executeUpdate();
    tx.commit();
    s.close();
}
Also used : PostgreSQLDialect(org.hibernate.dialect.PostgreSQLDialect) Transaction(org.hibernate.Transaction) List(java.util.List) Session(org.hibernate.Session) PostgreSQL81Dialect(org.hibernate.dialect.PostgreSQL81Dialect) Test(org.junit.Test)

Aggregations

PostgreSQL81Dialect (org.hibernate.dialect.PostgreSQL81Dialect)6 PostgreSQLDialect (org.hibernate.dialect.PostgreSQLDialect)6 Session (org.hibernate.Session)5 Test (org.junit.Test)5 Transaction (org.hibernate.Transaction)4 List (java.util.List)3 DB2Dialect (org.hibernate.dialect.DB2Dialect)3 HSQLDialect (org.hibernate.dialect.HSQLDialect)3 MySQLDialect (org.hibernate.dialect.MySQLDialect)3 AbstractHANADialect (org.hibernate.dialect.AbstractHANADialect)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 HibernateException (org.hibernate.HibernateException)1 Dialect (org.hibernate.dialect.Dialect)1 Oracle8iDialect (org.hibernate.dialect.Oracle8iDialect)1 PointbaseDialect (org.hibernate.dialect.PointbaseDialect)1 SAPDBDialect (org.hibernate.dialect.SAPDBDialect)1 Sybase11Dialect (org.hibernate.dialect.Sybase11Dialect)1 SybaseASE15Dialect (org.hibernate.dialect.SybaseASE15Dialect)1 SybaseDialect (org.hibernate.dialect.SybaseDialect)1