⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tinysql.java

📁 TinySQL是一个轻量级的纯java数据库引擎
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
 */              
               columnDefs = (Vector) h.get("COLUMN_DEF");
               AlterTableAddCol (tableName, columnDefs);
            } else if ( actionType.equals("ALTER_DROP") ) {
/*         
 *             SQL ALTER TABLE DROP
 */             
               columns = (Vector) 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 {
               System.out.println("Unrecognized action " + actionType);
            }
         }
      } catch (Exception e) {
         if ( tinySQLGlobals.EX_DEBUG ) e.printStackTrace(System.out);
         throw new tinySQLException(e.getMessage());
      }
      return rs;
   }
/*
 * Execute an SQL Select Statement
 */
   protected tsResultSet SelectStatement (Hashtable t,Vector c,
      tinySQLWhere w,String ot,boolean distinct,Object stmt)
      throws tinySQLException 
   {
      Hashtable tables;
      Vector tableList;
      tsResultSet jrs;
      tsColumn columnObject;
      int i;
/*
 *    Instantiate a new, empty tsResultSet
 */
      jrs = new tsResultSet(w, this);
      groupFunctions = new Hashtable();
      try
      {
      	jrs.setFetchSize(((tinySQLStatement)stmt).getFetchSize());
        jrs.setType(((tinySQLStatement)stmt).getResultSetType());
      } catch (SQLException sqle) {
        Utils.log ("Caught SQLException while setting Fetchsize and ResultSetType");
        Utils.log ("   This event is (should be) impossible!"); 
      }
      tables = t;
      tableList = (Vector)tables.get("TABLE_SELECT_ORDER");
/*
 *    Add the column objects to the ResultSet.
 */
      for (i = 0; i < c.size(); i++)
      {
         columnObject = (tsColumn)c.elementAt(i);
/*
 *       The column object is now added to the ResultSet
 */
         jrs.addColumn (columnObject);
         if ( debug ) System.out.println("Adding " 
         + columnObject.contextToString()
         + " column " + newLine + columnObject.toString() + newLine);
      }
      jrs.setState (1,tables,ot,distinct);
      contSelectStatement (jrs);
      return jrs;
  }
/*
 * Support function for restartable queries. Continue to
 * read the query result. The current state is taken from
 * tsResultSet. Proceed until maxFetchSize has reached.
 */
   protected void contSelectStatement (tsResultSet jrs)
      throws tinySQLException
   {
/*
 *    The table scan here is an iterative tree expansion, similar to
 *    the algorithm shown in the outline example in Chapter 5.
 */
      String columnName,columnString,whereStatus,tableAndAlias;
      boolean addOK;
      int i,rowCount;
      int level = jrs.getLevel ();
      tinySQLTable jtbl;
      tsColumn updateColumn;
      Hashtable tables = jrs.getTableState ();
      tinySQLWhere wc = jrs.getWhereClause();
/*
 *    Create a hashtable to enumerate the tables to be scanned and initialize
 *    with the first table name.
 */
      Hashtable tbl_list = new Hashtable();
      Vector groupFunction;
      Vector t = (Vector)tables.get("TABLE_SELECT_ORDER");
      String current = (String) t.elementAt(0);
      String firstColumn = "*";
      tbl_list.put( current, new Integer(1) );
/*
 *    Create a row object; this is added to the ResultSet.
 */
      tsRow record = new tsRow();
      Vector resultSet = new Vector();
/*
 *    Keep retrieving rows until we run out of rows to process.
 */
      while ( level > 0 )
      {
         boolean levelFound = false;
/*
 *       Find an item within the tbl_list which has the same level as the
 *       one we're on.
 */
         Enumeration keys = tbl_list.keys();
         while (keys.hasMoreElements()) 
         {
/*
 *          Get the next element in the "to be processed"
 *          Hashtable, and find out its level, storing this
 *          value in currLevel.
 */
            String hashkey = (String) keys.nextElement();
            int currLevel  = ((Integer) tbl_list.get(hashkey)).intValue();
/*
 *          As soon as an element is found whose level is equal to the 
 *          one currently being processed, grab it's primary key (the hashkey),
 *          flag levelfound, and break!
 */
            if (currLevel == level) 
            {
               current = hashkey; levelFound = true; break;
            }
         }
         boolean eof = false;        // did we hit eof?
         boolean haveRecord = false; // did we get a record or not?
/*
 *       If a table was found at the current level, then we should
 *       try to get another row from it.
 */
         if (levelFound) 
         {
/*
 *          Get the current table
 */
            jtbl = (tinySQLTable) tables.get(current);
            tableAndAlias = jtbl.table + "->" + jtbl.tableAlias;
            if ( tinySQLGlobals.WHERE_DEBUG )
               System.out.println("Processing level " + level 
               + " table " + tableAndAlias + "\n");
/*
 *          The following code is the start of setting up simple indexes
 *          for tinySQL.  The concept involves creating a new tinySQLCondition
 *          class, instances of which will be created by tinySQLWhere.  At least
 *          initially only conditions that are equal to a constant will be
 *          considered.  One of these conditions will be stored inside the
 *          dbfFileTable object. Calls to NextRecord would then have to add
 *          logic to check to see if an index exists.  The presence of a
 *          complete index would be indicated by a counter that was equal
 *          to the number of rows in the table.  In that case a single get
 *          would deliver the record numbers required for the where condition.
 *          The NextRecord function would return these record numbers in
 *          sequence.  If the index did not exist then new values in the 
 *          index Hashtable would be created.  None of this code has been 
 *          developed or implemented, let alone tested.
 *         
 *
 *          if ( wc != (tinySQLWhere)null )
 *             jtbl.setIndexCondition(wc.getIndexCondition(tableAndAlias));
 */
            if ( performDebug ) System.out.println("Selecting records from "
            + jtbl.table);
/*
 *          Skip to the next undeleted record; at some point,
 *          this will run out of records, and found will be false.
 */
            boolean found = false;
            while (jtbl.NextRecord()) 
            {
/*
 *             Clear the column values for this table before starting 
 *             to process the next row.
 */
               for ( i = 0; i < jrs.numcols(); i++ ) 
               {
                  updateColumn = jrs.columnAtIndex(i,true);
                  updateColumn.clear(tableAndAlias);
               }
               if ( wc != (tinySQLWhere)null )
                  wc.clearValues(tableAndAlias);
               if (!jtbl.isDeleted())
               {
/*
 *                Evaluate the where clause for each column in the table.  If
 *                it is false, skip to the next row.  Otherwise, add the
 *                column value to the output record.
 */
                  Enumeration cols = jtbl.column_info.keys();
                  found = true;
                  whereStatus = "TRUE";
                  while (cols.hasMoreElements())
                  {
                     columnName = tableAndAlias + "."
                     + (String)cols.nextElement();
                     columnString = jtbl.GetCol(columnName);
                     if ( wc != (tinySQLWhere)null ) 
                     {
                        whereStatus = wc.evaluate(columnName,columnString);
                        if ( whereStatus.equals("FALSE") )
                        {
/*
 *                         This column value caused the where clause to 
 *                         be FALSE.  Go to the next row in the table.
 */
                           found = false;
                           break;
                        }
                     }
/*
 *                   Update the ResultSet tsColumn values 
 */
                     jrs.updateColumns(columnName,columnString);
                  }
/*
 *                If no where condition has evaluated to false then this
 *                record is a candidate for output.  Break to the next table
 *                in this case for further WHERE processing.
 */
                  if ( found ) break;
               }
            }
            if ( found )
            {
               if ( tinySQLGlobals.DEBUG )
                  System.out.println("Build candidate record.");
               for ( i = 0; i < jrs.numcols(); i++ ) 
               {
                  updateColumn = jrs.columnAtIndex(i,true);
/*
 *                Evaluate all functions before adding the
 *                column to the output record.
 */
                  updateColumn.updateFunctions();
                  columnString = updateColumn.getString();
                  if ( updateColumn.isNotNull() )
                  {
                     record.put(updateColumn.name,columnString);
                  } else {
                     record.remove(updateColumn.name);
                  }
               }
               if ( performDebug )
                  System.out.println("Record is " + record.toString());
/*
 *             Since we were just able to get a row, then
 *             we are not at the end of file
 */
               eof = false;
/*
 *             If the table we are processing is not the last in
 *             the list, then we should increment level and loop to the top.
 */          
               if (level < t.size()) 
               {
/*
 *                Increment level
 */
                  level++;
/*
 *                Add the next table in the list of tables to
 *                the tbl_list, the Hashtable of "to be processed" tables.
 */
                  String next_tbl = (String) t.elementAt( level - 1);
                  tbl_list.put( next_tbl, new Integer(level) );
               } else {
/*

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -