Ejemplo n.º 1
0
        public TsResultSet executetinySQL(TinySQLPreparedStatement psql) //throws SQLException
        {
            TsResultSet result;

            // try to execute the SQL
            //
            try
            {
                result = tsql.sqlexec(psql);
            }
            catch (TinySQLException e)
            {
                if (debug)
                {
                    e.printStackTrace();
                }
                throw new java.sql.SQLException("Exception: " + e.getMessage());
            }
            return(result);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
 public virtual TsResultSet sqlexec(TinySQLPreparedStatement s)
 //throws TinySQLException
 {
     return(sql(s));
 }