Package org.apache.openjpa.jdbc.sql

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


            subclassMode) && subclassMode == JDBCFetchConfiguration.EAGER_JOIN;
        if (sels.size() > 1)
            start = 0L;

        if (unionable) {
            Union union = _store.getSQLFactory().newUnion(
                (Select[]) sels.toArray(new Select[sels.size()]));
            populateUnion(union, mappings, subclasses, facts, exps, states, ctx,
                false, eager, start, end);
            if (union.isUnion())
                return new String[] {union.toSelect(false, fetch).getSQL(true)};
            sels = Arrays.asList(union.getSelects());
        } else {
            Select sel;
            for (int i = 0, idx = 0; i < sels.size(); i++) {
                sel = (Select) sels.get(i);
                populateSelect(sel, (ClassMapping) selMappings.get(i),
View Full Code Here


    protected int count() {
        final ClassMapping[] elems = _strat.getIndependentElementMappings
            (false);
        final OpenJPAStateManager sm = assertOwner();
        final JDBCStore store = getStore();
        Union union = store.getSQLFactory().newUnion
            (Math.max(1, elems.length));
        union.select(new Union.Selector() {
            public void select(Select sel, int idx) {
                ClassMapping elem = (elems.length == 0) ? null : elems[idx];
                sel.whereForeignKey(_strat.getJoinForeignKey(elem),
                    sm.getObjectId(), _strat.getFieldMapping().
                    getDefiningMapping(), store);
            }
        });

        try {
            return union.getCount(store);
        } catch (SQLException se) {
            throw SQLExceptions.getStore(se, store.getDBDictionary());
        }
    }
View Full Code Here

    protected boolean has(final Object obj) {
        final ClassMapping[] elems = _strat.getIndependentElementMappings
            (false);
        final OpenJPAStateManager sm = assertOwner();
        final JDBCStore store = getStore();
        Union union = store.getSQLFactory().newUnion
            (Math.max(1, elems.length));
        union.select(new Union.Selector() {
            public void select(Select sel, int idx) {
                ClassMapping elem = (elems.length == 0) ? null : elems[idx];
                sel.whereForeignKey(_strat.getJoinForeignKey(elem),
                    sm.getObjectId(), _strat.getFieldMapping().
                    getDefiningMapping(), store);

                Object val = _strat.toDataStoreValue(obj, store);
                Column[] cols = _strat.getElementColumns(elem);
                Object[] vals = (cols.length == 1) ? null : (Object[]) val;
                SQLBuffer sql = new SQLBuffer(store.getDBDictionary());
                for (int i = 0; i < cols.length; i++) {
                    if (i > 0)
                        sql.append(" AND ");

                    sql.append(sel.getColumnAlias(cols[i]));
                    if (vals == null)
                        sql.append((val == null) ? " IS " : " = ").
                            appendValue(val, cols[i]);
                    else
                        sql.append((vals[i] == null) ? " IS " : " = ").
                            appendValue(vals[i], cols[i]);
                }
                sel.where(sql);
            }
        });

        try {
            return union.getCount(store) > 0;
        } catch (SQLException se) {
            throw SQLExceptions.getStore(se, store.getDBDictionary());
        }
    }
View Full Code Here

        final JDBCStore store = getStore();
        final JDBCFetchConfiguration fetch = store.getFetchConfiguration();
        final Joins[] resJoins = new Joins[Math.max(1, elems.length)];
        final FieldMapping fm = _strat.getFieldMapping();

        Union union = store.getSQLFactory().newUnion
            (Math.max(1, elems.length));
        if (fetch.getSubclassFetchMode(fm.getElementMapping().
            getTypeMapping()) != fetch.EAGER_JOIN)
            union.abortUnion();
        union.setLRS(true);
        union.select(new Union.Selector() {
            public void select(Select sel, int idx) {
                ClassMapping elem = (elems.length == 0) ? null : elems[idx];
                sel.whereForeignKey(_strat.getJoinForeignKey(elem),
                    sm.getObjectId(), fm.getDefiningMapping(), store);

                // order before select in case we're faking union with
                // multiple selects; order vals used to merge results
                fm.orderLocal(sel, elem, null);
                resJoins[idx] = _strat.joinElementRelation(sel.newJoins(),
                    elem);
                fm.orderRelation(sel, elem, resJoins[idx]);
                _strat.selectElement(sel, elem, store, fetch, fetch.EAGER_JOIN,
                    resJoins[idx]);
            }
        });

        try {
            Result res = union.execute(store, fetch);
            return new ResultIterator(sm, store, fetch, res, resJoins);
        } catch (SQLException se) {
            throw SQLExceptions.getStore(se, store.getDBDictionary());
        }
    }
View Full Code Here

            return fq.execute(sm, this, fetch);
        final JDBCStoreManager store = this;
        final int eager = Math.min(fetch.getEagerFetchMode(),
            JDBCFetchConfiguration.EAGER_JOIN);

        Union union = _sql.newUnion(mappings.length);
        union.setExpectedResultCount(1, false);
        if (fetch.getSubclassFetchMode(mapping) != fetch.EAGER_JOIN)
            union.abortUnion();
        union.select(new Union.Selector() {
            public void select(Select sel, int i) {
                sel.select(mappings[i], Select.SUBS_ANY_JOINABLE, store, fetch,
                    eager);
                sel.wherePrimaryKey(sm.getObjectId(), mappings[i], store);
            }
        });
        Result result = union.execute(this, fetch);
        cacheFinder(mapping, union, fetch);
        return result;
    }
View Full Code Here

                    return rops[0];
                return new MergedResultObjectProvider(rops);
            }

            // perform a union on all independent classes
            Union union = _sql.newUnion(mappings.length);
            union.setLRS(true);
            final BitSet[] paged = new BitSet[mappings.length];
            union.select(new Union.Selector() {
                public void select(Select sel, int idx) {
                    paged[idx] = selectExtent(sel, mappings[idx], jfetch,
                        subclasses);
                }
            });
View Full Code Here

        final ClassMapping[] clss = (derivedVal)
            ? _strat.getIndependentKeyMappings(false)
            : _strat.getIndependentValueMappings(false);
        final OpenJPAStateManager sm = assertOwner();
        final JDBCStore store = getStore();
        Union union = store.getSQLFactory().newUnion
            (Math.max(1, clss.length));
        union.select(new Union.Selector() {
            public void select(Select sel, int idx) {
                ClassMapping cls = (clss.length == 0) ? null : clss[idx];
                sel.whereForeignKey(_strat.getJoinForeignKey(cls),
                    sm.getObjectId(), _strat.getFieldMapping().
                    getDefiningMapping(), store);
            }
        });

        try {
            return union.getCount(store);
        } catch (SQLException se) {
            throw SQLExceptions.getStore(se, store.getDBDictionary());
        }
    }
View Full Code Here

            ? _strat.getIndependentKeyMappings(derivedVal)
            : _strat.getIndependentValueMappings(derivedKey);
        final OpenJPAStateManager sm = assertOwner();
        final JDBCStore store = getStore();

        Union union = store.getSQLFactory().newUnion
            (Math.max(1, clss.length));
        union.select(new Union.Selector() {
            public void select(Select sel, int idx) {
                ClassMapping cls = (clss.length == 0) ? null : clss[idx];
                sel.whereForeignKey(_strat.getJoinForeignKey(cls),
                    sm.getObjectId(), _strat.getFieldMapping().
                    getDefiningMapping(), store);

                Joins joins = null;
                Column[] cols;
                Object val;
                if (key) {
                    if (derivedKey)
                        joins = _strat.joinValueRelation(sel.newJoins(), cls);
                    val = _strat.toKeyDataStoreValue(obj, store);
                    cols = _strat.getKeyColumns(cls);
                } else {
                    if (derivedVal)
                        joins = _strat.joinKeyRelation(sel.newJoins(), cls);
                    val = _strat.toDataStoreValue(obj, store);
                    cols = _strat.getValueColumns(cls);
                }
                Object[] vals = (cols.length == 1) ? null : (Object[]) val;
                SQLBuffer sql = new SQLBuffer(store.getDBDictionary());
                for (int i = 0; i < cols.length; i++) {
                    if (i > 0)
                        sql.append(" AND ");

                    sql.append(sel.getColumnAlias(cols[i], joins));
                    if (vals == null)
                        sql.append((val == null) ? " IS " : " = ").
                            appendValue(val, cols[i]);
                    else
                        sql.append((vals[i] == null) ? " IS " : " = ").
                            appendValue(vals[i], cols[i]);
                }
                sel.where(sql, joins);
            }
        });

        try {
            return union.getCount(store) > 0;
        } catch (SQLException se) {
            throw SQLExceptions.getStore(se, store.getDBDictionary());
        }
    }
View Full Code Here

        final ClassMapping[] clss = _strat.getIndependentKeyMappings(true);
        final JDBCFetchConfiguration fetch = store.getFetchConfiguration();
        final Joins[] resJoins = new Joins[Math.max(1, clss.length)];

        Union union = store.getSQLFactory().newUnion
            (Math.max(1, clss.length));
        if (fetch.getSubclassFetchMode(_strat.getFieldMapping().
            getKeyMapping().getTypeMapping()) != fetch.EAGER_JOIN)
            union.abortUnion();
        union.select(new Union.Selector() {
            public void select(Select sel, int idx) {
                ClassMapping cls = (clss.length == 0) ? null : clss[idx];
                sel.whereForeignKey(_strat.getJoinForeignKey(cls),
                    sm.getObjectId(), _strat.getFieldMapping().
                    getDefiningMapping(), store);
                if (_strat.getFieldMapping().getElement().getValueMappedBy()
                    != null)
                    resJoins[idx] = _strat.joinKeyRelation(sel.newJoins(),
                        cls);

                Object val = _strat.toDataStoreValue(obj, store);
                Column[] cols = _strat.getValueColumns(cls);
                Object[] vals = (cols.length == 1) ? null : (Object[]) val;
                SQLBuffer sql = new SQLBuffer(store.getDBDictionary());
                for (int i = 0; i < cols.length; i++) {
                    if (i > 0)
                        sql.append(" AND ");

                    sql.append(sel.getColumnAlias(cols[i]));
                    if (vals == null)
                        sql.append((val == null) ? " IS " : " = ").
                            appendValue(val, cols[i]);
                    else
                        sql.append((vals[i] == null) ? " IS " : " = ").
                            appendValue(vals[i], cols[i]);
                }
                sel.where(sql);

                if (resJoins[idx] == null)
                    resJoins[idx] = _strat.joinKeyRelation(sel.newJoins(),
                        cls);
                _strat.selectKey(sel, cls, sm, store, fetch, resJoins[idx]);
            }
        });

        Result res = null;
        Collection keys = new ArrayList(3);
        try {
            res = union.execute(store, fetch);
            while (res.next())
                keys.add(_strat.loadKey(sm, store, fetch, res,
                    resJoins[res.indexOf()]));
            return keys;
        } catch (SQLException se) {
View Full Code Here

        }

        final JDBCFetchConfiguration fetch = store.getFetchConfiguration();
        final ClassMapping[] clss = _strat.getIndependentValueMappings(true);
        final Joins[] resJoins = new Joins[Math.max(1, clss.length)];
        Union union = store.getSQLFactory().newUnion(Math.max(1, clss.length));
        union.setExpectedResultCount(1, false);
        if (fetch.getSubclassFetchMode(_strat.getFieldMapping().
            getElementMapping().getTypeMapping())
            != JDBCFetchConfiguration.EAGER_JOIN)
            union.abortUnion();
        union.select(new Union.Selector() {
            public void select(Select sel, int idx) {
                ClassMapping cls = (clss.length == 0) ? null : clss[idx];
                sel.whereForeignKey(_strat.getJoinForeignKey(cls),
                    sm.getObjectId(), _strat.getFieldMapping().
                    getDefiningMapping(), store);
                if (_strat.getFieldMapping().getKey().getValueMappedBy()
                    != null)
                    resJoins[idx] = _strat.joinValueRelation(sel.newJoins(),
                        cls);

                Object key = _strat.toKeyDataStoreValue(obj, store);
                Column[] cols = _strat.getKeyColumns(cls);
                Object[] vals = (cols.length == 1) ? null : (Object[]) key;
                SQLBuffer sql = new SQLBuffer(store.getDBDictionary());
                for (int i = 0; i < cols.length; i++) {
                    if (i > 0)
                        sql.append(" AND ");

                    sql.append(sel.getColumnAlias(cols[i], resJoins[idx]));
                    if (vals == null)
                        sql.append((key == null) ? " IS " : " = ").
                            appendValue(key, cols[i]);
                    else
                        sql.append((vals[i] == null) ? " IS " : " = ").
                            appendValue(vals[i], cols[i]);
                }
                sel.where(sql, resJoins[idx]);

                if (resJoins[idx] == null)
                    resJoins[idx] = _strat.joinValueRelation(sel.newJoins(),
                        cls);
                _strat.selectValue(sel, cls, sm, store, fetch, resJoins[idx]);
            }
        });

        Result res = null;
        try {
            res = union.execute(store, fetch);
            if (res.next())
                return _strat.loadValue(sm, store, fetch, res,
                    resJoins[res.indexOf()]);
            return null;
        } catch (SQLException se) {
View Full Code Here

TOP

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

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.