Search in sources :

Example 66 with RDBMSStoreManager

use of org.datanucleus.store.rdbms.RDBMSStoreManager in project datanucleus-rdbms by datanucleus.

the class TemporalMonthMethod4 method getExpression.

/* (non-Javadoc)
     * @see org.datanucleus.store.rdbms.sql.method.SQLMethod#getExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression, java.util.List)
     */
public SQLExpression getExpression(SQLStatement stmt, SQLExpression expr, List<SQLExpression> args) {
    SQLExpression invokedExpr = getInvokedExpression(expr, args, "MONTH");
    RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
    JavaTypeMapping mapping2 = storeMgr.getMappingManager().getMapping(String.class);
    ArrayList funcArgs = new ArrayList();
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    funcArgs.add(exprFactory.newLiteral(stmt, mapping2, "%m"));
    funcArgs.add(invokedExpr);
    NumericExpression numExpr = new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class, true), "strftime", funcArgs);
    numExpr.encloseInParentheses();
    return numExpr;
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) ArrayList(java.util.ArrayList) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 67 with RDBMSStoreManager

use of org.datanucleus.store.rdbms.RDBMSStoreManager in project datanucleus-rdbms by datanucleus.

the class TemporalSecondMethod3 method getExpression.

/* (non-Javadoc)
     * @see org.datanucleus.store.rdbms.sql.method.SQLMethod#getExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression, java.util.List)
     */
public SQLExpression getExpression(SQLStatement stmt, SQLExpression expr, List<SQLExpression> args) {
    SQLExpression invokedExpr = getInvokedExpression(expr, args, "SECOND");
    RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
    JavaTypeMapping mapping = storeMgr.getMappingManager().getMapping(String.class);
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    SQLExpression day = exprFactory.newLiteral(stmt, mapping, "second");
    ArrayList funcArgs = new ArrayList();
    funcArgs.add(day);
    funcArgs.add(invokedExpr);
    NumericExpression secondExpr = new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class, true), "date_part", funcArgs);
    List castArgs = new ArrayList();
    castArgs.add(secondExpr);
    return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(Integer.class, true), "CAST", castArgs, asList("INTEGER"));
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) ArrayList(java.util.ArrayList) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) List(java.util.List) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 68 with RDBMSStoreManager

use of org.datanucleus.store.rdbms.RDBMSStoreManager in project datanucleus-rdbms by datanucleus.

the class BaseDatastoreAdapter method initialise.

public void initialise(StoreSchemaHandler handler, ManagedConnection mconn) {
    // Initialise the datastore mappings for this datastore
    RDBMSStoreManager storeMgr = (RDBMSStoreManager) handler.getStoreManager();
    ClassLoaderResolver clr = storeMgr.getNucleusContext().getClassLoaderResolver(null);
    loadDatastoreMappings(storeMgr.getNucleusContext().getPluginManager(), clr);
    // Initialise any types, including artificial ones added for the datastore when not provided by the JDBC driver
    initialiseTypes(handler, mconn);
    RDBMSTypesInfo types = (RDBMSTypesInfo) handler.getSchemaData(mconn.getConnection(), "types", null);
    Iterator<Map.Entry<Integer, String>> entryIter = supportedJdbcTypesById.entrySet().iterator();
    while (entryIter.hasNext()) {
        Map.Entry<Integer, String> entry = entryIter.next();
        int jdbcType = entry.getKey();
        if (types.getChild("" + jdbcType) == null) {
            // JDBC type not supported by adapter so deregister the mapping. TODO Remove any entries from built-in types to avoid this
            deregisterDatastoreMappingsForJDBCType(entry.getValue());
        }
    }
    entryIter = unsupportedJdbcTypesById.entrySet().iterator();
    while (entryIter.hasNext()) {
        Map.Entry<Integer, String> entry = entryIter.next();
        int jdbcType = entry.getKey();
        if (types.getChild("" + jdbcType) == null) {
            // JDBC type not supported by adapter so deregister the mapping. TODO Remove any entries from built-in types to avoid this
            deregisterDatastoreMappingsForJDBCType(entry.getValue());
        }
    }
    // Log the datastore mapping summary, for each Java type, showing the supported JDBC-types and SQL-types.
    if (NucleusLogger.DATASTORE.isDebugEnabled()) {
        Collection<String> javaTypes = new TreeSet<>(datastoreTypeMappingsByJavaType.keySet());
        for (String javaType : javaTypes) {
            DatastoreTypeMappings datastoreTypeMappings = datastoreTypeMappingsByJavaType.get(javaType);
            if (NucleusLogger.DATASTORE.isDebugEnabled()) {
                NucleusLogger.DATASTORE.debug(Localiser.msg("054009", javaType, StringUtils.collectionToString(datastoreTypeMappings.datastoreMappingByJdbcType.keySet()), StringUtils.collectionToString(datastoreTypeMappings.datastoreMappingBySqlType.keySet()), datastoreTypeMappings.defaultJdbcType, datastoreTypeMappings.defaultSqlType));
            }
        }
    }
}
Also used : ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) TreeSet(java.util.TreeSet) RDBMSTypesInfo(org.datanucleus.store.rdbms.schema.RDBMSTypesInfo) Map(java.util.Map) HashMap(java.util.HashMap)

Example 69 with RDBMSStoreManager

use of org.datanucleus.store.rdbms.RDBMSStoreManager in project tests by datanucleus.

the class RDBMSTestHelper method getIdentifierInCaseOfAdapter.

/**
 * Utility to convert an identifier to the case of the datastore.
 * @param identifier The identifier
 * @param quote Whether to quote the identifier (if necessary)
 * @return The cased identifier
 */
public static String getIdentifierInCaseOfAdapter(StoreManager storeMgr, String identifier, boolean quote) {
    if (storeMgr instanceof RDBMSStoreManager) {
        RDBMSStoreManager srm = (RDBMSStoreManager) storeMgr;
        StringBuffer str = new StringBuffer();
        if (quote && (srm.getIdentifierFactory().getNamingCase() == NamingCase.LOWER_CASE_QUOTED || srm.getIdentifierFactory().getNamingCase() == NamingCase.MIXED_CASE_QUOTED || srm.getIdentifierFactory().getNamingCase() == NamingCase.UPPER_CASE_QUOTED)) {
            str.append(srm.getDatastoreAdapter().getIdentifierQuoteString());
        }
        if (srm.getIdentifierFactory().getNamingCase() == NamingCase.LOWER_CASE || srm.getIdentifierFactory().getNamingCase() == NamingCase.LOWER_CASE_QUOTED) {
            str.append(identifier.toLowerCase());
        } else if (srm.getIdentifierFactory().getNamingCase() == NamingCase.UPPER_CASE || srm.getIdentifierFactory().getNamingCase() == NamingCase.UPPER_CASE_QUOTED) {
            str.append(identifier.toUpperCase());
        } else {
            str.append(identifier);
        }
        if (quote && (srm.getIdentifierFactory().getNamingCase() == NamingCase.LOWER_CASE_QUOTED || srm.getIdentifierFactory().getNamingCase() == NamingCase.MIXED_CASE_QUOTED || srm.getIdentifierFactory().getNamingCase() == NamingCase.UPPER_CASE_QUOTED)) {
            str.append(srm.getDatastoreAdapter().getIdentifierQuoteString());
        }
        return str.toString();
    } else {
        return null;
    }
}
Also used : RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 70 with RDBMSStoreManager

use of org.datanucleus.store.rdbms.RDBMSStoreManager in project tests by datanucleus.

the class JPQLSubqueryTest method testHavingSubquery.

/**
 * Simple query using a subquery in the HAVING clause.
 */
public void testHavingSubquery() {
    if (!(storeMgr instanceof RDBMSStoreManager)) {
        return;
    }
    DatastoreAdapter dba = ((RDBMSStoreManager) storeMgr).getDatastoreAdapter();
    if (!dba.supportsOption(DatastoreAdapter.SUBQUERY_IN_HAVING)) {
        return;
    }
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Account a1 = new Account();
            a1.setUsername("Flintstone");
            a1.setId(1);
            em.persist(a1);
            Organisation o1 = new Organisation("Flintstone");
            o1.setDescription("Freds organisation");
            em.persist(o1);
            // TODO Come up with a better sample query. Why does the JPA TCK/Spec have none?
            List<Person> result = em.createQuery("SELECT o FROM " + Organisation.class.getName() + " o " + "GROUP BY o.name HAVING EXISTS (SELECT a FROM " + Account.class.getName() + " a WHERE a.username = o.name)").getResultList();
            assertNotNull(result);
            assertEquals(1, result.size());
            tx.rollback();
        } catch (Exception e) {
            LOG.error("Exception in query", e);
            fail("Exception executing query with HAVING subquery : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Person.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Account(org.datanucleus.samples.annotations.models.company.Account) EntityManager(javax.persistence.EntityManager) Organisation(org.datanucleus.samples.annotations.models.company.Organisation) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) Person(org.datanucleus.samples.annotations.models.company.Person) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Aggregations

RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)197 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)84 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)82 SQLException (java.sql.SQLException)78 Connection (java.sql.Connection)76 HashSet (java.util.HashSet)72 DatabaseMetaData (java.sql.DatabaseMetaData)62 PersistenceManager (javax.jdo.PersistenceManager)61 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)56 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)55 Transaction (javax.jdo.Transaction)52 ArrayList (java.util.ArrayList)49 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)49 JDOFatalUserException (javax.jdo.JDOFatalUserException)48 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)44 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)42 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)41 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)38 NumericExpression (org.datanucleus.store.rdbms.sql.expression.NumericExpression)28 JDOFatalInternalException (javax.jdo.JDOFatalInternalException)25