Search in sources :

Example 1 with DatastoreIdentifier

use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier in project datanucleus-rdbms by datanucleus.

the class ViewImpl method validate.

/**
 * Method to validate the view in the datastore. Validates the existence of the table, and then the specifications of the Columns.
 * @param conn The JDBC Connection
 * @param validateColumnStructure Whether to validate down to column structure, or just their existence
 * @param autoCreate Whether to update the view to fix errors (not used).
 * @param autoCreateErrors Errors found during the auto-create process
 * @return Whether the database was modified
 * @throws SQLException Thrown when an error occurs in the JDBC calls
 */
public boolean validate(Connection conn, boolean validateColumnStructure, boolean autoCreate, Collection autoCreateErrors) throws SQLException {
    assertIsInitialized();
    // Check existence and validity
    RDBMSSchemaHandler handler = (RDBMSSchemaHandler) storeMgr.getSchemaHandler();
    String tableType = handler.getTableType(conn, this);
    if (tableType == null) {
        throw new MissingTableException(getCatalogName(), getSchemaName(), this.toString());
    } else if (// TODO Allow "MATERIALIZED VIEW" that some RDBMS support (e.g PostgreSQL)
    !tableType.equals("VIEW")) {
        throw new NotAViewException(this.toString(), tableType);
    }
    long startTime = System.currentTimeMillis();
    if (NucleusLogger.DATASTORE.isDebugEnabled()) {
        NucleusLogger.DATASTORE.debug(Localiser.msg("031004", this));
    }
    // Validate the column(s)
    Map<DatastoreIdentifier, Column> unvalidated = new HashMap(columnsByIdentifier);
    Iterator i = storeMgr.getColumnInfoForTable(this, conn).iterator();
    while (i.hasNext()) {
        RDBMSColumnInfo ci = (RDBMSColumnInfo) i.next();
        DatastoreIdentifier colIdentifier = storeMgr.getIdentifierFactory().newIdentifier(IdentifierType.COLUMN, ci.getColumnName());
        Column col = unvalidated.get(colIdentifier);
        if (col == null) {
            if (!hasColumnName(colIdentifier)) {
                throw new UnexpectedColumnException(this.toString(), ci.getColumnName(), this.getSchemaName(), this.getCatalogName());
            }
        // Otherwise it's a duplicate column name in the metadata and we ignore it.  Cloudscape is known to do this, although I think that's probably a bug.
        } else {
            if (validateColumnStructure) {
                col.validate(ci);
                unvalidated.remove(colIdentifier);
            } else {
                unvalidated.remove(colIdentifier);
            }
        }
    }
    if (unvalidated.size() > 0) {
        throw new MissingColumnException(this, unvalidated.values());
    }
    state = TABLE_STATE_VALIDATED;
    if (NucleusLogger.DATASTORE.isDebugEnabled()) {
        NucleusLogger.DATASTORE.debug(Localiser.msg("045000", (System.currentTimeMillis() - startTime)));
    }
    return false;
}
Also used : RDBMSColumnInfo(org.datanucleus.store.rdbms.schema.RDBMSColumnInfo) NotAViewException(org.datanucleus.store.rdbms.exceptions.NotAViewException) HashMap(java.util.HashMap) MissingColumnException(org.datanucleus.store.rdbms.exceptions.MissingColumnException) RDBMSSchemaHandler(org.datanucleus.store.rdbms.schema.RDBMSSchemaHandler) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) Iterator(java.util.Iterator) MissingTableException(org.datanucleus.store.rdbms.exceptions.MissingTableException) UnexpectedColumnException(org.datanucleus.store.rdbms.exceptions.UnexpectedColumnException)

Example 2 with DatastoreIdentifier

use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier in project datanucleus-rdbms by datanucleus.

the class RDBMSStoreManager method addSequenceTableForMetaData.

protected void addSequenceTableForMetaData(MetaData md, ClassLoaderResolver clr, Set<String> seqTablesGenerated) {
    String catName = null;
    String schName = null;
    String tableName = TableGenerator.DEFAULT_TABLE_NAME;
    String seqColName = TableGenerator.DEFAULT_SEQUENCE_COLUMN_NAME;
    String nextValColName = TableGenerator.DEFAULT_NEXTVALUE_COLUMN_NAME;
    if (md.hasExtension(ValueGenerator.PROPERTY_SEQUENCETABLE_CATALOG)) {
        catName = md.getValueForExtension(ValueGenerator.PROPERTY_SEQUENCETABLE_CATALOG);
    }
    if (md.hasExtension(ValueGenerator.PROPERTY_SEQUENCETABLE_SCHEMA)) {
        schName = md.getValueForExtension(ValueGenerator.PROPERTY_SEQUENCETABLE_SCHEMA);
    }
    if (md.hasExtension(ValueGenerator.PROPERTY_SEQUENCETABLE_TABLE)) {
        tableName = md.getValueForExtension(ValueGenerator.PROPERTY_SEQUENCETABLE_TABLE);
    }
    if (md.hasExtension(ValueGenerator.PROPERTY_SEQUENCETABLE_NAME_COLUMN)) {
        seqColName = md.getValueForExtension(ValueGenerator.PROPERTY_SEQUENCETABLE_NAME_COLUMN);
    }
    if (md.hasExtension(ValueGenerator.PROPERTY_SEQUENCETABLE_NEXTVAL_COLUMN)) {
        nextValColName = md.getValueForExtension(ValueGenerator.PROPERTY_SEQUENCETABLE_NEXTVAL_COLUMN);
    }
    if (!seqTablesGenerated.contains(tableName)) {
        ManagedConnection mconn = connectionMgr.getConnection(TransactionIsolation.NONE);
        Connection conn = (Connection) mconn.getConnection();
        try {
            DatastoreIdentifier tableIdentifier = identifierFactory.newTableIdentifier(tableName);
            if (catName != null) {
                tableIdentifier.setCatalogName(catName);
            }
            if (schName != null) {
                tableIdentifier.setSchemaName(schName);
            }
            SequenceTable seqTable = new SequenceTable(tableIdentifier, this, seqColName, nextValColName);
            seqTable.initialize(clr);
            seqTable.exists(conn, true);
        } catch (Exception e) {
        } finally {
            mconn.release();
        }
        seqTablesGenerated.add(tableName);
    }
}
Also used : DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) Connection(java.sql.Connection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) SequenceTable(org.datanucleus.store.rdbms.valuegenerator.SequenceTable) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) MacroString(org.datanucleus.util.MacroString) SQLException(java.sql.SQLException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) IOException(java.io.IOException) NucleusException(org.datanucleus.exceptions.NucleusException) UnsupportedDataTypeException(org.datanucleus.store.rdbms.exceptions.UnsupportedDataTypeException) NoTableManagedException(org.datanucleus.store.rdbms.exceptions.NoTableManagedException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) IncompatibleFieldTypeException(org.datanucleus.store.types.IncompatibleFieldTypeException)

Example 3 with DatastoreIdentifier

use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier in project datanucleus-rdbms by datanucleus.

the class SQLStatement method join.

/**
 * Method to form a join to the specified table using the provided mappings and applying the provided join condition (rather than generating one from the source/target mappings).
 * This is used with JPQL where we allow two root entities to be joined using a provide "ON" condition.
 * @param joinType Type of join.
 * @param sourceTable SQLTable for the source (null implies primaryTable)
 * @param target Table to join to
 * @param targetAlias Alias for the target table (if known)
 * @param tableGrpName Name of the table group for the target (null implies a new group)
 * @param joinCondition On clause for the join
 * @param applyToUnions Whether to apply to any unioned statements (only applies to SELECT statements)
 * @return SQLTable for the target
 */
public SQLTable join(JoinType joinType, SQLTable sourceTable, Table target, String targetAlias, String tableGrpName, BooleanExpression joinCondition, boolean applyToUnions) {
    invalidateStatement();
    // Create the SQLTable to join to.
    if (tables == null) {
        tables = new HashMap();
    }
    if (tableGrpName == null) {
        tableGrpName = "Group" + tableGroups.size();
    }
    if (targetAlias == null) {
        targetAlias = namer.getAliasForTable(this, target, tableGrpName);
    }
    if (sourceTable == null) {
        sourceTable = primaryTable;
    }
    DatastoreIdentifier targetId = rdbmsMgr.getIdentifierFactory().newTableIdentifier(targetAlias);
    SQLTable targetTbl = new SQLTable(this, target, targetId, tableGrpName);
    putSQLTableInGroup(targetTbl, tableGrpName, joinType);
    addJoin(joinType, sourceTable, targetTbl, joinCondition, null);
    return targetTbl;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)

Example 4 with DatastoreIdentifier

use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier in project datanucleus-rdbms by datanucleus.

the class SQLStatement method join.

/**
 * Method to form a join to the specified table using the provided mappings, with the join condition derived from the source-target mappings.
 * @param joinType Type of join.
 * @param sourceTable SQLTable for the source (null implies primaryTable)
 * @param sourceMapping Mapping in this table to join from
 * @param sourceParentMapping Optional, if this source mapping is a sub mapping (e.g interface impl).
 * @param target Table to join to
 * @param targetAlias Alias for the target table (if known)
 * @param targetMapping Mapping in the other table to join to (also defines the table to join to)
 * @param targetParentMapping Optional, if this source mapping is a sub mapping (e.g interface impl).
 * @param discrimValues Any discriminator values to apply for the joined table (null if not)
 * @param tableGrpName Name of the table group for the target (null implies a new group)
 * @param applyToUnions Whether to apply to any unioned statements (only applies to SELECT statements)
 * @param parentJoin Parent join when this join will be a sub-join (part of "join grouping")
 * @return SQLTable for the target
 */
public SQLTable join(JoinType joinType, SQLTable sourceTable, JavaTypeMapping sourceMapping, JavaTypeMapping sourceParentMapping, Table target, String targetAlias, JavaTypeMapping targetMapping, JavaTypeMapping targetParentMapping, Object[] discrimValues, String tableGrpName, boolean applyToUnions, SQLJoin parentJoin) {
    invalidateStatement();
    // Create the SQLTable to join to.
    if (tables == null) {
        tables = new HashMap();
    }
    if (tableGrpName == null) {
        tableGrpName = "Group" + tableGroups.size();
    }
    if (targetAlias == null) {
        targetAlias = namer.getAliasForTable(this, target, tableGrpName);
    }
    if (sourceTable == null) {
        sourceTable = primaryTable;
    }
    DatastoreIdentifier targetId = rdbmsMgr.getIdentifierFactory().newTableIdentifier(targetAlias);
    SQLTable targetTbl = new SQLTable(this, target, targetId, tableGrpName);
    putSQLTableInGroup(targetTbl, tableGrpName, joinType);
    // Generate the join condition to use
    BooleanExpression joinCondition = getJoinConditionForJoin(sourceTable, sourceMapping, sourceParentMapping, targetTbl, targetMapping, targetParentMapping, discrimValues);
    addJoin(joinType, sourceTable, targetTbl, joinCondition, parentJoin);
    return targetTbl;
}
Also used : BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)

Example 5 with DatastoreIdentifier

use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier in project datanucleus-rdbms by datanucleus.

the class SelectStatement method select.

/**
 * Add a select clause for the specified column.
 * @param table The SQLTable to select from (null implies the primary table)
 * @param column The column
 * @param alias Optional alias
 * @return The column index in the statement for the specified column (1 is first).
 */
public int select(SQLTable table, Column column, String alias) {
    if (column == null) {
        throw new NucleusException("Column to select is null");
    } else if (table == null) {
        // Default to the primary table if not specified
        table = primaryTable;
    }
    if (column.getTable() != table.getTable()) {
        throw new NucleusException("Table being selected from (\"" + table.getTable() + "\") is inconsistent with the column selected (\"" + column.getTable() + "\")");
    }
    invalidateStatement();
    DatastoreIdentifier colAlias = null;
    if (alias != null) {
        colAlias = rdbmsMgr.getIdentifierFactory().newColumnIdentifier(alias);
    }
    SQLColumn col = new SQLColumn(table, column, colAlias);
    int position = selectItem(new SQLText(col.getColumnSelectString()), alias != null ? colAlias.toString() : null, true);
    if (unions != null && allowUnions) {
        // Apply the select to all unions
        Iterator<SelectStatement> unionIter = unions.iterator();
        while (unionIter.hasNext()) {
            SelectStatement stmt = unionIter.next();
            stmt.select(table, column, alias);
        }
    }
    return position;
}
Also used : DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) NucleusException(org.datanucleus.exceptions.NucleusException)

Aggregations

DatastoreIdentifier (org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)45 IdentifierFactory (org.datanucleus.store.rdbms.identifier.IdentifierFactory)19 HashMap (java.util.HashMap)18 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)12 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)11 Iterator (java.util.Iterator)10 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)10 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)9 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)8 MacroString (org.datanucleus.util.MacroString)6 ArrayList (java.util.ArrayList)5 NucleusException (org.datanucleus.exceptions.NucleusException)5 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)4 Index (org.datanucleus.store.rdbms.key.Index)4 RDBMSColumnInfo (org.datanucleus.store.rdbms.schema.RDBMSColumnInfo)4 StoreSchemaHandler (org.datanucleus.store.schema.StoreSchemaHandler)4 SQLException (java.sql.SQLException)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3