Esempio n. 1
0
        /*
         * Given a column name, and a Hashtable containing tables, determine
         * which table "owns" a given column.
         */
        private TinySQLTable getTableForColumn(java.util.Hashtable <Object, Object> tables, String inputColumn)
        {
            TinySQLTable tbl;

            java.util.Vector <Object>            tableNames;
            java.util.Hashtable <Object, Object> columnInfo;
            String findColumn, tableAndAlias = (String)null, tableAlias;
            int    i, dotAt;

            findColumn = inputColumn.toUpperCase();
            dotAt      = findColumn.indexOf(".");
            tableNames = (java.util.Vector <Object>)tables.get("TABLE_SELECT_ORDER");
            if (dotAt > -1)
            {
                tableAlias = findColumn.substring(0, dotAt);
                try
                {
                    tableAndAlias = UtilString.findTableAlias(tableAlias, tableNames);
                }
                catch (Exception)
                {
                }
                if (tableAndAlias != (String)null)
                {
                    tbl = (TinySQLTable)tables.get(tableAndAlias);
                    if (tbl != (TinySQLTable)null)
                    {
                        return(tbl);
                    }
                }
            }
            else
            {
                for (i = 0; i < tableNames.size(); i++)
                {
                    tbl = (TinySQLTable)tables.get((String)tableNames.elementAt(i));

                    /*
                     *          Get the Hashtable containing column information, and see if it
                     *          contains the column we're looking for.
                     */
                    columnInfo = tbl.column_info;
                    if (columnInfo != (java.util.Hashtable <Object, Object>)null)
                    {
                        if (columnInfo.containsKey(findColumn))
                        {
                            return(tbl);
                        }
                    }
                }
            }
            return((TinySQLTable)null);
        }
Esempio n. 2
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;
                        }
                    }
                }
            }
        }