use of org.datanucleus.store.rdbms.sql.expression.BooleanLiteral 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.BooleanLiteral in project datanucleus-rdbms by datanucleus.
the class MapContainsEntryMethod 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() != 2) {
throw new NucleusException(Localiser.msg("060016", "containsValue", "MapExpression", 2));
}
MapExpression mapExpr = (MapExpression) expr;
SQLExpression keyExpr = args.get(0);
SQLExpression valExpr = args.get(1);
if (keyExpr.isParameter()) {
// Key is a parameter so make sure its type is set
AbstractMemberMetaData mmd = mapExpr.getJavaTypeMapping().getMemberMetaData();
if (mmd != null && mmd.getMap() != null) {
Class keyCls = stmt.getQueryGenerator().getClassLoaderResolver().classForName(mmd.getMap().getKeyType());
stmt.getQueryGenerator().bindParameter(keyExpr.getParameterName(), keyCls);
}
}
if (valExpr.isParameter()) {
// Value is a parameter so make sure its type is set
AbstractMemberMetaData mmd = mapExpr.getJavaTypeMapping().getMemberMetaData();
if (mmd != null && mmd.getMap() != null) {
Class valCls = stmt.getQueryGenerator().getClassLoaderResolver().classForName(mmd.getMap().getValueType());
stmt.getQueryGenerator().bindParameter(valExpr.getParameterName(), valCls);
}
}
if (mapExpr instanceof MapLiteral) {
MapLiteral lit = (MapLiteral) mapExpr;
Map map = (Map) lit.getValue();
if (map == null || map.size() == 0) {
return new BooleanLiteral(stmt, expr.getJavaTypeMapping(), Boolean.FALSE);
}
// TODO Handle this
return lit.getValueLiteral().invoke("contains", args);
}
if (stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.FILTER) {
boolean needsSubquery = getNeedsSubquery(stmt);
// TODO Check if *this* "containsEntry" is negated, not any of them (and remove above check)
if (needsSubquery) {
NucleusLogger.QUERY.debug("MapContainsEntry on " + mapExpr + "(" + keyExpr + "," + valExpr + ") using SUBQUERY");
return containsAsSubquery(stmt, mapExpr, keyExpr, valExpr);
}
NucleusLogger.QUERY.debug("MapContainsEntry on " + mapExpr + "(" + keyExpr + "," + valExpr + ") using INNERJOIN");
return containsAsInnerJoin(stmt, mapExpr, keyExpr, valExpr);
}
return containsAsSubquery(stmt, mapExpr, keyExpr, valExpr);
}
use of org.datanucleus.store.rdbms.sql.expression.BooleanLiteral in project datanucleus-rdbms by datanucleus.
the class MapIsEmptyMethod 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", "MapExpression"));
}
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
if (expr instanceof MapLiteral) {
Map map = (Map) ((MapLiteral) expr).getValue();
boolean isEmpty = (map == null || map.size() == 0);
JavaTypeMapping m = exprFactory.getMappingForType(boolean.class, false);
return new BooleanLiteral(stmt, m, isEmpty ? Boolean.TRUE : Boolean.FALSE);
}
SQLExpression sizeExpr = exprFactory.invokeMethod(stmt, Map.class.getName(), "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.BooleanLiteral in project datanucleus-rdbms by datanucleus.
the class StringMatchesMethod 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() > 2) {
throw new NucleusException("Incorrect arguments for String.matches(StringExpression)");
} else if (!(args.get(0) instanceof StringExpression) && !(args.get(0) instanceof ParameterLiteral)) {
throw new NucleusException("Incorrect arguments for String.matches(StringExpression)");
}
SQLExpression likeExpr = args.get(0);
if (!(likeExpr instanceof StringExpression) && !(likeExpr instanceof CharacterExpression) && !(likeExpr instanceof ParameterLiteral)) {
throw new NucleusException(Localiser.msg("060003", "like/matches", "StringExpression", 0, "StringExpression/CharacterExpression/ParameterLiteral"));
}
SQLExpression escapeExpr = null;
if (args.size() > 1) {
escapeExpr = args.get(1);
}
if ((likeExpr instanceof StringLiteral || likeExpr instanceof ParameterLiteral) && likeExpr.isParameter()) {
// Argument as parameter needs translation to use SQL "LIKE" syntax, so has to be embedded as literal
stmt.getQueryGenerator().useParameterExpressionAsLiteral((SQLLiteral) likeExpr);
}
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
if (expr instanceof StringLiteral && likeExpr instanceof StringLiteral) {
// String.matches(String) so evaluate in-memory
String primary = (String) ((StringLiteral) expr).getValue();
String pattern = (String) ((StringLiteral) likeExpr).getValue();
return new BooleanLiteral(stmt, exprFactory.getMappingForType(boolean.class, false), primary.matches(pattern));
} else if (expr instanceof StringLiteral) {
return getBooleanLikeExpression(stmt, expr, likeExpr, escapeExpr);
} else if (expr instanceof StringExpression && likeExpr instanceof StringLiteral) {
// Convert the pattern to use the regex constructs suitable for the datastore
String pattern = (String) ((StringLiteral) likeExpr).getValue();
if (stmt.getQueryGenerator().getQueryLanguage().equalsIgnoreCase(Query.LANGUAGE_JDOQL)) {
// JDOQL input is in java.lang.String regular expression format, so convert to SQL like
boolean caseSensitive = false;
if (pattern.startsWith("(?i)")) {
caseSensitive = true;
pattern = pattern.substring(4);
}
DatastoreAdapter dba = stmt.getDatastoreAdapter();
RegularExpressionConverter converter = new RegularExpressionConverter(dba.getPatternExpressionZeroMoreCharacters().charAt(0), dba.getPatternExpressionAnyCharacter().charAt(0), dba.getEscapeCharacter().charAt(0));
if (caseSensitive) {
SQLExpression patternExpr = exprFactory.newLiteral(stmt, likeExpr.getJavaTypeMapping(), converter.convert(pattern).toLowerCase());
return getBooleanLikeExpression(stmt, expr.invoke("toLowerCase", null), patternExpr, escapeExpr);
}
SQLExpression patternExpr = exprFactory.newLiteral(stmt, likeExpr.getJavaTypeMapping(), converter.convert(pattern));
return getBooleanLikeExpression(stmt, expr, patternExpr, escapeExpr);
}
SQLExpression patternExpr = exprFactory.newLiteral(stmt, likeExpr.getJavaTypeMapping(), pattern);
return getBooleanLikeExpression(stmt, expr, patternExpr, escapeExpr);
} else if (expr instanceof StringExpression) {
return getExpressionForStringExpressionInput(stmt, expr, likeExpr, escapeExpr);
} else {
throw new NucleusException(Localiser.msg("060001", "matches", expr));
}
}
use of org.datanucleus.store.rdbms.sql.expression.BooleanLiteral in project datanucleus-rdbms by datanucleus.
the class CollectionIsEmptyMethod 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", "CollectionExpression"));
}
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
if (expr instanceof CollectionLiteral) {
Collection coll = (Collection) ((CollectionLiteral) expr).getValue();
boolean isEmpty = (coll == null || coll.size() == 0);
JavaTypeMapping m = exprFactory.getMappingForType(boolean.class, false);
return new BooleanLiteral(stmt, m, isEmpty ? Boolean.TRUE : Boolean.FALSE);
}
AbstractMemberMetaData mmd = ((CollectionExpression) expr).getJavaTypeMapping().getMemberMetaData();
if (mmd.isSerialized()) {
throw new NucleusUserException("Cannot perform Collection.isEmpty when the collection is being serialised");
}
ClassLoaderResolver clr = stmt.getQueryGenerator().getClassLoaderResolver();
ApiAdapter api = stmt.getRDBMSManager().getApiAdapter();
Class elementType = clr.classForName(mmd.getCollection().getElementType());
if (!api.isPersistable(elementType) && mmd.getJoinMetaData() == null) {
throw new NucleusUserException("Cannot perform Collection.isEmpty when the collection<Non-Persistable> is not in a join table");
}
SQLExpression sizeExpr = exprFactory.invokeMethod(stmt, Collection.class.getName(), "size", expr, args);
JavaTypeMapping mapping = exprFactory.getMappingForType(Integer.class, true);
SQLExpression zeroExpr = exprFactory.newLiteral(stmt, mapping, 0);
return sizeExpr.eq(zeroExpr);
}
Aggregations