Search in sources :

Example 1 with DatasourcePlatform

use of org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform in project eclipselink by eclipse-ee4j.

the class NativeSequence method onConnect.

/**
 * INTERNAL:
 */
@Override
public void onConnect() {
    DatasourcePlatform platform = (DatasourcePlatform) getDatasourcePlatform();
    // Set shouldAcquireValueAfterInsert flag: identity -> true; sequence objects -> false.
    if (platform.supportsIdentity() && shouldUseIdentityIfPlatformSupports()) {
        // identity is both supported by platform and desired by the NativeSequence
        setShouldAcquireValueAfterInsert(true);
    } else if (platform.supportsSequenceObjects() && !shouldUseIdentityIfPlatformSupports()) {
        // sequence objects is both supported by platform and desired by the NativeSequence
        setShouldAcquireValueAfterInsert(false);
    } else {
        if (platform.getDefaultNativeSequenceToTable() || !platform.supportsNativeSequenceNumbers()) {
            // If native sequencing is not supported, or IDENTITY not desire, use TABLE.
            this.delegateSequence = new TableSequence();
            this.delegateSequence.setName(getName());
            this.delegateSequence.onConnect(platform);
            setShouldUseTransaction(this.delegateSequence.shouldUseTransaction());
            setShouldAcquireValueAfterInsert(this.delegateSequence.shouldAcquireValueAfterInsert());
            setShouldSkipUpdate(this.delegateSequence.shouldSkipUpdate());
            setShouldSelectBeforeUpdate(this.delegateSequence.shouldSelectBeforeUpdate());
            setUpdateQuery(this.delegateSequence.getUpdateQuery());
            super.onConnect();
            return;
        } else {
            // platform support contradicts to NativeSequence setting - go with platform supported choice.
            // platform must support either identity or sequence objects (otherwise ValidationException would've been thrown earlier),
            // therefore here dbPlatform.supportsIdentity() == !dbPlatform.supportsSequenceObjects().
            setShouldAcquireValueAfterInsert(platform.supportsIdentity());
        }
    }
    setShouldUseTransaction(platform.shouldNativeSequenceUseTransaction());
    super.onConnect();
}
Also used : DatasourcePlatform(org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform)

Example 2 with DatasourcePlatform

use of org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform in project eclipselink by eclipse-ee4j.

the class QuotedTableNameInitializationTest method test.

@Override
public void test() {
    RelationalDescriptor descriptor = new RelationalDescriptor();
    descriptor.setJavaClass(Employee.class);
    String tableName = "SPACED EMPLOYEE TABLE";
    DatabaseTable table = new DatabaseTable();
    table.setName(tableName);
    table.setUseDelimiters(true);
    descriptor.addTable(table);
    descriptor.addPrimaryKeyFieldName("EMP_ID");
    DirectToFieldMapping idMapping = new DirectToFieldMapping();
    idMapping.setAttributeName("id");
    idMapping.setFieldName("EMP_ID");
    descriptor.addMapping(idMapping);
    descriptor.preInitialize(getAbstractSession());
    DatasourcePlatform plaf = (DatasourcePlatform) getAbstractSession().getDatasourcePlatform();
    String expectedTableName = plaf.getStartDelimiter() + tableName + plaf.getEndDelimiter();
    String newTableName = table.getNameDelimited(plaf);
    assertEquals("Table name should be between the platform delimiters", expectedTableName, newTableName);
}
Also used : DirectToFieldMapping(org.eclipse.persistence.mappings.DirectToFieldMapping) RelationalDescriptor(org.eclipse.persistence.descriptors.RelationalDescriptor) DatasourcePlatform(org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable)

Example 3 with DatasourcePlatform

use of org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform in project eclipselink by eclipse-ee4j.

the class SessionsXMLSchemaSequencingTest method verify.

@Override
protected void verify() {
    for (int i = 0; i < numSessions; i++) {
        boolean isDefaultSequenceDefined = sessionName[i].indexOf("NoDefaultSequence") == -1;
        boolean isSequencesDefined = sessionName[i].indexOf("NoSequences") == -1;
        boolean ok;
        if (isDefaultSequenceDefined) {
            ok = definedDefaultSequence.equals(session[i].getDatasourcePlatform().getDefaultSequence());
        } else {
            ok = !((DatasourcePlatform) session[i].getDatasourcePlatform()).hasDefaultSequence();
        }
        if (!ok) {
            throw new TestErrorException("Session " + sessionName[i] + " has wrong defaultSequence");
        }
        if (isSequencesDefined) {
            Iterator it = definedSequences.values().iterator();
            while (it.hasNext()) {
                Sequence definedSequence = (Sequence) it.next();
                Sequence sequence = session[i].getDatasourcePlatform().getSequence(definedSequence.getName());
                ok = definedSequence.equals(sequence);
                if (!ok) {
                    throw new TestErrorException("Session " + sessionName[i] + " has wrong " + sequence.getName() + " sequence");
                }
            }
        } else {
            ok = session[i].getDatasourcePlatform().getSequences() == null || session[i].getDatasourcePlatform().getSequences().isEmpty();
            if (!ok) {
                throw new TestErrorException("Session " + sessionName[i] + " has sequences, but it's not supposed to.");
            }
        }
    }
}
Also used : DatasourcePlatform(org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) Iterator(java.util.Iterator) NativeSequence(org.eclipse.persistence.sequencing.NativeSequence) TableSequence(org.eclipse.persistence.sequencing.TableSequence) Sequence(org.eclipse.persistence.sequencing.Sequence) UnaryTableSequence(org.eclipse.persistence.sequencing.UnaryTableSequence) DefaultSequence(org.eclipse.persistence.sequencing.DefaultSequence)

Example 4 with DatasourcePlatform

use of org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform in project eclipselink by eclipse-ee4j.

the class SQLSelectStatement method normalize.

/**
 * INTERNAL:
 * Normalize an expression into a printable structure.
 * i.e. merge the expression with the join expressions.
 * Also replace table names with corresponding aliases.
 * @param clonedExpressions With 2612185 allows additional expressions
 * from multiple bases to be rebuilt on the correct cloned base.
 */
public void normalize(AbstractSession session, ClassDescriptor descriptor, Map clonedExpressions) {
    // Initialize the builder.
    if (getBuilder() == null) {
        if (getWhereClause() == null) {
            setBuilder(new ExpressionBuilder());
        } else {
            setBuilder(getWhereClause().getBuilder());
        }
    }
    ExpressionBuilder builder = getBuilder();
    // as of the correct time.
    if ((session.getAsOfClause() != null) && !isSubSelect()) {
        getWhereClause().asOf(session.getAsOfClause());
    } else if (builder.hasAsOfClause() && builder.getAsOfClause().isUniversal()) {
        // An as of clause set at the query level.
        getWhereClause().asOf(((UniversalAsOfClause) builder.getAsOfClause()).getAsOfClause());
    }
    // aliased it directly.
    if (getWhereClause() == builder) {
        setWhereClause(null);
    }
    builder.setSession(session.getRootSession(null));
    // Some queries are not on objects but for data, thus no descriptor.
    if (!builder.doesNotRepresentAnObjectInTheQuery()) {
        if (descriptor != null) {
            Class<?> queryClass = builder.getQueryClass();
            // that of the server session which is uninitialized.
            if ((queryClass == null) || descriptor.isChildDescriptor() || descriptor.hasTablePerMultitenantPolicy()) {
                builder.setQueryClassAndDescriptor(descriptor.getJavaClass(), descriptor);
            }
        }
    }
    // Compute all other expressions used other than the where clause, i.e. select, order by, group by.
    // Also must ensure that all expression use a unique builder.
    Vector allExpressions = new Vector();
    // Process select expressions.
    rebuildAndAddExpressions(getFields(), allExpressions, builder, clonedExpressions);
    // Process non-select expressions
    if (hasNonSelectFields()) {
        rebuildAndAddExpressions(getNonSelectFields(), allExpressions, builder, clonedExpressions);
    }
    // Process group by expressions.
    if (hasGroupByExpressions()) {
        rebuildAndAddExpressions(getGroupByExpressions(), allExpressions, builder, clonedExpressions);
    }
    // Process union expressions.
    if (hasUnionExpressions()) {
        rebuildAndAddExpressions(getUnionExpressions(), allExpressions, builder, clonedExpressions);
    }
    if (hasHavingExpression()) {
        // rebuildAndAddExpressions(getHavingExpression(), allExpressions, builder, clonedExpressions);
        Expression expression = getHavingExpression();
        ExpressionBuilder originalBuilder = expression.getBuilder();
        if (originalBuilder != builder) {
            // i.e. Report query items could be from parallel expressions.
            if (clonedExpressions.get(originalBuilder) != null) {
                expression = expression.copiedVersionFrom(clonedExpressions);
            } else {
                // Possibly the expression was built with the wrong builder.
                expression = expression.rebuildOn(builder);
            }
            setHavingExpression(expression);
        }
        allExpressions.add(expression);
    }
    // Process order by expressions.
    if (hasOrderByExpressions()) {
        normalizeOrderBy(builder, allExpressions, clonedExpressions, session);
    }
    // Process outer join by expressions.
    if (hasOuterJoinExpressions()) {
        for (OuterJoinExpressionHolder holder : this.getOuterJoinExpressionsHolders()) {
            if (holder.outerJoinedMappingCriteria != null) {
                Expression expression = rebuildExpression(holder.outerJoinedMappingCriteria, builder, clonedExpressions);
                if (holder.outerJoinedMappingCriteria != expression) {
                    holder.outerJoinedMappingCriteria = expression;
                }
                allExpressions.add(expression);
            }
            if (holder.outerJoinedAdditionalJoinCriteria != null) {
                rebuildAndAddExpressions(holder.outerJoinedAdditionalJoinCriteria, allExpressions, builder, clonedExpressions);
            }
        }
    }
    // Process hierarchical query expressions.
    if (hasStartWithExpression()) {
        startWithExpression = getStartWithExpression().rebuildOn(builder);
        allExpressions.add(startWithExpression);
    }
    if (hasConnectByExpression()) {
        connectByExpression = getConnectByExpression().rebuildOn(builder);
    }
    if (hasOrderSiblingsByExpressions()) {
        rebuildAndAddExpressions(getOrderSiblingsByExpressions(), allExpressions, builder, clonedExpressions);
    }
    // We have to handle the cases where the where
    // clause is initially empty but might have clauses forced into it because the class
    // has multiple tables, order by forces a join, etc.  So we have to create a builder
    // and add expressions for it and the extras, but throw it away if they didn't force anything
    Expression oldRoot = getWhereClause();
    ExpressionNormalizer normalizer = new ExpressionNormalizer(this);
    normalizer.setSession(session);
    normalizer.setClonedExpressions(clonedExpressions);
    boolean isDistinctComputed = isDistinctComputed();
    Expression newRoot = null;
    if (oldRoot != null) {
        newRoot = oldRoot.normalize(normalizer);
    }
    // there may be an expression that does not refer to the builder, i.e. value=value.
    if (descriptor != null) {
        builder.normalize(normalizer);
    }
    for (int index = 0; index < allExpressions.size(); index++) {
        Expression expression = (Expression) allExpressions.get(index);
        expression.getBuilder().setSession(session);
        expression.normalize(normalizer);
    }
    // distinct state has been set by normalization, see may be that should be reversed
    if (shouldDistinctBeUsed() && !isDistinctComputed && !session.getPlatform().isLobCompatibleWithDistinct()) {
        for (Object field : getFields()) {
            if (field instanceof DatabaseField) {
                if (Helper.isLob((DatabaseField) field)) {
                    dontUseDistinct();
                    break;
                }
            } else if (field instanceof Expression) {
                if (Helper.hasLob(((Expression) field).getSelectionFields(this.query))) {
                    dontUseDistinct();
                    break;
                }
            }
        }
    }
    // setNormalizedWhereClause must be called to avoid the builder side-effects
    if (newRoot == null) {
        setNormalizedWhereClause(normalizer.getAdditionalExpression());
    } else {
        setNormalizedWhereClause(newRoot.and(normalizer.getAdditionalExpression()));
    }
    if (getWhereClause() != null) {
        allExpressions.add(getWhereClause());
    }
    // there may be an expression that does not refer to the builder, i.e. value=value.
    if (descriptor != null) {
        allExpressions.add(builder);
    }
    // Must also assign aliases to outer joined mapping criterias.
    if (hasOuterJoinExpressions()) {
        // Check for null on criterias.
        for (OuterJoinExpressionHolder holder : this.outerJoinExpressionHolders) {
            // 
            Expression criteria = holder.outerJoinedMappingCriteria;
            if (criteria != null) {
                allExpressions.add(criteria);
            }
            Map<DatabaseTable, Expression> map = holder.outerJoinedAdditionalJoinCriteria;
            if (map != null) {
                Iterator<Expression> it = map.values().iterator();
                while (it.hasNext()) {
                    criteria = it.next();
                    if (criteria != null) {
                        allExpressions.add(criteria);
                    }
                }
            }
        }
    }
    // Bug 2956674 Remove validate call as validation will be completed as the expression was normalized
    // Assign all table aliases.
    assignAliases(allExpressions);
    // If this is data level then the tables must be set manually.
    if (descriptor == null) {
        computeTablesFromTables();
    } else {
        computeTables();
    }
    // normalize the subselect expressions.  For CR#4223.
    if (normalizer.encounteredSubSelectExpressions()) {
        normalizer.normalizeSubSelects(clonedExpressions);
    }
    // When a distinct is used the order bys must be in the select clause, so this forces them into the select.
    if (hasOrderByExpressions()) {
        // CR2114; If this is data level then we don't have a descriptor.
        // We don't have a target class so we must use the root platform. PWK
        // We are not fixing the informix.
        Class<?> queryClass = null;
        if (descriptor != null) {
            queryClass = descriptor.getJavaClass();
        }
        DatasourcePlatform platform = (DatasourcePlatform) session.getPlatform(queryClass);
        if (platform.shouldSelectIncludeOrderBy() || (shouldDistinctBeUsed() && platform.shouldSelectDistinctIncludeOrderBy())) {
            addOrderByExpressionToSelectForDistinct();
        }
    }
}
Also used : DatasourcePlatform(org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) UniversalAsOfClause(org.eclipse.persistence.internal.history.UniversalAsOfClause) Expression(org.eclipse.persistence.expressions.Expression) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) DecoratedDatabaseTable(org.eclipse.persistence.internal.history.DecoratedDatabaseTable) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) Vector(java.util.Vector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector)

Example 5 with DatasourcePlatform

use of org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform in project eclipselink by eclipse-ee4j.

the class SessionsFactory method processLoginConfig.

/**
 * INTERNAL:
 * Process the common elements of a Login.
 */
protected void processLoginConfig(LoginConfig loginConfig, DatasourceLogin login) {
    // Platform class
    String platformClassName = loginConfig.getPlatformClass();
    if (platformClassName != null) {
        try {
            @SuppressWarnings({ "unchecked" }) Class<DatasourcePlatform> platformClass = (Class<DatasourcePlatform>) m_classLoader.loadClass(platformClassName);
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                login.usePlatform(AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(platformClass)));
            } else {
                login.usePlatform(PrivilegedAccessHelper.newInstanceFromClass(platformClass));
            }
        } catch (Exception exception) {
            throw SessionLoaderException.failedToLoadTag("platform-class", platformClassName, exception);
        }
    }
    // Table qualifier
    String tableQualifier = loginConfig.getTableQualifier();
    if (tableQualifier != null) {
        login.setTableQualifier(tableQualifier);
    }
    // Username - setUserName checks for null
    login.setUserName(loginConfig.getUsername());
    // Encryption class (must be set before the password)
    // XML Schema default is org.eclipse.persistence.internal.security.JCEEncryptor
    login.setEncryptionClassName(loginConfig.getEncryptionClass());
    // Password is encrypted on the model - setEncryptedPassword checks for null
    login.setEncryptedPassword(loginConfig.getEncryptedPassword());
    // External connection pool - XML Schema default is false
    login.setUsesExternalConnectionPooling(loginConfig.getExternalConnectionPooling());
    // External transaction controller - XML Schema default is false
    login.setUsesExternalTransactionController(loginConfig.getExternalTransactionController());
    // Sequencing - XML Schema default is null
    if (loginConfig.getSequencingConfig() != null) {
        if (loginConfig.getSequencingConfig().getDefaultSequenceConfig() != null) {
            Sequence sequence = buildSequence(loginConfig.getSequencingConfig().getDefaultSequenceConfig());
            login.setDefaultSequence(sequence);
        }
        if ((loginConfig.getSequencingConfig().getSequenceConfigs() != null) && !loginConfig.getSequencingConfig().getSequenceConfigs().isEmpty()) {
            Enumeration<SequenceConfig> eSequenceConfigs = loginConfig.getSequencingConfig().getSequenceConfigs().elements();
            while (eSequenceConfigs.hasMoreElements()) {
                Sequence sequence = buildSequence(eSequenceConfigs.nextElement());
                login.addSequence(sequence);
            }
        }
    }
    // Properties (assumes they are all valid)
    if (loginConfig.getPropertyConfigs() != null) {
        Enumeration<PropertyConfig> e = loginConfig.getPropertyConfigs().elements();
        while (e.hasMoreElements()) {
            PropertyConfig propertyConfig = e.nextElement();
            login.getProperties().put(propertyConfig.getName(), propertyConfig.getValue());
        }
    }
}
Also used : PrivilegedNewInstanceFromClass(org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass) DatasourcePlatform(org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform) PropertyConfig(org.eclipse.persistence.internal.sessions.factories.model.property.PropertyConfig) PrivilegedNewInstanceFromClass(org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass) SequenceConfig(org.eclipse.persistence.internal.sessions.factories.model.sequencing.SequenceConfig) XMLFileSequenceConfig(org.eclipse.persistence.internal.sessions.factories.model.sequencing.XMLFileSequenceConfig) NativeSequenceConfig(org.eclipse.persistence.internal.sessions.factories.model.sequencing.NativeSequenceConfig) DefaultSequenceConfig(org.eclipse.persistence.internal.sessions.factories.model.sequencing.DefaultSequenceConfig) UnaryTableSequenceConfig(org.eclipse.persistence.internal.sessions.factories.model.sequencing.UnaryTableSequenceConfig) TableSequenceConfig(org.eclipse.persistence.internal.sessions.factories.model.sequencing.TableSequenceConfig) Sequence(org.eclipse.persistence.sequencing.Sequence) NativeSequence(org.eclipse.persistence.sequencing.NativeSequence) DefaultSequence(org.eclipse.persistence.sequencing.DefaultSequence) UnaryTableSequence(org.eclipse.persistence.sequencing.UnaryTableSequence) TableSequence(org.eclipse.persistence.sequencing.TableSequence) ValidationException(org.eclipse.persistence.exceptions.ValidationException) PrivilegedActionException(java.security.PrivilegedActionException) SessionLoaderException(org.eclipse.persistence.exceptions.SessionLoaderException)

Aggregations

DatasourcePlatform (org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform)7 Sequence (org.eclipse.persistence.sequencing.Sequence)4 DatabaseTable (org.eclipse.persistence.internal.helper.DatabaseTable)2 DefaultSequence (org.eclipse.persistence.sequencing.DefaultSequence)2 NativeSequence (org.eclipse.persistence.sequencing.NativeSequence)2 TableSequence (org.eclipse.persistence.sequencing.TableSequence)2 UnaryTableSequence (org.eclipse.persistence.sequencing.UnaryTableSequence)2 PrivilegedActionException (java.security.PrivilegedActionException)1 Iterator (java.util.Iterator)1 Vector (java.util.Vector)1 RelationalDescriptor (org.eclipse.persistence.descriptors.RelationalDescriptor)1 SessionLoaderException (org.eclipse.persistence.exceptions.SessionLoaderException)1 ValidationException (org.eclipse.persistence.exceptions.ValidationException)1 Expression (org.eclipse.persistence.expressions.Expression)1 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)1 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)1 NonSynchronizedVector (org.eclipse.persistence.internal.helper.NonSynchronizedVector)1 DecoratedDatabaseTable (org.eclipse.persistence.internal.history.DecoratedDatabaseTable)1 UniversalAsOfClause (org.eclipse.persistence.internal.history.UniversalAsOfClause)1 EntityManagerImpl (org.eclipse.persistence.internal.jpa.EntityManagerImpl)1