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

📄 tinysql.java

📁 TinySQL是一个轻量级的纯java数据库引擎
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * tinySQL.java 
 *
 * A trivial implementation of SQL in an abstract class.
 * Plug it in to your favorite non-SQL data source, and
 * QUERY AWAY!
 *
 * Copyright 1996, Brian C. Jepson
 *                 (bjepson@ids.net)
 *
 * $Author: davis $
 * $Date: 2004/12/18 21:23:50 $
 * $Revision: 1.1 $
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 * Revision History:
 *
 * Major rewrite in 2003/4 by Davis Swan SQL*Magic Ltd.
 */

package com.sqlmagic.tinysql;

import java.util.*;
import java.lang.*;
import java.io.*;
import java.sql.SQLException;
import java.sql.Types;

/**
 * @author Thomas Morger <mgs@sherito.org> Changed tinySQL to reflect changes
 * in tinySQLResultSet - an instance of the connection is passed to a new 
 * resultset at construction time.
 *
 * When fetching a resultset, the number of rows to fetch can be set in the
 * statement. The builder will return when the given number of rows has been 
 * reached and will be restarted by tsResultSet when more rows are needed.
 */
public abstract class tinySQL {

  boolean debug=false,groupBreak=false,keepRecord=true;
  boolean exDebug=false,performDebug=false;
  Hashtable groupFunctions;
  String newLine = System.getProperty("line.separator");
  static tinySQLTable insertTable=(tinySQLTable)null;
  tinySQLWhere wc;
  // This is the InputStream from which the parser reads.
  // 
  private InputStream SQLStream; 

  // This is the last SQL Statement processed by sqlexec(String s).
  // Note that sqlexec() does *not* support this.
  //
  private tinySQLStatement sqlstatement = null;
/*
 *
 * Constructs a new tinySQL object.
 *
 */
  public tinySQL() {

  }

/*
 * Reads SQL statements from System.in() and returns a
 * tsResultSet for the last statement executed. This is
 * really only good for testing.
 * 
 * @exception tinySQLException
 */
  public tsResultSet sqlexec() throws tinySQLException {

    SQLStream = (InputStream) System.in;
    System.err.println("Reading SQL Statements from STDIN...");
    System.err.println("CRASHING AFTER THIS POINT IS SURE...");
    System.err.println("Have no Statement, no connection and no clue how to continue ...");
    return sql(null);

  }

/*
 * Processes the SQL Statement s and returns
 * a tsResultSet.
 *
 * @param s SQL Statement to execute
 * @exception tinySQLException
 */
  public tsResultSet sqlexec(tinySQLStatement s) throws tinySQLException
  {
     return sql(s);
  }
  public tsResultSet sqlexec(tinySQLPreparedStatement s)
     throws tinySQLException
  {
     return sql(s);
  }

/*
 *
 * 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 tsResultSet sql(Object s) throws tinySQLException 
   {
/*
 *    Build the ResultSet
 */
      tsResultSet rs = null;
      tinySQLTable jtbl;
      tinySQLPreparedStatement pstmt=(tinySQLPreparedStatement)null;
      boolean useTinyParser=true,distinct=false;
      Vector actions,columns,columnDefs,values,columnContexts,columnAliases;
      String actionType,orderType,tableName,statementType,byteString;
      Hashtable h,selectTables;
      byte[] bStream;
      ByteArrayInputStream st;
      int i;
      String actionString;
      groupBreak=false;
      keepRecord=true;
      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 = (Vector)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 == (Vector)null ) 
         {
            st = new ByteArrayInputStream(bStream);
            SQLStream = (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 = (Hashtable)actions.elementAt(i);
            actionType = (String)h.get("TYPE");
            if ( tinySQLGlobals.DEBUG )
               System.out.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 ) System.out.println("ACTION: " + actionString);
            if ( actionType.equals("UPDATE") ) 
            {
/*     
 *             SQL UPDATE
 */            
               columns    = (Vector) h.get("COLUMNS");
               values     = (Vector) 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 = (Hashtable) h.get("TABLES");
               columns = (Vector) 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 = (Vector) h.get("COLUMNS");
               values = (Vector) 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 = (Vector) h.get("COLUMN_DEF");
               CreateTable (tableName, columnDefs);
            } else if ( actionType.equals("ALTER_ADD") ) {
/*        
 *             SQL ALTER TABLE ADD

⌨️ 快捷键说明

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