Examples of RDBMSAdapter


Examples of org.jpox.store.rdbms.adapter.RDBMSAdapter

     * @param lock Whether to lock the rows found by this SELECT
     * @return The statement
     */
    public String toString(boolean lock)
    {
        final RDBMSAdapter datastoreAdapter = ((RDBMSAdapter) mainTable.getStoreManager().getDatastoreAdapter());
       
        // Build the SELECT list
        StringBuffer stmt = new StringBuffer("SELECT ");
        Iterator iter = selected.iterator();
        while (iter.hasNext())
        {
            stmt.append(iter.next());
            if (iter.hasNext())
            {
                stmt.append(',');
            }
        }

        // Generate the FROM clause
        String[] mainTableColumnID = new String[mainTable.getIDMapping().getNumberOfDatastoreFields()];
        int countIdFields = mainTable.getIDMapping().getNumberOfDatastoreFields();
        for (int j=0; j<countIdFields; j++)
        {
            mainTableColumnID[j] = referenceDatastoreField(mainTable.getIDMapping().getDataStoreMapping(j).getDatastoreField());
        }
       
        stmt.append(" FROM ").append(mainTable.toString()).append(" ").append("THIS");

        // Add any locking of the rows for datastores that require WITH option to be placed after the FROM clause
        if (lock && datastoreAdapter.getPlaceWithOptionAfterFromClause() && datastoreAdapter.getSelectWithLockOption() != null)
        {
            stmt.append(" WITH ").append(datastoreAdapter.getSelectWithLockOption());
        }
       
        StringBuffer joinConditions = new StringBuffer();
        RDBMSAdapter dba = (RDBMSAdapter)mainTable.getStoreManager().getDatastoreAdapter();

        // LEFT OUTER JOINs
        if (!outerTableMappings.isEmpty())
        {
            JavaTypeMapping mainIdMapping = mainTable.getIDMapping();
            iter = outerTableMappings.entrySet().iterator();
            while (iter.hasNext())
            {
                Map.Entry entry = (Map.Entry)iter.next();
                String alias = (String)entry.getKey();
                OuterJoinDefinition def = (OuterJoinDefinition)entry.getValue();

                JavaTypeMapping refMapping = def.getReferenceMapping();
                JavaTypeMapping refDiscrimMapping = def.getReferenceDiscrimMapping();
                Object[] refDiscrimValues = def.getDiscriminatorValues();
                JavaTypeMapping refTypeMapping = def.getReferenceTypeMapping();

                String refTableName = ((Column)refMapping.getDataStoreMapping(0).getDatastoreField()).getDatastoreContainerObject().toString();
                if (dba.supportsAnsiJoinSyntax())
                {
                    // ANSI syntax left outer join
                    stmt.append(" LEFT OUTER JOIN ").append(refTableName).append(" ").append(alias);
                    if (datastoreAdapter.getPlaceWithOptionWithinJoinClauses())
                    {
                        stmt.append(" WITH ").append(datastoreAdapter.getSelectWithLockOption());                       
                    }
                    stmt.append(" ON ");
                    if (refDiscrimMapping != null && refDiscrimValues != null)
                    {
                        stmt.append("(");
                    }
                    int countMainIdFields = mainIdMapping.getNumberOfDatastoreFields();
                    for (int i=0; i<countMainIdFields; i++)
                    {
                        if (i > 0)
                        {
                            stmt.append(" AND ");
                        }
                        stmt.append(getColumnReference("THIS", mainIdMapping.getDataStoreMapping(i).getDatastoreField()));
                        stmt.append(" = ");
                        stmt.append(getColumnReference(alias, refMapping.getDataStoreMapping(i).getDatastoreField()));
                    }

                    if (refDiscrimMapping != null && refDiscrimValues != null)
                    {
                        // Add constraint on discriminator in related table
                        stmt.append(" AND (");
                        for (int i=0;i<refDiscrimValues.length;i++)
                        {
                            if (i > 0)
                            {
                                stmt.append(" OR ");
                            }
                            stmt.append(getColumnReference(alias, refDiscrimMapping.getDataStoreMapping(0).getDatastoreField()));
                            stmt.append(" = ");
                            // TODO Cater for numeric discriminator types
                            // TODO Should we always use single quote for character values ?
                            stmt.append("'" + refDiscrimValues[i] + "'");
                        }
                        stmt.append("))");
                    }
                    else if (refTypeMapping != null)
                    {
                        // Add join to reference type mapping table from reference table
                        String refTypeTableName = refTypeMapping.getDataStoreMapping(0).getDatastoreField().getDatastoreContainerObject().toString();
                        stmt.append(" INNER JOIN ").append(refTypeTableName).append(" ").append(alias + "_SUB");
                        if (datastoreAdapter.getPlaceWithOptionWithinJoinClauses())
                        {
                            stmt.append(" WITH ").append(datastoreAdapter.getSelectWithLockOption());                       
                        }
                        stmt.append(" ON ");
                        int countRefTypeTableFields = refTypeMapping.getNumberOfDatastoreFields();
                        JavaTypeMapping refIdMapping = refMapping.getDatastoreContainer().getIDMapping();
                        for (int i=0; i<countRefTypeTableFields; i++)
                        {
                            if (i > 0)
                            {
                                stmt.append(" AND ");
                            }
                            stmt.append(getColumnReference(alias + "_SUB", refTypeMapping.getDataStoreMapping(i).getDatastoreField()));
                            stmt.append(" = ");
                            stmt.append(getColumnReference(alias, refIdMapping.getDataStoreMapping(i).getDatastoreField()));
                        }
                    }
                }
                else
                {
                    // Non-ANSI syntax - add table to "stmt", and join definition to "joinConditions"
                    stmt.append(',').append(refTableName).append(" ").append(alias);

                    if (refDiscrimMapping != null && refDiscrimValues != null)
                    {
                        joinConditions.append("(");
                    }
                    int countMainIdFields = mainIdMapping.getNumberOfDatastoreFields();
                    for (int i=0; i<countMainIdFields; i++)
                    {
                        if (joinConditions.length() > 0)
                        {
                            joinConditions.append(" AND ");
                        }
                        joinConditions.append(
                            dba.getNonAnsiLeftOuterJoinWhereClause(mainIdMapping.getDataStoreMapping(i).getDatastoreField().toString(),
                                                                   refMapping.getDataStoreMapping(i).getDatastoreField().toString()));
                    }
                    if (refDiscrimMapping != null && refDiscrimValues != null)
                    {
                        // Add constraint on discriminator in related table
                        joinConditions.append(" AND (");
                        for (int i=0;i<refDiscrimValues.length;i++)
                        {
                            if (i > 0)
                            {
                                joinConditions.append(" OR ");
                            }
                            joinConditions.append(getColumnReference(alias, refDiscrimMapping.getDataStoreMapping(0).getDatastoreField()));
                            joinConditions.append(" = ");
                            // TODO Cater for numeric discriminator types
                            // TODO Should we always use single quote for character values ?
                            joinConditions.append("'" + refDiscrimValues[i] + "'");
                        }
                        joinConditions.append("))");
                    }
                    else if (refTypeMapping != null)
                    {
                        // Add join to ref type mapping table
                        String refTypeTableName = refTypeMapping.getDataStoreMapping(0).getDatastoreField().getDatastoreContainerObject().toString();
                        stmt.append(',').append(refTypeTableName).append(" ").append(alias + "_SUB");

                        int countRefTypeTableFields = refTypeMapping.getNumberOfDatastoreFields();
                        JavaTypeMapping refIdMapping = refMapping.getDatastoreContainer().getIDMapping();
                        for (int i=0; i<countRefTypeTableFields; i++)
                        {
                            if (i > 0)
                            {
                                stmt.append(" AND ");
                            }
                            joinConditions.append(
                                dba.getNonAnsiInnerJoinWhereClause(refTypeMapping.getDataStoreMapping(i).getDatastoreField().toString(),
                                                                   refIdMapping.getDataStoreMapping(i).getDatastoreField().toString()));
                        }
                    }
                }
            }
        }

        // INNER JOINs
        if (!innerTableMappings.isEmpty())
        {
            JavaTypeMapping mainIdMapping = mainTable.getIDMapping();
            iter = innerTableMappings.entrySet().iterator();
            while (iter.hasNext())
            {
                Map.Entry entry = (Map.Entry)iter.next();
                String alias = (String)entry.getKey();
                InnerJoinDefinition join = (InnerJoinDefinition)entry.getValue();
                if (dba.supportsAnsiJoinSyntax())
                {
                    stmt.append(" INNER JOIN ").append(join.getTable()).append(" ").append(alias);
                    if (lock && datastoreAdapter.getPlaceWithOptionWithinJoinClauses())
                    {
                        stmt.append(" WITH ").append(datastoreAdapter.getSelectWithLockOption());                       
                    }
                    stmt.append(" ON ");
                    int countReferenceFields = join.getReferenceMapping().getNumberOfDatastoreFields();
                    for (int i=0; i<countReferenceFields; i++)
                    {
                        stmt.append(getColumnReference("THIS", mainIdMapping.getDataStoreMapping(i).getDatastoreField()));
                        stmt.append(" = ");
                        stmt.append(getColumnReference(alias, join.getReferenceMapping().getDataStoreMapping(i).getDatastoreField()));
                        if (i < join.getReferenceMapping().getNumberOfDatastoreFields()-1)
                        {
                            stmt.append(" AND ");
                        }
                    }
                }
                else
                {
                    stmt.append(',').append(join.getTable()).append(" ").append(alias);

                    String[] supertableColumnID = new String[mainTable.getIDMapping().getNumberOfDatastoreFields()];
                    int countReferenceFields = join.getReferenceMapping().getNumberOfDatastoreFields();
                    for (int i=0; i<countReferenceFields; i++)
                    {
                        supertableColumnID[i] = getColumnReference(alias, join.getReferenceMapping().getDataStoreMapping(i).getDatastoreField());
                    }
                    for (int i=0; i<countReferenceFields; i++)
                    {
                        if (joinConditions.length() > 0)
                        {
                            joinConditions.append(" AND ");
                        }
                        joinConditions.append(dba.getNonAnsiInnerJoinWhereClause(mainTableColumnID[i], supertableColumnID[i]));
                    }
                }
            }
        }

View Full Code Here

Examples of org.jpox.store.rdbms.adapter.RDBMSAdapter

     **/
    public String getSQLDefinition()
    {
        StringBuffer def = new StringBuffer(identifier.toString());
        StringBuffer typeSpec = new StringBuffer(typeInfo.typeName);
        RDBMSAdapter adapter = (RDBMSAdapter) storeMgr.getDatastoreAdapter();

        // Add type specification.
        boolean specifyType = true;
        if (adapter.supportsIdentityFields() && autoIncrement && !adapter.supportsAutoIncrementColumnTypeSpecification())
        {
            specifyType = false;
        }

        if (specifyType)
        {
            // Parse and append createParams to the typeName if it looks like it's supposed to be appended,
            // i.e. if it contains parentheses, and the type name itself doesn't. createParams is mighty
            // ill-defined by the JDBC spec, but attempt to interpret it.
            if (typeInfo.createParams != null && typeInfo.createParams.indexOf('(') >= 0 && typeInfo.typeName.indexOf('(') < 0)
            {
                StringTokenizer toks = new StringTokenizer(typeInfo.createParams);
               
                while (toks.hasMoreTokens())
                {
                    String tok = toks.nextToken();
                   
                    if (tok.startsWith("[") && tok.endsWith("]"))
                    {
                        // The brackets look like they indicate an optional param so
                        // skip
                        continue;
                    }
                   
                    typeSpec.append(" " + tok);
                }
            }
           
            // Add any precision - note that there is no obvious flag in the JDBC typeinfo to
            // tell us whether the type allows this specification or not, and many JDBC just
            // return crap anyway. We use the allowsPrecisionSpec flag for this
            StringBuffer precSpec = new StringBuffer();
            int sqlPrecision = getSQLPrecision();
            if (sqlPrecision > 0 && typeInfo.allowsPrecisionSpec)
            {
                precSpec.append(sqlPrecision);
                if (columnMetaData.getScale() != null)
                {
                    precSpec.append("," + columnMetaData.getScale());
                }
            }
            else if (sqlPrecision > 0 && !typeInfo.allowsPrecisionSpec)
            {
                JPOXLogger.DATASTORE_SCHEMA.warn(LOCALISER.msg("020183", this.toString()));
            }
            int lParenIdx = typeSpec.toString().indexOf('(');
            int rParenIdx = typeSpec.toString().indexOf(')', lParenIdx);
            if (lParenIdx > 0 && rParenIdx > 0)
            {
                // Some databases (like DB2) give you typeNames with ()'s already
                // present ready for you to insert the values instead of appending them.
                if (precSpec.length() > 0)
                {
                    typeSpec.replace(lParenIdx + 1, rParenIdx, precSpec.toString());
                }
                else if (rParenIdx == lParenIdx + 1)
                {
                    throw new DatastoreFieldDefinitionException(LOCALISER.msg("020184", this.toString()));
                }
            }
            else if (precSpec.length() > 0)
            {
                typeSpec.append('(');
                typeSpec.append(precSpec.toString());
                typeSpec.append(')');
            }
            def.append(" " + typeSpec.toString());
        }

        // Add DEFAULT (if specifiable before NULL)
        if (adapter.supportsDefaultBeforeNullInColumnOptions() &&
            adapter.supportsDefaultKeywordInColumnOptions() &&
            columnMetaData.getDefaultValue() != null)
        {
            def.append(" ").append(getDefaultDefinition());
        }

        // Nullability
        if (adapter.supportsIdentityFields() && autoIncrement && !adapter.supportsAutoIncrementKeysNullSpecification())
        {
            // Do nothing since the adapter doesn't allow NULL specifications with autoincrement/identity
        }
        else
        {
            if (!isNullable())
            {
                if (columnMetaData.getDefaultValue() == null || adapter.supportsDefaultKeywordWithNotNullInColumnOptions())
                {
                    def.append(" NOT NULL");
                }
            }
            else if (typeInfo.nullable == DatabaseMetaData.columnNullable)
            {
                if (adapter.supportsNullsKeywordInColumnOptions())
                {
                    def.append(" NULL");
                }
            }
        }

        // Add DEFAULT (if specifiable after NULL)
        if (!adapter.supportsDefaultBeforeNullInColumnOptions() &&
            adapter.supportsDefaultKeywordInColumnOptions() &&
            columnMetaData.getDefaultValue() != null)
        {
            def.append(" ").append(getDefaultDefinition());
        }

        // Constraints checks
        if (adapter.supportsCheckInCreateStatements() && constraints != null)
        {
            def.append(" " + constraints.toString());
        }

        // Auto Increment
        if (adapter.supportsIdentityFields() && autoIncrement)
        {
            def.append(" " + adapter.getAutoIncrementKeyword());
        }

        // Uniqueness
        if (isUnique() && !adapter.supportsUniqueConstraintsInEndCreateStatements())
        {
            def.append(" UNIQUE");
        }

        return def.toString();
View Full Code Here

Examples of org.jpox.store.rdbms.adapter.RDBMSAdapter

        }
        if (c[TABLE_IDENTIFIER_TABLE] != null)
        {
            tableName = c[TABLE_IDENTIFIER_TABLE];
        }
        RDBMSAdapter dba = (RDBMSAdapter)storeMgr.getDatastoreAdapter();
        catalogName = JDBCUtils.getIdentifierNameStripped(catalogName, dba);
        schemaName = JDBCUtils.getIdentifierNameStripped(schemaName, dba);
        tableName = JDBCUtils.getIdentifierNameStripped(tableName, dba);

        ResultSet rs = conn.getMetaData().getTables(catalogName, schemaName, tableName, null);
View Full Code Here

Examples of org.jpox.store.rdbms.adapter.RDBMSAdapter

        }
        if (c[TABLE_IDENTIFIER_TABLE] != null)
        {
            tableName = c[TABLE_IDENTIFIER_TABLE];
        }
        RDBMSAdapter dba = (RDBMSAdapter)storeMgr.getDatastoreAdapter();
        catalogName = JDBCUtils.getIdentifierNameStripped(catalogName, dba);
        schemaName = JDBCUtils.getIdentifierNameStripped(schemaName, dba);
        tableName = JDBCUtils.getIdentifierNameStripped(tableName, dba);

        ResultSet rs = dba.getColumns(conn, catalogName, schemaName, tableName);
        try
        {
            while (rs.next())
            {
                cols.add(((RDBMSAdapter)storeMgr.getDatastoreAdapter()).newColumnInfo(rs));
View Full Code Here

Examples of org.jpox.store.rdbms.adapter.RDBMSAdapter

                                                        DatastoreIdentifier column)
    throws SQLException
    {
        ColumnInfo colInfo = null;

        RDBMSAdapter dba = (RDBMSAdapter)storeMgr.getDatastoreAdapter();
        // Calculate the catalog/schema names since we need to search fully qualified
        String[] t = splitTableIdentifierName(dba.getCatalogSeparator(), table.getIdentifier().getIdentifier());
        String catalogName = table.getCatalogName();
        String schemaName = table.getSchemaName();
        String tableName = table.getIdentifier().getIdentifier();
        if (t[TABLE_IDENTIFIER_CATALOG] != null)
        {
            catalogName = t[TABLE_IDENTIFIER_CATALOG];
        }
        if (t[TABLE_IDENTIFIER_SCHEMA] != null)
        {
            schemaName = t[TABLE_IDENTIFIER_SCHEMA];
        }
        if (t[TABLE_IDENTIFIER_TABLE] != null)
        {
            tableName = t[TABLE_IDENTIFIER_TABLE];
        }
        String[] c = splitColumnIdentifierName(dba.getCatalogSeparator(), column.getIdentifier());
        String columnName = column.getIdentifier();
        if (c[TABLE_IDENTIFIER_COLUMN] != null)
        {
            columnName = c[TABLE_IDENTIFIER_COLUMN];
        }
        //put in case, so JDBC driver will find the schema
        if (schemaName != null)
        {
            if (storeMgr.getIdentifierFactory().getIdentifierCase() == IdentifierFactory.IDENTIFIER_LOWER_CASE ||
                storeMgr.getIdentifierFactory().getIdentifierCase() == IdentifierFactory.IDENTIFIER_LOWER_CASE_QUOTED)
            {
                schemaName = schemaName.toLowerCase();
            }
            else if (storeMgr.getIdentifierFactory().getIdentifierCase() == IdentifierFactory.IDENTIFIER_UPPER_CASE ||
                storeMgr.getIdentifierFactory().getIdentifierCase() == IdentifierFactory.IDENTIFIER_UPPER_CASE_QUOTED)
            {
                schemaName= schemaName.toUpperCase();
            }
        }
        ResultSet rs = dba.getColumns(conn, catalogName, schemaName, tableName, columnName);
        try
        {
            if (rs.next())
            {
                colInfo = ((RDBMSAdapter)storeMgr.getDatastoreAdapter()).newColumnInfo(rs);
View Full Code Here

Examples of org.jpox.store.rdbms.adapter.RDBMSAdapter

        }
        if (c[TABLE_IDENTIFIER_TABLE] != null)
        {
            tableName = c[TABLE_IDENTIFIER_TABLE];
        }
        RDBMSAdapter dba = (RDBMSAdapter)storeMgr.getDatastoreAdapter();
        catalogName = JDBCUtils.getIdentifierNameStripped(catalogName, dba);
        schemaName = JDBCUtils.getIdentifierNameStripped(schemaName, dba);
        tableName = JDBCUtils.getIdentifierNameStripped(tableName, dba);

        ResultSet rs = conn.getMetaData().getImportedKeys(catalogName, schemaName, tableName);
        try
        {
            while (rs.next())
            {
                ForeignKeyInfo fki = dba.newForeignKeyInfo(rs);

                // Some RDBMS (e.g PostgreSQL) returns duplicate rows from getImportedKeys().
                if (!fk_cols.contains(fki))
                {
                    fk_cols.add(fki);
View Full Code Here

Examples of org.jpox.store.rdbms.adapter.RDBMSAdapter

                                  ClassLoaderResolver clr)
    throws Exception
    {
        // Initialise the Catalog/Schema names
        PersistenceConfiguration conf = omfContext.getPersistenceConfiguration();
        RDBMSAdapter rdba = (RDBMSAdapter)dba;
        if ((conf.hasProperty("org.jpox.mapping.Catalog") && rdba.supportsCatalogsInTableDefinitions()) ||
            (conf.hasProperty("org.jpox.mapping.Schema") && rdba.supportsSchemasInTableDefinitions()))
        {
            // User-specified catalog/schema
            catalogName = conf.getStringProperty("org.jpox.mapping.Catalog");
            schemaName = conf.getStringProperty("org.jpox.mapping.Schema");
        }
        else
        {
            // TODO Should we bother with this if the RDBMS doesnt support catalog/schema in the table identifiers ?
            // Try to find the catalog/schema from the connection
            try
            {
                try
                {
                    catalogName = rdba.getCatalogName(conn);
                    schemaName = rdba.getSchemaName(conn);
                }
                catch (UnsupportedOperationException e)
                {
                    if (!readOnlyDatastore && !fixedDatastore)
                    {
View Full Code Here

Examples of org.jpox.store.rdbms.adapter.RDBMSAdapter

     * @throws Exception If an error occurs in obtaining the schema information
     */
    public void outputDatastoreInformation(PrintStream ps)
    throws Exception
    {
        RDBMSAdapter dba = (RDBMSAdapter) getDatastoreAdapter();

        // Print out the DBA
        String msg = dba.toString();
        JPOXLogger.SCHEMATOOL.info(msg);
        ps.println(msg);
        ps.println();

        // Print out the type info
        msg = "Database TypeInfo";
        JPOXLogger.SCHEMATOOL.info(msg);
        ps.println(msg);

        Iterator typeInfosIter = dba.iteratorTypeInfo();
        while (typeInfosIter.hasNext())
        {
            JDBCTypeInfo jti = (JDBCTypeInfo)typeInfosIter.next();
            String typeStr = "JDBC Type=" + JDBCUtils.getNameForJDBCType(jti.getJdbcType()) +
                " datastoreTypes=" + StringUtils.objectArrayToString(jti.getTypeNames());
            JPOXLogger.SCHEMATOOL.info(typeStr);
            ps.println(typeStr);
            JPOXLogger.SCHEMATOOL.info(jti.getDefault());
            ps.println(jti.getDefault());
        }
        ps.println("");

        // Print out the keywords info
        msg = "Database Keywords";
        JPOXLogger.SCHEMATOOL.info(msg);
        ps.println(msg);

        Iterator reservedWordsIter = dba.iteratorReservedWords();
        while (reservedWordsIter.hasNext())
        {
            Object words = reservedWordsIter.next();
            JPOXLogger.SCHEMATOOL.info(words);
            ps.println(words);
View Full Code Here

Examples of org.jpox.store.rdbms.adapter.RDBMSAdapter

     * @throws Exception If an error occurs in obtaining the schema information
     */
    public void outputSchemaInformation(PrintStream ps)
    throws Exception
    {
        RDBMSAdapter dba = (RDBMSAdapter) getDatastoreAdapter();

        // Print out the DBA
        String msg = dba.toString();
        JPOXLogger.SCHEMATOOL.info(msg);
        ps.println(msg);
        ps.println();

        JPOXLogger.SCHEMATOOL.info("TABLES");
View Full Code Here

Examples of org.jpox.store.rdbms.adapter.RDBMSAdapter

        if (this.stmtText == null)
        {
            // Make sure we have any necessary ordering columns in the SELECT (where required)
            addOrderingColumnsToSelect();

            final RDBMSAdapter rdbmsAdapter = (RDBMSAdapter)storeMgr.getDatastoreAdapter();
            StatementText stmtText = null;

      stmtText = new StatementText("SELECT ");

            if (rangeOffset > -1 || rangeCount > -1)
            {
                if (rdbmsAdapter.getRangeByRowNumberColumn().length()>0)
                {
                    // When doing range queries using ROWNUM, we must wrap the original query
                    // in an outer query that does selection based on an aliased ROWNUM column
                    // of the inner (original) query (CORE-2721)
                    //
                    // see also http://72.14.207.104/search?q=cache:IZZXCUsqdIIJ:www.arrowsent.com/oratip/tip41.htm+oracle+rownum+range&hl=en&ct=clnk&cd=1
                    // SELECT the ROWNUM column and alias it in what will be the inner query
                    stmtText.append(rdbmsAdapter.getRangeByRowNumberColumn() + " rn, ");
                }
                else
                {
                    // Add a LIMIT clause to SELECT if it is supported
                    stmtText.append(rdbmsAdapter.getRangeByLimitSelectClause(rangeOffset, rangeCount));
                }
            }

            boolean usingDistinct = false;
            if (!isExistsSubQuery)
            {
              if (distinctResults)
              {
                  stmtText.append("DISTINCT ");
                    usingDistinct = true;
              }

              Iterator iterator = selected.iterator();
              while (iterator.hasNext())
              {
                    Object selectExpr = iterator.next();
                  stmtText.append(selectExpr.toString());
                  if (iterator.hasNext())
                  {
                      stmtText.append(',');
                  }
              }
                if ((rangeOffset > -1 || rangeCount > -1) && rdbmsAdapter.getRangeByRowNumberColumn().length() > 0)
                {
                    // Add a ROW NUMBER column if supported as the means of handling ranges by the RDBMS
                    stmtText.append(',').append(rdbmsAdapter.getRangeByRowNumberColumn());
                }
            }
            else
            {
                JavaTypeMapping m = rdbmsAdapter.getMapping(Integer.class, storeMgr);
                stmtText.append(m.newLiteral(this, new Integer("1")).toStatementText(ScalarExpression.PROJECTION).toStatementString(ScalarExpression.PROJECTION));
            }

            /*
             * 1. Push joins down
             * 2. try to make sure joins refer to previous table in stack
             *
             *  A
             *  JOIN B = A
             *  JOIN C = C
             *  JOIN D = D
             */
            //SORT JOINS
            List sorted = sortJoins(joins);
            final Join[] sortedJoins = (Join[]) sorted.toArray(new Join[sorted.size()]);

            // FROM condition(s)
            stmtText.append(" FROM ");

            List crossJoinss = new ArrayList();
            crossJoinss.add(mainTableExpr);
            crossJoinss.addAll(crossJoins);
            if (crossJoinss.size()==1)
            {
                stmtText.append(crossJoinss.get(0).toString());
                if (lock && rdbmsAdapter.getSelectWithLockOption() != null && rdbmsAdapter.getPlaceWithOptionAfterFromClause())
                {
                    // Add any locking of the rows for datastores that require WITH option to be placed after the FROM clause
                    stmtText.append(" WITH " + rdbmsAdapter.getSelectWithLockOption());
                }
                crossJoinss.remove(0);
            }
           
            for (int i=0; i<sortedJoins.length; i++)
            {
                final Join join = sortedJoins[i];
                for (int j=crossJoinss.size()-1; j>=0; j--)
                {
                    if ((sortedJoins[i].expr1.getLogicSetExpression().equals(crossJoinss.get(j)) ||
                         sortedJoins[i].expr2.getLogicSetExpression().equals(crossJoinss.get(j))))
                    {
                        if (i > 0)
                        {
                            stmtText.append(rdbmsAdapter.cartersianProduct((LogicSetExpression)crossJoinss.get(j)));
                        }
                        else
                        {
                            stmtText.append(crossJoinss.get(j).toString());
                        }
                        if (lock && rdbmsAdapter.getSelectWithLockOption() != null && rdbmsAdapter.getPlaceWithOptionAfterFromClause())
                        {
                            // Add any locking of the rows for datastores that require WITH option to be placed after the FROM clause
                            stmtText.append(" WITH " + rdbmsAdapter.getSelectWithLockOption());
                        }
                        crossJoinss.remove(j);
                        break;
                    }
                }
                stmtText.append(stmtJoinsSeparator).append(join.toString(rdbmsAdapter, lock));
            }
            crossJoinss.remove(null);
            for (int i=0; i<crossJoinss.size(); i++)
            {
                if (sortedJoins.length > 0 || i > 0)
                {
                    stmtText.append(rdbmsAdapter.cartersianProduct((LogicSetExpression)crossJoinss.get(i)));
                }
                else
                {
                    stmtText.append(crossJoinss.get(i).toString());
                }
                if (lock && rdbmsAdapter.getSelectWithLockOption() != null && rdbmsAdapter.getPlaceWithOptionAfterFromClause())
                {
                    // Add any locking of the rows for datastores that require WITH option to be placed after the FROM clause
                    stmtText.append(" WITH " + rdbmsAdapter.getSelectWithLockOption());
                }
            }

            // WHERE condition(s)
            if (whereExpr != null)
            {
                stmtText.append(" WHERE ").append(whereExpr.toStatementText(ScalarExpression.FILTER),ScalarExpression.FILTER);
            }

            // GROUP BY clause(s)
            if (groupingExpressions != null)
            {
                List groupBy = new ArrayList();
                for (int i=0;i<groupingExpressions.size();i++)
                {
                    // Add on the user-specified groupings
                    String exprText = ((ScalarExpression)groupingExpressions.get(i)).toStatementText(ScalarExpression.PROJECTION).toString();
                    if (!groupBy.contains(exprText))
                    {
                        groupBy.add(exprText);
                    }
                }
                if (groupBy.size() > 0 && hasAggregateExpression)
                {
                    stmtText.append(" GROUP BY ");
                    boolean first = true;
                    for (int i=0; i<groupBy.size(); i++)
                    {
                        if (!first)
                        {
                            stmtText.append(',');
                        }
                        stmtText.append((String)groupBy.get(i));
                        first = false;
                    }
                }
            }

            // HAVING clause
            if (havingExpr != null)
            {
                stmtText.append(" HAVING ").append(havingExpr.toStatementText(ScalarExpression.FILTER),ScalarExpression.FILTER);
            }

            // UNION to other query statements
            Iterator iterator = union.iterator();
            while (iterator.hasNext())
            {
                if (!rdbmsAdapter.supportsUnionSyntax())
                {
                    throw new JPOXException(LOCALISER.msg("052504", "UNION")).setFatal();
                }

                if (rdbmsAdapter.useUnionAll())
                {
                    stmtText.append(" UNION ALL ");
                }
                else
                {
                    stmtText.append(" UNION ");
                }
                QueryStatement qs = ((QueryStatement)iterator.next());
                //do not use for update in other union queries. is this correct for all databases?
                stmtText.append(qs.toStatementText(false),ScalarExpression.FILTER);
            }

            // ORDER clause
            if (!isExistsSubQuery)
            {
                if (orderingExpressions != null && orderingExpressions.length > 0)
                {
                    StatementText orderByStmt = generateOrderingStatement();
                  stmtText.append(" ORDER BY ").append(orderByStmt,ScalarExpression.PROJECTION);
              }
            }

            if (rangeOffset > -1 || rangeCount > -1)
            {
                // Add a LIMIT clause to WHERE if it is supported
                stmtText.append(rdbmsAdapter.getRangeByLimitWhereClause(rangeOffset, rangeCount));
            }

            // Add any required locking based on the RDBMS capability
            if (lock && rdbmsAdapter.supportsLockWithSelectForUpdate())
            {
                if (usingDistinct && !rdbmsAdapter.supportsDistinctWithSelectForUpdate())
                {
                    JPOXLogger.QUERY.warn(LOCALISER.msg("052502"));
                }
                else
                {
                    stmtText.append(" FOR UPDATE");
                }
            }
           
            if ((rangeOffset > -1 || rangeCount > -1) && rdbmsAdapter.getRangeByRowNumberColumn().length() > 0)
            {
                // range query using ROWNUM (CORE-2721): wrap in outer query,
                // which must select all columns of the inner query,
                // except for the ROWNUM column (which is not contained in
                // selected list)
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.