Package org.exolab.castor.persist.spi

Examples of org.exolab.castor.persist.spi.QueryExpression


       
        if (!handler.hasNext()) {
            // Create "SELECT seq_val FROM seq_table WHERE seq_key='table'"
            // with database-dependent keyword for lock
            // Note: Some databases (InstantDB, HypersonicSQL) don't support such locks.
            QueryExpression query = _factory.getQueryExpression();
            query.addColumn(_seqTable, _seqValue);
            query.addCondition(_seqTable, _seqKey, QueryExpression.OP_EQUALS, JDBCSyntax.PARAMETER);
            String lockSQL = query.getStatement(true);
           
            // For the case that "SELECT FOR UPDATE" is not supported, perform dirty checking
            String updateSQL = "UPDATE " +  _seqTable
                + " SET " + _seqValue + "=" + JDBCSyntax.PARAMETER
                + JDBCSyntax.WHERE + _seqKey + QueryExpression.OP_EQUALS + JDBCSyntax.PARAMETER
View Full Code Here


            if (factory == null) {
            throw new DTXException("dtx.NoFactory");
            }

            QueryExpression expr = factory.getQueryExpression();

            if (expr == null) {
            throw new DTXException("dtx.NoQueryExpression");
            }

            initQuery(_clsMapping, expr);

            if (token.hasMoreTokens()) {
            if (!token.nextToken().equalsIgnoreCase("WHERE")) {
                throw new DTXException("Missing WHERE clause");
            }
            addField(_clsMapping, token, expr);
            while (token.hasMoreTokens()) {
                if (!token.nextToken().equals("AND")) {
                throw new QueryException("Only AND supported in WHERE clause");
                }
                addField(_clsMapping, token, expr);
            }
            }

            String sql = expr.getStatement(false);

            sql = sql + " ORDER BY ";

            for (java.util.Iterator it = _ids.iterator(); it.hasNext(); ) {
            String id = (String) it.next();
View Full Code Here

         */

        // Create SQL sentence of the form
        // "SELECT pk FROM table WHERE pk=(SELECT MAX(t1.pk) FROM table t1)"
        // with database-dependent keyword for lock
        QueryExpression query = _factory.getQueryExpression();
        query.addColumn(table, column);
        query.addCondition(table, column, QueryExpression.OP_EQUALS,
                "(SELECT MAX(t1." + _factory.quoteName(column) + ") FROM "
                + _factory.quoteName(table) + " t1)");
        return query.getStatement(true);
    }
View Full Code Here

        buildStatement();
    }

    private void buildStatement() throws MappingException {
        try {
            QueryExpression expr = _factory.getQueryExpression();
           
            Map identitiesUsedForTable = new HashMap();
            Vector joinTables = new Vector();

            // join all the extended table
            ClassDescriptor curDesc = _engine.getDescriptor();
            ClassDescriptor baseDesc;
            while (curDesc.getExtends() != null) {
                baseDesc = curDesc.getExtends();
                String[] curDescIdNames = SQLHelper.getIdentitySQLNames(curDesc);
                String[] baseDescIdNames = SQLHelper.getIdentitySQLNames(baseDesc);
                expr.addInnerJoin(
                        new ClassDescriptorJDONature(curDesc).getTableName(), curDescIdNames,
                        new ClassDescriptorJDONature(baseDesc).getTableName(), baseDescIdNames);
                joinTables.add(new ClassDescriptorJDONature(baseDesc).getTableName());
                curDesc = baseDesc;
            }
           
            SQLColumnInfo[] ids = _engine.getColumnInfoForIdentities();
            SQLFieldInfo[] fields = _engine.getInfo();

            // join all the related/depended table
            String aliasOld = null;
            String alias = null;
           
            for (int i = 0; i < fields.length; i++) {
                SQLFieldInfo field = fields[i];
               
                if (i > 0) { aliasOld = alias; }
                alias = field.getTableName();

                // add id fields for root table if first field points to a separate table
                if ((i == 0) && field.isJoined()) {
                    String[] identities = SQLHelper.getIdentitySQLNames(_engine.getDescriptor());
                    for (int j = 0; j < identities.length; j++) {
                        expr.addColumn(
                                new ClassDescriptorJDONature(curDesc).getTableName(),
                                identities[j]);
                    }
                    identitiesUsedForTable.put(
                            new ClassDescriptorJDONature(curDesc).getTableName(),
                            Boolean.TRUE);
                }
               
                // add id columns to select statement
                if (!alias.equals(aliasOld) && !field.isJoined()) {
                    ClassDescriptor classDescriptor =
                        field.getFieldDescriptor().getContainingClassDescriptor();
                    boolean isTableNameAlreadyAdded = identitiesUsedForTable.containsKey(
                            new ClassDescriptorJDONature(classDescriptor).getTableName());
                    if (!isTableNameAlreadyAdded) {
                        String[] identities = SQLHelper.getIdentitySQLNames(classDescriptor);
                        for (int j = 0; j < identities.length; j++) {
                            expr.addColumn(alias, identities[j]);
                        }
                        identitiesUsedForTable.put(
                                new ClassDescriptorJDONature(classDescriptor).getTableName(),
                                Boolean.TRUE);
                    }
                }

                if (field.isJoined()) {
                    int offset = 0;
                    String[] rightCol = field.getJoinFields();
                    String[] leftCol = new String[ids.length - offset];
                    for (int j = 0; j < leftCol.length; j++) {
                        leftCol[j] = ids[j + offset].getName();
                    }
                    ClassDescriptor clsDescriptor = _engine.getDescriptor();
                    ClassDescriptorJDONature nature = new ClassDescriptorJDONature(clsDescriptor);
                    if (joinTables.contains(field.getTableName())
                            || nature.getTableName().equals(field.getTableName())) {
                       
                        // should not mix with aliases in ParseTreeWalker
                        alias = alias.replace('.', '_') + "_f" + i;
                        expr.addOuterJoin(_mapTo, leftCol, field.getTableName(), rightCol, alias);
                    } else {
                        expr.addOuterJoin(_mapTo, leftCol, field.getTableName(), rightCol);
                        joinTables.add(field.getTableName());
                    }
                }

                for (int j = 0; j < field.getColumnInfo().length; j++) {
                    expr.addColumn(alias, field.getColumnInfo()[j].getName());
                }
               
                expr.addTable(field.getTableName(), alias);
            }

            // 'join' all the extending tables
            List classDescriptorsToAdd = new LinkedList();
            ClassDescriptor classDescriptor = null;
            SQLHelper.addExtendingClassDescriptors(classDescriptorsToAdd,
                    new ClassDescriptorJDONature(_engine.getDescriptor()).getExtended());
           
            if (classDescriptorsToAdd.size() > 0) {
                for (Iterator iter = classDescriptorsToAdd.iterator(); iter.hasNext(); ) {
                    classDescriptor = (ClassDescriptor) iter.next();
                    ClassDescriptorJDONature clsDescNature =
                        new ClassDescriptorJDONature(classDescriptor);
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Adding outer left join for "
                                + classDescriptor.getJavaClass().getName() + " on table "
                                + clsDescNature.getTableName());
                    }
                   
                    String[] engDescIdNames = SQLHelper.getIdentitySQLNames(
                            _engine.getDescriptor());
                    String[] clsDescIdNames = SQLHelper.getIdentitySQLNames(classDescriptor);
                    expr.addOuterJoin(_mapTo, engDescIdNames,
                            clsDescNature.getTableName(), clsDescIdNames);

                    Persistence persistenceEngine;
                    try {
                        persistenceEngine = _factory.getPersistence(classDescriptor);
                    } catch (MappingException e) {
                        throw new QueryException(
                                "Problem obtaining persistence engine for ClassDescriptor "
                                + classDescriptor.getJavaClass().getName(), e);
                    }

                    SQLEngine engine = (SQLEngine) persistenceEngine;
                    SQLColumnInfo[] idInfos = engine.getColumnInfoForIdentities();
                    for (int i = 0; i < idInfos.length; i++) {
                        expr.addColumn(clsDescNature.getTableName(), idInfos[i].getName());
                    }
                   
                    SQLFieldInfo[] fieldInfos = ((SQLEngine) persistenceEngine).getInfo();
                    for (int i = 0; i < fieldInfos.length; i++) {
                        boolean hasFieldToAdd = false;
                        SQLColumnInfo[] columnInfos = fieldInfos[i].getColumnInfo();
                        if (clsDescNature.getTableName().equals(fieldInfos[i].getTableName())) {
                            for (int j = 0; j < columnInfos.length; j++) {
                                expr.addColumn(clsDescNature.getTableName(),
                                        fieldInfos[i].getColumnInfo()[j].getName());
                            }
                            hasFieldToAdd = true;
                        }
                       
                        if (hasFieldToAdd) {
                            expr.addTable(clsDescNature.getTableName());
                        }
                    }
                }
            }
           
            QueryExpression find = (QueryExpression) expr.clone();
           
            // get id columns' names
            for (int i = 0; i < ids.length; i++) {
                expr.addParameter(_mapTo, ids[i].getName(), QueryExpression.OP_EQUALS);
            }

            _statementNoLock = expr.getStatement(false);
            _statementLock = expr.getStatement(true);

            // add table information if the class in question does not have any non-identity
            // fields
            if (fields.length == 0) {
                for (int i = 0; i < ids.length; i++) {
                    find.addColumn(_mapTo, ids[i].getName());
                }
            }

            _queryExpression = find;
View Full Code Here

    }

    private void buildStatement() throws MappingException {
        try {
            SQLColumnInfo[] ids = _engine.getColumnInfoForIdentities();
            QueryExpression query = _factory.getQueryExpression();

            // initalize lookup query
            for (int i = 0; i < ids.length; i++) {
                query.addParameter(_mapTo, ids[i].getName(), QueryExpression.OP_EQUALS);
            }
            _statement = query.getStatement(true);
      } catch (QueryException except) {
          LOG.warn("Problem building SQL", except);
          throw new MappingException(except);
      }
    }
View Full Code Here

     */
    public void testGetQueryExpression() throws Exception {
        PersistenceFactory factory = new PointbaseFactory();
        assertNotNull(factory);
       
        QueryExpression expression = factory.getQueryExpression();
        assertNotNull(expression);
       
        assertEquals(PointbaseQueryExpression.class.getName(),
                expression.getClass().getName());
    }
View Full Code Here

            throws PersistenceException
    {
        String sql;
        PreparedStatement stmt = null;
        ResultSet rs;
        QueryExpression query;
        Object identity = null;

        try {
            query = _factory.getQueryExpression();
            if ( _factory.getFactoryName().equals( "mysql" ) ) {
                // Create SQL sentence of the form
                // "SELECT MAX(pk) FROM table"
                query.addSelect("MAX(" + _factory.quoteName(primKeyName)+ ")");
                query.addTable(tableName);

                // SELECT without lock
                sql = query.getStatement( false );
            } else {
                // Create SQL sentence of the form
                // "SELECT pk FROM table WHERE pk=(SELECT MAX(t1.pk) FROM table t1)"
                // with database-dependent keyword for lock
                query.addColumn( tableName, primKeyName);
                query.addCondition( tableName, primKeyName, QueryExpression.OpEquals,
                        "(SELECT MAX(t1." + _factory.quoteName(primKeyName) + ") FROM " + _factory.quoteName(tableName) + " t1)");

                // SELECT and put lock on the last record
                sql = query.getStatement( true );
            }

            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
View Full Code Here

            else if ( _sqlType == Types.BIGINT )
                last = new Long( ( (Long) last ).longValue() + 1 );
            else
                last = ((BigDecimal) last).add( ONE );
        } else {
            QueryExpression query;
            String sql;
            String sql2;
            PreparedStatement stmt = null;
            PreparedStatement stmt2 = null;
            ResultSet rs;
            boolean success;

            try {
                // the separate connection should be committed/rolled back at this point
                if ( ! _sameConnection )
                    conn.rollback();

                // Create SQL sentence of the form
                // "SELECT seq_val FROM seq_table WHERE seq_key='table'"
                // with database-dependent keyword for lock
                // [george stewart] Note, that some databases (InstantDB,
                // HypersonicSQL) don't support such locks.
                query = _factory.getQueryExpression();
                query.addColumn( _seqTable, _seqValue );
                query.addCondition( _seqTable, _seqKey, QueryExpression.OpEquals,
                                    JDBCSyntax.Parameter);

                // SELECT and put lock on the last record
                sql = query.getStatement( true );
                // For the case if the "SELECT FOR UPDATE" is not supported
                // we perform dirty checking
                sql2 = "UPDATE "+  _seqTable +
                    " SET " + _seqValue + "=" + JDBCSyntax.Parameter +
                    JDBCSyntax.Where + _seqKey + QueryExpression.OpEquals +
View Full Code Here

            else if ( _sqlType == Types.BIGINT )
                last = new Long( ( (Long) last ).longValue() + 1 );
            else
                last = ((BigDecimal) last).add( ONE );
        } else {
            QueryExpression query;
            String sql;
            String sql2;
            String pk;
            PreparedStatement stmt = null;
            PreparedStatement stmt2 = null;
            ResultSet rs;
            boolean success;

            try {
                // the separate connection should be committed/rolled back at this point
                if ( ! _sameConnection )
                    conn.rollback();

                // Create SQL sentence of the form
                // "SELECT seq_val FROM seq_table WHERE seq_key='table'"
                // with database-dependent keyword for lock
                // [george stewart] Note, that some databases (InstantDB,
                // HypersonicSQL) don't support such locks.
                query = _factory.getQueryExpression();
                query.addColumn( _seqTable, _seqValue );
                query.addCondition( _seqTable, _seqKey, QueryExpression.OpEquals,
                                    JDBCSyntax.Parameter);

                // SELECT and put lock on the last record
                sql = query.getStatement( true );
                // For the case if the "SELECT FOR UPDATE" is not supported
                // we perform dirty checking
                sql2 = "UPDATE "+  _seqTable +
                    " SET " + _seqValue + "=" + JDBCSyntax.Parameter +
                    JDBCSyntax.Where + _seqKey + QueryExpression.OpEquals +
View Full Code Here

    {
        String sql;
        String pk;
        PreparedStatement stmt = null;
        ResultSet rs;
        QueryExpression query;
        Object identity = null;

        try {
            query = _factory.getQueryExpression();
            if ( _factory.getFactoryName().equals( "mysql" ) ) {
                // Create SQL sentence of the form
                // "SELECT MAX(pk) FROM table"
                query.addSelect("MAX(" + _factory.quoteName(primKeyName)+ ")");
                query.addTable(tableName);

                // SELECT without lock
                sql = query.getStatement( false );
            } else {
                // Create SQL sentence of the form
                // "SELECT pk FROM table WHERE pk=(SELECT MAX(t1.pk) FROM table t1)"
                // with database-dependent keyword for lock
                query.addColumn( tableName, primKeyName);
                query.addCondition( tableName, primKeyName, QueryExpression.OpEquals,
                        "(SELECT MAX(t1." + _factory.quoteName(primKeyName) + ") FROM " + _factory.quoteName(tableName) + " t1)");

                // SELECT and put lock on the last record
                sql = query.getStatement( true );
            }

            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
View Full Code Here

TOP

Related Classes of org.exolab.castor.persist.spi.QueryExpression

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.