use of org.datanucleus.store.rdbms.sql.expression.NullLiteral in project datanucleus-rdbms by datanucleus.
the class DiscriminatorStatementGenerator method getStatement.
/**
* Accessor for the SelectStatement.
* @param ec ExecutionContext
* @return The SelectStatement for iterating through objects with a discriminator column
*/
public SelectStatement getStatement(ExecutionContext ec) {
SelectStatement stmt = null;
SQLTable discrimSqlTbl = null;
if (joinTable == null) {
// Select of candidate table
stmt = new SelectStatement(parentStmt, storeMgr, candidateTable, candidateTableAlias, candidateTableGroupName);
stmt.setClassLoaderResolver(clr);
discrimSqlTbl = stmt.getPrimaryTable();
} else {
// Select of join table, with join to element table
stmt = new SelectStatement(parentStmt, storeMgr, joinTable, joinTableAlias, candidateTableGroupName);
stmt.setClassLoaderResolver(clr);
JavaTypeMapping candidateIdMapping = candidateTable.getIdMapping();
if (hasOption(OPTION_ALLOW_NULLS)) {
// Put element table in same table group since all relates to the elements
discrimSqlTbl = stmt.join(JoinType.LEFT_OUTER_JOIN, null, joinElementMapping, candidateTable, null, candidateIdMapping, null, stmt.getPrimaryTable().getGroupName(), true);
} else {
// Put element table in same table group since all relates to the elements
discrimSqlTbl = stmt.join(JoinType.INNER_JOIN, null, joinElementMapping, candidateTable, null, candidateIdMapping, null, stmt.getPrimaryTable().getGroupName(), true);
}
}
JavaTypeMapping discMapping = candidateTable.getSurrogateMapping(SurrogateColumnType.DISCRIMINATOR, true);
if (discMapping != null) {
// Allow for discriminator being in super-table of the candidate table
discrimSqlTbl = SQLStatementHelper.getSQLTableForMappingOfTable(stmt, discrimSqlTbl, discMapping);
}
DiscriminatorMetaData dismd = discrimSqlTbl.getTable().getDiscriminatorMetaData();
boolean hasDiscriminator = (discMapping != null && dismd != null && dismd.getStrategy() != DiscriminatorStrategy.NONE);
// Check if we can omit the discriminator restriction
boolean restrictDiscriminator = hasOption(OPTION_RESTRICT_DISCRIM);
if (hasDiscriminator && restrictDiscriminator) {
// Add the discriminator expression to restrict accepted values
boolean multipleCandidates = false;
BooleanExpression discExpr = null;
if (candidates != null) {
// Multiple candidates
if (candidates.length > 1) {
multipleCandidates = true;
}
for (int i = 0; i < candidates.length; i++) {
if (Modifier.isAbstract(candidates[i].getModifiers())) {
// No point selecting this candidate since can't be instantiated
continue;
}
BooleanExpression discExprCandidate = SQLStatementHelper.getExpressionForDiscriminatorForClass(stmt, candidates[i].getName(), dismd, discMapping, discrimSqlTbl, clr);
if (discExpr != null) {
discExpr = discExpr.ior(discExprCandidate);
} else {
discExpr = discExprCandidate;
}
if (includeSubclasses) {
Collection<String> subclassNames = storeMgr.getSubClassesForClass(candidateType.getName(), true, clr);
Iterator<String> subclassIter = subclassNames.iterator();
if (!multipleCandidates) {
multipleCandidates = (subclassNames.size() > 0);
}
while (subclassIter.hasNext()) {
String subclassName = subclassIter.next();
BooleanExpression discExprSub = SQLStatementHelper.getExpressionForDiscriminatorForClass(stmt, subclassName, dismd, discMapping, discrimSqlTbl, clr);
discExpr = discExpr.ior(discExprSub);
}
}
}
} else {
// Single candidate
if (!Modifier.isAbstract(candidateType.getModifiers())) {
discExpr = SQLStatementHelper.getExpressionForDiscriminatorForClass(stmt, candidateType.getName(), dismd, discMapping, discrimSqlTbl, clr);
}
if (includeSubclasses) {
Collection<String> subclassNames = storeMgr.getSubClassesForClass(candidateType.getName(), true, clr);
Iterator<String> subclassIter = subclassNames.iterator();
multipleCandidates = (subclassNames.size() > 0);
while (subclassIter.hasNext()) {
String subclassName = subclassIter.next();
Class subclass = clr.classForName(subclassName);
if ((Modifier.isAbstract(subclass.getModifiers()))) {
continue;
}
BooleanExpression discExprCandidate = SQLStatementHelper.getExpressionForDiscriminatorForClass(stmt, subclassName, dismd, discMapping, discrimSqlTbl, clr);
if (discExpr == null) {
discExpr = discExprCandidate;
} else {
discExpr = discExpr.ior(discExprCandidate);
}
}
}
if (discExpr == null) {
// No possible candidates, so set expression as "1=0"
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
JavaTypeMapping m = exprFactory.getMappingForType(boolean.class, true);
discExpr = exprFactory.newLiteral(stmt, m, true).eq(exprFactory.newLiteral(stmt, m, false));
}
}
if (discExpr != null) {
if (hasOption(OPTION_ALLOW_NULLS)) {
// Allow for null value of discriminator
SQLExpression expr = stmt.getSQLExpressionFactory().newExpression(stmt, discrimSqlTbl, discMapping);
SQLExpression val = new NullLiteral(stmt, null, null, null);
BooleanExpression nullDiscExpr = expr.eq(val);
discExpr = discExpr.ior(nullDiscExpr);
if (!multipleCandidates) {
multipleCandidates = true;
}
}
// Apply the discriminator to the query statement
if (multipleCandidates) {
discExpr.encloseInParentheses();
}
stmt.whereAnd(discExpr, true);
}
}
JavaTypeMapping multitenancyMapping = candidateTable.getSurrogateMapping(SurrogateColumnType.MULTITENANCY, false);
if (multitenancyMapping != null) {
// Multi-tenancy restriction
AbstractClassMetaData cmd = candidateTable.getClassMetaData();
SQLTable tenantSqlTbl = stmt.getTable(multitenancyMapping.getTable(), discrimSqlTbl.getGroupName());
SQLExpression tenantExpr = stmt.getSQLExpressionFactory().newExpression(stmt, tenantSqlTbl, multitenancyMapping);
SQLExpression tenantVal = stmt.getSQLExpressionFactory().newLiteral(stmt, multitenancyMapping, ec.getNucleusContext().getMultiTenancyId(ec, cmd));
stmt.whereAnd(tenantExpr.eq(tenantVal), true);
}
JavaTypeMapping softDeleteMapping = candidateTable.getSurrogateMapping(SurrogateColumnType.SOFTDELETE, false);
if (softDeleteMapping != null && !hasOption(OPTION_INCLUDE_SOFT_DELETES)) {
// Soft-delete restriction
SQLTable softDeleteSqlTbl = stmt.getTable(softDeleteMapping.getTable(), discrimSqlTbl.getGroupName());
SQLExpression softDeleteExpr = stmt.getSQLExpressionFactory().newExpression(stmt, softDeleteSqlTbl, softDeleteMapping);
SQLExpression softDeleteVal = stmt.getSQLExpressionFactory().newLiteral(stmt, softDeleteMapping, Boolean.FALSE);
stmt.whereAnd(softDeleteExpr.eq(softDeleteVal), true);
}
return stmt;
}
use of org.datanucleus.store.rdbms.sql.expression.NullLiteral in project datanucleus-rdbms by datanucleus.
the class MathAbsMethod 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<SQLExpression> args) {
if (args == null || args.size() == 0) {
throw new NucleusUserException("Cannot invoke Math.abs without an argument");
}
SQLExpression expr = args.get(0);
if (expr == null) {
return new NullLiteral(stmt, null, null, null);
} else if (expr instanceof SQLLiteral) {
if (expr instanceof ByteLiteral) {
int originalValue = ((BigInteger) ((ByteLiteral) expr).getValue()).intValue();
BigInteger absValue = new BigInteger(String.valueOf(Math.abs(originalValue)));
return new ByteLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
} else if (expr instanceof IntegerLiteral) {
int originalValue = ((Number) ((IntegerLiteral) expr).getValue()).intValue();
Integer absValue = Integer.valueOf(Math.abs(originalValue));
return new IntegerLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
} else if (expr instanceof FloatingPointLiteral) {
double originalValue = ((BigDecimal) ((FloatingPointLiteral) expr).getValue()).doubleValue();
Double absValue = new Double(Math.abs(originalValue));
return new FloatingPointLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
}
throw new IllegalExpressionOperationException("Math.abs()", expr);
} else {
// Relay to the equivalent "abs(expr)" function
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
return exprFactory.invokeMethod(stmt, null, "abs", null, args);
}
}
use of org.datanucleus.store.rdbms.sql.expression.NullLiteral in project datanucleus-rdbms by datanucleus.
the class MathAsinMethod 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<SQLExpression> args) {
if (args == null || args.size() == 0) {
throw new NucleusUserException("Cannot invoke Math.asin without an argument");
}
SQLExpression expr = args.get(0);
if (expr == null) {
return new NullLiteral(stmt, null, null, null);
} else if (expr instanceof SQLLiteral) {
if (expr instanceof ByteLiteral) {
int originalValue = ((BigInteger) ((ByteLiteral) expr).getValue()).intValue();
BigInteger absValue = new BigInteger(String.valueOf(Math.asin(originalValue)));
return new ByteLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
} else if (expr instanceof IntegerLiteral) {
int originalValue = ((Number) ((IntegerLiteral) expr).getValue()).intValue();
Double absValue = new Double(Math.asin(originalValue));
return new FloatingPointLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
} else if (expr instanceof FloatingPointLiteral) {
double originalValue = ((BigDecimal) ((FloatingPointLiteral) expr).getValue()).doubleValue();
Double absValue = new Double(Math.asin(originalValue));
return new FloatingPointLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
}
throw new IllegalExpressionOperationException("Math.asin()", expr);
} else {
// Relay to the equivalent "asin(expr)" function
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
return exprFactory.invokeMethod(stmt, null, "asin", null, args);
}
}
use of org.datanucleus.store.rdbms.sql.expression.NullLiteral in project datanucleus-rdbms by datanucleus.
the class ListGetMethod 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 || args.size() > 1) {
throw new NucleusException(Localiser.msg("060016", "get", "CollectionExpression", 1));
}
CollectionExpression listExpr = (CollectionExpression) expr;
AbstractMemberMetaData mmd = listExpr.getJavaTypeMapping().getMemberMetaData();
if (!List.class.isAssignableFrom(mmd.getType())) {
throw new UnsupportedOperationException("Query contains " + expr + ".get(int) yet the field is not a List!");
} else if (mmd.getOrderMetaData() != null && !mmd.getOrderMetaData().isIndexedList()) {
throw new UnsupportedOperationException("Query contains " + expr + ".get(int) yet the field is not an 'indexed' List!");
}
SQLExpression idxExpr = args.get(0);
if (idxExpr instanceof SQLLiteral) {
if (!(((SQLLiteral) idxExpr).getValue() instanceof Number)) {
throw new UnsupportedOperationException("Query contains " + expr + ".get(int) yet the index is not a numeric literal so not yet supported");
}
} else {
throw new UnsupportedOperationException("Query contains " + expr + ".get(int) yet the index is not a numeric literal so not yet supported");
}
if (listExpr instanceof CollectionLiteral && idxExpr instanceof SQLLiteral) {
CollectionLiteral lit = (CollectionLiteral) expr;
if (lit.getValue() == null) {
return new NullLiteral(stmt, null, null, null);
}
return lit.invoke("get", args);
}
if (stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.FILTER) {
return getAsInnerJoin(stmt, listExpr, idxExpr);
} else if (stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.ORDERING || stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.RESULT || stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.HAVING) {
return getAsSubquery(stmt, listExpr, idxExpr);
}
throw new NucleusException("List.get() is not supported for " + listExpr + " with argument " + idxExpr + " for query component " + stmt.getQueryGenerator().getCompilationComponent());
}
use of org.datanucleus.store.rdbms.sql.expression.NullLiteral in project datanucleus-rdbms by datanucleus.
the class MathCosMethod 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<SQLExpression> args) {
if (args == null || args.size() == 0) {
throw new NucleusUserException("Cannot invoke Math.cos without an argument");
}
SQLExpression expr = args.get(0);
if (expr == null) {
return new NullLiteral(stmt, null, null, null);
} else if (expr instanceof SQLLiteral) {
if (expr instanceof ByteLiteral) {
int originalValue = ((BigInteger) ((ByteLiteral) expr).getValue()).intValue();
BigInteger absValue = new BigInteger(String.valueOf(Math.cos(originalValue)));
return new ByteLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
} else if (expr instanceof IntegerLiteral) {
int originalValue = ((Number) ((IntegerLiteral) expr).getValue()).intValue();
Double absValue = new Double(Math.cos(originalValue));
return new FloatingPointLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
} else if (expr instanceof FloatingPointLiteral) {
double originalValue = ((BigDecimal) ((FloatingPointLiteral) expr).getValue()).doubleValue();
Double absValue = new Double(Math.cos(originalValue));
return new FloatingPointLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
}
throw new IllegalExpressionOperationException("Math.cos()", expr);
} else {
// Relay to the equivalent "cos(expr)" function
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
return exprFactory.invokeMethod(stmt, null, "cos", null, args);
}
}
Aggregations