Package org.jpox.store.rdbms

Examples of org.jpox.store.rdbms.SQLController$ConnectionStatementState


            // Set the state to "inserting" (may already be at this state if multiple inheritance level INSERT)
            sm.changeActivityState(ActivityState.INSERTING, table);

            ObjectManager om = sm.getObjectManager();
            RDBMSManager storeMgr = (RDBMSManager)om.getStoreManager();
            SQLController sqlControl = storeMgr.getSQLController();
            ManagedConnection mconn = storeMgr.getConnection(om);
            try
            {
                PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, insertStmt, batch);

                try
                {
                    // Provide the primary key field(s)
                    if (table.getIdentityType() == IdentityType.DATASTORE)
                    {
                        if (!table.isObjectIDDatastoreAttributed() || !table.isBaseDatastoreClass())
                        {
                            int[] paramNumber = {IDPARAMNUMBER};
                            table.getDataStoreObjectIdMapping().setObject(om, ps, paramNumber, sm.getInternalObjectId());
                        }
                    }
                    else if (table.getIdentityType() == IdentityType.APPLICATION)
                    {
                        sm.provideFields(pkFieldNumbers, new ParameterSetter(sm, ps, statementExpressionIndex, true));
                    }

                    // Provide all non-key fields needed for the insert.
                    // This provides "persistence-by-reachability" for these fields
                    int numberOfFieldsToProvide = 0;
                    for (int i = 0; i < insertFieldNumbers.length; i++)
                    {
                        if (insertFieldNumbers[i] < sm.getHighestFieldNumber())
                        {
                            numberOfFieldsToProvide++;
                        }
                    }
                    int j = 0;
                    int[] classFieldNumbers = new int[numberOfFieldsToProvide];
                    for (int i = 0; i < insertFieldNumbers.length; i++)
                    {
                        if (insertFieldNumbers[i] < sm.getHighestFieldNumber())
                        {
                            classFieldNumbers[j++] = insertFieldNumbers[i];
                        }
                        else
                        {
                            // Any fields out of range for the class, we default or null.
                            // This happens when we insert a base class object
                            // and the sub-class is persisted to the same table ("superclass-table").
                            StatementExpressionIndex stmtExprIndex = statementExpressionIndex[insertFieldNumbers[i]];
                            stmtExprIndex.getMapping().setDefault(om, ps, stmtExprIndex.getParameterIndex());
                        }
                    }
                    sm.provideFields(classFieldNumbers, new ParameterSetter(sm, ps, statementExpressionIndex, true));

                    if (table.getVersionMapping(false) != null)
                    {
                        // Surrogate version - set the new version for the object
                        Object currentVersion = sm.getObjectManager().getApiAdapter().getVersion(sm);
                        Object nextOptimisticVersion = VersionMapping.getNextVersion(table.getVersionMetaData(),
                            currentVersion);
                        table.getVersionMapping(false).setObject(om, ps, versionStatementExpressionIndex.getParameterIndex(), nextOptimisticVersion);
                        sm.setTransactionalVersion(nextOptimisticVersion);
                    }
                    else if (vermd != null && vermd.getFieldName() != null)
                    {
                        // Version field - set the new version for the object
                        Object currentVersion = sm.getObjectManager().getApiAdapter().getVersion(sm);
                        Object nextOptimisticVersion = VersionMapping.getNextVersion(table.getVersionMetaData(),
                            currentVersion);
                        sm.setTransactionalVersion(nextOptimisticVersion);
                    }

                    // Discriminator mapping (optional)
                    if (table.getDiscriminatorMapping(false) != null)
                    {
                        DiscriminatorMetaData dismd = table.getDiscriminatorMetaData();
                        if (dismd.getStrategy() == DiscriminatorStrategy.CLASS_NAME)
                        {
                            table.getDiscriminatorMapping(false).setObject(om, ps, discriminatorStatementExpressionIndex.getParameterIndex(),
                                sm.getObject().getClass().getName());
                        }
                        else if (dismd.getStrategy() == DiscriminatorStrategy.VALUE_MAP)
                        {
                            // Use Discriminator info for the actual class
                            dismd = sm.getClassMetaData().getInheritanceMetaData().getDiscriminatorMetaData();
                            table.getDiscriminatorMapping(false).setObject(om, ps, discriminatorStatementExpressionIndex.getParameterIndex(), dismd.getValue());
                        }
                    }

                    // External FK columns (optional)
                    if (externalFKStatementExpressionIndex != null)
                    {
                        for (int i=0;i<externalFKStatementExpressionIndex.length;i++)
                        {
                            Object fkValue = sm.getValueForExternalField(externalFKStatementExpressionIndex[i].getMapping());
                            if (fkValue != null)
                            {
                                // Need to provide the owner field number so PCMapping can work out if it is inserted yet
                                AbstractMemberMetaData ownerFmd =
                                    table.getMetaDataForExternalMapping(externalFKStatementExpressionIndex[i].getMapping(),
                                        MappingConsumer.MAPPING_TYPE_EXTERNAL_FK);
                                externalFKStatementExpressionIndex[i].getMapping().setObject(om, ps,
                                    externalFKStatementExpressionIndex[i].getParameterIndex(), fkValue, null, ownerFmd.getAbsoluteFieldNumber());
                            }
                            else
                            {
                                // We're inserting a null so dont need the owner field
                                externalFKStatementExpressionIndex[i].getMapping().setObject(om, ps,
                                    externalFKStatementExpressionIndex[i].getParameterIndex(), null);
                            }
                        }
                    }

                    // External FK discriminator columns (optional)
                    if (externalFKDiscrimStatementExpressionIndex != null)
                    {
                        for (int i=0;i<externalFKDiscrimStatementExpressionIndex.length;i++)
                        {
                            Object discrimValue = sm.getValueForExternalField(externalFKDiscrimStatementExpressionIndex[i].getMapping());
                            externalFKDiscrimStatementExpressionIndex[i].getMapping().setObject(om, ps,
                                externalFKDiscrimStatementExpressionIndex[i].getParameterIndex(), discrimValue);
                        }
                    }

                    // External order columns (optional)
                    if (externalOrderStatementExpressionIndex != null)
                    {
                        for (int i=0;i<externalOrderStatementExpressionIndex.length;i++)
                        {
                            Object orderValue = sm.getValueForExternalField(externalOrderStatementExpressionIndex[i].getMapping());
                            if (orderValue == null)
                            {
                                // No order value so use -1
                                orderValue = new Integer(-1);
                            }
                            externalOrderStatementExpressionIndex[i].getMapping().setObject(om, ps,
                                externalOrderStatementExpressionIndex[i].getParameterIndex(), orderValue);
                        }
                    }

                    sqlControl.executeStatementUpdate(mconn, insertStmt, ps, !batch);

                    if (hasAutoIncrementColumn)
                    {
                        // Identity was set in the datastore using auto-increment/identity/serial etc
                        Object newId = getInsertedDatastoreIdentity(om, sqlControl, sm, mconn, ps);
                        if (JPOXLogger.DATASTORE_PERSIST.isDebugEnabled())
                        {
                            JPOXLogger.DATASTORE_PERSIST.debug(LOCALISER.msg("052206",
                                StringUtils.toJVMIDString(sm.getObject()), newId));
                        }
                        sm.setPostStoreNewObjectId(newId);
                    }

                    // Update the insert status for this table
                    sm.changeActivityState(ActivityState.INSERTING_CALLBACKS, table);

                    // Make sure all relation fields (1-1, N-1 with FK) we processed in the INSERT are attached.
                    // This is necessary because with a bidir relation and the other end attached we can just
                    // do the INSERT above first and THEN attach the other end here
                    // (if we did it the other way around we would get a NotYetFlushedException thrown above).
                    for (int i=0;i<relationFieldNumbers.length;i++)
                    {
                        Object value = sm.provideField(relationFieldNumbers[i]);
                        if (value != null && om.getApiAdapter().isDetached(value))
                        {
                            Object valueAttached = om.persistObjectInternal(value, null, null, -1, StateManager.PC);
                            sm.replaceField(relationFieldNumbers[i], valueAttached, false);
                        }
                    }

                    // Perform reachability on all fields that have no datastore column (1-1 bi non-owner, N-1 bi join)
                    int numberOfReachableFields = 0;
                    for (int i = 0; i < reachableFieldNumbers.length; i++)
                    {
                        if (reachableFieldNumbers[i] < sm.getHighestFieldNumber())
                        {
                            numberOfReachableFields++;
                        }
                    }
                    classFieldNumbers = new int[numberOfReachableFields];
                    j = 0;
                    for (int i = 0; i < reachableFieldNumbers.length; i++)
                    {
                        if (reachableFieldNumbers[i] < sm.getHighestFieldNumber())
                        {
                            classFieldNumbers[j++] = reachableFieldNumbers[i];
                        }
                        else
                        {
                            // Any fields out of range for the class, we default or null.
                            // This happens when we insert a base class object
                            // and the sub-class is persisted to the same table ("superclass-table").
                            StatementExpressionIndex stmtExprIndex = retrievedStatementExpressionIndex[reachableFieldNumbers[i]];
                            stmtExprIndex.getMapping().setDefault(om, ps, stmtExprIndex.getParameterIndex());
                        }
                    }
                    sm.provideFields(classFieldNumbers, new ParameterSetter(sm, ps, retrievedStatementExpressionIndex, true));
                }
                finally
                {
                    sqlControl.closeStatement(mconn, ps);
                }
            }
            finally
            {
                mconn.release();
View Full Code Here


        String removeAllStmt = getRemoveAllStmt(sm, elements);
        try
        {
            ObjectManager om = sm.getObjectManager();
            ManagedConnection mconn = storeMgr.getConnection(om);
            SQLController sqlControl = storeMgr.getSQLController();
            try
            {
                PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, removeAllStmt, false);
                try
                {
                    int jdbcPosition = 1;
                    Iterator iter = elements.iterator();
                    while (iter.hasNext())
                    {
                        Object element = iter.next();
                        jdbcPosition = populateOwnerInStatement(sm, om, ps, jdbcPosition);
                        jdbcPosition = populateElementInStatement(om, ps, element, jdbcPosition);
                        if (relationDiscriminatorMapping != null)
                        {
                            jdbcPosition = populateRelationDiscriminatorInStatement(om, ps, jdbcPosition);
                        }
                    }

                    int[] number = sqlControl.executeStatementUpdate(mconn, removeAllStmt, ps, true);
                    if (number[0] > 0)
                    {
                        modified = true;
                    }
                }
                finally
                {
                    sqlControl.closeStatement(mconn, ps);
                }
            }
            finally
            {
                mconn.release();
View Full Code Here

        String stmt = getLocateStmt();
        try
        {
            ObjectManager om = sm.getObjectManager();
            ManagedConnection mconn = storeMgr.getConnection(om);
            SQLController sqlControl = storeMgr.getSQLController();
            try
            {
                PreparedStatement ps = sqlControl.getStatementForQuery(mconn, stmt);
                try
                {
                    int jdbcPosition = 1;
                    jdbcPosition = populateOwnerInStatement(sm, om, ps, jdbcPosition);
                    jdbcPosition = populateElementInStatement(om, ps, element, jdbcPosition);
                    if (relationDiscriminatorMapping != null)
                    {
                        jdbcPosition = populateRelationDiscriminatorInStatement(om, ps, jdbcPosition);
                    }

                    ResultSet rs = sqlControl.executeStatementQuery(mconn, stmt, ps);
                    try
                    {
                        if (!rs.next())
                        {
                            exists = false;
                        }
                    }
                    catch (SQLException sqle)
                    {
                        rs.close();
                    }
                }
                finally
                {
                    sqlControl.closeStatement(mconn, ps);
                }
            }
            finally
            {
                mconn.release();
View Full Code Here

        try
        {
            ObjectManager om = sm.getObjectManager();
            ManagedConnection mconn = storeMgr.getConnection(om);
            SQLController sqlControl = storeMgr.getSQLController();
            sqlControl.processStatementsForConnection(mconn); // Process all waiting batched statements before we start our work

            try
            {
                int nextOrderID = 0;
                if (orderMapping != null)
View Full Code Here

        if (toBeInserted)
        {
            String addStmt = getAddStmt();
            boolean notYetFlushedError = false;
            ObjectManager om = sm.getObjectManager();
            SQLController sqlControl = storeMgr.getSQLController();
            PreparedStatement ps = sqlControl.getStatementForUpdate(conn, addStmt, batched);
            try
            {
                // Insert the join table row
                int jdbcPosition = 1;
                jdbcPosition = populateOwnerInStatement(sm, om, ps, jdbcPosition);
                jdbcPosition = populateElementInStatement(om, ps, element, jdbcPosition);
                if (orderMapping != null)
                {
                    jdbcPosition = populateOrderInStatement(om, ps, orderId, jdbcPosition);
                }
                if (relationDiscriminatorMapping != null)
                {
                    jdbcPosition = populateRelationDiscriminatorInStatement(om, ps, jdbcPosition);
                }

                // Execute the statement
                return sqlControl.executeStatementUpdate(conn, addStmt, ps, executeNow);
            }
            catch (NotYetFlushedException nfe)
            {
                notYetFlushedError = true;
                throw nfe;
            }
            finally
            {
                if (notYetFlushedError)
                {
                    sqlControl.abortStatementForConnection(conn, ps);
                }
                else
                {
                    sqlControl.closeStatement(conn, ps);
                }
            }
        }
        return null;
    }
View Full Code Here

        String stmt = getMaxOrderColumnIdStmt();
        try
        {
            ManagedConnection mconn = storeMgr.getConnection(om);
            SQLController sqlControl = storeMgr.getSQLController();

            try
            {
                PreparedStatement ps = sqlControl.getStatementForQuery(mconn, stmt);
                try
                {
                    int jdbcPosition = 1;
                    jdbcPosition = populateOwnerInStatement(sm, om, ps, jdbcPosition);
                    if (relationDiscriminatorMapping != null)
                    {
                        jdbcPosition = populateRelationDiscriminatorInStatement(om, ps, jdbcPosition);
                    }

                    ResultSet rs = sqlControl.executeStatementQuery(mconn, stmt, ps);
                    try
                    {
                        if (!rs.next())
                        {
                            nextID = 1;
                        }
                        else
                        {
                            nextID = rs.getInt(1)+1;
                        }

                        SQLWarnings.log(rs);
                    }
                    finally
                    {
                        rs.close();
                    }
                }
                finally
                {
                    sqlControl.closeStatement(mconn, ps);
                }
            }
            finally
            {
                mconn.release();
View Full Code Here

            Collection qr = null;
            try
            {
                RDBMSManager storeMgr = (RDBMSManager)om.getStoreManager();
                ManagedConnection mconn = storeMgr.getConnection(om);
                SQLController sqlControl = storeMgr.getSQLController();

                try
                {
                    PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, compiledSQL, false);
                    try
                    {
                        // Set the values of any parameters
                        if (parameters != null)
                        {
                            for (int i=0;i<parameters.size();i++)
                            {
                                Object obj = parameters.get(new Integer(i+1));
                                ps.setObject((i+1), obj);
                            }
                        }

                        // Execute the update statement
                        int[] rcs = sqlControl.executeStatementUpdate(mconn, compiledSQL, ps, true);
                        qr = new ArrayList();
                        qr.add(new Long(rcs[0])); // Return a single Long with the number of records updated
                    }
                    finally
                    {
                        sqlControl.closeStatement(mconn, ps);
                    }
                }
                finally
                {
                    mconn.release();
View Full Code Here

            ObjectManager om = sm.getObjectManager();
            RDBMSManager storeMgr = (RDBMSManager)om.getStoreManager();
            try
            {
                ManagedConnection mconn = storeMgr.getConnection(om);
                SQLController sqlControl = storeMgr.getSQLController();

                try
                {
                    PreparedStatement ps = sqlControl.getStatementForQuery(mconn, locateStmt);

                    try
                    {
                        if (sm.getInternalObjectId() instanceof OID)
                        {
                            // datastore identity
                            table.getDataStoreObjectIdMapping().setObject(om, ps,
                                mappingStatementIndex.getDatastoreId().getParameterIndex(),
                                sm.getInternalObjectId());
                        }
                        else
                        {
                            // application identity - provide primary key fields
                            sm.provideFields(pkFieldNumbers, new ParameterSetter(sm, ps,
                                mappingStatementIndex.getFields(), false));
                        }

                        ResultSet rs = sqlControl.executeStatementQuery(mconn, locateStmt, ps);
                        try
                        {
                            if (!rs.next())
                            {
                                JPOXLogger.DATASTORE_RETRIEVE.info(LOCALISER.msg("050018",
                                    sm.getInternalObjectId()));
                                throw new JPOXObjectNotFoundException("No such database row", sm.getInternalObjectId());
                            }
                        }
                        finally
                        {
                            rs.close();
                        }
                    }
                    finally
                    {
                        sqlControl.closeStatement(mconn, ps);
                    }
                }
                finally
                {
                    mconn.release();
View Full Code Here

TOP

Related Classes of org.jpox.store.rdbms.SQLController$ConnectionStatementState

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.