Search in sources :

Example 46 with SQLController

use of org.datanucleus.store.rdbms.SQLController in project datanucleus-rdbms by datanucleus.

the class SequenceTable method deleteSequence.

/**
 * Method to delete a sequence.
 * @param sequenceName Name of the sequence
 * @param conn Connection to the datastore
 * @throws SQLException Thrown when an error occurs deleting the schema.
 */
public void deleteSequence(String sequenceName, ManagedConnection conn) throws SQLException {
    PreparedStatement ps = null;
    SQLController sqlControl = storeMgr.getSQLController();
    try {
        ps = sqlControl.getStatementForUpdate(conn, deleteStmt, false);
        ps.setString(1, sequenceName);
        sqlControl.executeStatementUpdate(null, conn, deleteStmt, ps, true);
    // TODO : handle any warning messages
    } finally {
        if (ps != null) {
            sqlControl.closeStatement(conn, ps);
        }
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) SQLController(org.datanucleus.store.rdbms.SQLController)

Example 47 with SQLController

use of org.datanucleus.store.rdbms.SQLController in project datanucleus-rdbms by datanucleus.

the class SchemaTable method deleteAllClasses.

/**
 * Method to delete all classes from the SchemaTable.
 * This is called when DataNucleus is required to clean out its supported classes
 * (and hence DB table).
 *
 * @param conn Connection to the datastore
 * @throws SQLException Thrown when an error occurs deleting the schema.
 */
public void deleteAllClasses(ManagedConnection conn) throws SQLException {
    SQLController sqlControl = storeMgr.getSQLController();
    PreparedStatement ps = sqlControl.getStatementForUpdate(conn, deleteAllStmt, false);
    try {
        sqlControl.executeStatementUpdate(null, conn, deleteAllStmt, ps, true);
    // TODO : handle any warning messages
    } finally {
        sqlControl.closeStatement(conn, ps);
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) SQLController(org.datanucleus.store.rdbms.SQLController)

Example 48 with SQLController

use of org.datanucleus.store.rdbms.SQLController in project datanucleus-rdbms by datanucleus.

the class SchemaTable method deleteClass.

/**
 * Method to delete a class from the SchemaTable.
 * This is called when DataNucleus is required to clean out support for a particular class.
 * @param class_name Name of class to delete
 * @param conn Connection to the datastore
 * @throws SQLException Thrown when an error occurs deleting the schema.
 */
public void deleteClass(String class_name, ManagedConnection conn) throws SQLException {
    SQLController sqlControl = storeMgr.getSQLController();
    PreparedStatement ps = sqlControl.getStatementForUpdate(conn, deleteStmt, false);
    try {
        ps.setString(1, class_name);
        sqlControl.executeStatementUpdate(null, conn, deleteStmt, ps, true);
    // TODO : handle any warning messages
    } finally {
        sqlControl.closeStatement(conn, ps);
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) SQLController(org.datanucleus.store.rdbms.SQLController)

Example 49 with SQLController

use of org.datanucleus.store.rdbms.SQLController in project datanucleus-rdbms by datanucleus.

the class DatastoreUUIDHexGenerator method reserveBlock.

/**
 * Reserve a block of ids.
 * @param size Block size
 * @return The reserved block
 */
protected synchronized ValueGenerationBlock<String> reserveBlock(long size) {
    if (size < 1) {
        return null;
    }
    List<String> oids = new ArrayList<>();
    try {
        ManagedConnection mconn = connectionProvider.retrieveConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        RDBMSStoreManager rdbmsMgr = (RDBMSStoreManager) storeMgr;
        SQLController sqlControl = rdbmsMgr.getSQLController();
        try {
            // Find the next ID from the database
            DatastoreAdapter dba = rdbmsMgr.getDatastoreAdapter();
            String stmt = dba.getSelectNewUUIDStmt();
            ps = sqlControl.getStatementForQuery(mconn, stmt);
            for (int i = 1; i < size; i++) {
                rs = sqlControl.executeStatementQuery(null, mconn, stmt, ps);
                if (rs.next()) {
                    oids.add(rs.getString(1));
                }
            }
        } catch (SQLException e) {
            throw new ValueGenerationException(Localiser.msg("040008", e.getMessage()));
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    sqlControl.closeStatement(mconn, ps);
                }
            } catch (SQLException e) {
            // non-recoverable error
            }
        }
    } finally {
        connectionProvider.releaseConnection();
    }
    return new ValueGenerationBlock(oids);
}
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) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter)

Example 50 with SQLController

use of org.datanucleus.store.rdbms.SQLController in project datanucleus-rdbms by datanucleus.

the class SequenceTable method incrementSequence.

/**
 * Method to increment a sequence
 * @param conn Connection to the datastore
 * @throws SQLException Thrown when an error occurs incrementing the sequence.
 */
private void incrementSequence(String sequenceName, long incrementBy, ManagedConnection conn) throws SQLException {
    PreparedStatement ps = null;
    SQLController sqlControl = storeMgr.getSQLController();
    try {
        ps = sqlControl.getStatementForUpdate(conn, incrementByStmt, false);
        nextValMapping.setLong(null, ps, new int[] { 1 }, incrementBy);
        sequenceNameMapping.setString(null, ps, new int[] { 2 }, sequenceName);
        sqlControl.executeStatementUpdate(null, conn, incrementByStmt, ps, true);
    // TODO : handle any warning messages
    } finally {
        if (ps != null) {
            sqlControl.closeStatement(conn, ps);
        }
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) SQLController(org.datanucleus.store.rdbms.SQLController)

Aggregations

SQLController (org.datanucleus.store.rdbms.SQLController)83 PreparedStatement (java.sql.PreparedStatement)82 SQLException (java.sql.SQLException)72 ExecutionContext (org.datanucleus.ExecutionContext)63 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)63 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)60 ResultSet (java.sql.ResultSet)36 MappedDatastoreException (org.datanucleus.store.rdbms.exceptions.MappedDatastoreException)20 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)19 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)17 ArrayList (java.util.ArrayList)15 List (java.util.List)13 ObjectProvider (org.datanucleus.state.ObjectProvider)12 StatementClassMapping (org.datanucleus.store.rdbms.query.StatementClassMapping)12 Iterator (java.util.Iterator)11 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)10 PersistentClassROF (org.datanucleus.store.rdbms.query.PersistentClassROF)10 ResultObjectFactory (org.datanucleus.store.rdbms.query.ResultObjectFactory)10 NucleusException (org.datanucleus.exceptions.NucleusException)9 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)9