use of org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory in project datanucleus-rdbms by datanucleus.
the class MapSizeMethod 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", "MapExpression"));
}
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
if (expr instanceof MapLiteral) {
// Just return the map length since we have the value
Map map = (Map) ((MapLiteral) expr).getValue();
return exprFactory.newLiteral(stmt, exprFactory.getMappingForType(int.class, false), Integer.valueOf(map.size()));
}
AbstractMemberMetaData ownerMmd = expr.getJavaTypeMapping().getMemberMetaData();
RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
ClassLoaderResolver clr = stmt.getQueryGenerator().getClassLoaderResolver();
// TODO Allow for interface keys/values, etc
JavaTypeMapping ownerMapping = null;
Table mapTbl = null;
if (ownerMmd.getMap().getMapType() == MapType.MAP_TYPE_JOIN) {
// JoinTable
mapTbl = storeMgr.getTable(ownerMmd);
ownerMapping = ((JoinTable) mapTbl).getOwnerMapping();
} else if (ownerMmd.getMap().getMapType() == MapType.MAP_TYPE_KEY_IN_VALUE) {
// ForeignKey from value table to key
AbstractClassMetaData valueCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(ownerMmd.getMap().getValueType(), clr);
mapTbl = storeMgr.getDatastoreClass(ownerMmd.getMap().getValueType(), clr);
if (ownerMmd.getMappedBy() != null) {
ownerMapping = mapTbl.getMemberMapping(valueCmd.getMetaDataForMember(ownerMmd.getMappedBy()));
} else {
ownerMapping = ((DatastoreClass) mapTbl).getExternalMapping(ownerMmd, MappingType.EXTERNAL_FK);
}
} else if (ownerMmd.getMap().getMapType() == MapType.MAP_TYPE_VALUE_IN_KEY) {
// ForeignKey from key table to value
AbstractClassMetaData keyCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(ownerMmd.getMap().getKeyType(), clr);
mapTbl = storeMgr.getDatastoreClass(ownerMmd.getMap().getKeyType(), clr);
if (ownerMmd.getMappedBy() != null) {
ownerMapping = mapTbl.getMemberMapping(keyCmd.getMetaDataForMember(ownerMmd.getMappedBy()));
} else {
ownerMapping = ((DatastoreClass) mapTbl).getExternalMapping(ownerMmd, MappingType.EXTERNAL_FK);
}
} else {
throw new NucleusException("Invalid map for " + expr + " in size() call");
}
SelectStatement subStmt = new SelectStatement(stmt, storeMgr, mapTbl, 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.SQLExpressionFactory in project datanucleus-rdbms by datanucleus.
the class MathAcosMethod 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.acos 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.acos(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.acos(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.acos(originalValue));
return new FloatingPointLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
}
throw new IllegalExpressionOperationException("Math.acos()", expr);
} else {
// Relay to the equivalent "acos(expr)" function
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
return exprFactory.invokeMethod(stmt, null, "acos", null, args);
}
}
use of org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory in project datanucleus-rdbms by datanucleus.
the class MathToDegreesMethod 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.toDegrees 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.toDegrees()", expr);
} else {
// Relay to the equivalent "degrees(expr)" function
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
return exprFactory.invokeMethod(stmt, null, "degrees", null, args);
}
}
use of org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory in project datanucleus-rdbms by datanucleus.
the class OptionalGetMethod 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("Optional.get should be passed no arguments");
}
OptionalMapping opMapping = (OptionalMapping) ((OptionalExpression) expr).getJavaTypeMapping();
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
return exprFactory.newExpression(stmt, expr.getSQLTable(), opMapping.getWrappedMapping());
}
use of org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory in project datanucleus-rdbms by datanucleus.
the class OptionalOrElseMethod 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() != 1) {
throw new NucleusException("Optional.orElse should be passed 1 argument");
}
SQLExpression elseExpr = args.get(0);
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
OptionalMapping opMapping = (OptionalMapping) ((OptionalExpression) expr).getJavaTypeMapping();
JavaTypeMapping javaMapping = opMapping.getWrappedMapping();
SQLExpression getExpr = exprFactory.newExpression(stmt, expr.getSQLTable(), javaMapping);
SQLExpression isNotNullExpr = exprFactory.newExpression(stmt, expr.getSQLTable(), javaMapping).ne(new NullLiteral(stmt, javaMapping, null, null));
if (javaMapping instanceof StringMapping) {
return new CaseStringExpression(new SQLExpression[] { isNotNullExpr }, new SQLExpression[] { getExpr }, elseExpr);
} else if (javaMapping instanceof IntegerMapping || javaMapping instanceof LongMapping || javaMapping instanceof ShortMapping || javaMapping instanceof FloatMapping || javaMapping instanceof DoubleMapping || javaMapping instanceof BigIntegerMapping || javaMapping instanceof BigDecimalMapping) // TODO Maybe use javaMapping.getJavaType compared to Number to avoid the check above
{
return new CaseNumericExpression(new SQLExpression[] { isNotNullExpr }, new SQLExpression[] { getExpr }, elseExpr);
} else if (javaMapping instanceof BooleanMapping) {
return new CaseBooleanExpression(new SQLExpression[] { isNotNullExpr }, new SQLExpression[] { getExpr }, elseExpr);
}
return new CaseExpression(new SQLExpression[] { isNotNullExpr }, new SQLExpression[] { getExpr }, elseExpr);
}
Aggregations