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

📄 tinysql.java

📁 TinySQL是一个轻量级的纯java数据库引擎
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
 *                If the table that was just processed is the last in
 *                the list, then we have drilled down to the bottom;
 *                all columns have values, and we can add it to the
 *                result set. The next time through, the program
 *                will try to read another row at this level; if it's
 *                found, only columns for the table being read will
 *                be overwritten in the tsRow.
 *                 
 *                Columns for the other table will be left alone, and 
 *                another row will be added to the result set. Here
 *                is the essence of the Cartesian Product which is
 *                being built here.
 */                
                  haveRecord = true;
               }
            } else {
/*
 *             We didn't find any more records at this level.
 *             Reset the record pointer to the top of the table,
 *             and decrement level. We have hit end of file here.
 */
               if ( wc != (tinySQLWhere)null )
                  wc.clearValues(tableAndAlias);
               level--;
               eof = true; 
               jtbl.GoTop();
            }
         } else {
/*
 *          No tables were found at this level; back up a level
 *          and see if there's any up there.
 */
            level--;
         }
/*
 *       If we got a record, then add it to the result set.
 */
         if (haveRecord)
         {
/*
 *          If group functions are involved, add records only after a break,
 *          which is defined as a change in all of the group columns.  
 *          Otherwise, update the current record.
 */
            if ( jrs.isGrouped() )
            {
               if ( groupBreak )
               {
                  addOK = jrs.addRow((tsRow)record.clone());
                  if (addOK == false)
                  {
                     jrs.setLevel (level);
                     return; 
                  }
                  groupBreak = false;
               } else {
                  jrs.updateRow( (tsRow) record.clone());
               }
            } else {
/*
 *             No group functions are involved.  Just add the record.
 */
               addOK = jrs.addRow((tsRow)record.clone());
               if (addOK == false)
               {
                  jrs.setLevel (level);
                  return; 
               }
            }
            firstColumn = "*";
         }
      }
/*
 *    Close all the tables
 */
      for ( i = 0; i < t.size(); i++ )
      {
         jtbl = (tinySQLTable)tables.get((String)t.elementAt(i));
         jtbl.close();
      }
/*
 *    return a result set
 */
      jrs.setLevel (0);
   }
/*
 * Delete rows which match a where clause.
 */
   private void DeleteStatement (String tableName, tinySQLWhere wc) 
      throws tinySQLException 
   {
      tinySQLTable jtbl;
      Hashtable tables;
      String columnName,columnString,whereStatus;
      Enumeration cols;
/* 
 *    Create a table object and put it in the Hashtable.
 */
      jtbl = getTable(tableName);
      tables = new Hashtable();
      tables.put(tableName, jtbl);
/*
 *    Process each row in the table ignoring deleted rows.
 */
      jtbl.GoTop();
      while (jtbl.NextRecord()) 
      {
         if (!jtbl.isDeleted()) 
         {
            cols = jtbl.column_info.keys();
            whereStatus = "TRUE";
            while (cols.hasMoreElements()) 
            {
               columnName = jtbl.table + "->" + jtbl.tableAlias + "."
               + (String)cols.nextElement();
               columnString = jtbl.GetCol(columnName);
/*
 *             Check the status of the where clause for each column value.
 */
               if ( wc != (tinySQLWhere)null )
                  whereStatus = wc.evaluate(columnName,columnString);
               if ( whereStatus.equals("FALSE") ) break;
            }
            if ( whereStatus.equals("TRUE") ) jtbl.DeleteRow();
            if ( wc != (tinySQLWhere)null )
               wc.clearValues(jtbl.table + "->" + jtbl.tableAlias);
         }
      }
      jtbl.close();
   }
/*
 * Update rows which match a WHERE clause
 */
   private void UpdateStatement(String tableName, Vector c, Vector v, tinySQLWhere wc)
       throws tinySQLException 
   {
/*
 *    Create a table object and put it in the Hashtable.
 */
      tinySQLTable jtbl = getTable(tableName);
      Hashtable tables = new Hashtable();
      tables.put(tableName, jtbl);
      String columnName,columnString,whereStatus;
/*
 *    Process each row in the table ignoring deleted rows.
 */
      jtbl.GoTop();
      while (jtbl.NextRecord()) 
      {
         if (!jtbl.isDeleted())
         {
            Enumeration cols = jtbl.column_info.keys();
            whereStatus = "TRUE";
            while (cols.hasMoreElements()) 
            {
/*
 *             Use the table name for the table alias for updates.
 */ 
               columnName = jtbl.table + "->" + jtbl.table 
               + "." + (String) cols.nextElement();
               columnString = jtbl.GetCol(columnName);
/*
 *             Check the status of the where clause for each column value.
 */
               if ( wc != (tinySQLWhere)null )
                  whereStatus = wc.evaluate(columnName,columnString);
               if ( whereStatus.equals("FALSE") ) break;
            }
            if ( whereStatus.equals("TRUE") ) jtbl.UpdateCurrentRow(c, v);
            if ( wc != (tinySQLWhere)null )
               wc.clearValues(jtbl.table + "->" + jtbl.tableAlias);
         }
      }
      jtbl.close();
   }
/*
 * Create the tinySQLTable object, then insert a row, and update
 * it with the c and v Vectors
 */
   private void InsertStatement (String statementType, String tableName,
                                 Vector c, Vector v) 
        throws tinySQLException 
   {
      String columnName,valueString;
      int i,columnType,columnSize;
      tinySQLTable jtbl=(tinySQLTable)null;
      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 = Double.parseDouble(valueString);
            } catch (Exception e) {
               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 + " " 
            + Integer.toString(valueString.length())
            + " > " + 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();
   }
/*
 * Creates a table given a tableName, and a Vector of column
 * definitions.
 *
 * The column definitions are an array of tsColumn
 */
  abstract void CreateTable (String tableName, Vector v) 
    throws IOException, tinySQLException;
/*
 * Creates new Columns given a tableName, and a Vector of
 * column definition (tsColumn) arrays.<br>
 *
 * ALTER TABLE table [ * ] ADD [ COLUMN ] column type 
 */
  abstract void AlterTableAddCol (String tableName, Vector v) 
    throws IOException, tinySQLException;

/*
 * Deletes Columns given a tableName, and a Vector of
 * column definition (tsColumn) arrays.<br>
 *
 * ALTER TABLE table DROP [ COLUMN ] column { RESTRICT | CASCADE }
 */
  abstract void AlterTableDropCol (String tableName, Vector v) 
    throws IOException, tinySQLException;
/*
 * Rename columns
 *
 * ALTER TABLE table RENAME war TO peace
 */
  abstract void AlterTableRenameCol (String tableName, String oldColumnName,
    String newColumnName) throws tinySQLException;
/*
 * Drops a table by name
 */
   abstract void DropTable (String tableName) throws tinySQLException;
/*
 * Create a tinySQLTable object by table name
 */
   abstract tinySQLTable getTable(String tableName) throws tinySQLException;

}

⌨️ 快捷键说明

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