use of org.datanucleus.store.rdbms.sql.expression.SQLExpression in project datanucleus-rdbms by datanucleus.
the class ArrayIsEmptyMethod 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) {
if (args != null && args.size() > 0) {
throw new NucleusException(Localiser.msg("060015", "isEmpty", "ArrayExpression"));
}
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
if (expr instanceof ArrayLiteral) {
Object arr = ((ArrayLiteral) expr).getValue();
boolean isEmpty = (arr == null || Array.getLength(arr) == 0);
JavaTypeMapping m = exprFactory.getMappingForType(boolean.class, false);
return new BooleanLiteral(stmt, m, isEmpty ? Boolean.TRUE : Boolean.FALSE);
}
SQLExpression sizeExpr = exprFactory.invokeMethod(stmt, "ARRAY", "size", expr, args);
JavaTypeMapping mapping = exprFactory.getMappingForType(Integer.class, true);
SQLExpression zeroExpr = exprFactory.newLiteral(stmt, mapping, 0);
return sizeExpr.eq(zeroExpr);
}
use of org.datanucleus.store.rdbms.sql.expression.SQLExpression in project datanucleus-rdbms by datanucleus.
the class ArraySizeMethod 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) {
if (args != null && args.size() > 0) {
throw new NucleusException(Localiser.msg("060015", "size/length", "ArrayExpression"));
}
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
if (expr instanceof ArrayLiteral) {
// Just return the array length since we have the value
return exprFactory.newLiteral(stmt, exprFactory.getMappingForType(int.class, false), Integer.valueOf(Array.getLength(((ArrayLiteral) expr).getValue())));
}
AbstractMemberMetaData ownerMmd = expr.getJavaTypeMapping().getMemberMetaData();
String elementType = ownerMmd.getArray().getElementType();
RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
ClassLoaderResolver clr = stmt.getQueryGenerator().getClassLoaderResolver();
// TODO Allow for interface elements, etc
JavaTypeMapping ownerMapping = null;
Table arrayTbl = null;
if (ownerMmd.getMappedBy() != null) {
// Bidirectional
AbstractMemberMetaData elementMmd = ownerMmd.getRelatedMemberMetaData(clr)[0];
if (ownerMmd.getJoinMetaData() != null || elementMmd.getJoinMetaData() != null) {
// JoinTable
arrayTbl = storeMgr.getTable(ownerMmd);
ownerMapping = ((JoinTable) arrayTbl).getOwnerMapping();
} else {
// ForeignKey
arrayTbl = storeMgr.getDatastoreClass(elementType, clr);
ownerMapping = arrayTbl.getMemberMapping(elementMmd);
}
} else {
// Unidirectional
if (ownerMmd.getJoinMetaData() != null) {
// JoinTable
arrayTbl = storeMgr.getTable(ownerMmd);
ownerMapping = ((JoinTable) arrayTbl).getOwnerMapping();
} else {
// ForeignKey
arrayTbl = storeMgr.getDatastoreClass(elementType, clr);
ownerMapping = ((DatastoreClass) arrayTbl).getExternalMapping(ownerMmd, MappingType.EXTERNAL_FK);
}
}
SelectStatement subStmt = new SelectStatement(stmt, storeMgr, arrayTbl, null, null);
subStmt.setClassLoaderResolver(clr);
JavaTypeMapping mapping = storeMgr.getMappingManager().getMappingWithDatastoreMapping(String.class, false, false, clr);
SQLExpression countExpr = exprFactory.newLiteral(subStmt, mapping, "COUNT(*)");
((StringLiteral) countExpr).generateStatementWithoutQuotes();
subStmt.select(countExpr, null);
SQLExpression elementOwnerExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(), ownerMapping);
SQLExpression ownerIdExpr = exprFactory.newExpression(stmt, expr.getSQLTable(), expr.getSQLTable().getTable().getIdMapping());
subStmt.whereAnd(elementOwnerExpr.eq(ownerIdExpr), true);
JavaTypeMapping subqMapping = exprFactory.getMappingForType(Integer.class, false);
SQLExpression subqExpr = new NumericSubqueryExpression(stmt, subStmt);
subqExpr.setJavaTypeMapping(subqMapping);
return subqExpr;
}
use of org.datanucleus.store.rdbms.sql.expression.SQLExpression in project datanucleus-rdbms by datanucleus.
the class AvgWithCastFunction method getAggregateExpression.
protected SQLExpression getAggregateExpression(SQLStatement stmt, List args, JavaTypeMapping m) {
Class argType = ((SQLExpression) args.get(0)).getJavaTypeMapping().getJavaType();
List<SQLExpression> checkedArgs = null;
// Only add the CAST if the argument is a non-floating point
if (!argType.equals(Double.class) && !argType.equals(Float.class)) {
checkedArgs = new ArrayList<>();
checkedArgs.add(new StringExpression(stmt, m, "CAST", args, asList("double")));
} else {
checkedArgs = args;
}
return new AggregateNumericExpression(stmt, m, getFunctionName(), checkedArgs);
}
use of org.datanucleus.store.rdbms.sql.expression.SQLExpression in project datanucleus-rdbms by datanucleus.
the class CoalesceFunction 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) {
if (expr == null) {
// Find expression type that this handles - all expressions need to be consistent in our implementation
Class exprType = null;
Class cls = null;
int clsLevel = 0;
for (int i = 0; i < args.size(); i++) {
SQLExpression argExpr = args.get(i);
if (exprType == null) {
if (argExpr instanceof NumericExpression) {
exprType = NumericExpression.class;
cls = Integer.class;
} else if (argExpr instanceof StringExpression) {
exprType = StringExpression.class;
cls = String.class;
} else if (argExpr instanceof TemporalExpression) {
exprType = TemporalExpression.class;
cls = argExpr.getJavaTypeMapping().getJavaType();
} else {
exprType = argExpr.getClass();
cls = argExpr.getJavaTypeMapping().getJavaType();
}
} else {
if (!exprType.isAssignableFrom(argExpr.getClass())) {
throw new NucleusUserException("COALESCE invocation first argument of type " + exprType.getName() + " yet subsequent argument of type " + argExpr.getClass().getName());
}
}
if (exprType == NumericExpression.class) {
// Priority order is Double, Float, BigDecimal, BigInteger, Long, Integer
Class argType = argExpr.getJavaTypeMapping().getJavaType();
if (clsLevel < 5 && (argType == double.class || argType == Double.class)) {
cls = Double.class;
clsLevel = 5;
} else if (clsLevel < 4 && (argType == float.class || argType == Float.class)) {
cls = Float.class;
clsLevel = 4;
} else if (clsLevel < 3 && argType == BigDecimal.class) {
cls = BigDecimal.class;
clsLevel = 3;
} else if (clsLevel < 2 && argType == BigInteger.class) {
cls = BigInteger.class;
clsLevel = 2;
} else if (clsLevel < 1 && (argType == long.class || argType == Long.class)) {
cls = Long.class;
clsLevel = 1;
}
}
}
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
if (exprType == NumericExpression.class) {
return new NumericExpression(stmt, exprFactory.getMappingForType(cls, true), "COALESCE", args);
} else if (exprType == StringExpression.class) {
return new StringExpression(stmt, exprFactory.getMappingForType(cls, true), "COALESCE", args);
} else if (exprType == TemporalExpression.class) {
return new TemporalExpression(stmt, exprFactory.getMappingForType(cls, true), "COALESCE", args);
} else {
return new ObjectExpression(stmt, exprFactory.getMappingForType(cls, true), "COALESCE", args);
}
}
throw new NucleusException(Localiser.msg("060002", "COALESCE", expr));
}
use of org.datanucleus.store.rdbms.sql.expression.SQLExpression in project datanucleus-rdbms by datanucleus.
the class IndexFunction 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 ignore, List args) {
if (ignore == null) {
if (args == null || args.size() != 2) {
throw new NucleusException("INDEX can only be used with 2 arguments - the element expression, and the collection expression");
}
SQLExpression elemSqlExpr = (SQLExpression) args.get(0);
SQLExpression collSqlExpr = (SQLExpression) args.get(1);
AbstractMemberMetaData mmd = collSqlExpr.getJavaTypeMapping().getMemberMetaData();
if (!mmd.hasCollection()) {
throw new NucleusException("INDEX expression for field " + mmd.getFullFieldName() + " does not represent a collection!");
} else if (!mmd.getOrderMetaData().isIndexedList()) {
throw new NucleusException("INDEX expression for field " + mmd.getFullFieldName() + " does not represent an indexed list!");
}
JavaTypeMapping orderMapping = null;
SQLTable orderTable = null;
Table joinTbl = stmt.getRDBMSManager().getTable(mmd);
if (joinTbl != null) {
// 1-N via join table
CollectionTable collTable = (CollectionTable) joinTbl;
orderTable = stmt.getTableForDatastoreContainer(collTable);
// TODO If the join table is not yet referenced, or referenced multiple times then fix this
orderMapping = collTable.getOrderMapping();
} else {
// 1-N via FK
orderTable = elemSqlExpr.getSQLTable();
orderMapping = ((ClassTable) elemSqlExpr.getSQLTable().getTable()).getExternalMapping(mmd, MappingType.EXTERNAL_INDEX);
}
return new NumericExpression(stmt, orderTable, orderMapping);
}
throw new NucleusException(Localiser.msg("060002", "INDEX", ignore));
}
Aggregations