Exemple #1
0
        /*
         * Validate the column specifications by checking against the tables.
         */
        public void validateColumns() //throws TinySQLException
        {
            String       columnName, columnAlias, columnContext;
            TsColumn     columnObject;
            bool         selectStar;
            TinySQLTable jtbl;
            int          i, j;

/*
 *    Check for a column named *
 */
            selectStar = false;
            for (i = 0; i < columnList.size(); i++)
            {
                columnName    = (String)columnList.elementAt(i);
                columnContext = (String)contextList.elementAt(i);
                if (columnName.equals("*"))
                {
                    if (!columnContext.equals("SELECT"))
                    {
                        throw new TinySQLException("* must be a SELECT column.");
                    }
                    selectStar = true;
                    break;
                }
            }
            if (selectStar)
            {
/*
 *       A column * has been found.  Delete the existing list of SELECT
 *       columns and replace by using an enumeration variable to cycle through
 *       the columns in the tables Hashtable.
 */
                for (i = 0; i < columnList.size(); i++)
                {
                    columnContext = (String)contextList.elementAt(i);
                    if (columnContext.equals("SELECT"))
                    {
                        columnList.removeElementAt(i);
                        contextList.removeElementAt(i);
                        columnAliasList.removeElementAt(i);
                    }
                }
                for (i = 0; i < tableList.size(); i++)
                {
                    jtbl = (TinySQLTable)tables.get((String)tableList.elementAt(i));

/*
 *          Expand to all columns.
 */
                    for (j = 0; j < jtbl.columnNameKeys.size(); j++)
                    {
                        columnName = (String)jtbl.columnNameKeys.elementAt(j);
                        columnList.addElement(jtbl.table + "->" + jtbl.tableAlias
                                              + "." + columnName);
                        columnAliasList.addElement(columnName);
                        contextList.addElement("SELECT");
                    }
                }
            }

/*
 *    Build a column object for each selected column.
 */
            if (tables == (java.util.Hashtable <Object, Object>)null)
            {
                java.lang.SystemJ.outJ.println("*****Column validation - no tables defined.");
            }
            for (i = 0; i < columnList.size(); i++)
            {
                columnName    = (String)columnList.elementAt(i);
                columnContext = (String)contextList.elementAt(i);
                columnAlias   = (String)null;
                if (i < columnAliasList.size())
                {
                    columnAlias = (String)columnAliasList.elementAt(i);
                }
                columnObject       = new TsColumn(columnName, tables, columnContext);
                columnObject.alias = UtilString.removeQuotes(columnAlias);
                columns.addElement(columnObject);
            }
        }
Exemple #2
0
        /**
         *
         * Given a tsColumn object, returns its value as a String.
         *
         * @param column the tsColumn object
         *
         */
        public String columnAsString(TsColumn column)
        {
            String outputString, valueString, functionName, argList, nextArg;

            java.lang.StringBuffer functionBuffer;
            FieldTokenizer         ft1, ft2;

            /*
             *    Next try to retrieve as a group function, which will not have a
             *    tablename prefix.
             */
            outputString = (String)get(column.name);
            if (outputString != (String)null)
            {
                return(outputString);
            }
            ft1 = new FieldTokenizer(column.name, '(', false);
            if (ft1.countFields() == 2)
            {
                /*
                 *       The column is a function.  If it is a group function, the value
                 *       will be stored in the record.  Otherwise, the function value
                 *       will be determined here by retrieving and processing all the
                 *       columns in the function arguments.
                 */
                outputString = (String)get(column.name);
                if (outputString != (String)null)
                {
                    return(outputString);
                }
                functionName   = ft1.getField(0);
                argList        = ft1.getField(1);
                ft2            = new FieldTokenizer(argList, ',', false);
                functionBuffer = new java.lang.StringBuffer();

                /*
                 *       Function arguments must consist of table.column names or constants.
                 */
                while (ft2.hasMoreFields())
                {
                    nextArg     = ft2.nextField();
                    valueString = (String)get(nextArg);
                    if (debug)
                    {
                        java.lang.SystemJ.outJ.println("Function " + functionName
                                                       + " argument " + nextArg + " has value " + valueString);
                    }

                    /*
                     *          If the valueString is null then it is a constant rather than
                     *          a database column.  Remove enclosing quotes.
                     */
                    if (valueString == (String)null)
                    {
                        valueString = UtilString.removeQuotes(nextArg);
                    }
                    else
                    {
                        valueString = valueString.trim();
                    }
                    if (functionName.equals("CONCAT"))
                    {
                        functionBuffer.append(valueString);
                    }
                }
                outputString = functionBuffer.toString();
            }
            else if (column.tableName == (String)null)
            {
                /*
                 *       This is a constant.  Return the table name which will be equal to
                 *       the constant value.
                 */
                outputString = UtilString.removeQuotes(column.name);
            }
            else
            {
                /*
                 *       Retrieve as a simple database column.
                 */
                outputString = (String)get(column.tableName + "." + column.name);
                if (Utils.isDateColumn(column.type))
                {
                    /*
                     *          Format dates as DD-MON-YY for output.  Note that the value
                     *          stored in the database may already be in this format because
                     *          of incorrect storage of date strings prior to version 2.3.
                     */
                    try
                    {
                        outputString = UtilString.toStandardDate(outputString);
                    }
                    catch (Exception dateEx)
                    {
                        java.lang.SystemJ.outJ.println(dateEx.getMessage());
                    }
                }
            }
            return(outputString);
        }
Exemple #3
0
        /*
         * This method sets up particular phrase elements for the SQL command.
         * Examples would be a list of selected columns and tables for a SELECT
         * statement, or a list of column definitions for a CREATE TABLE
         * statement.  These phrase elements will be added to the action list
         * once the entire statement has been parsed.
         */
        public void setPhrase(String inputKeyWord, String inputString)
        //throws TinySQLException
        {
            String nextField, upperField,
                   fieldString, tempString, columnName, columnAlias;

            java.lang.StringBuffer concatBuffer;
            FieldTokenizer         ft1, ft2, ft3;
            TsColumn createColumn;

/*
 *    Handle compound keywords.
 */
            if (inputString == (String)null)
            {
                lastKeyWord = inputKeyWord;
                return;
            }
            else if (inputString.trim().length() == 0)
            {
                lastKeyWord = inputKeyWord;
                return;
            }
            if (TinySQLGlobals.PARSER_DEBUG)
            {
                java.lang.SystemJ.outJ.println("setPhrase " + inputString);
            }
            ft1 = new FieldTokenizer(inputString, ',', false);
            while (ft1.hasMoreFields())
            {
                nextField = ft1.nextField().trim();
                if (TinySQLGlobals.PARSER_DEBUG)
                {
                    java.lang.SystemJ.outJ.println(inputKeyWord + " field is " + nextField);
                }
                upperField = nextField.toUpperCase();
                if (inputKeyWord.equals("SELECT"))
                {
/*
 *          Check for the keyword DISTINCT
 */
                    if (nextField.toUpperCase().startsWith("DISTINCT"))
                    {
                        distinct  = true;
                        nextField = nextField.substring(9).trim();
                    }

/*
 *          Check for and set column alias.
 */
                    ft2         = new FieldTokenizer(nextField, ' ', false);
                    columnName  = ft2.getField(0);
                    columnAlias = (String)null;

/*
 *          A column alias can be preceded by the keyword AS which will
 *          be ignored by tinySQL.
 */
                    if (ft2.countFields() == 2)
                    {
                        columnAlias = ft2.getField(1);
                    }
                    else if (ft2.countFields() == 3)
                    {
                        columnAlias = ft2.getField(2);
                    }

/*
 *          Check for column concatenation using the | symbol
 */
                    ft2 = new FieldTokenizer(columnName, '|', false);
                    if (ft2.countFields() > 1)
                    {
                        concatBuffer = new java.lang.StringBuffer("CONCAT(");
                        while (ft2.hasMoreFields())
                        {
                            if (concatBuffer.length() > 7)
                            {
                                concatBuffer.append(",");
                            }
                            concatBuffer.append(ft2.nextField());
                        }
                        columnName = concatBuffer.toString() + ")";
                    }
                    columnList.addElement(columnName);
                    columnAliasList.addElement(columnAlias);
                    contextList.addElement(inputKeyWord);
                }
                else if (inputKeyWord.equals("TABLE"))
                {
/*
 *          If the input keyword is TABLE, update the statement type to be a
 *          compound type such as CREATE_TABLE, DROP_TABLE, or ALTER_TABLE.
 */
                    if (!statementType.equals("INSERT"))
                    {
                        statementType = statementType + "_TABLE";
                    }
                    if (statementType.equals("CREATE_TABLE"))
                    {
/*
 *             Parse out the column definition.
 */
                        ft2 = new FieldTokenizer(nextField, '(', false);
                        if (ft2.countFields() != 2)
                        {
                            throwException(1);
                        }
                        tableName   = ft2.getField(0);
                        fieldString = ft2.getField(1);
                        ft2         = new FieldTokenizer(fieldString, ',', false);
                        while (ft2.hasMoreFields())
                        {
                            tempString   = ft2.nextField();
                            createColumn = parseColumnDefn(tempString);
                            if (createColumn != (TsColumn)null)
                            {
                                columnList.addElement(createColumn);
                            }
                        }
                    }
                    else if (statementType.equals("DROP_TABLE"))
                    {
/*
 *             Handle dropping of non-existent tables
 */
                        tableName = upperField;
                        try
                        {
                            validateTable(upperField, true);
                        } catch (Exception) {
                            throw new TinySQLException("Table " + tableName
                                                       + " does not exist.");
                        }
                    }
                    else
                    {
                        tableName = upperField;
                        validateTable(upperField, true);
                    }
                }
                else if (inputKeyWord.equals("BY"))
                {
/*
 *          Set up Group by and Order by columns.
 */
                    if (lastKeyWord == (String)null)
                    {
                        throwException(6);
                    }
                    else
                    {
                        ft3 = new FieldTokenizer(upperField, ' ', false);
                        columnList.addElement(ft3.getField(0));
                        if (ft3.countFields() == 2)
                        {
/*
 *                ASC or DESC are the only allowable directives after GROUP BY
 */
                            if (ft3.getField(1).startsWith("ASC") |
                                ft3.getField(1).startsWith("DESC"))
                            {
                                orderType = ft3.getField(1);
                            }
                            else
                            {
                                throwException(7);
                            }
                        }
                        if (lastKeyWord.equals("ORDER"))
                        {
                            defaultOrderBy = false;
                        }
                        contextList.addElement(lastKeyWord);
                    }
                }
                else if (inputKeyWord.equals("DROP"))
                {
/*
 *          Parse list of columns to be dropped.
 */
                    statementType = "ALTER_DROP";
                    ft2           = new FieldTokenizer(upperField, ' ', false);
                    while (ft2.hasMoreFields())
                    {
                        columnList.addElement(UtilString.removeQuotes(ft2.nextField()));
                    }
                }
                else if (inputKeyWord.equals("RENAME"))
                {
/*
 *          Parse old and new column name.
 */
                    statementType = "ALTER_RENAME";
                    ft2           = new FieldTokenizer(upperField, ' ', false);
                    oldColumnName = ft2.getField(0);
                    newColumnName = ft2.getField(1);
                    if (newColumnName.equals("TO") & ft2.countFields() == 3)
                    {
                        newColumnName = ft2.getField(2);
                    }
                    if (newColumnName.length() > 11)
                    {
                        newColumnName = TinySQLGlobals.getShortName(newColumnName);
                    }
                }
                else if (inputKeyWord.equals("ADD"))
                {
/*
 *          Parse definition of columns to be added.
 */
                    statementType = "ALTER_ADD";
                    createColumn  = parseColumnDefn(nextField);
                    if (createColumn != (TsColumn)null)
                    {
                        columnList.addElement(createColumn);
                    }
                }
                else if (inputKeyWord.equals("FROM"))
                {
/*
 *          Check for valid table
 */
                    tableName = upperField;
                    validateTable(tableName);
                }
                else if (inputKeyWord.equals("INTO"))
                {
                    ft2 = new FieldTokenizer(nextField, '(', false);
                    if (ft2.countFields() != 2)
                    {
                        throwException(3);
                    }
                    tableName = ft2.getField(0).toUpperCase();
                    validateTable(tableName);
                    fieldString = ft2.getField(1).toUpperCase();
                    ft2         = new FieldTokenizer(fieldString, ',', false);
                    while (ft2.hasMoreFields())
                    {
                        tempString = UtilString.removeQuotes(ft2.nextField());
                        columnList.addElement(tempString);
                        contextList.addElement(inputKeyWord);
                    }
                }
                else if (inputKeyWord.equals("VALUES"))
                {
                    ft2         = new FieldTokenizer(nextField, '(', false);
                    fieldString = ft2.getField(0);
                    ft2         = new FieldTokenizer(fieldString, ',', false);
                    while (ft2.hasMoreFields())
                    {
                        tempString = UtilString.removeQuotes(ft2.nextField());
                        tempString = UtilString.replaceAll(tempString, "''", "'");
                        valueList.addElement(tempString);
                    }
                }
                else if (inputKeyWord.equals("UPDATE"))
                {
                    tableName = nextField.toUpperCase();
                    validateTable(tableName);
                }
                else if (inputKeyWord.equals("SET"))
                {
/*
 *          Parse the update column name/value pairs
 */
                    ft2 = new FieldTokenizer(nextField, '=', false);
                    if (ft2.countFields() != 2)
                    {
                        throwException(4);
                    }
                    columnList.addElement(ft2.getField(0));
                    contextList.addElement(inputKeyWord);
                    valueList.addElement(UtilString.removeQuotes(ft2.getField(1)));
                }
                else if (inputKeyWord.equals("WHERE"))
                {
                    whereClause = new TinySQLWhere(nextField, tables);
                }
                else if (!inputKeyWord.equals("TABLE"))
                {
                    throwException(10);
                }
            }
            lastKeyWord = inputKeyWord;
        }
Exemple #4
0
        /*
         * Create the tinySQLTable object, then insert a row, and update
         * it with the c and v Vectors
         */
        private void InsertStatement(String statementType, String tableName,
                                     java.util.Vector <Object> c, java.util.Vector <Object> v)
        //throws TinySQLException
        {
            String columnName, valueString;
            int    i, columnType, columnSize;
            double value;

            insertTable = getTable(tableName);

            /*
             *    Check that the values supplied are the correct type and length.
             */
            for (i = 0; i < c.size(); i++)
            {
                columnName  = (String)c.elementAt(i);
                valueString = (String)v.elementAt(i);
                if (valueString == (String)null)
                {
                    continue;
                }
                valueString = UtilString.removeQuotes(valueString);
                valueString = UtilString.replaceAll(valueString, "''", "'");
                columnType  = insertTable.ColType(columnName);
                if (Utils.isNumberColumn(columnType))
                {
                    try
                    {
                        value = java.lang.Double.parseDouble(valueString);
                    }
                    catch (Exception)
                    {
                        throw new TinySQLException("Insert failed: column "
                                                   + columnName + " is numeric - found " + valueString);
                    }
                }
                else if (Utils.isDateColumn(columnType))
                {
                    try
                    {
                        /*
                         *             Modify the input to be the standard YYYYMMDD format
                         */
                        if (valueString.trim().length() > 0)
                        {
                            v.setElementAt(UtilString.dateValue(valueString), i);
                        }
                    }
                    catch (Exception e)
                    {
                        throw new TinySQLException("Insert failed: " + e.getMessage());
                    }
                }
                columnSize = insertTable.ColSize(columnName);
                if (valueString.length() > columnSize)
                {
                    throw new TinySQLException("Insert failed: string too long for "
                                               + " column " + columnName + " "
                                               + java.lang.Integer.toString(valueString.length())
                                               + " > " + java.lang.Integer.toString(columnSize) + "<" + valueString + ">");
                }
            }
            insertTable.InsertRow(c, v);

            /*
             *    Close the table file that has just been updated unless this is a
             *    PreparedStatement.  In that case an explicit close must be done
             *    on the statement object to close any open files.
             */
            if (!statementType.endsWith("tinySQLPreparedStatement"))
            {
                insertTable.close();
            }
        }
Exemple #5
0
        /*
         *
         * Read SQL Statements from the SQLStream, and
         * return a result set for the last SQL Statement
         * executed.
         *
         * @returns the ResultSet or null, if no result set was created
         * @exception TinySQLException
         *
         */
        protected virtual TsResultSet sql(Object s) //throws TinySQLException
        {
            /*
             *    Build the ResultSet
             */
            TsResultSet              rs = null;
            TinySQLTable             jtbl;
            TinySQLPreparedStatement pstmt = (TinySQLPreparedStatement)null;
            bool distinct = false;

            java.util.Vector <Object> actions, columns, columnDefs, values;
            String actionType, orderType, tableName, statementType, byteString;

            java.util.Hashtable <Object, Object> h, selectTables;
            byte[] bStream;
            java.io.ByteArrayInputStream st;
            int    i;
            String actionString;

            groupBreak    = false;
            statementType = s.getClass().getName();
            try
            {
                /*
                 *       Instantiate a new parser object which reads from the SQLStream.  This
                 *       should probably be changed to a String at some point.  Note that
                 *       parsing is only done once for a PreparedStatement.
                 */
                actions = (java.util.Vector <Object>)null;
                if (statementType.endsWith("tinySQLPreparedStatement"))
                {
                    pstmt = (TinySQLPreparedStatement)s;
                    pstmt.updateActions(actions);
                    actions    = pstmt.getActions();
                    byteString = pstmt.getSQLString();
                    bStream    = (byte[])null;
                    if (pstmt.getSQLString() != (String)null)
                    {
                        bStream = pstmt.getSQLString().getBytes();
                    }
                }
                else if (statementType.endsWith("tinySQLStatement"))
                {
                    bStream = ((TinySQLStatement)s).getSQLString().getBytes();
                }
                else
                {
                    throw new TinySQLException("Unknown statement type"
                                               + statementType);
                }
                if (actions == (java.util.Vector <Object>)null)
                {
                    st        = new java.io.ByteArrayInputStream(bStream);
                    SQLStream = (java.io.InputStream)st;
                    TinySQLParser tinyp = new TinySQLParser(SQLStream, this);
                    TinySQLGlobals.writeLongNames();
                    actions = tinyp.getActions();
                    if (statementType.endsWith("tinySQLPreparedStatement"))
                    {
                        pstmt.updateActions(actions);
                    }
                }

                /*
                 *       The actions Vector consists of a list of Hashtables.  Each of these
                 *       action Hashtables contains elements required for a particular SQL
                 *       statement. The following elements are used for various actions;
                 *
                 *       Type          Name           Description
                 *
                 *       String        tableName      The name of the affected table for
                 *                                    CREATE,INSERT,UPDATE,DELETE actions.
                 *
                 *       Hashtable     selectTables   Hashtable of tables in a SELECT action.
                 *
                 *       Vector        columns        A list of column names used by the
                 *                                    the action.
                 *
                 *       Vector        columnContexts A list of Strings indicating the context
                 *                                    for the elements in the columns Vector.
                 *                                    Values can be SELECT,ORDER,GROUP.
                 *
                 *       Vector        columnDefs     A list of column objects used by the
                 *                                    CREATE TABLE and ALTER TABLE ADD actions.
                 *
                 *       Vector        values         A list of String values used in INSERT
                 *                                    and UPDATE actions.
                 *
                 *       String        oldColumnName  Old column name for the
                 *                                    ALTER TABLE RENAME action.
                 *
                 *       String        newColumnName  New column name for the
                 *                                    ALTER TABLE RENAME action.
                 *
                 *       String        orderType      Type or ORDER BY - ASC or DESC.
                 *
                 *       String        distinct       Distinct rows only - TRUE or NULL
                 *
                 *       tinySQLWhere  whereClause    An object containing the where clause
                 *                                    which can be updated and queried.
                 */
                for (i = 0; i < actions.size(); i++)
                {
                    h          = (java.util.Hashtable <Object, Object>)actions.elementAt(i);
                    actionType = (String)h.get("TYPE");
                    if (TinySQLGlobals.DEBUG)
                    {
                        java.lang.SystemJ.outJ.println("Action: " + actionType);
                    }

                    /*
                     *          Many actions have a table specification.  If this one, build
                     *          a table object to update the where clause if there is one.
                     */
                    tableName = (String)h.get("TABLE");
                    wc        = (TinySQLWhere)h.get("WHERE");
                    if (tableName != (String)null & !actionType.equals("DROP_TABLE") &
                        !actionType.equals("CREATE_TABLE") & !actionType.equals("INSERT")
                        & !actionType.startsWith("ALTER"))
                    {
                        jtbl = getTable(tableName);

                        /*
                         *             For prepared statements, store any table objects that
                         *             are created so that they can be explicitly closed when
                         *             the statement processing is complete.
                         */
                        if (statementType.endsWith("tinySQLPreparedStatement"))
                        {
                            pstmt.addTable(jtbl);
                        }
                    }
                    actionString = UtilString.actionToString(h);
                    if (debug)
                    {
                        java.lang.SystemJ.outJ.println("ACTION: " + actionString);
                    }
                    if (actionType.equals("UPDATE"))
                    {
                        /*
                         *             SQL UPDATE
                         */
                        columns = (java.util.Vector <Object>)h.get("COLUMNS");
                        values  = (java.util.Vector <Object>)h.get("VALUES");
                        UpdateStatement(tableName, columns, values, wc);
                    }
                    else if (actionType.equals("DELETE"))
                    {
                        /*
                         *             SQL DELETE
                         */
                        DeleteStatement(tableName, wc);
                    }
                    else if (actionType.equals("SELECT"))
                    {
                        /*
                         *             SQL SELECT
                         */
                        selectTables = (java.util.Hashtable <Object, Object>)h.get("TABLES");
                        columns      = (java.util.Vector <Object>)h.get("COLUMNS");
                        orderType    = (String)h.get("ORDER_TYPE");
                        if ((String)h.get("DISTINCT") != (String)null)
                        {
                            distinct = true;
                        }
                        rs = SelectStatement(selectTables, columns,
                                             wc, orderType, distinct, s);
                    }
                    else if (actionType.equals("INSERT"))
                    {
                        /*
                         *             SQL INSERT
                         */
                        columns = (java.util.Vector <Object>)h.get("COLUMNS");
                        values  = (java.util.Vector <Object>)h.get("VALUES");
                        InsertStatement(statementType, tableName, columns, values);
                    }
                    else if (actionType.equals("CREATE_TABLE"))
                    {
                        /*
                         *             SQL CREATE TABLE
                         *
                         *             CREATE TABLE User(user_oid  NUMBER(8)    NOT NULL,
                         *                               userType  VARCHAR(80)  DEFAULT '-' NOT NULL,
                         *                               PRIMARY KEY (user_oid))
                         *
                         *             -> DEFAULT / NOT NULL / PRIMARY KEY is not supported
                         *
                         */
                        columnDefs = (java.util.Vector <Object>)h.get("COLUMN_DEF");
                        CreateTable(tableName, columnDefs);
                    }
                    else if (actionType.equals("ALTER_ADD"))
                    {
                        /*
                         *             SQL ALTER TABLE ADD
                         */
                        columnDefs = (java.util.Vector <Object>)h.get("COLUMN_DEF");
                        AlterTableAddCol(tableName, columnDefs);
                    }
                    else if (actionType.equals("ALTER_DROP"))
                    {
                        /*
                         *             SQL ALTER TABLE DROP
                         */
                        columns = (java.util.Vector <Object>)h.get("COLUMNS");
                        AlterTableDropCol(tableName, columns);
                    }
                    else if (actionType.equals("ALTER_RENAME"))
                    {
                        /*
                         *             SQL ALTER TABLE RENAME
                         */
                        String oldColumnName = (String)h.get("OLD_COLUMN");
                        String newColumnName = (String)h.get("NEW_COLUMN");
                        AlterTableRenameCol(tableName, oldColumnName, newColumnName);
                    }
                    else if (actionType.equals("DROP_TABLE"))
                    {
                        /*
                         *             SQL DROP TABLE
                         */
                        DropTable(tableName);
                    }
                    else
                    {
                        java.lang.SystemJ.outJ.println("Unrecognized action " + actionType);
                    }
                }
            }
            catch (java.lang.Throwable e)
            {
                if (TinySQLGlobals.EX_DEBUG)
                {
                    e.printStackTrace(java.lang.SystemJ.outJ);
                }
                throw new TinySQLException(e.getMessage());
            }
            return(rs);
        }
Exemple #6
0
        public int compareTo(Object inputObj) //throws tinySQLException
        {
            String   thisYMD, inputYMD;
            TsColumn inputColumn;
            int      inputType, returnValue;
            double   thisValue, inputValue;

            inputColumn = (TsColumn)inputObj;
            inputType   = inputColumn.type;
            thisValue   = java.lang.Double.MIN_VALUE;
            inputValue  = java.lang.Double.MIN_VALUE;
            returnValue = 0;
            if (Utils.isCharColumn(type))
            {
                /*
                 *       Compare character java.sql.Types.
                 */
                if (!Utils.isCharColumn(inputType))
                {
                    throw new TinySQLException("Type mismatch between "
                                               + getString() + " and " + inputColumn.getString());
                }
                else if (stringValue == (String)null |
                         inputColumn.stringValue == (String)null)
                {
                    throw new TinySQLException("One of the values is NULL");
                }
                else
                {
                    returnValue = stringValue.compareTo(inputColumn.stringValue);
                }
            }
            else if (Utils.isDateColumn(type))
            {
                /*
                 *       Compare date java.sql.Types.
                 */
                if (!Utils.isDateColumn(inputType))
                {
                    throw new TinySQLException("Type mismatch between "
                                               + getString() + " and " + inputColumn.getString());
                }
                else if (stringValue == (String)null |
                         inputColumn.stringValue == (String)null)
                {
                    throw new TinySQLException("One of the values is NULL");
                }
                else
                {
                    inputYMD    = UtilString.toStandardDate(inputColumn.stringValue);
                    thisYMD     = UtilString.toStandardDate(stringValue);
                    returnValue = thisYMD.compareTo(inputYMD);
                }
            }
            else if (Utils.isNumberColumn(type))
            {
                if (type == java.sql.Types.INTEGER)
                {
                    thisValue = (double)intValue;
                }
                else if (type == java.sql.Types.FLOAT)
                {
                    thisValue = (double)floatValue;
                }
                if (inputType == java.sql.Types.INTEGER)
                {
                    inputValue = (double)inputColumn.intValue;
                }
                else if (inputType == java.sql.Types.FLOAT)
                {
                    inputValue = (double)inputColumn.floatValue;
                }
                if (thisValue > inputValue)
                {
                    returnValue = 1;
                }
                else if (thisValue < inputValue)
                {
                    returnValue = -1;
                }
            }
            else
            {
                java.lang.SystemJ.outJ.println("Cannot sort unknown type");
            }
            if (TinySQLGlobals.DEBUG)
            {
                java.lang.SystemJ.outJ.println("Comparing " + getString() + " to " +
                                               inputColumn.getString() + " gave " + returnValue);
            }
            return(returnValue);
        }
Exemple #7
0
        internal TsColumn(String s, java.util.Hashtable <Object, Object> tableDefs, String inputContext)
        //throws tinySQLException
        {
            FieldTokenizer ft, ftArgs;
            int            j, numericType, nameLength, dotAt, argIndex;
            String         upperName, checkName, nextArg;
            TinySQLTable   jtbl;
            TsColumn       tcol;

            java.util.Vector <Object>      t;
            java.util.Enumeration <Object> col_keys;
            name        = s;
            longName    = name;
            nameLength  = name.length();
            contextList = new java.util.Vector <Object>();
            contextList.addElement(inputContext);
            ft = new FieldTokenizer(name, '(', false);
            if (ft.countFields() == 2)
            {
                /*
                 *       This is a function rather than a simple column or constant
                 */
                functionName = ft.getField(0).toUpperCase();
                if (functionName.equals("COUNT"))
                {
                    type          = java.sql.Types.INTEGER;
                    size          = 10;
                    intValue      = java.lang.Integer.MIN_VALUE;
                    groupedColumn = true;
                }
                else if (functionName.equals("SUM"))
                {
                    type          = java.sql.Types.FLOAT;
                    size          = 10;
                    groupedColumn = true;
                }
                else if (functionName.equals("TO_DATE"))
                {
                    type = java.sql.Types.DATE;
                    size = 10;
                }
                else if (functionName.equals("CONCAT") |
                         functionName.equals("UPPER") |
                         functionName.equals("SUBSTR") |
                         functionName.equals("TRIM"))
                {
                    type = java.sql.Types.CHAR;
                }
                functionArgString = ft.getField(1);
                ftArgs            = new FieldTokenizer(functionArgString, ',', false);
                functionArgs      = new java.util.Vector <Object>();
                argIndex          = 0;
                while (ftArgs.hasMoreFields())
                {
                    nextArg = ftArgs.nextField();
                    tcol    = new TsColumn(nextArg, tableDefs, inputContext);
                    if (tcol.isGroupedColumn())
                    {
                        groupedColumn = true;
                    }

                    /*
                     *          MAX and MIN functions can be either FLOAT or CHAR types
                     *          depending upon the type of the argument.
                     */
                    if (functionName.equals("MAX") | functionName.equals("MIN"))
                    {
                        if (argIndex > 0)
                        {
                            throw new TinySQLException("Function can only have 1 argument");
                        }
                        groupedColumn = true;
                        type          = tcol.type;
                        size          = tcol.size;
                    }
                    else if (functionName.equals("CONCAT"))
                    {
                        type  = java.sql.Types.CHAR;
                        size += tcol.size;
                    }
                    else if (functionName.equals("UPPER"))
                    {
                        type = java.sql.Types.CHAR;
                        size = tcol.size;
                    }
                    else if (functionName.equals("TO_DATE"))
                    {
                        type = java.sql.Types.DATE;
                        size = 10;
                    }
                    else if (functionName.equals("TRIM"))
                    {
                        type = java.sql.Types.CHAR;
                        size = tcol.size;
                    }
                    else if (functionName.equals("SUBSTR"))
                    {
                        type = java.sql.Types.CHAR;
                        if (argIndex == 0 & tcol.type != java.sql.Types.CHAR)
                        {
                            throw new TinySQLException("SUBSTR first argument must be character");
                        }
                        else if (argIndex == 1)
                        {
                            if (tcol.type != java.sql.Types.INTEGER | tcol.intValue < 1)
                            {
                                throw new TinySQLException("SUBSTR second argument "
                                                           + tcol.getString() + " must be integer > 0");
                            }
                        }
                        else if (argIndex == 2)
                        {
                            if (tcol.type != java.sql.Types.INTEGER | tcol.intValue < 1)
                            {
                                throw new TinySQLException("SUBSTR third argument "
                                                           + tcol.getString() + " must be integer > 0");
                            }
                            size = tcol.intValue;
                        }
                    }
                    argIndex++;
                    functionArgs.addElement(tcol);
                }
            }
            else
            {
                /*
                 *       Check for SYSDATE
                 */
                if (name.toUpperCase().equals("SYSDATE"))
                {
                    isConstant = true;
                    type       = java.sql.Types.DATE;
                    size       = 10;
                    notNull    = true;
                    valueSet   = true;
                    //Basties mote: not really SimpleDateFormat needed... - need yyyy-MM-dd
                    // stringValue = fmtyyyyMMdd.format(today.getTime());
                    stringValue = System.DateTime.Today.ToString("yyyy-MM-dd");

                    /*
                     *          Check for a quoted string
                     */
                }
                else if (UtilString.isQuotedString(name))
                {
                    isConstant  = true;
                    type        = java.sql.Types.CHAR;
                    stringValue = UtilString.removeQuotes(name);
                    if (stringValue != (String)null)
                    {
                        size     = stringValue.length();
                        notNull  = true;
                        valueSet = true;
                    }
                }
                else
                {
                    /*
                     *          Check for a numeric constant
                     */
                    numericType = UtilString.getValueType(name);
                    if (numericType == java.sql.Types.INTEGER)
                    {
                        intValue   = java.lang.Integer.valueOf(name).intValue();
                        size       = 10;
                        type       = numericType;
                        isConstant = true;
                        notNull    = true;
                        valueSet   = true;
                    }
                    else if (numericType == java.sql.Types.FLOAT)
                    {
                        floatValue = java.lang.Float.valueOf(name).floatValue();
                        size       = 10;
                        type       = numericType;
                        isConstant = true;
                        notNull    = true;
                        valueSet   = true;
                    }
                    else
                    {
                        /*
                         *             This should be a column name.
                         */
                        columnTable = (TinySQLTable)null;
                        upperName   = name.toUpperCase();
                        if (TinySQLGlobals.DEBUG)
                        {
                            java.lang.SystemJ.outJ.println("Trying to find table for " + upperName);
                        }
                        dotAt = upperName.indexOf(".");
                        if (dotAt > -1)
                        {
                            tableName = upperName.substring(0, dotAt);
                            if (tableDefs != (java.util.Hashtable <Object, Object>)null &
                                tableName.indexOf("->") < 0)
                            {
                                t         = (java.util.Vector <Object>)tableDefs.get("TABLE_SELECT_ORDER");
                                tableName = UtilString.findTableAlias(tableName, t);
                            }
                            upperName = upperName.substring(dotAt + 1);

                            /*
                             *                Check to see if this column name has a short equivalent.
                             */
                            if (upperName.length() > 11)
                            {
                                longName  = name;
                                upperName = TinySQLGlobals.getShortName(upperName);
                            }
                            columnTable = (TinySQLTable)tableDefs.get(tableName);
                        }
                        else if (tableDefs != (java.util.Hashtable <Object, Object>)null)
                        {
                            /*
                             *                Check to see if this column name has a short equivalent.
                             */
                            if (upperName.length() > 11)
                            {
                                longName  = name;
                                upperName = TinySQLGlobals.getShortName(upperName);
                            }

                            /*
                             *                Use an enumeration to go through all of the tables to find
                             *                this column.
                             */
                            t = (java.util.Vector <Object>)tableDefs.get("TABLE_SELECT_ORDER");
                            for (j = 0; j < t.size(); j++)
                            {
                                tableName = (String)t.elementAt(j);
                                jtbl      = (TinySQLTable)tableDefs.get(tableName);
                                col_keys  = jtbl.column_info.keys();

                                /*
                                 *                   Check all columns.
                                 */
                                while (col_keys.hasMoreElements())
                                {
                                    checkName = (String)col_keys.nextElement();
                                    if (checkName.equals(upperName))
                                    {
                                        upperName   = checkName;
                                        columnTable = jtbl;
                                        break;
                                    }
                                }
                                if (columnTable != (TinySQLTable)null)
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            if (TinySQLGlobals.DEBUG)
                            {
                                java.lang.SystemJ.outJ.println("No table definitions.");
                            }
                        }
                        if (columnTable != (TinySQLTable)null)
                        {
                            name = columnTable.table + "->" + columnTable.tableAlias
                                   + "." + upperName;
                            type          = columnTable.ColType(upperName);
                            size          = columnTable.ColSize(upperName);
                            decimalPlaces = columnTable.ColDec(upperName);
                            tableName     = columnTable.table + "->" + columnTable.tableAlias;
                        }
                    }
                }
            }
        }