use of org.datanucleus.store.rdbms.adapter.DatastoreAdapter in project datanucleus-rdbms by datanucleus.
the class SelectStatement method addOrderingColumnsToSelect.
/**
* Convenience method to add any necessary columns to the SELECT that are needed
* by the ordering constraint.
*/
protected void addOrderingColumnsToSelect() {
if (// Don't do this for subqueries, since we will be selecting just the necessary column(s)
orderingExpressions != null && parent == null) {
// Add any ordering columns to the SELECT
DatastoreAdapter dba = getDatastoreAdapter();
if (dba.supportsOption(DatastoreAdapter.ORDERBY_USING_SELECT_COLUMN_INDEX)) {
// Order using the indexes of the ordering columns in the SELECT
orderingColumnIndexes = new int[orderingExpressions.length];
// Add the ordering columns to the selected list, saving the positions
for (int i = 0; i < orderingExpressions.length; ++i) {
orderingColumnIndexes[i] = selectItem(orderingExpressions[i].toSQLText(), null, !aggregated);
if (unions != null && allowUnions) {
Iterator<SelectStatement> iterator = unions.iterator();
while (iterator.hasNext()) {
SelectStatement stmt = iterator.next();
stmt.selectItem(orderingExpressions[i].toSQLText(), null, !aggregated);
}
}
}
} else if (dba.supportsOption(DatastoreAdapter.INCLUDE_ORDERBY_COLS_IN_SELECT)) {
// Order using column aliases "NUCORDER{i}"
for (int i = 0; i < orderingExpressions.length; ++i) {
if (orderingExpressions[i] instanceof ResultAliasExpression) {
// Nothing to do since this is ordering by a result alias
} else if (orderingExpressions[i].getNumberOfSubExpressions() == 1 || aggregated) {
String orderExprAlias = rdbmsMgr.getIdentifierFactory().getIdentifierInAdapterCase("NUCORDER" + i);
if (unions != null && allowUnions) {
Iterator<SelectStatement> iterator = unions.iterator();
while (iterator.hasNext()) {
SelectStatement stmt = iterator.next();
stmt.selectItem(orderingExpressions[i].toSQLText(), aggregated ? null : orderExprAlias, !aggregated);
}
}
selectItem(orderingExpressions[i].toSQLText(), aggregated ? null : orderExprAlias, !aggregated);
} else {
JavaTypeMapping m = orderingExpressions[i].getJavaTypeMapping();
DatastoreMapping[] mappings = m.getDatastoreMappings();
for (int j = 0; j < mappings.length; j++) {
String alias = rdbmsMgr.getIdentifierFactory().getIdentifierInAdapterCase("NUCORDER" + i + "_" + j);
DatastoreIdentifier aliasId = rdbmsMgr.getIdentifierFactory().newColumnIdentifier(alias);
SQLColumn col = new SQLColumn(orderingExpressions[i].getSQLTable(), mappings[j].getColumn(), aliasId);
selectItem(new SQLText(col.getColumnSelectString()), alias, !aggregated);
if (unions != null && allowUnions) {
Iterator<SelectStatement> iterator = unions.iterator();
while (iterator.hasNext()) {
SelectStatement stmt = iterator.next();
stmt.selectItem(new SQLText(col.getColumnSelectString()), alias, !aggregated);
}
}
}
}
}
}
}
}
use of org.datanucleus.store.rdbms.adapter.DatastoreAdapter in project datanucleus-rdbms by datanucleus.
the class SelectStatement method getSqlForJoins.
/**
* Convenience method to return the JOIN clause implied by the "joins" List.
* @param lock Whether to add locking on the join clause (only for some RDBMS)
* @return The SQL for the join clause
*/
protected SQLText getSqlForJoins(boolean lock) {
// TODO Consider performing a reorder in more situations to cater for use of implicit joins potentially not being in optimum order
if (requiresJoinReorder) {
List<SQLJoin> theJoins = reorderJoins(joins);
joins = theJoins;
}
SQLText sql = new SQLText();
DatastoreAdapter dba = getDatastoreAdapter();
Iterator<SQLJoin> iter = joins.iterator();
while (iter.hasNext()) {
SQLJoin join = iter.next();
if (join.getType() == JoinType.CROSS_JOIN) {
if (dba.supportsOption(DatastoreAdapter.ANSI_CROSSJOIN_SYNTAX)) {
// ANSI-92 style joins, separate joins by space
sql.append(" ").append(join.toSQLText(dba, lock));
} else if (dba.supportsOption(DatastoreAdapter.CROSSJOIN_ASINNER11_SYNTAX)) {
sql.append(" INNER JOIN " + join.getTargetTable() + " ON 1=1");
} else {
// "ANSI-86" style cross join, separate join by comma
sql.append(",").append(join.getTargetTable().toString());
}
} else {
if (dba.supportsOption(DatastoreAdapter.ANSI_JOIN_SYNTAX)) {
// ANSI-92 style joins, separate joins by space
sql.append(" ").append(join.toSQLText(dba, lock));
} else {
// "ANSI-86" style joins, separate joins by comma
sql.append(",").append(join.toSQLText(dba, lock));
}
}
}
return sql;
}
use of org.datanucleus.store.rdbms.adapter.DatastoreAdapter in project datanucleus-rdbms by datanucleus.
the class ExpressionUtils method getNumericExpression.
/**
* Method to return a numeric expression for the supplied SQL expression.
* Makes use of the RDBMS function to convert to a numeric.
* @param expr The expression
* @return The numeric expression for this SQL expression
*/
public static NumericExpression getNumericExpression(SQLExpression expr) {
RDBMSStoreManager storeMgr = expr.getSQLStatement().getRDBMSManager();
SQLExpressionFactory factory = storeMgr.getSQLExpressionFactory();
DatastoreAdapter dba = expr.getSQLStatement().getDatastoreAdapter();
if (expr instanceof CharacterLiteral) {
char c = ((Character) ((CharacterLiteral) expr).getValue()).charValue();
BigInteger value = new BigInteger("" + (int) c);
return (NumericExpression) factory.newLiteral(expr.getSQLStatement(), storeMgr.getMappingManager().getMapping(value.getClass()), value);
} else if (expr instanceof SQLLiteral) {
BigInteger value = new BigInteger((String) ((SQLLiteral) expr).getValue());
return (NumericExpression) factory.newLiteral(expr.getSQLStatement(), storeMgr.getMappingManager().getMapping(value.getClass()), value);
}
ArrayList args = new ArrayList();
args.add(expr);
return new NumericExpression(expr.getSQLStatement(), expr.getJavaTypeMapping(), dba.getNumericConversionFunction(), args);
}
use of org.datanucleus.store.rdbms.adapter.DatastoreAdapter in project datanucleus-rdbms by datanucleus.
the class SQLExpressionFactory method invokeOperation.
/**
* Accessor for the result of an SQLOperation call on the supplied expression with the supplied args.
* Throws a NucleusException is the method is not supported.
* @param name Operation to be invoked
* @param expr The first expression to perform the operation on
* @param expr2 The second expression to perform the operation on
* @return The result
* @throws UnsupportedOperationException if the operation is not specified
*/
public SQLExpression invokeOperation(String name, SQLExpression expr, SQLExpression expr2) {
// Check for instantiated plugin SQLOperation
DatastoreAdapter dba = storeMgr.getDatastoreAdapter();
SQLOperation operation = sqlOperationsByName.get(name);
if (operation != null) {
return operation.getExpression(expr, expr2);
}
// Check for built-in SQLOperation class definition
Class sqlOpClass = dba.getSQLOperationClass(name);
if (sqlOpClass != null) {
try {
// Instantiate it
operation = (SQLOperation) sqlOpClass.newInstance();
sqlOperationsByName.put(name, operation);
return operation.getExpression(expr, expr2);
} catch (Exception e) {
throw new NucleusException("Error creating SQLOperation of type " + sqlOpClass.getName() + " for operation " + name);
}
}
// Check for plugin definition of this operation for this datastore
// 1). Try datastore-dependent key
String datastoreId = dba.getVendorID();
String key = getSQLOperationKey(datastoreId, name);
boolean datastoreDependent = true;
if (!pluginSqlOperationKeysSupported.contains(key)) {
// 2). No datastore-dependent method, so try a datastore-independent key
key = getSQLOperationKey(null, name);
datastoreDependent = false;
if (!pluginSqlOperationKeysSupported.contains(key)) {
throw new UnsupportedOperationException("Operation " + name + " on datastore=" + datastoreId + " not supported");
}
}
PluginManager pluginMgr = storeMgr.getNucleusContext().getPluginManager();
String[] attrNames = (datastoreDependent ? new String[] { "name", "datastore" } : new String[] { "name" });
String[] attrValues = (datastoreDependent ? new String[] { name, datastoreId } : new String[] { name });
try {
operation = (SQLOperation) pluginMgr.createExecutableExtension("org.datanucleus.store.rdbms.sql_operation", attrNames, attrValues, "evaluator", null, null);
synchronized (operation) {
sqlOperationsByName.put(key, operation);
return operation.getExpression(expr, expr2);
}
} catch (Exception e) {
throw new NucleusUserException(Localiser.msg("060011", "operation=" + name), e);
}
}
use of org.datanucleus.store.rdbms.adapter.DatastoreAdapter in project datanucleus-rdbms by datanucleus.
the class StringLength2Method 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) {
DatastoreAdapter dba = stmt.getDatastoreAdapter();
if (!(dba instanceof FirebirdAdapter)) {
throw new NucleusException("StringLength2Method being used for evaluation of String.length yet this is for Firebird ONLY. Please report this");
}
FirebirdAdapter fba = (FirebirdAdapter) dba;
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
if (fba.supportsCharLengthFunction()) {
// Firebird v2+ support CHAR_LENGTH
if (!expr.isParameter() && expr instanceof StringLiteral) {
JavaTypeMapping m = exprFactory.getMappingForType(int.class, false);
String val = (String) ((StringLiteral) expr).getValue();
return new IntegerLiteral(stmt, m, Integer.valueOf(val.length()), null);
} else if (expr instanceof StringExpression || expr instanceof ParameterLiteral) {
ArrayList funcArgs = new ArrayList();
funcArgs.add(expr);
return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "CHAR_LENGTH", funcArgs);
} else {
throw new NucleusException(Localiser.msg("060001", "length", expr));
}
}
if (expr instanceof StringLiteral) {
// Firebird v1 requires STRLEN
JavaTypeMapping m = exprFactory.getMappingForType(int.class, false);
String val = (String) ((StringLiteral) expr).getValue();
return new IntegerLiteral(stmt, m, Integer.valueOf(val.length()), null);
} else if (expr instanceof StringExpression || expr instanceof ParameterLiteral) {
ArrayList funcArgs = new ArrayList();
funcArgs.add(expr);
return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "STRLEN", funcArgs);
} else {
throw new NucleusException(Localiser.msg("060001", "length", expr));
}
}
Aggregations