Search in sources :

Example 6 with DatastoreAdapter

use of org.datanucleus.store.rdbms.adapter.DatastoreAdapter in project datanucleus-rdbms by datanucleus.

the class RDBMSSchemaHandler method getRDBMSTableFKInfoForTable.

/**
 * Convenience method to get the ForeignKey info for the specified table from the datastore.
 * @param conn Connection to use
 * @param table The table
 * @return The foreign key info
 */
protected RDBMSTableFKInfo getRDBMSTableFKInfoForTable(Connection conn, Table table) {
    // Calculate the catalog/schema names since we need to search fully qualified
    DatastoreAdapter dba = getDatastoreAdapter();
    String[] c = splitTableIdentifierName(dba.getCatalogSeparator(), table.getIdentifier().getName());
    String catalogName = table.getCatalogName();
    String schemaName = table.getSchemaName();
    String tableName = table.getIdentifier().getName();
    if (c[0] != null) {
        catalogName = c[0];
    }
    if (c[1] != null) {
        schemaName = c[1];
    }
    if (c[2] != null) {
        tableName = c[2];
    }
    catalogName = getIdentifierForUseWithDatabaseMetaData(catalogName);
    schemaName = getIdentifierForUseWithDatabaseMetaData(schemaName);
    tableName = getIdentifierForUseWithDatabaseMetaData(tableName);
    return getRDBMSTableFKInfoForTable(conn, catalogName, schemaName, tableName);
}
Also used : DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter)

Example 7 with DatastoreAdapter

use of org.datanucleus.store.rdbms.adapter.DatastoreAdapter in project datanucleus-rdbms by datanucleus.

the class RDBMSSchemaHandler method getRDBMSTableIndexInfoForTable.

/**
 * Convenience method to get the index info for the specified table from the datastore.
 * Returns ALL indexes regardless of whether unique or not.
 * @param conn Connection to use
 * @param table The table
 * @return The index info
 */
protected RDBMSTableIndexInfo getRDBMSTableIndexInfoForTable(Connection conn, Table table) {
    // Calculate the catalog/schema names since we need to search fully qualified
    DatastoreAdapter dba = getDatastoreAdapter();
    String[] c = splitTableIdentifierName(dba.getCatalogSeparator(), table.getIdentifier().getName());
    String catalogName = table.getCatalogName();
    String schemaName = table.getSchemaName();
    String tableName = table.getIdentifier().getName();
    if (c[0] != null) {
        catalogName = c[0];
    }
    if (c[1] != null) {
        schemaName = c[1];
    }
    if (c[2] != null) {
        tableName = c[2];
    }
    catalogName = getIdentifierForUseWithDatabaseMetaData(catalogName);
    schemaName = getIdentifierForUseWithDatabaseMetaData(schemaName);
    tableName = getIdentifierForUseWithDatabaseMetaData(tableName);
    return getRDBMSTableIndexInfoForTable(conn, catalogName, schemaName, tableName);
}
Also used : DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter)

Example 8 with DatastoreAdapter

use of org.datanucleus.store.rdbms.adapter.DatastoreAdapter in project datanucleus-rdbms by datanucleus.

the class RDBMSSchemaHandler method getRDBMSTableFKInfoForTable.

/**
 * Convenience method to get the ForeignKey info for the specified table from the datastore.
 * @param conn Connection to use
 * @param catalogName Catalog
 * @param schemaName Schema
 * @param tableName Name of the table
 * @return The foreign key info
 */
protected RDBMSTableFKInfo getRDBMSTableFKInfoForTable(Connection conn, String catalogName, String schemaName, String tableName) {
    // We don't cache FK info, so retrieve it directly
    RDBMSTableFKInfo info = new RDBMSTableFKInfo(catalogName, schemaName, tableName);
    DatastoreAdapter dba = getDatastoreAdapter();
    try {
        ResultSet rs = conn.getMetaData().getImportedKeys(catalogName, schemaName, tableName);
        try {
            while (rs.next()) {
                ForeignKeyInfo fki = dba.newFKInfo(rs);
                if (!info.getChildren().contains(fki)) {
                    // Ignore any duplicate FKs
                    info.addChild(fki);
                }
            }
        } finally {
            rs.close();
        }
    } catch (SQLException sqle) {
        throw new NucleusDataStoreException("Exception thrown while querying foreign keys for table=" + tableName, sqle);
    }
    return info;
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter)

Example 9 with DatastoreAdapter

use of org.datanucleus.store.rdbms.adapter.DatastoreAdapter in project datanucleus-rdbms by datanucleus.

the class RDBMSSchemaHandler method getTableType.

/**
 * Returns the type of a database table/view in the datastore.
 * Uses DatabaseMetaData.getTables() to extract this information.
 * @param conn Connection to the database.
 * @param table The table/view
 * @return The table type (consistent with the return from DatabaseMetaData.getTables())
 * @throws SQLException if an error occurs obtaining the information
 */
public String getTableType(Connection conn, Table table) throws SQLException {
    String tableType = null;
    // Calculate the catalog/schema names since we need to search fully qualified
    DatastoreAdapter dba = getDatastoreAdapter();
    String[] c = splitTableIdentifierName(dba.getCatalogSeparator(), table.getIdentifier().getName());
    String catalogName = table.getCatalogName();
    String schemaName = table.getSchemaName();
    String tableName = table.getIdentifier().getName();
    if (c[0] != null) {
        catalogName = c[0];
    }
    if (c[1] != null) {
        schemaName = c[1];
    }
    if (c[2] != null) {
        tableName = c[2];
    }
    catalogName = getIdentifierForUseWithDatabaseMetaData(catalogName);
    schemaName = getIdentifierForUseWithDatabaseMetaData(schemaName);
    tableName = getIdentifierForUseWithDatabaseMetaData(tableName);
    try {
        ResultSet rs = conn.getMetaData().getTables(catalogName, schemaName, tableName, null);
        try {
            boolean insensitive = identifiersCaseInsensitive();
            while (rs.next()) {
                if ((insensitive && tableName.equalsIgnoreCase(rs.getString(3))) || (!insensitive && tableName.equals(rs.getString(3)))) {
                    tableType = rs.getString(4).toUpperCase();
                    break;
                }
            }
        } finally {
            rs.close();
        }
    } catch (SQLException sqle) {
        throw new NucleusDataStoreException("Exception thrown finding table type using DatabaseMetaData.getTables()", sqle);
    }
    return tableType;
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter)

Example 10 with DatastoreAdapter

use of org.datanucleus.store.rdbms.adapter.DatastoreAdapter in project datanucleus-rdbms by datanucleus.

the class SelectStatement method generateOrderingStatement.

/**
 * Convenience method to generate the ordering statement to add to the overall query statement.
 * @return The ordering statement
 */
protected SQLText generateOrderingStatement() {
    SQLText orderStmt = null;
    if (orderingExpressions != null && orderingExpressions.length > 0) {
        DatastoreAdapter dba = getDatastoreAdapter();
        if (dba.supportsOption(DatastoreAdapter.ORDERBY_USING_SELECT_COLUMN_INDEX)) {
            // Order using the indexes of the ordering columns in the SELECT
            orderStmt = new SQLText();
            for (int i = 0; i < orderingExpressions.length; ++i) {
                if (i > 0) {
                    orderStmt.append(',');
                }
                orderStmt.append(Integer.toString(orderingColumnIndexes[i]));
                if (orderingDirections[i]) {
                    orderStmt.append(" DESC");
                }
                if (orderNullDirectives != null && orderNullDirectives[i] != null && dba.supportsOption(DatastoreAdapter.ORDERBY_NULLS_DIRECTIVES)) {
                    // Apply "NULLS [FIRST | LAST]" since supported by this datastore
                    orderStmt.append(" " + (orderNullDirectives[i] == NullOrderingType.NULLS_FIRST ? "NULLS FIRST" : "NULLS LAST"));
                }
            }
        } else {
            // TODO Cater for ResultAliasExpression, so we just put the order aliasName
            // Order using column aliases "NUCORDER{i}"
            orderStmt = new SQLText();
            boolean needsSelect = dba.supportsOption(DatastoreAdapter.INCLUDE_ORDERBY_COLS_IN_SELECT);
            if (parent != null) {
                // Don't select ordering columns with subqueries, since we will select just the required column(s)
                needsSelect = false;
            }
            for (int i = 0; i < orderingExpressions.length; ++i) {
                SQLExpression orderExpr = orderingExpressions[i];
                boolean orderDirection = orderingDirections[i];
                NullOrderingType orderNullDirective = (orderNullDirectives != null ? orderNullDirectives[i] : null);
                if (i > 0) {
                    orderStmt.append(',');
                }
                if (needsSelect && !aggregated) {
                    if (orderExpr instanceof ResultAliasExpression) {
                        String orderStr = rdbmsMgr.getIdentifierFactory().getIdentifierInAdapterCase(((ResultAliasExpression) orderExpr).getResultAlias());
                        addOrderComponent(orderStmt, orderStr, orderExpr, orderDirection, orderNullDirective, dba);
                    } else {
                        // Order by the "NUCORDER?" if we need them to be selected and it isn't an aggregate
                        String orderString = "NUCORDER" + i;
                        if (orderExpr.getNumberOfSubExpressions() == 1) {
                            String orderStr = rdbmsMgr.getIdentifierFactory().getIdentifierInAdapterCase(orderString);
                            addOrderComponent(orderStmt, orderStr, orderExpr, orderDirection, orderNullDirective, dba);
                        } else {
                            DatastoreMapping[] mappings = orderExpr.getJavaTypeMapping().getDatastoreMappings();
                            for (int j = 0; j < mappings.length; j++) {
                                String orderStr = rdbmsMgr.getIdentifierFactory().getIdentifierInAdapterCase(orderString + "_" + j);
                                addOrderComponent(orderStmt, orderStr, orderExpr, orderDirection, orderNullDirective, dba);
                                if (j < mappings.length - 1) {
                                    orderStmt.append(',');
                                }
                            }
                        }
                    }
                } else {
                    if (orderExpr instanceof ResultAliasExpression) {
                        String orderStr = rdbmsMgr.getIdentifierFactory().getIdentifierInAdapterCase(((ResultAliasExpression) orderExpr).getResultAlias());
                        addOrderComponent(orderStmt, orderStr, orderExpr, orderDirection, orderNullDirective, dba);
                    } else {
                        // Order by the "THIS.COLUMN" otherwise
                        addOrderComponent(orderStmt, orderExpr.toSQLText().toSQL(), orderExpr, orderDirection, orderNullDirective, dba);
                    }
                }
            }
        }
    }
    return orderStmt;
}
Also used : NullOrderingType(org.datanucleus.query.NullOrderingType) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) ResultAliasExpression(org.datanucleus.store.rdbms.sql.expression.ResultAliasExpression) DatastoreMapping(org.datanucleus.store.rdbms.mapping.datastore.DatastoreMapping) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter)

Aggregations

DatastoreAdapter (org.datanucleus.store.rdbms.adapter.DatastoreAdapter)46 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)24 SQLException (java.sql.SQLException)17 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)10 ResultSet (java.sql.ResultSet)8 List (java.util.List)8 Connection (java.sql.Connection)7 Statement (java.sql.Statement)7 EntityTransaction (javax.persistence.EntityTransaction)7 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)7 Person (org.datanucleus.samples.annotations.models.company.Person)7 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)7 PreparedStatement (java.sql.PreparedStatement)6 ArrayList (java.util.ArrayList)6 SQLController (org.datanucleus.store.rdbms.SQLController)6 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)6 StoredProcedureQuery (javax.persistence.StoredProcedureQuery)5 JPAEntityManager (org.datanucleus.api.jpa.JPAEntityManager)5 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)5 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)5