Search in sources :

Example 81 with SQLController

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

the class SchemaTable method getAllClasses.

/**
 * Accessor for the classes already supported by this Schema Table.
 * @param conn Connection for this datastore.
 * @return The HashSet of class names (StoreData)
 * @throws SQLException Thrown when an error occurs in the process.
 */
public HashSet getAllClasses(ManagedConnection conn) throws SQLException {
    HashSet schema_data = new HashSet();
    if (storeMgr.getDdlWriter() != null && !tableExists((Connection) conn.getConnection())) {
        // do not query non-existing schema table when DDL is only written to file
        return schema_data;
    }
    SQLController sqlControl = storeMgr.getSQLController();
    PreparedStatement ps = sqlControl.getStatementForQuery(conn, fetchAllStmt);
    try {
        ResultSet rs = sqlControl.executeStatementQuery(null, conn, fetchAllStmt, ps);
        try {
            while (rs.next()) {
                StoreData data = new RDBMSStoreData(rs.getString(1), rs.getString(2), rs.getString(4).equals("1") ? true : false, rs.getString(3).equals("FCO") ? StoreData.Type.FCO : StoreData.Type.SCO, rs.getString(6));
                schema_data.add(data);
            }
        } finally {
            rs.close();
        }
    } finally {
        sqlControl.closeStatement(conn, ps);
    }
    return schema_data;
}
Also used : RDBMSStoreData(org.datanucleus.store.rdbms.RDBMSStoreData) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) HashSet(java.util.HashSet) SQLController(org.datanucleus.store.rdbms.SQLController) RDBMSStoreData(org.datanucleus.store.rdbms.RDBMSStoreData) StoreData(org.datanucleus.store.StoreData)

Example 82 with SQLController

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

the class SchemaTable method addClass.

/**
 * Method to insert a row in the SchemaTable. This is called when DataNucleus is
 * now supporting a new class (and hence DB table).
 * @param data Data for the class
 * @param conn Connection to the datastore
 * @throws SQLException Thrown when an error occurs inserting the schema.
 */
public void addClass(RDBMSStoreData data, ManagedConnection conn) throws SQLException {
    if (storeMgr.getDdlWriter() != null) {
        // No interest in actually adding the class to the table
        return;
    }
    if (hasClass(data, conn)) {
        // Data already exists, so remove the chance of a duplicate insert
        return;
    }
    SQLController sqlControl = storeMgr.getSQLController();
    PreparedStatement ps = sqlControl.getStatementForUpdate(conn, insertStmt, false);
    try {
        int jdbc_id = 1;
        classMapping.setString(null, ps, MappingHelper.getMappingIndices(jdbc_id, classMapping), data.getName());
        jdbc_id += classMapping.getNumberOfDatastoreMappings();
        tableMapping.setString(null, ps, MappingHelper.getMappingIndices(jdbc_id, tableMapping), data.hasTable() ? data.getTableName() : "");
        jdbc_id += tableMapping.getNumberOfDatastoreMappings();
        typeMapping.setString(null, ps, MappingHelper.getMappingIndices(jdbc_id, typeMapping), data.isFCO() ? "FCO" : "SCO");
        jdbc_id += typeMapping.getNumberOfDatastoreMappings();
        ownerMapping.setString(null, ps, MappingHelper.getMappingIndices(jdbc_id, ownerMapping), data.isTableOwner() ? "1" : "0");
        jdbc_id += ownerMapping.getNumberOfDatastoreMappings();
        // TODO Sort out version
        versionMapping.setString(null, ps, MappingHelper.getMappingIndices(jdbc_id, versionMapping), "DataNucleus");
        jdbc_id += versionMapping.getNumberOfDatastoreMappings();
        interfaceNameMapping.setString(null, ps, MappingHelper.getMappingIndices(jdbc_id, interfaceNameMapping), data.getInterfaceName());
        jdbc_id += interfaceNameMapping.getNumberOfDatastoreMappings();
        sqlControl.executeStatementUpdate(null, conn, insertStmt, 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 83 with SQLController

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

the class SchemaTable method hasClass.

/**
 * Method to verify the a class is already stored in the table.
 * @param data Data for the class
 * @param conn Connection to the datastore
 * @return if the SchemaTable already has the class
 * @throws SQLException Thrown when an error occurs inserting the schema.
 */
private boolean hasClass(StoreData data, ManagedConnection conn) throws SQLException {
    if (!tableExists((Connection) conn.getConnection())) {
        return false;
    }
    SQLController sqlControl = storeMgr.getSQLController();
    PreparedStatement ps = sqlControl.getStatementForQuery(conn, fetchStmt);
    try {
        int jdbc_id = 1;
        tableMapping.setString(null, ps, MappingHelper.getMappingIndices(jdbc_id, tableMapping), data.getName());
        ResultSet rs = sqlControl.executeStatementQuery(null, conn, fetchStmt, ps);
        try {
            if (rs.next()) {
                return true;
            }
        } finally {
            rs.close();
        }
    } finally {
        sqlControl.closeStatement(conn, ps);
    }
    return false;
}
Also used : Connection(java.sql.Connection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) ResultSet(java.sql.ResultSet) 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