use of org.datanucleus.exceptions.NucleusException in project datanucleus-rdbms by datanucleus.
the class StringLength3Method 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 instanceof StringLiteral) {
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
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), "LENGTH", funcArgs);
} else {
throw new NucleusException(Localiser.msg("060001", "length", expr));
}
}
use of org.datanucleus.exceptions.NucleusException in project datanucleus-rdbms by datanucleus.
the class SimpleNumericAggregateMethod 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) {
throw new NucleusException(Localiser.msg("060002", getFunctionName(), expr));
}
if (args == null || args.size() != 1) {
throw new NucleusException(getFunctionName() + " is only supported with a single argument");
}
if (stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.RESULT || stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.HAVING) {
// FUNC(argExpr)
// Use same java type as the argument
SQLExpression argExpr = args.get(0);
JavaTypeMapping m = argExpr.getJavaTypeMapping();
return new AggregateNumericExpression(stmt, m, getFunctionName(), args);
}
ClassLoaderResolver clr = stmt.getQueryGenerator().getClassLoaderResolver();
// Handle as Subquery "SELECT AVG(expr) FROM tbl"
SQLExpression argExpr = args.get(0);
SelectStatement subStmt = new SelectStatement(stmt, stmt.getRDBMSManager(), argExpr.getSQLTable().getTable(), argExpr.getSQLTable().getAlias(), null);
subStmt.setClassLoaderResolver(clr);
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
JavaTypeMapping mapping = stmt.getRDBMSManager().getMappingManager().getMappingWithColumnMapping(String.class, false, false, clr);
String aggregateString = getFunctionName() + "(" + argExpr.toSQLText() + ")";
SQLExpression aggExpr = exprFactory.newLiteral(subStmt, mapping, aggregateString);
((StringLiteral) aggExpr).generateStatementWithoutQuotes();
subStmt.select(aggExpr, null);
JavaTypeMapping subqMapping = exprFactory.getMappingForType(Integer.class, false);
SQLExpression subqExpr = new NumericSubqueryExpression(stmt, subStmt);
subqExpr.setJavaTypeMapping(subqMapping);
return subqExpr;
}
use of org.datanucleus.exceptions.NucleusException in project datanucleus-rdbms by datanucleus.
the class StringConcat1Method 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(Localiser.msg("060003", "concat", "StringExpression", 0, "StringExpression/CharacterExpression/Parameter"));
}
SQLExpression otherExpr = args.get(0);
if (!(otherExpr instanceof StringExpression) && !(otherExpr instanceof CharacterExpression) && !(otherExpr instanceof ParameterLiteral)) {
throw new NucleusException(Localiser.msg("060003", "concat", "StringExpression", 0, "StringExpression/CharacterExpression/Parameter"));
}
ArrayList funcArgs = new ArrayList();
funcArgs.add(expr);
funcArgs.add(otherExpr);
return new StringExpression(expr, Expression.OP_CONCAT, otherExpr);
}
use of org.datanucleus.exceptions.NucleusException in project datanucleus-rdbms by datanucleus.
the class RDBMSStoreManager method deleteSchemaForClasses.
public void deleteSchemaForClasses(Set<String> classNames, Properties props) {
if (!classNames.isEmpty()) {
// Delete the tables
String ddlFilename = props != null ? props.getProperty("ddlFilename") : null;
String completeDdlProp = props != null ? props.getProperty("completeDdl") : null;
boolean completeDdl = completeDdlProp != null && completeDdlProp.equalsIgnoreCase("true");
String autoStartProp = props != null ? props.getProperty("autoStartTable") : null;
boolean autoStart = autoStartProp != null && autoStartProp.equalsIgnoreCase("true");
ClassLoaderResolver clr = nucleusContext.getClassLoaderResolver(null);
FileWriter ddlFileWriter = null;
try {
performingDeleteSchemaForClasses = true;
if (ddlFilename != null) {
// Open the DDL file for writing
File ddlFile = StringUtils.getFileForFilename(ddlFilename);
if (ddlFile.exists()) {
// Delete existing file
ddlFile.delete();
}
if (ddlFile.getParentFile() != null && !ddlFile.getParentFile().exists()) {
// Make sure the directory exists
ddlFile.getParentFile().mkdirs();
}
ddlFile.createNewFile();
ddlFileWriter = new FileWriter(ddlFile);
SimpleDateFormat fmt = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
ddlFileWriter.write("------------------------------------------------------------------\n");
ddlFileWriter.write("-- DataNucleus SchemaTool " + "(ran at " + fmt.format(new java.util.Date()) + ")\n");
ddlFileWriter.write("------------------------------------------------------------------\n");
ddlFileWriter.write("-- Delete schema required for the following classes:-\n");
Iterator classNameIter = classNames.iterator();
while (classNameIter.hasNext()) {
ddlFileWriter.write("-- " + classNameIter.next() + "\n");
}
ddlFileWriter.write("--\n");
}
try {
if (ddlFileWriter != null) {
this.ddlWriter = ddlFileWriter;
this.completeDDL = completeDdl;
this.writtenDdlStatements = new HashSet();
}
// Generate the tables/constraints for these classes (so we know the tables to delete)
// TODO This will add CREATE to the DDL, need to be able to omit this
String[] classNameArray = classNames.toArray(new String[classNames.size()]);
// Add them to mgr first
manageClasses(clr, classNameArray);
// Delete the tables of the required classes TODO Why use READ_COMMITTED for delete but SERIALIZABLE for add?
int isolationLevel = hasProperty(PropertyNames.PROPERTY_SCHEMA_TXN_ISOLATION) ? TransactionUtils.getTransactionIsolationLevelForName(getStringProperty(PropertyNames.PROPERTY_SCHEMA_TXN_ISOLATION)) : Connection.TRANSACTION_READ_COMMITTED;
DeleteTablesSchemaTransaction deleteTablesTxn = new DeleteTablesSchemaTransaction(this, isolationLevel, storeDataMgr);
deleteTablesTxn.setWriter(ddlWriter);
boolean success = true;
try {
deleteTablesTxn.execute(clr);
} catch (NucleusException ne) {
success = false;
throw ne;
} finally {
if (success) {
clearSchemaData();
}
}
if (autoStart) {
// TODO Delete the SchemaTable auto-starter table
}
// TODO Delete sequences and sequenceTables
} finally {
performingDeleteSchemaForClasses = false;
if (ddlFileWriter != null) {
this.ddlWriter = null;
this.completeDDL = false;
this.writtenDdlStatements.clear();
this.writtenDdlStatements = null;
ddlFileWriter.close();
}
}
} catch (IOException ioe) {
// Error in writing DDL file
// TODO Handle this
}
} else {
String msg = Localiser.msg("014039");
NucleusLogger.DATASTORE_SCHEMA.error(msg);
System.out.println(msg);
throw new NucleusException(msg);
}
}
use of org.datanucleus.exceptions.NucleusException in project datanucleus-rdbms by datanucleus.
the class RDBMSStoreManager method initialiseIdentifierFactory.
/**
* Method to create the IdentifierFactory to be used by this store.
* Relies on the datastore adapter existing before creation
* @param nucleusContext context
*/
protected void initialiseIdentifierFactory(NucleusContext nucleusContext) {
if (dba == null) {
throw new NucleusException("DatastoreAdapter not yet created so cannot create IdentifierFactory!");
}
String idFactoryName = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_FACTORY);
try {
// Create the control properties for identifier generation
Map props = new HashMap();
if (catalogName != null) {
props.put(IdentifierFactory.PROPERTY_DEFAULT_CATALOG, catalogName);
}
if (schemaName != null) {
props.put(IdentifierFactory.PROPERTY_DEFAULT_SCHEMA, schemaName);
}
String val = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_CASE);
props.put(IdentifierFactory.PROPERTY_REQUIRED_CASE, val != null ? val : getDefaultIdentifierCase());
val = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_WORD_SEPARATOR);
if (val != null) {
props.put(IdentifierFactory.PROPERTY_WORD_SEPARATOR, val);
}
val = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_TABLE_PREFIX);
if (val != null) {
props.put(IdentifierFactory.PROPERTY_TABLE_PREFIX, val);
}
val = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_TABLE_SUFFIX);
if (val != null) {
props.put(IdentifierFactory.PROPERTY_TABLE_SUFFIX, val);
}
props.put(IdentifierFactory.PROPERTY_NAMING_FACTORY, getNamingFactory());
// Create the IdentifierFactory
ClassLoaderResolver clr = nucleusContext.getClassLoaderResolver(null);
if ("datanucleus2".equalsIgnoreCase(idFactoryName)) {
identifierFactory = new DN2IdentifierFactory(dba, clr, props);
} else if ("jpa".equalsIgnoreCase(idFactoryName) || "jakarta".equalsIgnoreCase(idFactoryName)) {
identifierFactory = new JPAIdentifierFactory(dba, clr, props);
} else if ("datanucleus1".equalsIgnoreCase(idFactoryName)) {
identifierFactory = new DNIdentifierFactory(dba, clr, props);
} else if ("jpox".equalsIgnoreCase(idFactoryName)) {
identifierFactory = new JPOXIdentifierFactory(dba, clr, props);
} else {
// Fallback to the plugin mechanism
Class[] argTypes = new Class[] { DatastoreAdapter.class, ClassConstants.CLASS_LOADER_RESOLVER, Map.class };
Object[] args = new Object[] { dba, nucleusContext.getClassLoaderResolver(null), props };
identifierFactory = (IdentifierFactory) nucleusContext.getPluginManager().createExecutableExtension("org.datanucleus.store.rdbms.identifierfactory", "name", idFactoryName, "class-name", argTypes, args);
}
} catch (ClassNotFoundException cnfe) {
throw new NucleusUserException(Localiser.msg("039004", idFactoryName), cnfe).setFatal();
} catch (Exception e) {
NucleusLogger.PERSISTENCE.error("Exception creating IdentifierFactory", e);
throw new NucleusException(Localiser.msg("039005", idFactoryName), e).setFatal();
}
}
Aggregations