Example #1
0
  /**
   * Returns an object with the following fields:
   *
   * <p>name: The name of the column orgname: The original name if an alias was specified table: The
   * name of the table orgtable: The original name if an alias was specified def: default value for
   * this field, represented as a string max_length: The maximum width of the field for the result
   * set flags: An integer representing the bit-flags for the field type: An integer respresenting
   * the data type used for this field decimals: The number of decimals used (for integer fields)
   *
   * @param env the PHP executing environment
   * @param fieldLength the field length as defined in the table declaration.
   * @param name the field name
   * @param originalName the field original name
   * @param table the field table name
   * @param type the field type
   * @param scale the field scale
   * @return an object containing field metadata
   */
  protected Value fetchFieldImproved(
      Env env,
      int fieldLength,
      String name,
      String originalName,
      String table,
      int jdbcType,
      String mysqlType,
      int scale) {
    Value result = env.createObject();

    try {
      ResultSetMetaData md = getMetaData();

      if (!_rs.next()) return BooleanValue.FALSE;

      result.putField(env, "name", env.createString(name));
      result.putField(env, "orgname", env.createString(originalName));
      result.putField(env, "table", env.createString(table));
      // XXX: orgtable same as table
      result.putField(env, "orgtable", env.createString(table));

      result.putField(env, "def", env.createString(_rs.getString(6)));

      // "max_length" is the maximum width of this field in this
      // result set.

      result.putField(env, "max_length", LongValue.ZERO);

      // "length" is the width of the field defined in the table
      // declaration.

      result.putField(env, "length", LongValue.create(fieldLength));

      // generate flags
      long flags = 0;

      if (!isInResultString(4, "YES")) flags += MysqliModule.NOT_NULL_FLAG;

      if (isInResultString(5, "PRI")) {
        flags += MysqliModule.PRI_KEY_FLAG;
        flags += MysqliModule.PART_KEY_FLAG;
      }

      if (isInResultString(5, "MUL")) {
        flags += MysqliModule.MULTIPLE_KEY_FLAG;
        flags += MysqliModule.PART_KEY_FLAG;
      }

      if (isInResultString(2, "blob")
          || (jdbcType == Types.LONGVARCHAR)
          || (jdbcType == Types.LONGVARBINARY)) flags += MysqliModule.BLOB_FLAG;

      if (isInResultString(2, "unsigned")) flags += MysqliModule.UNSIGNED_FLAG;

      if (isInResultString(2, "zerofill")) flags += MysqliModule.ZEROFILL_FLAG;

      // php/1f73 - null check
      if (isInResultString(3, "bin")
          || (jdbcType == Types.LONGVARBINARY)
          || (jdbcType == Types.DATE)
          || (jdbcType == Types.TIMESTAMP)) flags += MysqliModule.BINARY_FLAG;

      if (isInResultString(2, "enum")) flags += MysqliModule.ENUM_FLAG;

      if (isInResultString(7, "auto")) flags += MysqliModule.AUTO_INCREMENT_FLAG;

      if (isInResultString(2, "set")) flags += MysqliModule.SET_FLAG;

      if ((jdbcType == Types.BIGINT)
          || (jdbcType == Types.BIT)
          || (jdbcType == Types.BOOLEAN)
          || (jdbcType == Types.DECIMAL)
          || (jdbcType == Types.DOUBLE)
          || (jdbcType == Types.REAL)
          || (jdbcType == Types.INTEGER)
          || (jdbcType == Types.SMALLINT)) flags += MysqliModule.NUM_FLAG;

      result.putField(env, "flags", LongValue.create(flags));

      // generate PHP type
      int quercusType = 0;
      switch (jdbcType) {
        case Types.DECIMAL:
          quercusType = MysqliModule.MYSQLI_TYPE_DECIMAL;
          break;
        case Types.BIT:
          // Connector-J enables the tinyInt1isBit property
          // by default and converts TINYINT to BIT. Use
          // the mysql type name to tell the two apart.

          if (mysqlType.equals("BIT")) {
            quercusType = MysqliModule.MYSQLI_TYPE_BIT;
          } else {
            quercusType = MysqliModule.MYSQLI_TYPE_TINY;
          }
          break;
        case Types.SMALLINT:
          quercusType = MysqliModule.MYSQLI_TYPE_SHORT;
          break;
        case Types.INTEGER:
          {
            if (!isInResultString(2, "medium")) quercusType = MysqliModule.MYSQLI_TYPE_LONG;
            else quercusType = MysqliModule.MYSQLI_TYPE_INT24;
            break;
          }
        case Types.REAL:
          quercusType = MysqliModule.MYSQLI_TYPE_FLOAT;
          break;
        case Types.DOUBLE:
          quercusType = MysqliModule.MYSQLI_TYPE_DOUBLE;
          break;
        case Types.BIGINT:
          quercusType = MysqliModule.MYSQLI_TYPE_LONGLONG;
          break;
        case Types.DATE:
          if (mysqlType.equals("YEAR")) {
            quercusType = MysqliModule.MYSQLI_TYPE_YEAR;
          } else {
            quercusType = MysqliModule.MYSQLI_TYPE_DATE;
          }
          break;
        case Types.TINYINT:
          quercusType = MysqliModule.MYSQLI_TYPE_TINY;
          break;
        case Types.TIME:
          quercusType = MysqliModule.MYSQLI_TYPE_TIME;
          break;
        case Types.TIMESTAMP:
          if (mysqlType.equals("TIMESTAMP")) {
            quercusType = MysqliModule.MYSQLI_TYPE_TIMESTAMP;
          } else {
            quercusType = MysqliModule.MYSQLI_TYPE_DATETIME;
          }
          break;
        case Types.LONGVARBINARY:
        case Types.LONGVARCHAR:
          quercusType = MysqliModule.MYSQLI_TYPE_BLOB;
          break;
        case Types.BINARY:
        case Types.CHAR:
          quercusType = MysqliModule.MYSQLI_TYPE_STRING;
          break;
        case Types.VARBINARY:
        case Types.VARCHAR:
          quercusType = MysqliModule.MYSQLI_TYPE_VAR_STRING;
          break;
          // XXX: may need to revisit default
        default:
          quercusType = MysqliModule.MYSQLI_TYPE_NULL;
          break;
      }
      result.putField(env, "type", LongValue.create(quercusType));
      result.putField(env, "decimals", LongValue.create(scale));

      // The "charsetnr" field is an integer identifier
      // for the character set used to encode the field.
      // This integer is sent by the server to the JDBC client
      // and is stored as com.mysql.jdbc.Field.charsetIndex,
      // but this field is private and the class does not provide
      // any means to access the field. There is also no way
      // to lookup the mysql index given a Java or Mysql encoding
      // name.

      result.putField(env, "charsetnr", LongValue.ZERO);
    } catch (SQLException e) {
      log.log(Level.FINE, e.toString(), e);
      return BooleanValue.FALSE;
    }

    return result;
  }
Example #2
0
  protected Value fetchFieldImproved(Env env, ResultSetMetaData md, int offset) {
    Value result = env.createObject();

    try {
      int jdbcType = md.getColumnType(offset);
      String catalogName = md.getCatalogName(offset);
      String fieldTable = md.getTableName(offset);
      String fieldSchema = md.getSchemaName(offset);
      String fieldName = md.getColumnName(offset);
      String fieldAlias = md.getColumnLabel(offset);
      String mysqlType = md.getColumnTypeName(offset);
      int fieldLength = md.getPrecision(offset);
      int scale = md.getScale(offset);

      if ((fieldTable == null || "".equals(fieldTable))
          && ((Mysqli) getConnection()).isLastSqlDescribe()) fieldTable = "COLUMNS";

      result.putField(env, "name", env.createString(fieldAlias));
      result.putField(env, "orgname", env.createString(fieldName));
      result.putField(env, "table", env.createString(fieldTable));
      // XXX: orgtable same as table
      result.putField(env, "orgtable", env.createString(fieldTable));

      result.putField(env, "def", env.createString(""));

      // "max_length" is the maximum width of this field in this
      // result set.

      result.putField(env, "max_length", LongValue.ZERO);

      // "length" is the width of the field defined in the table
      // declaration.

      result.putField(env, "length", LongValue.create(fieldLength));

      // generate flags
      long flags = 0;

      result.putField(env, "flags", LongValue.create(flags));

      // generate PHP type
      int quercusType = 0;
      switch (jdbcType) {
        case Types.DECIMAL:
          quercusType = MysqliModule.MYSQLI_TYPE_DECIMAL;
          break;
        case Types.BIT:
          // Connector-J enables the tinyInt1isBit property
          // by default and converts TINYINT to BIT. Use
          // the mysql type name to tell the two apart.

          if (mysqlType.equals("BIT")) {
            quercusType = MysqliModule.MYSQLI_TYPE_BIT;
          } else {
            quercusType = MysqliModule.MYSQLI_TYPE_TINY;
          }
          break;
        case Types.SMALLINT:
          quercusType = MysqliModule.MYSQLI_TYPE_SHORT;
          break;
        case Types.INTEGER:
          {
            if (!isInResultString(2, "medium")) quercusType = MysqliModule.MYSQLI_TYPE_LONG;
            else quercusType = MysqliModule.MYSQLI_TYPE_INT24;
            break;
          }
        case Types.REAL:
          quercusType = MysqliModule.MYSQLI_TYPE_FLOAT;
          break;
        case Types.DOUBLE:
          quercusType = MysqliModule.MYSQLI_TYPE_DOUBLE;
          break;
        case Types.BIGINT:
          quercusType = MysqliModule.MYSQLI_TYPE_LONGLONG;
          break;
        case Types.DATE:
          if (mysqlType.equals("YEAR")) {
            quercusType = MysqliModule.MYSQLI_TYPE_YEAR;
          } else {
            quercusType = MysqliModule.MYSQLI_TYPE_DATE;
          }
          break;
        case Types.TINYINT:
          quercusType = MysqliModule.MYSQLI_TYPE_TINY;
          break;
        case Types.TIME:
          quercusType = MysqliModule.MYSQLI_TYPE_TIME;
          break;
        case Types.TIMESTAMP:
          if (mysqlType.equals("TIMESTAMP")) {
            quercusType = MysqliModule.MYSQLI_TYPE_TIMESTAMP;
          } else {
            quercusType = MysqliModule.MYSQLI_TYPE_DATETIME;
          }
          break;
        case Types.LONGVARBINARY:
        case Types.LONGVARCHAR:
          quercusType = MysqliModule.MYSQLI_TYPE_BLOB;
          break;
        case Types.BINARY:
        case Types.CHAR:
          quercusType = MysqliModule.MYSQLI_TYPE_STRING;
          break;
        case Types.VARBINARY:
        case Types.VARCHAR:
          quercusType = MysqliModule.MYSQLI_TYPE_VAR_STRING;
          break;
          // XXX: may need to revisit default
        default:
          quercusType = MysqliModule.MYSQLI_TYPE_NULL;
          break;
      }

      result.putField(env, "type", LongValue.create(quercusType));
      result.putField(env, "decimals", LongValue.create(scale));

      // The "charsetnr" field is an integer identifier
      // for the character set used to encode the field.
      // This integer is sent by the server to the JDBC client
      // and is stored as com.mysql.jdbc.Field.charsetIndex,
      // but this field is private and the class does not provide
      // any means to access the field. There is also no way
      // to lookup the mysql index given a Java or Mysql encoding
      // name.

      result.putField(env, "charsetnr", LongValue.ZERO);
    } catch (SQLException e) {
      log.log(Level.FINE, e.toString(), e);
      return BooleanValue.FALSE;
    }

    return result;
  }