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;
}
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"));
}
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));
}
}
}
}
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;
}
}
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);
}
}
Aggregations