Search in sources :

Example 56 with RDBMSStoreManager

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();
}
Also used : RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 57 with RDBMSStoreManager

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
        }
    }
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ValueGenerationBlock(org.datanucleus.store.valuegenerator.ValueGenerationBlock) PreparedStatement(java.sql.PreparedStatement) ValueGenerationException(org.datanucleus.store.valuegenerator.ValueGenerationException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) SQLController(org.datanucleus.store.rdbms.SQLController) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter)

Example 58 with RDBMSStoreManager

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;
}
Also used : SQLException(java.sql.SQLException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) PreparedStatement(java.sql.PreparedStatement) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) ValueGenerationException(org.datanucleus.store.valuegenerator.ValueGenerationException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) SQLController(org.datanucleus.store.rdbms.SQLController)

Example 59 with RDBMSStoreManager

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()));
    }
}
Also used : SQLException(java.sql.SQLException) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) ArrayList(java.util.ArrayList) ValueGenerationBlock(org.datanucleus.store.valuegenerator.ValueGenerationBlock) ValueGenerationException(org.datanucleus.store.valuegenerator.ValueGenerationException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 60 with RDBMSStoreManager

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);
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) ArrayList(java.util.ArrayList) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Aggregations

RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)197 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)84 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)82 SQLException (java.sql.SQLException)78 Connection (java.sql.Connection)76 HashSet (java.util.HashSet)72 DatabaseMetaData (java.sql.DatabaseMetaData)62 PersistenceManager (javax.jdo.PersistenceManager)61 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)56 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)55 Transaction (javax.jdo.Transaction)52 ArrayList (java.util.ArrayList)49 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)49 JDOFatalUserException (javax.jdo.JDOFatalUserException)48 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)44 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)42 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)41 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)38 NumericExpression (org.datanucleus.store.rdbms.sql.expression.NumericExpression)28 JDOFatalInternalException (javax.jdo.JDOFatalInternalException)25