Package org.apache.openjpa.jdbc.sql

Examples of org.apache.openjpa.jdbc.sql.Select


            // if this is ordered we need to know the next seq to use in case
            // objects are added to the collection
            if (field.getOrderColumn() != null) {
                // we don't allow ordering table per class one-many's, so
                // we know we don't need a union
                Select sel = store.getSQLFactory().newSelect();
                sel.setAggregate(true);
                StringBuffer sql = new StringBuffer();
                sql.append("MAX(").
                    append(sel.getColumnAlias(field.getOrderColumn())).
                    append(")");
                sel.select(sql.toString(), field);
                ClassMapping rel = getDefaultElementMapping(false);
                sel.whereForeignKey(getJoinForeignKey(rel),
                    sm.getObjectId(), field.getDefiningMapping(), store);

                Result res = sel.execute(store, fetch);
                try {
                    res.next();
                    coll.getChangeTracker().setNextSequence
                        (res.getInt(field) + 1);
                } finally {
                    res.close();
                }
            }
            sm.storeObjectField(field.getIndex(), coll);
            return;
        }

        //cache union for field here
        // select data for this sm
        boolean found = true;
        final ClassMapping[] elems = getIndependentElementMappings(true);
        final Joins[] resJoins = new Joins[Math.max(1, elems.length)];
        List parmList = null;
        Union union = null;
        SelectImpl sel = null;
        Map<JDBCStoreManager.SelectKey, Object[]> storeCollectionUnionCache = null;
        JDBCStoreManager.SelectKey selKey = null;
        if (!((JDBCStoreManager)store).isQuerySQLCacheOn() || elems.length > 1)
            union = newUnion(sm, store, fetch, elems, resJoins);
        else {
            parmList = new ArrayList();
            JDBCFetchConfiguration fetchClone = new JDBCFetchConfigurationImpl();
            fetchClone.copy(fetch);
          
            // to specify the type so that no cast is needed
            storeCollectionUnionCache = ((JDBCStoreManager)store).
                getCacheMapFromQuerySQLCache(StoreCollectionFieldStrategy.class);
            selKey =
                new JDBCStoreManager.SelectKey(null, field, fetchClone);
            Object[] objs = storeCollectionUnionCache.get(selKey);
            if (objs != null) {
                union = (Union) objs[0];
                resJoins[0] = (Joins) objs[1];
            }
            else {
                synchronized(storeCollectionUnionCache) {
                    objs = storeCollectionUnionCache.get(selKey);
                    if (objs == null) {
                        // select data for this sm
                        union = newUnion(sm, store, fetch, elems, resJoins);
                        found = false;
                    } else {
                        union = (Union) objs[0];
                        resJoins[0] = (Joins) objs[1];
                    }

                    sel = ((LogicalUnion.UnionSelect)union.getSelects()[0]).
                        getDelegate();
                    if (sel.getSQL() == null) {
                      ((SelectImpl)sel).setSQL(store, fetch);
                        found = false;
                    }

                    // only cache the union when elems length is 1 for now
                    if (!found) {
                        Object[] objs1 = new Object[2];
                        objs1[0] = union;
                        objs1[1] = resJoins[0];
                        ((JDBCStoreManager)store).addToSqlCache(
                            storeCollectionUnionCache, selKey, objs1);
                     }
                }
            }
           
            Log log = store.getConfiguration().
                getLog(JDBCConfiguration.LOG_JDBC);
            if (log.isTraceEnabled()) {
                if (found)
                    log.trace(_loc.get("cache-hit", field, this.getClass()));
                else
                    log.trace(_loc.get("cache-missed", field, this.getClass()));
            }
           
            ClassMapping mapping = field.getDefiningMapping();
            Object oid = sm.getObjectId();
            Column[] cols = mapping.getPrimaryKeyColumns();
            if (sel == null)
                sel = ((LogicalUnion.UnionSelect)union.getSelects()[0]).
                getDelegate();

            sel.wherePrimaryKey(mapping, cols, cols, oid, store,
                  null, null, parmList);
            List nonFKParams = sel.getSQL().getNonFKParameters();
            if (nonFKParams != null && nonFKParams.size() > 0)
                parmList.addAll(nonFKParams);
        }
       
        // create proxy
View Full Code Here


    public void customUpdate(OpenJPAStateManager sm, JDBCStore store)
        throws SQLException {
        // select existing value for update
        Column col = field.getColumns()[0];
        Select sel = store.getSQLFactory().newSelect();
        sel.select(col);
        field.wherePrimaryKey(sel, sm, store);
        SQLBuffer sql = sel.toSelect(true, store.getFetchConfiguration());

        Connection conn = store.getConnection();
        PreparedStatement stmnt = null;
        ResultSet rs = null;
        try {
View Full Code Here

    public void load(OpenJPAStateManager sm, JDBCStore store,
        JDBCFetchConfiguration fetch)
        throws SQLException {
        Column col = field.getColumns()[0];
        Select sel = store.getSQLFactory().newSelect();
        sel.select(col);
        field.wherePrimaryKey(sel, sm, store);

        Result res = sel.execute(store, fetch);
        Object val = null;
        try {
            if (res.next())
                val = load(col, res, null);
        } finally {
View Full Code Here

        return null;
    }

    public void delete(OpenJPAStateManager sm, JDBCStore store, RowManager rm)
        throws SQLException {
        Select sel = createSelect(sm, store);
        store.getDBDictionary().deleteStream(store, sel);
    }
View Full Code Here

        throws SQLException {
        Object ob = toDataStoreValue(sm.fetchObjectField
            (field.getIndex()), store);
        Row row = field.getRow(sm, store, rm, Row.ACTION_INSERT);
        if (field.getColumnIO().isInsertable(0, ob == null)) {
            Select sel = createSelect(sm, store);
            if (isBlob) {
                store.getDBDictionary().insertBlobForStreamingLoad
                    (row, field.getColumns()[0], store, ob, sel);
            } else {
                store.getDBDictionary().insertClobForStreamingLoad
View Full Code Here

        throws SQLException {
        Object ob = toDataStoreValue(sm.fetchObjectField
            (field.getIndex()), store);
        if (field.getColumnIO().isInsertable(0, ob == null)) {
            if (ob != null) {
                Select sel = createSelect(sm, store);
                if (isBlob) {
                    store.getDBDictionary().updateBlob
                        (sel, store, (InputStream)ob);
                } else {
                    store.getDBDictionary().updateClob
View Full Code Here

        throws SQLException {
        Object ob = toDataStoreValue(sm.fetchObjectField
            (field.getIndex()), store);
        if (field.getColumnIO().isUpdatable(0, ob == null)) {
            Row row = field.getRow(sm, store, rm, Row.ACTION_UPDATE);
            Select sel = createSelect(sm, store);
            if (isBlob) {
                store.getDBDictionary().insertBlobForStreamingLoad
                    (row, field.getColumns()[0], store, ob, sel);
            } else {
                store.getDBDictionary().insertClobForStreamingLoad
View Full Code Here

        throws SQLException {
        Object ob = toDataStoreValue(sm.fetchObjectField
                (field.getIndex()), store);
        if (field.getColumnIO().isUpdatable(0, ob == null)) {
            if (ob != null) {
                Select sel = createSelect(sm, store);
                if (isBlob) {
                    store.getDBDictionary().updateBlob
                        (sel, store, (InputStream)ob);
                } else {
                    store.getDBDictionary().updateClob
View Full Code Here

            }
        }
    }

    private Select createSelect(OpenJPAStateManager sm, JDBCStore store) {
        Select sel = store.getSQLFactory().newSelect();
        sel.select(field.getColumns()[0]);
        sel.selectPrimaryKey(field.getDefiningMapping());
        sel.wherePrimaryKey
            (sm.getObjectId(), field.getDefiningMapping(), store);
        sel.setLob(true);
        return sel;
    }
View Full Code Here

    public boolean checkVersion(OpenJPAStateManager sm, JDBCStore store,
        boolean updateVersion)
        throws SQLException {
        Column[] cols = vers.getColumns();
        Select sel = store.getSQLFactory().newSelect();
        sel.select(cols);
        sel.wherePrimaryKey(sm.getObjectId(), vers.getClassMapping(), store);

        Result res = sel.execute(store, null);
        try {
            if (!res.next())
                return false;

            Object memVersion = sm.getVersion();
View Full Code Here

TOP

Related Classes of org.apache.openjpa.jdbc.sql.Select

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.