Exemple #1
0
  public static int getTypeNr(String name) {

    int i = typeNames.get(name, Integer.MIN_VALUE);

    if (i == Integer.MIN_VALUE) {
      i = typeAliases.get(name, Integer.MIN_VALUE);
    }

    return i;
  }
  public static int getTypeNr(String name) throws HsqlException {

    int i = typeNames.get(name, Integer.MIN_VALUE);

    if (i == Integer.MIN_VALUE) {
      i = typeAliases.get(name, Integer.MIN_VALUE);
    }

    if (i == Integer.MIN_VALUE) {
      throw Trace.error(Trace.WRONG_DATA_TYPE, name);
    }

    return i;
  }
  /**
   * Releases the link betwen the session and all compiled statement objects it is linked to.
   *
   * <p>If any such statement is not linked with any other session, it is removed from management.
   *
   * @param sid the session identifier
   */
  synchronized void removeSession(int sid) {

    IntKeyIntValueHashMap scsMap;
    int csid;
    Iterator i;

    scsMap = (IntKeyIntValueHashMap) sessionMap.remove(sid);

    if (scsMap == null) {
      return;
    }

    i = scsMap.keySet().iterator();

    while (i.hasNext()) {
      csid = i.nextInt();

      int usecount = useMap.get(csid, 1) - 1;

      if (usecount == 0) {
        String sql = (String) sqlLookup.remove(csid);

        sqlMap.remove(sql);
        csidMap.remove(csid);
        useMap.remove(csid);
      } else {
        useMap.put(csid, usecount);
      }
    }
  }
  /**
   * @param SQL type string
   * @return java.sql.Types int value
   * @throws HsqlException
   */
  static int getTypeNr(String type) throws HsqlException {

    int i = typeAliases.get(type, Integer.MIN_VALUE);

    Trace.check(i != Integer.MIN_VALUE, Trace.WRONG_DATA_TYPE, type);

    return i;
  }
  /** Clears all internal data structures, removing any references to compiled statements. */
  synchronized void reset() {

    sqlMap.clear();
    sqlLookup.clear();
    csidMap.clear();
    sessionMap.clear();
    useMap.clear();

    next_cs_id = 0;
  }
  /**
   * Registers a compiled statement to be managed.
   *
   * <p>The only caller should be a Session that is attempting to prepare a statement for the first
   * time, or a statement that has been invalidated due to DDL changes.
   *
   * @param csid existing id or negative if the statement is not yet managed
   * @param cs The CompiledStatement to add
   * @return The compiled statement id assigned to the CompiledStatement object
   */
  synchronized int registerStatement(int csid, CompiledStatement cs) {

    if (csid < 0) {
      csid = nextID();

      sqlMap.put(cs.sql, csid);
      sqlLookup.put(csid, cs.sql);
    }

    csidMap.put(csid, cs);

    return csid;
  }
  /**
   * Retieves the type number corresponding to the class of an IN, IN OUT or OUT parameter.
   *
   * <p>This method extends getTypeNr to return OTHER for primitive arrays, classes that directly
   * implement java.io.Serializable and non-primitive arrays whose base component implements
   * java.io.Serializable, allowing, for instance, arguments and return types of primitive arrays,
   * Serializable objects and arrays, of Serializable objects. Direct primitive types other than
   * those mapping directly to the internal wrapper form are not yet handled. That is, HSQLDB cannot
   * yet properly deal with CALLs involving methods with primitive byte, short, float or their
   * corresponding wrappers, due to the way internal conversion works and lack of detection and
   * narrowing code in Function to allow this. In other words, passing in or retrieving any of the
   * mentioned types always causes conversion to a wider internal wrapper which is genrally
   * incompatible under reflective invocation, resulting in an IllegalArgumentException.
   *
   * @param c a Class instance
   * @return java.sql.Types int value
   * @throws HsqlException
   */
  static int getParameterTypeNr(Class c) throws HsqlException {

    String name;
    String msg;
    int type;

    Trace.doAssert(c != null, "c is null");

    if (Void.TYPE.equals(c)) {
      return Types.NULL;
    }

    name = c.getName();
    msg = "Unsupported parameter/return value class: ";

    if (illegalParameterClasses.contains(c)) {
      throw Trace.error(Trace.WRONG_DATA_TYPE, msg + name);
    }

    type = typeAliases.get(name, Integer.MIN_VALUE);

    if (type == Integer.MIN_VALUE) {

      // byte[] is already covered as BINARY in typeAliases
      if (c.isArray()) {
        while (c.isArray()) {
          c = c.getComponentType();
        }

        if (c.isPrimitive() || java.io.Serializable.class.isAssignableFrom(c)) {
          type = OTHER;
        }
      } else if (java.io.Serializable.class.isAssignableFrom(c)) {
        type = OTHER;
      }
    }

    Trace.check(type != Integer.MIN_VALUE, Trace.WRONG_DATA_TYPE, msg + name);

    return type;
  }
  /**
   * Removes the link between a session and a compiled statement.
   *
   * <p>If the statement is not linked with any other session, it is removed from management.
   *
   * @param csid the compiled statment identifier
   * @param sid the session identifier
   */
  synchronized void freeStatement(int csid, int sid) {

    IntKeyIntValueHashMap scsMap = (IntKeyIntValueHashMap) sessionMap.get(sid);
    int count = scsMap.get(csid) - 1;

    if (count != 0) {
      scsMap.put(csid, count);
    } else {
      scsMap.remove(csid);

      int usecount = useMap.get(csid, 1) - 1;

      if (usecount == 0) {
        String sql = (String) sqlLookup.remove(csid);

        sqlMap.remove(sql);
        csidMap.remove(csid);
        useMap.remove(csid);
      } else {
        useMap.put(csid, usecount);
      }
    }
  }
 public static int get(String token) {
   return commandSet.get(token, -1);
 }
  /**
   * Retrieves a new map from set of string tokens to numeric tokens for commonly encountered
   * database command token occurences.
   *
   * @return a new map for the database command token set
   */
  private static IntValueHashMap newCommandSet() {

    IntValueHashMap commandSet;

    commandSet = new IntValueHashMap(67);

    commandSet.put(T_ADD, ADD);
    commandSet.put(T_ALIAS, ALIAS);
    commandSet.put(T_ALTER, ALTER);
    commandSet.put(T_AUTOCOMMIT, AUTOCOMMIT);
    commandSet.put(T_CACHED, CACHED);
    commandSet.put(T_CALL, CALL);
    commandSet.put(T_CHECK, CHECK);
    commandSet.put(T_CHECKPOINT, CHECKPOINT);
    commandSet.put(T_COLUMN, COLUMN);
    commandSet.put(T_COMMIT, COMMIT);
    commandSet.put(T_CONNECT, CONNECT);
    commandSet.put(T_CONSTRAINT, CONSTRAINT);
    commandSet.put(T_CREATE, CREATE);
    commandSet.put(T_DATABASE, DATABASE);
    commandSet.put(T_DELETE, DELETE);
    commandSet.put(T_DEFRAG, DEFRAG);
    commandSet.put(T_DISCONNECT, DISCONNECT);
    commandSet.put(T_DROP, DROP);
    commandSet.put(T_EXCEPT, EXCEPT);
    commandSet.put(T_EXPLAIN, EXPLAIN);
    commandSet.put(T_FOREIGN, FOREIGN);
    commandSet.put(T_GRANT, GRANT);
    commandSet.put(T_IGNORECASE, IGNORECASE);
    commandSet.put(T_INCREMENT, INCREMENT);
    commandSet.put(T_INDEX, INDEX);
    commandSet.put(T_INITIAL, INITIAL);
    commandSet.put(T_INSERT, INSERT);
    commandSet.put(T_INTERSECT, INTERSECT);
    commandSet.put(T_LOGSIZE, LOGSIZE);
    commandSet.put(T_MAXROWS, MAXROWS);
    commandSet.put(T_MEMORY, MEMORY);
    commandSet.put(T_MINUS, MINUS);
    commandSet.put(T_NEXT, NEXT);
    commandSet.put(T_NOT, NOT);
    commandSet.put(T_OPENBRACKET, OPENBRACKET);
    commandSet.put(T_PASSWORD, PASSWORD);
    commandSet.put(T_PLAN, PLAN);
    commandSet.put(T_PRIMARY, PRIMARY);
    commandSet.put(T_PROPERTY, PROPERTY);
    commandSet.put(T_READONLY, READONLY);
    commandSet.put(T_REFERENTIAL_INTEGRITY, REFERENTIAL_INTEGRITY);
    commandSet.put(T_RELEASE, RELEASE);
    commandSet.put(T_RENAME, RENAME);
    commandSet.put(T_RESTART, RESTART);
    commandSet.put(T_REVOKE, REVOKE);
    commandSet.put(T_ROLE, ROLE);
    commandSet.put(T_ROLLBACK, ROLLBACK);
    commandSet.put(T_SAVEPOINT, SAVEPOINT);
    commandSet.put(T_SCRIPT, SCRIPT);
    commandSet.put(T_SCRIPTFORMAT, SCRIPTFORMAT);
    commandSet.put(T_SELECT, SELECT);
    commandSet.put(T_SEMICOLON, SEMICOLON);
    commandSet.put(T_SEQUENCE, SEQUENCE);
    commandSet.put(T_SET, SET);
    commandSet.put(T_SHUTDOWN, SHUTDOWN);
    commandSet.put(T_SOURCE, SOURCE);
    commandSet.put(T_TABLE, TABLE);
    commandSet.put(T_TEMP, TEMP);
    commandSet.put(T_TEXT, TEXT);
    commandSet.put(T_TRIGGER, TRIGGER);
    commandSet.put(T_UNIQUE, UNIQUE);
    commandSet.put(T_UPDATE, UPDATE);
    commandSet.put(T_UNION, UNION);
    commandSet.put(T_USER, USER);
    commandSet.put(T_VALUES, VALUES);
    commandSet.put(T_VIEW, VIEW);
    commandSet.put(T_WRITE_DELAY, WRITE_DELAY);
    commandSet.put(T_SCHEMA, SCHEMA);

    return commandSet;
  }
  static {
    typeNames = new IntValueHashMap(37);

    typeNames.put("CHARACTER", Types.SQL_CHAR);
    typeNames.put("VARCHAR", Types.SQL_VARCHAR);
    typeNames.put("VARCHAR_IGNORECASE", Types.VARCHAR_IGNORECASE);
    typeNames.put("DATE", Types.SQL_DATE);
    typeNames.put("TIME", Types.SQL_TIME);
    typeNames.put("TIMESTAMP", Types.SQL_TIMESTAMP);
    typeNames.put("INTERVAL", Types.SQL_INTERVAL);
    typeNames.put("TINYINT", Types.TINYINT);
    typeNames.put("SMALLINT", Types.SQL_SMALLINT);
    typeNames.put("INTEGER", Types.SQL_INTEGER);
    typeNames.put("BIGINT", Types.SQL_BIGINT);
    typeNames.put("REAL", Types.SQL_REAL);
    typeNames.put("FLOAT", Types.SQL_FLOAT);
    typeNames.put("DOUBLE", Types.SQL_DOUBLE);
    typeNames.put("NUMERIC", Types.SQL_NUMERIC);
    typeNames.put("DECIMAL", Types.SQL_DECIMAL);
    typeNames.put("BOOLEAN", Types.SQL_BOOLEAN);
    typeNames.put("BINARY", Types.SQL_BINARY);
    typeNames.put("VARBINARY", Types.SQL_VARBINARY);
    typeNames.put("CLOB", Types.SQL_CLOB);
    typeNames.put("BLOB", Types.SQL_BLOB);
    typeNames.put("OTHER", Types.OTHER);

    //
    typeAliases = new IntValueHashMap(67, 1);

    typeAliases.put("CHAR", Types.SQL_CHAR);
    typeAliases.put("CHAR VARYING", Types.SQL_VARCHAR);
    typeAliases.put("CHARACTER VARYING", Types.SQL_VARCHAR);
    typeAliases.put("CHARACTER LARGE OBJECT", Types.SQL_CLOB);
    typeAliases.put("INT", Types.SQL_INTEGER);
    typeAliases.put("DEC", Types.SQL_DECIMAL);
    typeAliases.put("LONGVARCHAR", Types.SQL_VARCHAR);
    typeAliases.put("DATETIME", Types.SQL_TIMESTAMP);
    typeAliases.put("BIT", Types.SQL_BOOLEAN);
    typeAliases.put("LONGVARBINARY", Types.SQL_VARBINARY);
    typeAliases.put("OBJECT", Types.OTHER);
  }
  static {
    typeAliases = new IntValueHashMap(67, 1);

    typeAliases.put("INTEGER", Types.INTEGER);
    typeAliases.put("INT", Types.INTEGER);
    typeAliases.put("int", Types.INTEGER);
    typeAliases.put("java.lang.Integer", Types.INTEGER);
    typeAliases.put("IDENTITY", Types.INTEGER);
    typeAliases.put("DOUBLE", Types.DOUBLE);
    typeAliases.put("double", Types.DOUBLE);
    typeAliases.put("java.lang.Double", Types.DOUBLE);
    typeAliases.put("FLOAT", Types.FLOAT);
    typeAliases.put("REAL", Types.REAL);
    typeAliases.put("VARCHAR", Types.VARCHAR);
    typeAliases.put("java.lang.String", Types.VARCHAR);
    typeAliases.put("CHAR", Types.CHAR);
    typeAliases.put("CHARACTER", Types.CHAR);
    typeAliases.put("LONGVARCHAR", Types.LONGVARCHAR);
    typeAliases.put("VARCHAR_IGNORECASE", VARCHAR_IGNORECASE);
    typeAliases.put("DATE", Types.DATE);
    typeAliases.put("java.sql.Date", Types.DATE);
    typeAliases.put("TIME", Types.TIME);
    typeAliases.put("java.sql.Time", Types.TIME);
    typeAliases.put("TIMESTAMP", Types.TIMESTAMP);
    typeAliases.put("java.sql.Timestamp", Types.TIMESTAMP);
    typeAliases.put("DATETIME", Types.TIMESTAMP);
    typeAliases.put("DECIMAL", Types.DECIMAL);
    typeAliases.put("java.math.BigDecimal", Types.DECIMAL);
    typeAliases.put("NUMERIC", Types.NUMERIC);
    typeAliases.put("BIT", Types.BIT);
    typeAliases.put("boolean", Types.BIT);
    typeAliases.put("java.lang.Boolean", Types.BIT);
    typeAliases.put("TINYINT", Types.TINYINT);
    typeAliases.put("byte", Types.TINYINT);
    typeAliases.put("java.lang.Byte", Types.TINYINT);
    typeAliases.put("SMALLINT", Types.SMALLINT);
    typeAliases.put("short", Types.SMALLINT);
    typeAliases.put("java.lang.Short", Types.SMALLINT);
    typeAliases.put("BIGINT", Types.BIGINT);
    typeAliases.put("long", Types.BIGINT);
    typeAliases.put("java.lang.Long", Types.BIGINT);
    typeAliases.put("BINARY", Types.BINARY);
    typeAliases.put("[B", Types.BINARY);
    typeAliases.put("VARBINARY", Types.VARBINARY);
    typeAliases.put("LONGVARBINARY", Types.LONGVARBINARY);
    typeAliases.put("OTHER", Types.OTHER);
    typeAliases.put("OBJECT", Types.OTHER);
    typeAliases.put("java.lang.Object", Types.OTHER);
    typeAliases.put("NULL", Types.NULL);
    typeAliases.put("void", Types.NULL);
    typeAliases.put("java.lang.Void", Types.NULL);

    //
    typeNames = new IntKeyHashMap(37);

    typeNames.put(Types.NULL, "NULL");
    typeNames.put(Types.INTEGER, "INTEGER");
    typeNames.put(Types.DOUBLE, "DOUBLE");
    typeNames.put(VARCHAR_IGNORECASE, "VARCHAR_IGNORECASE");
    typeNames.put(Types.VARCHAR, "VARCHAR");
    typeNames.put(Types.CHAR, "CHAR");
    typeNames.put(Types.LONGVARCHAR, "LONGVARCHAR");
    typeNames.put(Types.DATE, "DATE");
    typeNames.put(Types.TIME, "TIME");
    typeNames.put(Types.DECIMAL, "DECIMAL");
    typeNames.put(Types.BIT, "BIT");
    typeNames.put(Types.TINYINT, "TINYINT");
    typeNames.put(Types.SMALLINT, "SMALLINT");
    typeNames.put(Types.BIGINT, "BIGINT");
    typeNames.put(Types.REAL, "REAL");
    typeNames.put(Types.FLOAT, "FLOAT");
    typeNames.put(Types.NUMERIC, "NUMERIC");
    typeNames.put(Types.TIMESTAMP, "TIMESTAMP");
    typeNames.put(Types.BINARY, "BINARY");
    typeNames.put(Types.VARBINARY, "VARBINARY");
    typeNames.put(Types.LONGVARBINARY, "LONGVARBINARY");
    typeNames.put(Types.OTHER, "OBJECT");

    //
    illegalParameterClasses = new org.hsqldb.lib.HashSet(13);

    illegalParameterClasses.add(Byte.TYPE);
    illegalParameterClasses.add(Short.TYPE);
    illegalParameterClasses.add(Float.TYPE);
    illegalParameterClasses.add(Byte.class);
    illegalParameterClasses.add(Short.class);
    illegalParameterClasses.add(Float.class);
  }
 /**
  * Retrieves the registered compiled statement identifier associated with the specified SQL
  * String, or a value less than zero, if no such statement has been registered.
  *
  * @param sql the SQL String
  * @return the compiled statement identifier associated with the specified SQL String
  */
 synchronized int getStatementID(String sql) {
   return sqlMap.get(sql, -1);
 }