/** * Test the case of executing a specific (e.g. PostgreSQL) database script * when no general script is present (therefore no overriding mechanism is required). * * @throws Exception */ @Test public void canExecuteSpecificDialectScript() throws Exception { scriptExecutor.executeScriptUrl("scriptexec/${db.script.dialect}/specific.sql"); String select = "select message from alf_test_script_exec_specific"; List<String> res = jdbcTmpl.queryForList(select, String.class); assertEquals(1, res.size()); if (dialect.getClass().equals(MySQLInnoDBDialect.class)) { assertEquals("mysql", res.get(0)); } else if (dialect.getClass().equals(PostgreSQLDialect.class)) { assertEquals("postgresql", res.get(0)); } else { log.warn("No suitable dialect-specific DB script for test canExecuteSpecificDialectScript()"); } }
/** * Test the case of executing a specific database script (e.g. PostgreSQL) when * a more generic script also exists -- the more generic script is not run. * * @throws Exception */ @Test public void canExecuteSpecificDialectOverridingGenericScript() throws Exception { scriptExecutor.executeScriptUrl("scriptexec/${db.script.dialect}/override.sql"); String select = "select message from alf_test_script_exec_override"; List<String> res = jdbcTmpl.queryForList(select, String.class); assertEquals(1, res.size()); if (dialect.getClass().equals(MySQLInnoDBDialect.class)) { assertEquals("mysql", res.get(0)); } else if (dialect.getClass().equals(PostgreSQLDialect.class)) { assertEquals("postgresql", res.get(0)); } else { log.warn("No suitable dialect-specific DB script for test canExecuteSpecificDialectOverridingGenericScript()"); } }
/** * Determine the Hibernate database dialect class for the given target database. * @param database the target database * @return the Hibernate database dialect class, or {@code null} if none found */ @SuppressWarnings("deprecation") protected Class<?> determineDatabaseDialectClass(Database database) { switch (database) { case DB2: return DB2Dialect.class; case DERBY: return DerbyDialect.class; // DerbyDialect deprecated in 4.x case H2: return H2Dialect.class; case HSQL: return HSQLDialect.class; case INFORMIX: return InformixDialect.class; case MYSQL: return MySQLDialect.class; case ORACLE: return Oracle9iDialect.class; case POSTGRESQL: return PostgreSQLDialect.class; // PostgreSQLDialect deprecated in 4.x case SQL_SERVER: return SQLServerDialect.class; case SYBASE: return org.hibernate.dialect.SybaseDialect.class; // SybaseDialect deprecated in 3.6 but not 4.x default: return null; } }
private Properties getPostgresHbProps(Properties obProps) { isPostgresDatabase = true; final Properties props = new Properties(); props.setProperty(Environment.DIALECT, PostgreSQLDialect.class.getName()); if (isJNDIModeOn(obProps)) { setJNDI(obProps, props); } else { props.setProperty(Environment.DRIVER, "org.postgresql.Driver"); props.setProperty(Environment.URL, obProps.getProperty("bbdd.url") + "/" + obProps.getProperty("bbdd.sid")); props.setProperty(Environment.USER, obProps.getProperty("bbdd.user")); props.setProperty(Environment.PASS, obProps.getProperty("bbdd.password")); } return props; }
/** * Copied from {@link HQLTest#testExpressionWithParamInFunction} */ 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 ) ) { // HSQLDB does not like the abs(? - ?) syntax... s.createQuery( "from Animal where abs(:x - :y) < 2.0" ).setLong( "x", 1 ).setLong( "y", 1 ).list(); } 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(); s.createQuery( "from Animal where lower(upper('foo') || upper(:bar)) like 'f%'" ).setString( "bar", "xyz" ).list(); if ( ! ( getDialect() instanceof PostgreSQLDialect || 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(); }
public void testExpressionWithParamInFunction() { assertTranslation("from Animal a where abs(a.bodyWeight-:param) < 2.0"); assertTranslation("from Animal a where abs(:param - a.bodyWeight) < 2.0"); assertTranslation("from Animal where abs(:x - :y) < 2.0"); assertTranslation("from Animal where lower(upper(:foo)) like 'f%'"); if ( ! ( getDialect() instanceof SybaseDialect ) ) { // SybaseDialect maps the length function -> len; classic translator does not consider that *when nested* assertTranslation("from Animal a where abs(abs(a.bodyWeight - 1.0 + :param) * abs(length('ffobar')-3)) = 3.0"); } if ( !( getDialect() instanceof MySQLDialect || getDialect() instanceof SybaseDialect ) ) { assertTranslation("from Animal where lower(upper('foo') || upper(:bar)) like 'f%'"); } if ( getDialect() instanceof PostgreSQLDialect ) { return; } assertTranslation("from Animal where abs(cast(1 as float) - cast(:param as float)) = 1.0"); }
/** * Determine the Hibernate database dialect class for the given target database. * @param database the target database * @return the Hibernate database dialect class, or {@code null} if none found */ protected Class determineDatabaseDialectClass(Database database) { switch (database) { case DB2: return DB2Dialect.class; case DERBY: return DerbyDialect.class; case H2: return H2Dialect.class; case HSQL: return HSQLDialect.class; case INFORMIX: return InformixDialect.class; case MYSQL: return MySQLDialect.class; case ORACLE: return Oracle9iDialect.class; case POSTGRESQL: return PostgreSQLDialect.class; case SQL_SERVER: return SQLServerDialect.class; case SYBASE: return SybaseDialect.class; default: return null; } }
public void setUp() throws Exception { dialect = (Dialect) ctx.getBean("dialect"); if (dialect instanceof PostgreSQLDialect) { // Note: PostgreSQL does not support \u0000 char embedded in a string // http://archives.postgresql.org/pgsql-jdbc/2007-02/msg00115.php COMPLEX_LOCAL_NAME = COMPLEX_LOCAL_NAME_NO_U0000; } serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY); transactionService = serviceRegistry.getTransactionService(); dictionaryService = BaseNodeServiceTest.loadModel(ctx); nodeService = serviceRegistry.getNodeService(); this.authenticationComponent = (AuthenticationComponent) ctx.getBean("authenticationComponent"); this.authenticationComponent.setSystemUserAsCurrentUser(); // get the indexer and searcher factory IndexerAndSearcher indexerAndSearcher = (IndexerAndSearcher) ctx.getBean("indexerAndSearcherFactory"); searcher = new SearcherComponent(); searcher.setIndexerAndSearcherFactory(indexerAndSearcher); // create a test workspace StoreRef storeRef = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, getName() + "_" + System.currentTimeMillis()); rootNodeRef = nodeService.getRootNode(storeRef); // begin a transaction txn = transactionService.getUserTransaction(); txn.begin(); }
@Test public void exportDb() throws Exception { Class dialectClass = dialect.getClass(); if (logger.isDebugEnabled()) { logger.debug("Using dialect class " + dialectClass.getName()); } if (PostgreSQLDialect.class.isAssignableFrom(dialectClass)) { exportTester = new PostgreSQLDialectExportTester(exporter, tx, jdbcTemplate); } else if (AlfrescoMariaDBDialect.class.isAssignableFrom(dialectClass)) { exportTester = new AlfrescoMariaDBDialectExportTester(exporter, tx, jdbcTemplate); } else if (MySQLDialect.class.isAssignableFrom(dialectClass)) { exportTester = new MySQLDialectExportTester(exporter, tx, jdbcTemplate); } if (exportTester != null) { // Run the DBMS specific tests. exportTester.runExportTest(); } else { if (logger.isInfoEnabled()) { logger.info("Unsupported dialect for this test " + dialectClass.getName()); } } }
@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 ?"; } }
public HibernateDatabaseConfigurationFactory() { builtInConfigurations = new HashMap<String, DatabaseConfiguration>(); builtInConfigurations.put(SQLServerDialect.class.getName(), new MSSQLServerDatabaseConfiguration()); builtInConfigurations.put(Oracle10gDialect.class.getName(), new OracleDatabaseConfiguration()); builtInConfigurations.put(PostgreSQLDialect.class.getName(), new PostgreSQLDatabaseConfiguration()); }
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException { Dialect dialect = criteriaQuery.getFactory().getDialect(); String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName); if (columns.length!=1) throw new HibernateException("ilike may only be used with single-column properties"); if ( dialect instanceof PostgreSQLDialect ) { return columns[0] + " ilike ?"; } else { return dialect.getLowercaseFunction() + '(' + columns[0] + ") like ?"; } //TODO: get SQL rendering out of this package! }
public void testGroupByFunction() { if ( getDialect() instanceof Oracle9Dialect ) return; if ( getDialect() instanceof Oracle8iDialect ) return; // the new hiearchy... if ( getDialect() instanceof PostgreSQLDialect ) return; assertTranslation( "select count(*) from Human h group by year(h.birthdate)" ); assertTranslation( "select count(*) from Human h group by trunc( sqrt(h.bodyWeight*4)/2 )" ); assertTranslation( "select count(*) from Human h group by year(sysdate)" ); }
/** * @throws Exception */ public void testAddEscapedChild() throws Exception { String COMPLEX_LOCAL_NAME = "\u0020\u0060\u00ac\u00a6\u0021\"\u00a3\u0024\u0025\u005e\u0026\u002a\u0028\u0029\u002d\u005f\u003d\u002b\t\n\\\u0000\u005b\u005d\u007b\u007d\u003b\u0027\u0023\u003a\u0040\u007e\u002c\u002e\u002f\u003c\u003e\u003f\\u007c\u005f\u0078\u0054\u0036\u0035\u0041\u005f"; if (dialect instanceof PostgreSQLDialect) { // Note: PostgreSQL does not support \u0000 char embedded in a string // http://archives.postgresql.org/pgsql-jdbc/2007-02/msg00115.php COMPLEX_LOCAL_NAME = "\u0020\u0060\u00ac\u00a6\u0021\"\u00a3\u0024\u0025\u005e\u0026\u002a\u0028\u0029\u002d\u005f\u003d\u002b\t\n\\\u005b\u005d\u007b\u007d\u003b\u0027\u0023\u003a\u0040\u007e\u002c\u002e\u002f\u003c\u003e\u003f\\u007c\u005f\u0078\u0054\u0036\u0035\u0041\u005f"; } luceneFTS.pause(); buildBaseIndex(); runBaseTests(); ADMLuceneIndexerImpl indexer = ADMLuceneIndexerImpl.getUpdateIndexer(rootNodeRef.getStoreRef(), "delta" + System.currentTimeMillis(), luceneConfig); indexer.setMaxAtomicTransformationTime(1000000); indexer.setNodeService(nodeService); // indexer.setLuceneIndexLock(luceneIndexLock); indexer.setDictionaryService(dictionaryService); indexer.setTenantService(tenantService); indexer.setFullTextSearchIndexer(luceneFTS); indexer.setContentService(contentService); indexer.setTransactionService(transactionService); ChildAssociationRef car = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}" + COMPLEX_LOCAL_NAME), testSuperType); indexer.createNode(car); indexer.commit(); ADMLuceneSearcherImpl searcher = buildSearcher(); ResultSet results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/namespace:" + ISO9075.encode(COMPLEX_LOCAL_NAME) + "\"", null); assertEquals(1, results.length()); results.close(); }
@Test public void exportDb() throws Exception { Class dialectClass = dialect.getClass(); if (logger.isDebugEnabled()) { logger.debug("Using dialect class " + dialectClass.getName()); } if (PostgreSQLDialect.class.isAssignableFrom(dialectClass)) { exportTester = new PostgreSQLDialectExportTester(exporter, tx, jdbcTemplate); } else if (MySQLDialect.class.isAssignableFrom(dialectClass)) { exportTester = new MySQLDialectExportTester(exporter, tx, jdbcTemplate); } if (exportTester != null) { // Run the DBMS specific tests. exportTester.runExportTest(); } else { if (logger.isInfoEnabled()) { logger.info("Unsupported dialect for this test " + dialectClass.getName()); } } }
/** * Determine the Hibernate database dialect class for the given target database. * @param database the target database * @return the Hibernate database dialect class, or <code>null<code> if none found */ protected Class determineDatabaseDialectClass(Database database) { switch (database) { case DB2: return DB2Dialect.class; case HSQL: return HSQLDialect.class; case INFORMIX: return InformixDialect.class; case MYSQL: return MySQLDialect.class; case ORACLE: return Oracle9iDialect.class; case POSTGRESQL: return PostgreSQLDialect.class; case SQL_SERVER: return SQLServerDialect.class; case SYBASE: return SybaseASE15Dialect.class; default: return null; } }
public PostgreSQLDialect() { super(); registerFunction("minute", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(minute from ?1)")); registerFunction("hour", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(hour from ?1)")); }
@Override protected Dialect createHibernateDialect() { return new PostgreSQLDialect(); }
@Override protected Dialect createHibernateDialect() { return new PostgreSQLDialect(); // nearest match }
public void testMixedInheritance() { Session s = openSession( new DocumentInterceptor() ); Transaction t = s.beginTransaction(); Folder f = new Folder(); f.setName( "/" ); s.save( f ); Document d = new Document(); d.setName( "Hibernate in Action" ); d.setContent( Hibernate.createBlob( "blah blah blah".getBytes() ) ); d.setParent( f ); Long did = (Long) s.save( d ); SecureDocument d2 = new SecureDocument(); d2.setName( "Secret" ); d2.setContent( Hibernate.createBlob( "wxyz wxyz".getBytes() ) ); d2.setPermissionBits( (byte) 664 ); d2.setOwner( "gavin" ); d2.setParent( f ); Long d2id = (Long) s.save( d2 ); t.commit(); s.close(); if ( getDialect() instanceof PostgreSQLDialect ) return; s = openSession( new DocumentInterceptor() ); t = s.beginTransaction(); Item id = (Item) s.load( Item.class, did ); assertEquals( did, id.getId() ); assertEquals( "Hibernate in Action", id.getName() ); assertEquals( "/", id.getParent().getName() ); Item id2 = (Item) s.load( Item.class, d2id ); assertEquals( d2id, id2.getId() ); assertEquals( "Secret", id2.getName() ); assertEquals( "/", id2.getParent().getName() ); id.setName( "HiA" ); d2 = (SecureDocument) s.load( SecureDocument.class, d2id ); d2.setOwner( "max" ); s.flush(); s.clear(); d = (Document) s.load( Document.class, did ); assertEquals( did, d.getId() ); assertEquals( "HiA", d.getName() ); assertNotNull( d.getContent() ); assertEquals( "/", d.getParent().getName() ); assertNotNull( d.getCreated() ); assertNotNull( d.getModified() ); d2 = (SecureDocument) s.load( SecureDocument.class, d2id ); assertEquals( d2id, d2.getId() ); assertEquals( "Secret", d2.getName() ); assertNotNull( d2.getContent() ); assertEquals( "max", d2.getOwner() ); assertEquals( "/", d2.getParent().getName() ); assertEquals( (byte) 664, d2.getPermissionBits() ); assertNotNull( d2.getCreated() ); assertNotNull( d2.getModified() ); s.delete( d.getParent() ); s.delete( d ); s.delete( d2 ); t.commit(); s.close(); }
public void testInterfaceProxies() { if ( getDialect() instanceof PostgreSQLDialect ) { // TODO : why? return; } Session s = openSession( new DocumentInterceptor() ); Transaction t = s.beginTransaction(); Document d = new DocumentImpl(); d.setName("Hibernate in Action"); d.setContent( Hibernate.createBlob( "blah blah blah".getBytes() ) ); Long did = (Long) s.save(d); SecureDocument d2 = new SecureDocumentImpl(); d2.setName("Secret"); d2.setContent( Hibernate.createBlob( "wxyz wxyz".getBytes() ) ); d2.setPermissionBits( (byte) 664 ); d2.setOwner("gavin"); Long d2id = (Long) s.save(d2); t.commit(); s.close(); s = openSession( new DocumentInterceptor() ); t = s.beginTransaction(); d = (Document) s.load(ItemImpl.class, did); assertEquals( did, d.getId() ); assertEquals( "Hibernate in Action", d.getName() ); assertNotNull( d.getContent() ); d2 = (SecureDocument) s.load(ItemImpl.class, d2id); assertEquals( d2id, d2.getId() ); assertEquals( "Secret", d2.getName() ); assertNotNull( d2.getContent() ); s.clear(); d = (Document) s.load(DocumentImpl.class, did); assertEquals( did, d.getId() ); assertEquals( "Hibernate in Action", d.getName() ); assertNotNull( d.getContent() ); d2 = (SecureDocument) s.load(SecureDocumentImpl.class, d2id); assertEquals( d2id, d2.getId() ); assertEquals( "Secret", d2.getName() ); assertNotNull( d2.getContent() ); assertEquals( "gavin", d2.getOwner() ); //s.clear(); d2 = (SecureDocument) s.load(SecureDocumentImpl.class, did); assertEquals( did, d2.getId() ); assertEquals( "Hibernate in Action", d2.getName() ); assertNotNull( d2.getContent() ); try { d2.getOwner(); //CCE assertFalse(true); } catch (ClassCastException cce) { //correct } s.createQuery( "delete ItemImpl" ).executeUpdate(); t.commit(); s.close(); }
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 ) 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(); }
public void testLoadSaveRepository() throws Exception { Connection theConnection = createConnection(); Class theHibernateDialect = PostgreSQLDialect.class; String theModelResource = "/de/erdesignerng/test/io/repository/examplemodel.mxm"; String theNewFile = RepositioryHelper.performRepositorySaveAndLoad(theModelResource, theHibernateDialect, theConnection); String theOriginalFile = IOUtils.toString(getClass().getResourceAsStream(theModelResource)); assertTrue(compareStrings(theOriginalFile, theNewFile)); }