Search in sources :

Example 86 with RDBMSStoreManager

use of org.datanucleus.store.rdbms.RDBMSStoreManager in project tests by datanucleus.

the class SchemaTest method testEmbeddedPCSchemaWithEmbeddedFlag.

/**
 * Test the schema generation for embedded PC objects when just using "embedded=true".
 * Checks that the tables are correctly generated and the correct columns are present.
 */
public void testEmbeddedPCSchemaWithEmbeddedFlag() throws Exception {
    addClassesToSchema(new Class[] { EmbeddedA.class });
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
    Connection conn = null;
    ManagedConnection mconn = null;
    try {
        tx.begin();
        HashSet embeddedColumnNames = new HashSet();
        embeddedColumnNames.add("EMBEDDEDA_ID");
        embeddedColumnNames.add("EMBEDDEDB_ID");
        embeddedColumnNames.add("NAME");
        mconn = databaseMgr.getConnectionManager().getConnection(0);
        conn = (Connection) mconn.getConnection();
        DatabaseMetaData dmd = conn.getMetaData();
        // Check container columns
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "EMBEDDEDA", embeddedColumnNames);
        tx.commit();
    } catch (Exception e) {
        LOG.error(e);
        fail("Specification of table and column names for embedded PC object is incorrect. Exception was thrown : " + e.getMessage());
    } finally {
        if (conn != null) {
            mconn.close();
        }
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Connection(java.sql.Connection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) DatabaseMetaData(java.sql.DatabaseMetaData) JDOFatalUserException(javax.jdo.JDOFatalUserException) JDOFatalInternalException(javax.jdo.JDOFatalInternalException) SQLException(java.sql.SQLException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) HashSet(java.util.HashSet)

Example 87 with RDBMSStoreManager

use of org.datanucleus.store.rdbms.RDBMSStoreManager in project tests by datanucleus.

the class SchemaTest method testOverridingColumnsBaseSubclassTableSubNewTable.

/**
 * Test for overriding of columns where the base class uses "subclass-table" and the sub class uses "new-table".
 * The expected result is for the subclass table to the columns for the base class but using the overridden names.
 */
public void testOverridingColumnsBaseSubclassTableSubNewTable() throws Exception {
    addClassesToSchema(new Class[] { MBase.class, MSub1.class, MSub2.class, MRelated.class });
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
    Connection conn = null;
    ManagedConnection mconn = null;
    try {
        tx.begin();
        mconn = databaseMgr.getConnectionManager().getConnection(0);
        conn = (Connection) mconn.getConnection();
        DatabaseMetaData dmd = conn.getMetaData();
        HashSet columnNames = new HashSet();
        columnNames.add("ID");
        columnNames.add("VALUE");
        columnNames.add("BASE_2A_NAME");
        columnNames.add("BASE_2A_RELATED_ID");
        // Check base table column names
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "M_SUB1_OVERRIDE", columnNames);
        columnNames = new HashSet();
        columnNames.add("ID");
        columnNames.add("VALUE");
        columnNames.add("BASE_2B_NAME");
        columnNames.add("BASE_2B_RELATED_ID");
        // Check sub table column names
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "M_SUB2_OVERRIDE", columnNames);
        tx.commit();
    } catch (Exception e) {
        LOG.error(e);
        fail("Specification of table and column names must have been ignored when creating the schema for " + "inheritance case where the fields were overridden. Exception was thrown : " + e.getMessage());
    } finally {
        if (conn != null) {
            mconn.close();
        }
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Connection(java.sql.Connection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) DatabaseMetaData(java.sql.DatabaseMetaData) JDOFatalUserException(javax.jdo.JDOFatalUserException) JDOFatalInternalException(javax.jdo.JDOFatalInternalException) SQLException(java.sql.SQLException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) HashSet(java.util.HashSet)

Example 88 with RDBMSStoreManager

use of org.datanucleus.store.rdbms.RDBMSStoreManager in project tests by datanucleus.

the class SchemaTest method testColumnSpecifications.

/**
 * Test of the specification of columns.
 * Table is created in the constructor, and checks the DatabaseMetaData for the details.
 */
public void testColumnSpecifications() {
    // Postgresql doesnt support DECIMAL
    if (vendorID.equals("postgresql")) {
        return;
    }
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
        DatabaseMetaData dmd = ((Connection) databaseMgr.getConnectionManager().getConnection(0).getConnection()).getMetaData();
        LOG.info("JDBC Types CHAR = " + Types.CHAR + ", VARCHAR=" + Types.VARCHAR + ", INTEGER=" + Types.INTEGER + ", DOUBLE=" + Types.DOUBLE + ", BINARY=" + Types.BINARY + ", LONGVARBINARY=" + Types.LONGVARBINARY + ", NUMERIC=" + Types.NUMERIC + ", VARBINARY=" + Types.VARBINARY);
        String tableName = RDBMSTestHelper.getIdentifierInCaseOfAdapter(storeMgr, "EMPLOYEE", false);
        ResultSet rs = dmd.getColumns(null, null, tableName, null);
        while (rs.next()) {
            String colName = rs.getString(4);
            int colType = rs.getInt(5);
            int colLength = rs.getInt(7);
            int colScale = rs.getInt(9);
            if (// CHAR column
            colName.equalsIgnoreCase("SERIAL_NO")) {
                // MySQL returns CHAR as being VARCHAR here even though it is CHAR in the DB!
                // assertTrue("Type of field (SERIAL_NO) was wrong : should have been " + Types.CHAR + " but was " + colType, colType == Types.CHAR);
                assertEquals("Length of field (SERIAL_NO) was wrong", 12, colLength);
            } else if (colName.equalsIgnoreCase("SALARY")) {
                assertEquals("Type of field (SALARY) was wrong", Types.DECIMAL, colType);
                assertEquals("Precision of field (SALARY) was wrong", 10, colLength);
                assertEquals("Scale of field (SALARY) was wrong", 2, colScale);
            } else if (// int field
            colName.equalsIgnoreCase("YRS_IN_COMPANY")) {
                assertEquals("Type of field (YRS_IN_COMPANY) was wrong", Types.INTEGER, colType);
            // TODO Check "default" setting
            }
            if (// Restricted String field
            colName.equalsIgnoreCase("FIRSTNAME")) {
                assertEquals("Type of field (FIRSTNAME) was wrong", Types.VARCHAR, colType);
                assertEquals("Length of field (FIRSTNAME) was wrong", 32, colLength);
            } else if (// Unlimited String field
            colName.equalsIgnoreCase("GLOBALNUM")) {
                assertEquals("Type of field (GLOBALNUM) was wrong", Types.VARCHAR, colType);
                assertEquals("Length of field (GLOBALNUM) was wrong", 255, colLength);
            }
        }
        tableName = RDBMSTestHelper.getIdentifierInCaseOfAdapter(storeMgr, "PERSON", false);
        rs = dmd.getColumns(null, null, tableName, null);
        while (rs.next()) {
            String colName = rs.getString(4);
            int colType = rs.getInt(5);
            int colLength = rs.getInt(7);
            if (// Restricted String field
            colName.equalsIgnoreCase("FIRSTNAME")) {
                assertEquals("Type of field (FIRSTNAME) was wrong", Types.VARCHAR, colType);
                assertEquals("Length of field (FIRSTNAME) was wrong", 32, colLength);
            } else if (// Unlimited String field
            colName.equalsIgnoreCase("GLOBALNUM")) {
                assertEquals("Type of field (GLOBALNUM) was wrong", Types.VARCHAR, colType);
                assertEquals("Length of field (GLOBALNUM) was wrong", 255, colLength);
            }
        }
        tx.commit();
    } catch (Exception e) {
        LOG.error(e);
        fail("Failed interpretation of column specification for Employee. Exception was thrown : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Connection(java.sql.Connection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData) JDOFatalUserException(javax.jdo.JDOFatalUserException) JDOFatalInternalException(javax.jdo.JDOFatalInternalException) SQLException(java.sql.SQLException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 89 with RDBMSStoreManager

use of org.datanucleus.store.rdbms.RDBMSStoreManager in project tests by datanucleus.

the class SchemaTest method testSet.

/**
 * Test the schema generation for Sets with all possible types of declarations.
 */
public void testSet() throws Exception {
    addClassesToSchema(new Class[] { SetHolder.class });
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
    Connection conn = null;
    ManagedConnection mconn = null;
    try {
        tx.begin();
        mconn = databaseMgr.getConnectionManager().getConnection(0);
        conn = (Connection) mconn.getConnection();
        DatabaseMetaData dmd = conn.getMetaData();
        HashSet columnNames = new HashSet();
        columnNames.add("SETHOLDER_ID");
        columnNames.add("NAME");
        // Set<String> serialised
        columnNames.add("SETNONPC_SERIAL_1");
        // Set<Date> serialised
        columnNames.add("SETNONPC_SERIAL_2");
        // Set<String> with no join table, so serialised
        columnNames.add("SETNONPC_1");
        // Set<Date> with no join table, so serialised
        columnNames.add("SETNONPC_2");
        // Set<PC> embedded with no join table, so serialised
        columnNames.add("SETPC_EMBEDDED");
        // FK for self-referring Set
        columnNames.add("SETHOLDER_FK2_ID_OID");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "SETHOLDER", columnNames);
        columnNames.clear();
        // Set<PC> using join table
        columnNames.add("SETHOLDER_ID_OID");
        columnNames.add("PCJOINELEMENT_ID");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "SETHOLDER_PC", columnNames);
        columnNames.clear();
        // Set<String> using join table
        columnNames.add("SETHOLDER_ID_OID");
        columnNames.add("STRING_ELEMENT");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "SETHOLDER_NONPC1", columnNames);
        columnNames.clear();
        // Set<Date> using join table
        columnNames.add("SETHOLDER_ID_OID");
        columnNames.add("DATE_ELEMENT");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "SETHOLDER_NONPC2", columnNames);
        columnNames.clear();
        // Set<String> using join table with no schema info
        columnNames.add("SETHOLDER_ID_OID");
        columnNames.add("ELEMENT");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "SETHOLDER_NONPC3", columnNames);
        columnNames.clear();
        // Set<PC> using join table with elements serialised
        columnNames.add("SETHOLDER_ID_OID");
        columnNames.add("PCJOINELEMENT_SERIAL");
        columnNames.add("IDX");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "SETHOLDER_PCSERIAL", columnNames);
        columnNames.clear();
        // Set<PC> using shared join table
        columnNames.add("SETHOLDER_ID_OID");
        columnNames.add("PCJOINELEMENT_ID");
        columnNames.add("COLLECTION_TYPE");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "SETHOLDER_SHARED", columnNames);
        columnNames.clear();
        // Set<PC> using FK
        columnNames.add("PCFKSETELEMENT_ID");
        columnNames.add("NAME");
        columnNames.add("SETHOLDER_ID_OID");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "PCFKSETELEMENT", columnNames);
        columnNames.clear();
        // Set<PC> using FK shared
        columnNames.add("PCFKSETELEMENTSHARE_ID");
        columnNames.add("NAME");
        columnNames.add("SETHOLDER_ID_OID");
        columnNames.add("COLLECTION_TYPE");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "PCFKSETELEMENTSHARE", columnNames);
        columnNames.clear();
        tx.commit();
    } catch (Exception e) {
        LOG.error(e);
        fail("Specification of table and column names for various types of Set fields is incorrect. Exception was thrown : " + e.getMessage());
    } finally {
        if (conn != null) {
            mconn.close();
        }
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Connection(java.sql.Connection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) DatabaseMetaData(java.sql.DatabaseMetaData) JDOFatalUserException(javax.jdo.JDOFatalUserException) JDOFatalInternalException(javax.jdo.JDOFatalInternalException) SQLException(java.sql.SQLException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) HashSet(java.util.HashSet)

Example 90 with RDBMSStoreManager

use of org.datanucleus.store.rdbms.RDBMSStoreManager in project tests by datanucleus.

the class SchemaTest method testList.

/**
 * Test the schema generation for Lists with all possible types of declarations.
 */
public void testList() throws Exception {
    addClassesToSchema(new Class[] { ListHolder.class });
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
    Connection conn = null;
    ManagedConnection mconn = null;
    try {
        tx.begin();
        mconn = databaseMgr.getConnectionManager().getConnection(0);
        conn = (Connection) mconn.getConnection();
        DatabaseMetaData dmd = conn.getMetaData();
        HashSet columnNames = new HashSet();
        columnNames.add("LISTHOLDER_ID");
        // List<String> serialised
        columnNames.add("LISTNONPC_SERIAL_1");
        // List<Date> serialised
        columnNames.add("LISTNONPC_SERIAL_2");
        // List<String> with no join table, so serialised
        columnNames.add("LISTNONPC_1");
        // List<Date> with no join table, so serialised
        columnNames.add("LISTNONPC_2");
        // List<PC> embedded with no join table, so serialised
        columnNames.add("LISTPC_EMBEDDED");
        // FK for self-referring List
        columnNames.add("LISTHOLDER_FK2_ID_OID");
        // IDX for self-referring List
        columnNames.add("LISTHOLDER_FK2_IDX");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "LISTHOLDER", columnNames);
        columnNames.clear();
        // Liset<PC> using join table
        columnNames.add("LISTHOLDER_ID_OID");
        columnNames.add("PCJOINELEMENT_ID");
        columnNames.add("IDX");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "LISTHOLDER_PC", columnNames);
        columnNames.clear();
        // List<String> using join table
        columnNames.add("LISTHOLDER_ID_OID");
        columnNames.add("STRING_ELEMENT");
        columnNames.add("IDX");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "LISTHOLDER_NONPC1", columnNames);
        columnNames.clear();
        // List<Date> using join table
        columnNames.add("LISTHOLDER_ID_OID");
        columnNames.add("DATE_ELEMENT");
        columnNames.add("IDX");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "LISTHOLDER_NONPC2", columnNames);
        columnNames.clear();
        // List<PC> using join table with elements serialised
        columnNames.add("LISTHOLDER_ID_OID");
        columnNames.add("PCJOINELEMENT_SERIAL");
        columnNames.add("IDX");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "LISTHOLDER_PCSERIAL", columnNames);
        columnNames.clear();
        // List<PC> using shared join table
        columnNames.add("LISTHOLDER_ID_OID");
        columnNames.add("PCJOINELEMENT_ID");
        columnNames.add("IDX");
        columnNames.add("COLLECTION_TYPE");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "LISTHOLDER_SHARED", columnNames);
        columnNames.clear();
        // List<PC> using FK
        columnNames.add("PCFKLISTELEMENT_ID");
        columnNames.add("NAME");
        columnNames.add("LISTHOLDER_ID_OID");
        columnNames.add("LISTHOLDER2_ID_OID");
        columnNames.add("IDX");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "PCFKLISTELEMENT", columnNames);
        columnNames.clear();
        // List<PC> using FK shared
        columnNames.add("PCFKLISTELEMENTSHARE_ID");
        columnNames.add("NAME");
        columnNames.add("LISTHOLDER_ID_OID");
        columnNames.add("IDX");
        columnNames.add("COLLECTION_TYPE");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "PCFKLISTELEMENTSHARE", columnNames);
        columnNames.clear();
        tx.commit();
    } catch (Exception e) {
        LOG.error(e);
        fail("Specification of table and column names for various types of List fields is incorrect. Exception was thrown : " + e.getMessage());
    } finally {
        if (conn != null) {
            mconn.close();
        }
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Connection(java.sql.Connection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) DatabaseMetaData(java.sql.DatabaseMetaData) JDOFatalUserException(javax.jdo.JDOFatalUserException) JDOFatalInternalException(javax.jdo.JDOFatalInternalException) SQLException(java.sql.SQLException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) HashSet(java.util.HashSet)

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