Package org.datanucleus.store.rdbms.sql

Examples of org.datanucleus.store.rdbms.sql.SQLStatement


                    ownerMapping = ((DatastoreClass)arrayTbl).getExternalMapping(ownerMmd,
                        MappingConsumer.MAPPING_TYPE_EXTERNAL_FK);
                }
            }

            SQLStatement subStmt = new SQLStatement(stmt, storeMgr, arrayTbl, null, null);
            subStmt.setClassLoaderResolver(clr);
            JavaTypeMapping mapping =
                storeMgr.getMappingManager().getMappingWithDatastoreMapping(String.class, false, false, clr);
            SQLExpression countExpr = exprFactory.newLiteral(subStmt, mapping, "COUNT(*)");
            ((StringLiteral)countExpr).generateStatementWithoutQuotes();
            subStmt.select(countExpr, null);

            SQLExpression elementOwnerExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(),
                ownerMapping);
            SQLExpression ownerIdExpr = exprFactory.newExpression(stmt, expr.getSQLTable(),
                expr.getSQLTable().getTable().getIdMapping());
            subStmt.whereAnd(elementOwnerExpr.eq(ownerIdExpr), true);

            JavaTypeMapping subqMapping = exprFactory.getMappingForType(Integer.class, false);
            SQLExpression subqExpr = new NumericSubqueryExpression(stmt, subStmt);
            subqExpr.setJavaTypeMapping(subqMapping);
            return subqExpr;
View Full Code Here


            {
                elemType = varType.getName();
                elemCmd = storeMgr.getMetaDataManager().getMetaDataForClass(elemType, clr);
            }
        }
        SQLStatement subStmt = null;
        if (joinTbl != null)
        {
            // JoinTable Collection
            if (elemCmd == null)
            {
                // Collection<Non-PC>
                subStmt = new SQLStatement(stmt, storeMgr, joinTbl, null, null);
                subStmt.setClassLoaderResolver(clr);
                JavaTypeMapping oneMapping = storeMgr.getMappingManager().getMapping(Integer.class);
                subStmt.select(exprFactory.newLiteral(subStmt, oneMapping, 1), null);

                // Restrict to collection owner
                JavaTypeMapping ownerMapping = ((JoinTable)joinTbl).getOwnerMapping();
                SQLExpression ownerExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(), ownerMapping);
                SQLExpression ownerIdExpr = exprFactory.newExpression(stmt, collExpr.getSQLTable(),
                    collExpr.getSQLTable().getTable().getIdMapping());
                subStmt.whereAnd(ownerExpr.eq(ownerIdExpr), true);

                SQLExpression elemIdExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(),
                    joinTbl.getElementMapping());
                if (elemIsUnbound)
                {
                    // Bind the variable in the QueryGenerator
                    stmt.getQueryGenerator().bindVariable(varName, null, elemIdExpr.getSQLTable(),
                        elemIdExpr.getJavaTypeMapping());
                }
                else
                {
                    // Add restrict to element
                    subStmt.whereAnd(elemIdExpr.eq(elemExpr), true);
                }
            }
            else
            {
                // Collection<PC>
                DatastoreClass elemTbl = storeMgr.getDatastoreClass(elemType, clr);
                subStmt = new SQLStatement(stmt, storeMgr, elemTbl, null, null);
                subStmt.setClassLoaderResolver(clr);
                JavaTypeMapping oneMapping = storeMgr.getMappingManager().getMapping(Integer.class);
                subStmt.select(exprFactory.newLiteral(subStmt, oneMapping, 1), null);

                // Join to join table
                SQLTable joinSqlTbl = subStmt.innerJoin(subStmt.getPrimaryTable(), elemTbl.getIdMapping(),
                    joinTbl, null, joinTbl.getElementMapping(), null, null);

                // Restrict to collection owner
                JavaTypeMapping ownerMapping = ((JoinTable)joinTbl).getOwnerMapping();
                SQLExpression ownerExpr = exprFactory.newExpression(subStmt, joinSqlTbl, ownerMapping);
                SQLExpression ownerIdExpr = exprFactory.newExpression(stmt, collExpr.getSQLTable(),
                    collExpr.getSQLTable().getTable().getIdMapping());
                subStmt.whereAnd(ownerExpr.eq(ownerIdExpr), true);

                SQLExpression elemIdExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(),
                    elemTbl.getIdMapping());
                if (elemIsUnbound)
                {
                    // Bind the variable in the QueryGenerator
                    stmt.getQueryGenerator().bindVariable(varName, elemCmd, elemIdExpr.getSQLTable(),
                        elemIdExpr.getJavaTypeMapping());
                }
                else
                {
                    // Add restrict to element
                    subStmt.whereAnd(elemIdExpr.eq(elemExpr), true);
                }
            }
        }
        else
        {
            // FK Collection
            DatastoreClass elemTbl = storeMgr.getDatastoreClass(mmd.getCollection().getElementType(), clr);
            subStmt = new SQLStatement(stmt, storeMgr, elemTbl, null, null);
            subStmt.setClassLoaderResolver(clr);
            JavaTypeMapping oneMapping = storeMgr.getMappingManager().getMapping(Integer.class);
            subStmt.select(exprFactory.newLiteral(subStmt, oneMapping, 1), null);

            // Restrict to collection owner
            JavaTypeMapping ownerMapping = null;
            if (mmd.getMappedBy() != null)
            {
                ownerMapping = elemTbl.getMemberMapping(mmd.getRelatedMemberMetaData(clr)[0]);
            }
            else
            {
                ownerMapping = elemTbl.getExternalMapping(mmd, MappingConsumer.MAPPING_TYPE_EXTERNAL_FK);
            }
            SQLExpression ownerExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(), ownerMapping);
            SQLExpression ownerIdExpr = exprFactory.newExpression(stmt, collExpr.getSQLTable(),
                collExpr.getSQLTable().getTable().getIdMapping());
            subStmt.whereAnd(ownerExpr.eq(ownerIdExpr), true);

            if (elemIsUnbound)
            {
                SQLExpression elemIdExpr = null;
                if (!elemType.equals(mmd.getCollection().getElementType()))
                {
                    // Variable is defined as a subclass of the declared type so add extra join to variable type
                    DatastoreClass varTbl = storeMgr.getDatastoreClass(elemType, clr);
                    SQLTable varSqlTbl = stmt.innerJoin(subStmt.getPrimaryTable(), elemTbl.getIdMapping(),
                        varTbl, null, varTbl.getIdMapping(), null, null);
                    elemIdExpr = exprFactory.newExpression(stmt, varSqlTbl, varTbl.getIdMapping());
                }
                else
                {
                    elemIdExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(),
                        elemTbl.getIdMapping());
                }

                // Bind the variable in the QueryGenerator
                stmt.getQueryGenerator().bindVariable(varName, elemCmd, elemIdExpr.getSQLTable(),
                    elemIdExpr.getJavaTypeMapping());
            }
            else
            {
                // Add restrict to element
                SQLExpression elemIdExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(),
                    elemTbl.getIdMapping());
                subStmt.whereAnd(elemIdExpr.eq(elemExpr), true);
            }
        }

        return new BooleanSubqueryExpression(stmt, "EXISTS", subStmt);
    }
View Full Code Here

                            List<SQLStatement> unionStmts = stmt.getUnions();
                            Iterator<SQLStatement> iter = unionStmts.iterator();
                            while (iter.hasNext())
                            {
                                SQLStatement unionStmt = iter.next();
                                Class unionCandidateCls = clr.classForName(unionStmt.getCandidateClassName());
                                if (type.isAssignableFrom(unionCandidateCls) == not)
                                {
                                    SQLExpression unionClauseExpr = exprFactory.newLiteral(unionStmt, m, true).eq(
                                        exprFactory.newLiteral(unionStmt, m, false));
                                    unionStmt.whereAnd((BooleanExpression)unionClauseExpr, false);
                                }
                            }

                            // Just return true since we applied the condition direct to the unions
                            SQLExpression returnExpr = exprFactory.newLiteral(stmt, m, true).eq(
View Full Code Here

            else
            {
                throw new NucleusException("Invalid map for " + expr + " in size() call");
            }

            SQLStatement subStmt = new SQLStatement(stmt, storeMgr, mapTbl, null, null);
            subStmt.setClassLoaderResolver(clr);
            JavaTypeMapping mapping =
                storeMgr.getMappingManager().getMappingWithDatastoreMapping(String.class, false, false, clr);
            SQLExpression countExpr = exprFactory.newLiteral(subStmt, mapping, "COUNT(*)");
            ((StringLiteral)countExpr).generateStatementWithoutQuotes();
            subStmt.select(countExpr, null);

            SQLExpression elementOwnerExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(),
                ownerMapping);
            SQLExpression ownerIdExpr = exprFactory.newExpression(stmt, expr.getSQLTable(),
                expr.getSQLTable().getTable().getIdMapping());
            subStmt.whereAnd(elementOwnerExpr.eq(ownerIdExpr), true);

            JavaTypeMapping subqMapping = exprFactory.getMappingForType(Integer.class, false);
            SQLExpression subqExpr = new NumericSubqueryExpression(stmt, subStmt);
            subqExpr.setJavaTypeMapping(subqMapping);
            return subqExpr;
View Full Code Here

                    ownerMapping = ((DatastoreClass)collectionTbl).getExternalMapping(mmd,
                        MappingConsumer.MAPPING_TYPE_EXTERNAL_FK);
                }
            }

            SQLStatement subStmt = new SQLStatement(stmt, storeMgr, collectionTbl, null, null);
            subStmt.setClassLoaderResolver(clr);
            JavaTypeMapping mapping =
                storeMgr.getMappingManager().getMappingWithDatastoreMapping(String.class, false, false, clr);
            SQLExpression countExpr = exprFactory.newLiteral(subStmt, mapping, "COUNT(*)");
            ((StringLiteral)countExpr).generateStatementWithoutQuotes();
            subStmt.select(countExpr, null);

            SQLExpression elementOwnerExpr = exprFactory.newExpression(subStmt,
                subStmt.getPrimaryTable(), ownerMapping);
            SQLExpression ownerIdExpr = exprFactory.newExpression(stmt, expr.getSQLTable(),
                expr.getSQLTable().getTable().getIdMapping());
            subStmt.whereAnd(elementOwnerExpr.eq(ownerIdExpr), true);

            JavaTypeMapping subqMapping = exprFactory.getMappingForType(Integer.class, false);
            SQLExpression subqExpr = new NumericSubqueryExpression(stmt, subStmt);
            subqExpr.setJavaTypeMapping(subqMapping);
            return subqExpr;
View Full Code Here

TOP

Related Classes of org.datanucleus.store.rdbms.sql.SQLStatement

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.