Esempio n. 1
0
        public TinySQLParser(java.io.InputStream sqlInput, TinySQL inputEngine)
        //throws TinySQLException
        {
            java.io.StreamTokenizer st;
            FieldTokenizer          ft;

            java.io.Reader r;
            String         nextToken, upperField, nextField, keyWord = (String)null;

            java.lang.StringBuffer cmdBuffer, inputSQLBuffer;
            int lastIndex, keyIndex;

            r               = new java.io.BufferedReader(new java.io.InputStreamReader(sqlInput));
            dbEngine        = inputEngine;
            actionList      = new java.util.Vector <Object>();
            columnList      = new java.util.Vector <Object>();
            columns         = new java.util.Vector <Object>();
            columnAliasList = new java.util.Vector <Object>();
            contextList     = new java.util.Vector <Object>();
            valueList       = new java.util.Vector <Object>();
            tableName       = (String)null;
            whereClause     = (TinySQLWhere)null;

/*
 *    The tableList is a list of table names, in the optimal order
 *    in which they should be scanned for the SELECT phrase.
 *    The java.util.Hashtable<Object,Object> tables contains table objects keyed by table
 *    alias and name.
 */
            tableList = new java.util.Vector <Object>();
            tables    = new java.util.Hashtable <Object, Object>();
            tables.put("TABLE_SELECT_ORDER", tableList);
            try
            {
                st = new java.io.StreamTokenizer(r);
                st.eolIsSignificant(false);
                st.wordChars('\'', '}');
                st.wordChars('?', '?');
                st.wordChars('"', '.');
                st.ordinaryChars('0', '9');
                st.wordChars('0', '9');
                cmdBuffer      = new java.lang.StringBuffer();
                inputSQLBuffer = new java.lang.StringBuffer();
                while (st.nextToken() != java.io.StreamTokenizer.TT_EOF)
                {
                    if (st.ttype == java.io.StreamTokenizer.TT_WORD)
                    {
                        nextToken = st.sval.trim();
                    }
                    else
                    {
                        continue;
                    }
                    if (inputSQLBuffer.length() > 0)
                    {
                        inputSQLBuffer.append(" ");
                    }
                    inputSQLBuffer.append(nextToken);
                }
                ft = new FieldTokenizer(inputSQLBuffer.toString(), ' ', false);
                while (ft.hasMoreFields())
                {
                    nextField  = ft.nextField();
                    upperField = nextField.toUpperCase();
                    if (statementType == (String)null)
                    {
                        statementType = upperField;
                        lastIndex     = getKeywordIndex(statementType, statementType);
                        if (lastIndex != 0)
                        {
                            throwException(9);
                        }
                        keyWord = statementType;
                    }
                    else
                    {
                        keyIndex = getKeywordIndex(statementType, upperField);
                        if (keyIndex < 0)
                        {
                            if (cmdBuffer.length() > 0)
                            {
                                cmdBuffer.append(" ");
                            }
                            cmdBuffer.append(nextField);
                        }
                        else
                        {
                            setPhrase(keyWord, cmdBuffer.toString());
                            cmdBuffer = new java.lang.StringBuffer();
                            keyWord   = upperField;
                            if (TinySQLGlobals.PARSER_DEBUG)
                            {
                                java.lang.SystemJ.outJ.println("Found keyword " + keyWord);
                            }
                        }
                    }
                }
                if (keyWord != (String)null)
                {
                    setPhrase(keyWord, cmdBuffer.toString());
                }
                addAction();
                if (TinySQLGlobals.PARSER_DEBUG)
                {
                    java.lang.SystemJ.outJ.println("SQL:" + inputSQLBuffer.toString());
                }
            } catch (Exception ex) {
                if (TinySQLGlobals.DEBUG)
                {
                    java.lang.SystemJ.outJ.println(ex.StackTrace);                 //ex.printStackTrace(java.lang.SystemJ.outJ);
                }
                throw new TinySQLException(ex.getMessage());
            }
        }
Esempio n. 2
0
        // end methods implemented from tinySQLTable.java
        // the rest of this stuff is internal methods
        // for textFileTable
        //

        /*
         *
         * Reads in a table definition and populates the column_info
         * Hashtable
         *
         */
        void readColumnInfo()
        {//throws tinySQLException {
            try
            {
                column_info = new java.util.Hashtable <Object, Object>();

                // Open an FileInputStream to the .def (table
                // definition) file
                //
                java.io.FileInputStream fdef =
                    new java.io.FileInputStream(dataDir + "/" + table + ".def");

                // use a StreamTokenizer to break up the stream.
                //
                java.io.Reader r = new java.io.BufferedReader(
                    new java.io.InputStreamReader(fdef));
                java.io.StreamTokenizer def = new java.io.StreamTokenizer(r);

                // set the | as a delimiter, and set everything between
                // 0 and z as word characters. Let it know that eol is
                // *not* significant, and that it should parse numbers.
                //
                def.whitespaceChars('|', '|');
                def.wordChars('0', 'z');
                def.eolIsSignificant(false);
                def.parseNumbers();

                // read each token from the tokenizer
                //
                while (def.nextToken() != java.io.StreamTokenizer.TT_EOF)
                {
                    // first token is the datatype
                    //
                    // Q&D: Default is char value, numeric is special
                    String datatype = java.lang.StringJ.valueOf(java.sql.Types.CHAR);
                    if (def.sval.equals("NUMERIC"))
                    {
                        datatype = java.lang.StringJ.valueOf(java.sql.Types.NUMERIC);
                    }

                    // get the next token; it's the column name
                    //
                    def.nextToken();
                    String column = def.sval;

                    // get the third token; it's the size of the column
                    //
                    def.nextToken();
                    long size = (new java.lang.Double(def.nval)).longValue();

                    // create an info array
                    //
                    String[] info = new String[3];

                    // store the datatype, the size, and the position
                    // within the record (the record length *before*
                    // we increment it with the size of this column
                    //
                    info[COLUMN_TYPE] = datatype;
                    info[COLUMN_SIZE] = java.lang.Long.toString(size);
                    info[COLUMN_POS]  = java.lang.Long.toString(record_length);

                    // this is the start position of the next column
                    //
                    record_length += size;

                    // store this info in the column_info hash,
                    // keyed by column name.
                    //
                    column_info.put(column, info);
                }

                fdef.close(); // close the file
            }
            catch (Exception e)
            {
                throw new TinySQLException(e.getMessage());
            }
        }