Package org.exolab.castor.jdo.oql

Examples of org.exolab.castor.jdo.oql.ParamInfo


        if (_fieldNum == _paramInfo.size()) {
            throw new IllegalArgumentException("Only " + _paramInfo.size()
                    + " fields in this query");
        }
        try {
            ParamInfo info = (ParamInfo) _paramInfo.get(new Integer(_fieldNum + 1));

            //do type checking and conversion
            Class paramClass = info.getTheClass();
            Class fieldClass = info.getFieldType();
            Class sqlClass = info.getSQLType();

            if (internalValue != null) {
                Class valueClass = internalValue.getClass();

                if (paramClass.isAssignableFrom(valueClass)) {
                    LockEngine lockEngine = ((AbstractDatabaseImpl) _database).getLockEngine();
                    ClassMolder molder = lockEngine.getClassMolder(valueClass);

                    if (molder != null) {
                        Identity temp = molder.getActualIdentity(
                                _database.getClassLoader(), internalValue);
                        if (temp == null) {
                            internalValue = null;
                        } else  if (temp.size() == 1) {
                            internalValue = temp.get(0);
                        } else {
                            throw new IllegalArgumentException(
                                    "Unable to bind multi column identities");
                        }
                    }
                } else if (info.isUserDefined()) {
                        //If the user specified a type they must pass that exact type.

                        throw new IllegalArgumentException(
                                "Query paramter " + (_fieldNum + 1)
                                + " is not of the expected type " + paramClass
                                + " it is an instance of the class " + valueClass);
                }
                if ((sqlClass != null) && !sqlClass.isAssignableFrom(valueClass)) {
                    // First convert the actual value to the field value
                    if (fieldClass != valueClass) {
                        try {
                            TypeConvertor tc = getTypeConvertorRegistry().getConvertor(
                                    valueClass, fieldClass, null);
                            internalValue = tc.convert(internalValue);
                        } catch (MappingException e) {
                            throw new IllegalArgumentException("Query parameter "
                                    + (_fieldNum + 1) + " cannot be converted from " + valueClass
                                    + " to " + paramClass + ", because no convertor can be found.");
                        }
                    }
                    // Perform conversion from field type to SQL type, if needed
                    if (info.getConvertor() != null) {
                        internalValue = info.getConvertor().convert(internalValue);
                    }
                }
            }
            if (_bindValues == null) {
                _bindValues = new Object[ _bindTypes.length ];
View Full Code Here


        // create the types array and fill it
        _bindTypes = new Class[_paramInfo.size()];
        int paramIndex = 0;
        for (Enumeration e = _paramInfo.elements(); e.hasMoreElements(); ) {
            ParamInfo info = (ParamInfo) e.nextElement();

            _bindTypes[paramIndex++] =
                ((info.getSQLType() == null)) ? info.getTheClass() : info.getSQLType();
        }
    }
View Full Code Here

        int as;
        int leftParen;
        int rightParen;
        int paramCnt;
        String objType;
        ParamInfo info;
        StringBuffer sb;
        Integer paramNo;

        if (!oql.startsWith("CALL ")) {
            throw new QueryException("Stored procedure call must start with CALL");
        }

        // Fix for bug #995
        // as = oql.indexOf( " AS " );
        as = oql.lastIndexOf(" AS ");
        if (as < 0) {
            throw new QueryException("Stored procedure call must end with \"AS <class-name>\"");
        }
        leftParen = oql.indexOf("(");
        rightParen = oql.indexOf(")");
        sql = new StringBuffer();
        paramCnt = 0;
        _paramInfo = new Hashtable();
        if (oql.startsWith("CALL SQL")) {
            int startOff = oql.toUpperCase().indexOf("WHERE "); // parameters begin here!

            if (!(startOff < 0)) {
                startOff += 6;
                sql.append(oql.substring(5, startOff));

                int i = startOff;
                while (i < as) {
                    if (oql.charAt(i) == '$') {
                        // get parameter number if given
                        sb = new StringBuffer();
                        for (int j = i + 1; j < as; j++) {
                            char c = oql.charAt(j);
                            if (!Character.isDigit(c)) {
                                break;
                            }
                            sb.append(c);
                        }
                        sql.append('?'); // replace "$" with "?"
                        if (sb.length() > 0) {
                            sql.append(sb); // and add parameter number to it
                            paramNo = Integer.valueOf(sb.toString());
                        } else {
                            paramNo = new Integer(paramCnt + 1);
                        }
                        info = (ParamInfo) _paramInfo.get(paramNo);
                        if (info == null) {
                            info = new ParamInfo("", "java.lang.Object", null,
                                    _database.getClassLoader());
                        }
                        //info.mapToSQLParam( paramCnt + 1 );
                        _paramInfo.put(paramNo , info);
                        paramCnt++;

                        i += sb.length() + 1;
                    } else {
                        sql.append(oql.charAt(i));
                        i++;
                    }
                }
            } else {
                sql.append(oql.substring(5, as));
            }
        } else if ((leftParen < 0) && (rightParen < 0)) {
            sql.append(oql.substring(5, as));
        } else {
            if (((leftParen < 0) && (rightParen >= 0))
                    || (leftParen > rightParen)) {
                throw new QueryException("Syntax error: parenthesis");
            }
            sql.append(oql.substring(5, leftParen));
            sql.append('(');
            for (int i = leftParen + 1; i < rightParen; i++) {
                if (oql.charAt(i) == '$') {
                    // get parameter number if given
                    sb = new StringBuffer();
                    for (int j = i + 1; j < rightParen; j++) {
                        char c = oql.charAt(j);
                        if (!Character.isDigit(c)) {
                            break;
                        }
                        sb.append(c);
                    }
                    if (sb.length() > 0) {
                        paramNo = Integer.valueOf(sb.toString());
                    } else {
                        paramNo = new Integer(paramCnt + 1);
                    }
                    info = (ParamInfo) _paramInfo.get(paramNo);
                    if (info == null) {
                        info = new ParamInfo("", "java.lang.Object", null,
                                _database.getClassLoader());
                    }
                    //info.mapToSQLParam( paramCnt + 1 );
                    _paramInfo.put(paramNo , info);
                    paramCnt++;
View Full Code Here

            throw new IllegalStateException( "Must create query before using it" );
        if ( _fieldNum == _paramInfo.size() )
            throw new IllegalArgumentException( "Only " + _paramInfo.size() +
                                                " fields in this query" );
        try {
            ParamInfo info = (ParamInfo) _paramInfo.get(new Integer( _fieldNum + 1 ));

            //do type checking and conversion
            Class paramClass = info.getTheClass();
            Class fieldClass = info.getFieldType();
            Class sqlClass = info.getSQLType();

            if ( value != null ) {
                Class valueClass = value.getClass();

                if ( paramClass.isAssignableFrom( valueClass ) ) {
                    ClassMolder molder = _dbImpl.getLockEngine().getClassMolder( valueClass );

                    if ( molder != null ) {
                        value = molder.getActualIdentity( _dbImpl.getClassLoader(), value );
                    }
                } else if ( info.isUserDefined() ) {
                        //If the user specified a type they must pass that exact type.

                        throw new IllegalArgumentException( "Query paramter " +
                                                            ( _fieldNum + 1 ) +
                                                            " is not of the expected type " +
                                                            paramClass +
                                                            " it is an instance of the class "
                                                            + valueClass );
                }
                if ( sqlClass != null && ! sqlClass.isAssignableFrom( valueClass ) ) {
                    // First convert the actual value to the field value
                    if ( fieldClass != valueClass ) {
                        try {
                            TypeConvertor tc = SQLTypes.getConvertor( valueClass, fieldClass );
                            value = tc.convert( value, null );
                        } catch ( MappingException e ) {
                            throw new IllegalArgumentException( "Query parameter "
                                                                + ( _fieldNum + 1 )
                                                                + " cannot be converted from "
                                                                + valueClass + " to "
                                                                + paramClass
                                                                + ", because no convertor can be found." );
                        }
                    }
                    // Perform conversion from field type to SQL type, if needed
                    if (info.getConvertor() != null) {
                        value = info.getConvertor().convert( value, info.getConvertorParam() );
                    }
                }
            }
            if ( _bindValues == null )
                _bindValues = new Object[ _bindTypes.length ];

            for (Enumeration e = info.getParamMap().elements(); e.hasMoreElements(); )
            {
                int fieldNum = ( (Integer) e.nextElement() ).intValue();
                _bindValues[ fieldNum - 1 ] = value;
            }
View Full Code Here

        //port param info types back to the format of old bind types.
        //first get the maximum SQL param.
        int max = 0;
        for (Enumeration e = _paramInfo.elements(); e.hasMoreElements(); ) {
            ParamInfo info = (ParamInfo) e.nextElement();
            for (Enumeration f = info.getParamMap().elements(); f.hasMoreElements(); )
            {
                int paramIndex = ( (Integer) f.nextElement() ).intValue();
                if paramIndex > max )
                    max = paramIndex;
            }
        }

        //then create the types array and fill it
        _bindTypes = new Class[max];
        for (Enumeration e = _paramInfo.elements(); e.hasMoreElements(); )
        {
            ParamInfo info = (ParamInfo) e.nextElement();
            for (Enumeration f = info.getParamMap().elements(); f.hasMoreElements(); )
            {
                int paramIndex = ( (Integer) f.nextElement() ).intValue();
                _bindTypes[ paramIndex - 1 ] = (info.getSQLType() == null ? info.getTheClass()
                                                                          : info.getSQLType());
            }
        }

    }
View Full Code Here

        int as;
        int leftParen;
        int rightParen;
        int paramCnt;
        String objType;
        ParamInfo info;
        StringBuffer sb;
        Integer paramNo;

        if ( !oql.startsWith("CALL ") ) {
            throw new QueryException( "Stored procedure call must start with CALL" );
        }

        // Fix for bug #995
        // as = oql.indexOf( " AS " );
        as = oql.lastIndexOf( " AS " );
        if ( as < 0 ) {
            throw new QueryException( "Stored procedure call must end with \"AS <class-name>\"" );
        }
        leftParen = oql.indexOf( "(" );
        rightParen = oql.indexOf( ")" );
        sql = new StringBuffer();
        paramCnt = 0;
        _paramInfo = new Hashtable();
        if ( oql.startsWith("CALL SQL") ) {
            int startOff = oql.toUpperCase().indexOf("WHERE "); // parameters begin here!

            if (!(startOff < 0)) {
                startOff += 6;
                sql.append(oql.substring(5, startOff));

                for ( int i = startOff; i < as; ++i ) {
                    if ( oql.charAt( i ) == '$' ) {
                        // get parameter number if given
                        sb = new StringBuffer();
                        boolean didParam = false;
                        for ( int j = i + 1; j < as; j++ ) {
                            char c = oql.charAt( j );
                            if (!Character.isDigit(c)) {
                                didParam = true;
                                sql.append("?"); // replace characters with "?"
                                break;
                            }
                            sb.append( c );
                        }
                        if (!didParam) sql.append('?'); // we reached the end of the string!
                        if ( sb.length() > 0 ) {
                            paramNo = Integer.valueOf( sb.toString() );
                        } else {
                            paramNo = new Integer( paramCnt + 1 );
                        }
                        info = (ParamInfo) _paramInfo.get( paramNo );
                        if ( info == null ) {
                            info = new ParamInfo( "", "java.lang.Object", null);
                        }
                        info.mapToSQLParam( paramCnt + 1 );
                        _paramInfo.put( paramNo , info );
                        paramCnt++;

                        i += sb.length();
                    } else {
                        sql.append(oql.charAt(i));
                    }
                }
            } else {
                sql.append(oql.substring(5, as));
            }
        }
        else if ((leftParen < 0 && rightParen < 0) ) {
            sql.append( oql.substring( 5, as ) );
        } else {
            if ( ( leftParen < 0 && rightParen >= 0 )
                    || ( leftParen > rightParen ) ) {
                throw new QueryException( "Syntax error: parenthesis" );
            }
            sql.append( oql.substring( 5, leftParen ) );
            sql.append( '(' );
            for ( int i = leftParen + 1; i < rightParen; i++ ) {
                if ( oql.charAt( i ) == '$' ) {
                    // get parameter number if given
                    sb = new StringBuffer();
                    for ( int j = i + 1; j < rightParen; j++ ) {
                        char c;

                        c = oql.charAt( j );
                        if ( c < '0' || c > '9' ) {
                            break;
                        }
                        sb.append( c );
                    }
                    if ( sb.length() > 0 ) {
                        paramNo = Integer.valueOf( sb.toString() );
                    } else {
                        paramNo = new Integer( paramCnt + 1 );
                    }
                    info = (ParamInfo) _paramInfo.get( paramNo );
                    if ( info == null ) {
                        info = new ParamInfo( "", "java.lang.Object", null);
                    }
                    info.mapToSQLParam( paramCnt + 1 );
                    _paramInfo.put( paramNo , info );
                    paramCnt++;
                }
            }
            for ( int i = 0; i < paramCnt; i++ ) {
View Full Code Here

TOP

Related Classes of org.exolab.castor.jdo.oql.ParamInfo

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.