use of org.datanucleus.store.rdbms.RDBMSStoreManager in project datanucleus-rdbms by datanucleus.
the class MaxGenerator method getStatement.
/**
* Return the SQL statement.
* TODO Allow this to work in different catalog/schema
* @return statement
*/
private String getStatement() {
RDBMSStoreManager srm = (RDBMSStoreManager) storeMgr;
StringBuilder stmt = new StringBuilder();
stmt.append("SELECT max(");
stmt.append(srm.getIdentifierFactory().getIdentifierInAdapterCase((String) properties.get(ValueGenerator.PROPERTY_COLUMN_NAME)));
stmt.append(") FROM ");
stmt.append(srm.getIdentifierFactory().getIdentifierInAdapterCase((String) properties.get(ValueGenerator.PROPERTY_TABLE_NAME)));
return stmt.toString();
}
use of org.datanucleus.store.rdbms.RDBMSStoreManager in project datanucleus-rdbms by datanucleus.
the class SequenceGenerator method reserveBlock.
/**
* Reserve a block of ids.
* @param size Block size
* @return The reserved block
*/
protected synchronized ValueGenerationBlock<Long> reserveBlock(long size) {
if (size < 1) {
return null;
}
PreparedStatement ps = null;
ResultSet rs = null;
List oid = new ArrayList();
RDBMSStoreManager srm = (RDBMSStoreManager) storeMgr;
SQLController sqlControl = srm.getSQLController();
try {
// Get next available id
DatastoreAdapter dba = srm.getDatastoreAdapter();
String stmt = dba.getSequenceNextStmt(sequenceName);
ps = sqlControl.getStatementForQuery(connection, stmt);
rs = sqlControl.executeStatementQuery(null, connection, stmt, ps);
Long nextId = Long.valueOf(0);
if (rs.next()) {
nextId = Long.valueOf(rs.getLong(1));
oid.add(nextId);
}
for (int i = 1; i < size; i++) {
// size must match key-increment-by otherwise it will
// cause duplicates keys
nextId = Long.valueOf(nextId.longValue() + 1);
oid.add(nextId);
}
if (NucleusLogger.VALUEGENERATION.isDebugEnabled()) {
NucleusLogger.VALUEGENERATION.debug(Localiser.msg("040004", "" + size));
}
return new ValueGenerationBlock<>(oid);
} catch (SQLException e) {
throw new ValueGenerationException(Localiser.msg("061001", e.getMessage()), e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
sqlControl.closeStatement(connection, ps);
}
} catch (SQLException e) {
// non-recoverable error
}
}
}
use of org.datanucleus.store.rdbms.RDBMSStoreManager in project datanucleus-rdbms by datanucleus.
the class SequenceGenerator method createRepository.
/**
* Method to create the sequence.
* @return Whether it was created successfully.
*/
protected boolean createRepository() {
PreparedStatement ps = null;
RDBMSStoreManager srm = (RDBMSStoreManager) storeMgr;
DatastoreAdapter dba = srm.getDatastoreAdapter();
SQLController sqlControl = srm.getSQLController();
if (!srm.getSchemaHandler().isAutoCreateTables()) {
throw new NucleusUserException(Localiser.msg("040010", sequenceName));
}
Integer min = properties.containsKey(ValueGenerator.PROPERTY_KEY_MIN_VALUE) ? Integer.valueOf(properties.getProperty(ValueGenerator.PROPERTY_KEY_MIN_VALUE)) : null;
Integer max = properties.containsKey(ValueGenerator.PROPERTY_KEY_MAX_VALUE) ? Integer.valueOf(properties.getProperty(ValueGenerator.PROPERTY_KEY_MAX_VALUE)) : null;
Integer start = properties.containsKey(ValueGenerator.PROPERTY_KEY_INITIAL_VALUE) ? Integer.valueOf(properties.getProperty(ValueGenerator.PROPERTY_KEY_INITIAL_VALUE)) : null;
Integer incr = properties.containsKey(ValueGenerator.PROPERTY_KEY_CACHE_SIZE) ? Integer.valueOf(properties.getProperty(ValueGenerator.PROPERTY_KEY_CACHE_SIZE)) : null;
Integer cacheSize = properties.containsKey(ValueGenerator.PROPERTY_KEY_DATABASE_CACHE_SIZE) ? Integer.valueOf(properties.getProperty(ValueGenerator.PROPERTY_KEY_DATABASE_CACHE_SIZE)) : null;
String stmt = dba.getSequenceCreateStmt(sequenceName, min, max, start, incr, cacheSize);
try {
ps = sqlControl.getStatementForUpdate(connection, stmt, false);
sqlControl.executeStatementUpdate(null, connection, stmt, ps, true);
} catch (SQLException e) {
NucleusLogger.DATASTORE.error(e);
throw new ValueGenerationException(Localiser.msg("061000", e.getMessage()) + stmt);
} finally {
try {
if (ps != null) {
sqlControl.closeStatement(connection, ps);
}
} catch (SQLException e) {
// non-recoverable error
}
}
return true;
}
use of org.datanucleus.store.rdbms.RDBMSStoreManager in project datanucleus-rdbms by datanucleus.
the class TableGenerator method reserveBlock.
/**
* Method to reserve a block of "size" identities.
* @param size Block size
* @return The reserved block
*/
public ValueGenerationBlock<Long> reserveBlock(long size) {
if (size < 1) {
return null;
}
// search for an ID in the database
List<Long> oid = new ArrayList<>();
try {
if (sequenceTable == null) {
initialiseSequenceTable();
}
DatastoreIdentifier sourceTableIdentifier = null;
if (properties.containsKey(ValueGenerator.PROPERTY_TABLE_NAME)) {
sourceTableIdentifier = ((RDBMSStoreManager) storeMgr).getIdentifierFactory().newTableIdentifier(properties.getProperty(ValueGenerator.PROPERTY_TABLE_NAME));
// TODO Apply passed in catalog/schema to this identifier rather than the default for the factory
}
Long nextId = sequenceTable.getNextVal(sequenceName, connection, (int) size, sourceTableIdentifier, properties.getProperty(ValueGenerator.PROPERTY_COLUMN_NAME), initialValue);
for (int i = 0; i < size; i++) {
oid.add(nextId);
nextId = Long.valueOf(nextId.longValue() + 1);
}
if (NucleusLogger.VALUEGENERATION.isDebugEnabled()) {
NucleusLogger.VALUEGENERATION.debug(Localiser.msg("040004", "" + size));
}
return new ValueGenerationBlock<>(oid);
} catch (SQLException e) {
throw new ValueGenerationException(Localiser.msg("061001", e.getMessage()));
}
}
use of org.datanucleus.store.rdbms.RDBMSStoreManager in project datanucleus-rdbms by datanucleus.
the class TemporalSecondMethod6 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) {
SQLExpression invokedExpr = getInvokedExpression(expr, args, "SECOND");
RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
JavaTypeMapping mapping = storeMgr.getMappingManager().getMapping(String.class);
SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
SQLExpression sec = exprFactory.newLiteral(stmt, mapping, "%S");
ArrayList funcArgs = new ArrayList();
funcArgs.add(sec);
funcArgs.add(invokedExpr);
return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "strftime", funcArgs);
}
Aggregations