Package com.mysql.clusterj.core.spi

Examples of com.mysql.clusterj.core.spi.SessionSPI


     * @return the results of executing the query
     */
    public List<T> getResultList(QueryExecutionContext context) {
        assertAllParametersBound(context);

        SessionSPI session = context.getSession();
        session.startAutoTransaction();
        // set up results and table information
        List<T> resultList = new ArrayList<T>();
        try {
            // execute the query
            ResultData resultData = getResultData(context);
            // put the result data into the result list
            while (resultData.next()) {
                T row = (T) session.newInstance(cls);
                ValueHandler handler =domainTypeHandler.getValueHandler(row);
                // set values from result set into object
                domainTypeHandler.objectSetValues(resultData, handler);
                resultList.add(row);
            }
            session.endAutoTransaction();
            return resultList;
        } catch (ClusterJException ex) {
            session.failAutoTransaction();
            throw ex;
        } catch (Exception ex) {
            session.failAutoTransaction();
            throw new ClusterJException(
                    local.message("ERR_Exception_On_Query"), ex);
        }
    }
View Full Code Here


     * @param context the query context, including the bound parameters
     * @return the raw result data from the query
     * @throws ClusterJUserException if not all parameters are bound
     */
    public ResultData getResultData(QueryExecutionContext context) {
  SessionSPI session = context.getSession();
        // execute query based on what kind of scan is needed
        // if no where clause, scan the entire table
        CandidateIndexImpl index = where==null?
            CandidateIndexImpl.getIndexForNullWhereClause():
            where.getBestCandidateIndex(context);
        ScanType scanType = index.getScanType();
        Map<String, Object> explain = newExplain(index, scanType);
        context.setExplain(explain);
        ResultData result = null;
        Index storeIndex;

        switch (scanType) {

            case PRIMARY_KEY: {
                // perform a select operation
                Operation op = session.getSelectOperation(domainTypeHandler.getStoreTable());
                // set key values into the operation
                index.operationSetKeys(context, op);
                // set the expected columns into the operation
                domainTypeHandler.operationGetValues(op);
                // execute the select and get results
                result = op.resultData();
                break;
            }

            case INDEX_SCAN: {
                storeIndex = index.getStoreIndex();
                if (logger.isDetailEnabled()) logger.detail("Using index scan with index " + index.getIndexName());
                IndexScanOperation op;
                // perform an index scan operation
                if (index.isMultiRange()) {
                    op = session.getIndexScanOperationMultiRange(storeIndex, domainTypeHandler.getStoreTable());
                   
                } else {
                    op = session.getIndexScanOperation(storeIndex, domainTypeHandler.getStoreTable());
                   
                }
                // set the expected columns into the operation
                domainTypeHandler.operationGetValues(op);
                // set the bounds into the operation
                index.operationSetBounds(context, op);
                // set additional filter conditions
                where.filterCmpValue(context, op);
                // execute the scan and get results
                result = op.resultData();
                break;
            }

            case TABLE_SCAN: {
                if (logger.isDetailEnabled()) logger.detail("Using table scan");
                // perform a table scan operation
                ScanOperation op = session.getTableScanOperation(domainTypeHandler.getStoreTable());
                // set the expected columns into the operation
                domainTypeHandler.operationGetValues(op);
                // set the bounds into the operation
                if (where != null) {
                    where.filterCmpValue(context, op);
                }
                // execute the scan and get results
                result = op.resultData();
                break;
            }

            case UNIQUE_KEY: {
                storeIndex = index.getStoreIndex();
                if (logger.isDetailEnabled()) logger.detail("Using unique lookup with index " + index.getIndexName());
                // perform a unique lookup operation
                IndexOperation op = session.getUniqueIndexOperation(storeIndex, domainTypeHandler.getStoreTable());
                // set the keys of the indexName into the operation
                where.operationEqual(context, op);
                // set the expected columns into the operation
                //domainTypeHandler.operationGetValuesExcept(op, indexName);
                domainTypeHandler.operationGetValues(op);
                // execute the select and get results
                result = op.resultData();
                break;
            }

            default:
                session.failAutoTransaction();
                throw new ClusterJFatalInternalException(
                        local.message("ERR_Illegal_Scan_Type", scanType));
        }
        context.deleteFilters();
        return result;
View Full Code Here

     * @param context the query context, including the bound parameters
     * @return the number of instances deleted
     * @throws ClusterJUserException if not all parameters are bound
     */
    public int deletePersistentAll(QueryExecutionContext context) {
                                SessionSPI session = context.getSession();
        // calculate what kind of scan is needed
        // if no where clause, scan the entire table
        CandidateIndexImpl index = where==null?
            CandidateIndexImpl.getIndexForNullWhereClause():
            where.getBestCandidateIndex(context);
        ScanType scanType = index.getScanType();
        Map<String, Object> explain = newExplain(index, scanType);
        context.setExplain(explain);
        int result = 0;
        int errorCode = 0;
        Index storeIndex;
        session.startAutoTransaction();

        try {
            switch (scanType) {

                case PRIMARY_KEY: {
                    // perform a delete by primary key operation
                    if (logger.isDetailEnabled()) logger.detail("Using delete by primary key.");
                    Operation op = session.getDeleteOperation(domainTypeHandler.getStoreTable());
                    // set key values into the operation
                    index.operationSetKeys(context, op);
                    // execute the delete operation
                    session.executeNoCommit(false, true);
                    errorCode = op.errorCode();
                    // a non-zero result means the row was not deleted
                    result = (errorCode == 0?1:0);
                    break;
                }

                case UNIQUE_KEY: {
                    storeIndex = index.getStoreIndex();
                    if (logger.isDetailEnabled()) logger.detail(
                            "Using delete by unique key  " + index.getIndexName());
                    // perform a delete by unique key operation
                    IndexOperation op = session.getUniqueIndexDeleteOperation(storeIndex,
                            domainTypeHandler.getStoreTable());
                    // set the keys of the indexName into the operation
                    where.operationEqual(context, op);
                    // execute the delete operation
                    session.executeNoCommit(false, true);
                    errorCode = op.errorCode();
                    // a non-zero result means the row was not deleted
                    result = (errorCode == 0?1:0);
                    break;
                }

                case INDEX_SCAN: {
                    storeIndex = index.getStoreIndex();
                    if (logger.isDetailEnabled()) logger.detail(
                            "Using delete by index scan with index " + index.getIndexName());
                    // perform an index scan operation
                    IndexScanOperation op = session.getIndexScanDeleteOperation(storeIndex,
                            domainTypeHandler.getStoreTable());
                    // set the expected columns into the operation
                    domainTypeHandler.operationGetValues(op);
                    // set the bounds into the operation
                    index.operationSetBounds(context, op);
                    // set additional filter conditions
                    where.filterCmpValue(context, op);
                    // delete results of the scan; don't abort if no row found
                    result = session.deletePersistentAll(op, false);
                    break;
                }

                case TABLE_SCAN: {
                    if (logger.isDetailEnabled()) logger.detail("Using delete by table scan");
                    // perform a table scan operation
                    ScanOperation op = session.getTableScanDeleteOperation(domainTypeHandler.getStoreTable());
                    // set the expected columns into the operation
                    domainTypeHandler.operationGetValues(op);
                    // set the bounds into the operation
                    if (where != null) {
                        where.filterCmpValue(context, op);
                    }
                    // delete results of the scan; don't abort if no row found
                    result = session.deletePersistentAll(op, false);
                    break;
                }

                default:
                    throw new ClusterJFatalInternalException(
                            local.message("ERR_Illegal_Scan_Type", scanType));
            }
            context.deleteFilters();
            session.endAutoTransaction();
            return result;
        } catch (ClusterJException e) {
            session.failAutoTransaction();
            throw e;
        } catch (Exception e) {
            session.failAutoTransaction();
            throw new ClusterJException(local.message("ERR_Exception_On_Query"), e);
        }
    }
View Full Code Here

        public ResultSetInternalMethods execute(InterceptorImpl interceptor,
                ParameterBindings parameterBindings) throws SQLException {
            // create value handler to copy data from parameters to ndb
            // count the parameters
            int count = countParameters(parameterBindings);
            SessionSPI session = interceptor.getSession();
            Map<String, Object> parameters = createParameterMap(queryDomainType, parameterBindings, 0, count);
            QueryExecutionContext context = new QueryExecutionContextImpl(session, parameters);
            session.startAutoTransaction();
            try {
                ResultData resultData = queryDomainType.getResultData(context);
                // session.endAutoTransaction();
                return new ResultSetInternalMethodsImpl(resultData, columnNumberToFieldNumberMap,
                        columnNameToFieldNumberMap, session);
            } catch (Exception e) {
                e.printStackTrace();
                session.failAutoTransaction();
                return null;
            }
        }
View Full Code Here

            super(domainTypeHandler);
        }

        public ResultSetInternalMethods execute(InterceptorImpl interceptor,
                ParameterBindings parameterBindings) throws SQLException {
            SessionSPI session = interceptor.getSession();
            if (queryDomainType == null) {
                int rowsDeleted = session.deletePersistentAll(domainTypeHandler);
                if (logger.isDebugEnabled()) logger.debug("deleteAll deleted: " + rowsDeleted);
                return new ResultSetInternalMethodsUpdateCount(rowsDeleted);
            } else {
                int numberOfBoundParameters = countParameters(parameterBindings);
                int numberOfStatements = numberOfBoundParameters / numberOfParameters;
View Full Code Here

            super(domainTypeHandler, columnNames, columnNames.size());
        }

        public ResultSetInternalMethods execute(InterceptorImpl interceptor,
                ParameterBindings parameterBindings) throws SQLException {
            SessionSPI session = interceptor.getSession();
            int numberOfBoundParameters = countParameters(parameterBindings);
            int numberOfStatements = numberOfBoundParameters / numberOfParameters;
            if (logger.isDebugEnabled()) logger.debug("SQLExecutor.Insert.execute"
                    + " numberOfParameters: " + numberOfParameters
                    + " numberOfBoundParameters: " + numberOfBoundParameters
                    + " numberOfFields: " + numberOfFields
                    + " numberOfStatements: " + numberOfStatements
                    );
            // interceptor.beforeClusterjStart();
            // session asks for values by field number which are converted to parameter number
            for (int offset = 0; offset < numberOfBoundParameters; offset += numberOfParameters) {
                ValueHandler valueHandler = getValueHandler(parameterBindings, fieldNumberToColumnNumberMap, offset);
                session.insert(domainTypeHandler, valueHandler);
            }
            session.flush();
            // interceptor.afterClusterjStart();
            return new ResultSetInternalMethodsUpdateCount(numberOfStatements);
        }
View Full Code Here

                        .registerDependency(relatedTypeMapping);
                this.relatedFieldName = relatedFieldMapping.getName();
                relatedFieldLoadManager = new RelatedFieldLoadManager() {
                    public void load(OpenJPAStateManager sm, NdbOpenJPAStoreManager store,
                            JDBCFetchConfiguration fetch) throws SQLException {
                        SessionSPI session = store.getSession();
                        session.startAutoTransaction();
                        NdbOpenJPAResult queryResult = queryRelated(sm, store);
                        Object related = null;
                        try {
                            if (queryResult.next()) {
                                // instantiate the related object from the result of the query
                                related = store.load(relatedTypeMapping, fetch, (BitSet) null, queryResult);
                            }
                            if (logger.isDetailEnabled()) logger.detail("related object is: " + related);
                            // store the value of the related object in this field
                            sm.storeObjectField(fieldNumber, related);
                            session.endAutoTransaction();
                        } catch (Exception e) {
                            session.failAutoTransaction();
                        }
                    }
                };
                if (logger.isDetailEnabled()) logger.detail("Single-valued relationship field " + name
                        + " is mapped by " + relatedTypeName + " field " + relatedFieldName
                        + " with relatedDomainTypeHandler " + relatedDomainTypeHandler.getName());
            } else if (isMappedBy && !isToOne) {
                // mapped by other side with multiple instances
                this.relatedTypeMapping = mappedByMapping.getDeclaringMapping();
                this.relatedDomainTypeHandler = ((NdbOpenJPADomainTypeHandlerImpl<?>)domainTypeHandler)
                        .registerDependency(relatedTypeMapping);
                this.relatedFieldName = mappedByMapping.getName();
                relatedTypeName = relatedDomainTypeHandler.getName();
                if (logger.isDetailEnabled()) logger.detail("Multi-valued relationship field " + name
                        + " is mapped by " + relatedTypeName + " field " + relatedFieldName);
                    relatedFieldLoadManager = new RelatedFieldLoadManager() {
                        public void load(OpenJPAStateManager sm, NdbOpenJPAStoreManager store,
                                JDBCFetchConfiguration fetch) throws SQLException {
                            SessionSPI session = store.getSession();
                            session.startAutoTransaction();
                            try {
                                NdbOpenJPAResult queryResult = queryRelated(sm, store);
                                while (queryResult.next()) {
                                    if (logger.isDetailEnabled()) logger.detail("loading related instance of type: " + relatedTypeMapping.getDescribedType().getName());
                                    store.load(relatedTypeMapping, fetch, (BitSet) null, queryResult);
                                }
                                fieldMapping.load(sm, store, fetch);
                                session.endAutoTransaction();
                            } catch (Exception e) {
                                session.failAutoTransaction();
                                throw new ClusterJException(local.message("ERR_Exception_While_Loading"), e);
                            }
                    }
                };
            } else {
View Full Code Here

TOP

Related Classes of com.mysql.clusterj.core.spi.SessionSPI

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.