mysqliresult.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 936 行 · 第 1/2 页

JAVA
936
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source 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, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Charles Reich */package com.caucho.quercus.lib.db;import com.caucho.quercus.annotation.Optional;import com.caucho.quercus.annotation.ResourceType;import com.caucho.quercus.annotation.ReturnNullAsFalse;import com.caucho.quercus.env.ArrayValue;import com.caucho.quercus.env.ArrayValueImpl;import com.caucho.quercus.env.BooleanValue;import com.caucho.quercus.env.Env;import com.caucho.quercus.env.LongValue;import com.caucho.quercus.env.Value;import com.caucho.util.L10N;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.Statement;import java.sql.Types;import java.util.logging.Level;import java.util.logging.Logger;/** * mysqli object oriented API facade */@ResourceType("mysql result")public class MysqliResult extends JdbcResultResource {  private static final Logger log    = Logger.getLogger(MysqliResult.class.getName());  private static final L10N L    = new L10N(MysqliResult.class);  /**   * Constructor for MysqliResult   *   * @param stmt the corresponding statement   * @param rs the corresponding result set   * @param conn the corresponding connection   */  public MysqliResult(Env env,		      Statement stmt,                      ResultSet rs,                      Mysqli conn)  {    super(env, stmt, rs, conn);  }  /**   * Constructor for MysqliResult   *   * @param metaData the corresponding result set meta data   * @param conn the corresponding connection   */  public MysqliResult(Env env,                      ResultSetMetaData metaData,                      Mysqli conn)  {    super(env, metaData, conn);  }  public String getResourceType()  {    return "mysql result";  }    public boolean isLastSqlDescribe()  {    return ((Mysqli) getConnection()).isLastSqlDescribe();  }  /**   * Seeks to an arbitrary result pointer specified   * by the offset in the result set represented by result.   *   * @param env the PHP executing environment   * @param rowNumber the row offset   * @return true on success or false on failure   */  public boolean data_seek(Env env, int rowNumber)  {    if (seek(env, rowNumber)) {      return true;    } else {      return false;    }  }  /**   * Fetch a result row as an associative, a numeric array, or both.   *   * @param type one of MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH (default)   * By using the MYSQLI_ASSOC constant this function will behave   * identically to the mysqli_fetch_assoc(), while MYSQLI_NUM will   * behave identically to the mysqli_fetch_row() function. The final   * option MYSQLI_BOTH will create a single array with the attributes   * of both.   *   * @return a result row as an associative, a numeric array, or both   * or null if there are no more rows in the result set   */  @ReturnNullAsFalse  public ArrayValue fetch_array(Env env,                                @Optional("MYSQLI_BOTH") int type)  {    if (type != MysqliModule.MYSQLI_ASSOC	    && type != MysqliModule.MYSQLI_BOTH	    && type != MysqliModule.MYSQLI_NUM) {      env.warning(L.l("invalid result_type"));      return null;    }    return fetchArray(env, type);  }  /**   * Returns an associative array representing the row.   *   * @return an associative array representing the row   * or null if there are no more rows in the result set   */  public ArrayValue fetch_assoc(Env env)  {    return fetchArray(env, JdbcResultResource.FETCH_ASSOC);  }  /**   * Returns field metadata for a single field.   *   * @param env the PHP executing environment   * @param offset the field number   * @return the field metadata or false if no field   * information for specified offset is available   */  public Value fetch_field_direct(Env env, int offset)  {    return fetchFieldDirect(env, offset);  }  /**   * Returns the next field in the result set.   *   * @param env the PHP executing environment   * @return the next field in the result set or   * false if no information is available   */  public Value fetch_field(Env env)  {    return fetchNextField(env);  }  /**   * Returns metadata for all fields in the result set.   *   * @param env the PHP executing environment   * @return an array of objects which contains field   * definition information or FALSE if no field   * information is available   */  public Value fetch_fields(Env env)  {    return getFieldDirectArray(env);  }  /**   * Returns the lengths of the columns of the   * current row in the result set.   *   * @return an array with the lengths of the   * columns of the current row in the result set   * or false if you call it before calling   * mysqli_fetch_row/array/object or after   * retrieving all rows in the result set   */  public Value fetch_lengths()  {    return getLengths();  }  /**   * Returns an object representing the current row.   *   * @param env the PHP executing environment   * @return an object that corresponds to the   * fetched row or NULL if there are no more   * rows in resultset   */  public Value fetch_object(Env env)  {    return fetchObject(env);  }  /**   * Returns a numerical array representing the current row.   *   * @return an array that corresponds to the   * fetched row or NULL if there are no more   * rows in result set   */  public ArrayValue fetch_row(Env env)  {    return fetchArray(env, JdbcResultResource.FETCH_NUM);  }  /**   * Returns the number of fields in the result set.   *   * @param env the PHP executing environment   * @return the number of fields in the result set   */  public int field_count(Env env)  {    return getFieldCount();  }  /**   * returns an object containing the following field information:   *   * 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 (see _constMap).   * type: The data type used for this field (an integer... also see _constMap)   * decimals: The number of decimals used (for integer fields)   *   * @param env the PHP executing environment   * @param fieldOffset 0 <= fieldOffset < number of fields   * @return an object or BooleanValue.FALSE   */  protected Value fetchFieldDirect(Env env,                                   int fieldOffset)  {    if (! isValidFieldOffset(fieldOffset)) {      // php/1f77 : No warning printed for invalid index      return BooleanValue.FALSE;    }    try {      ResultSetMetaData md = getMetaData();      if (md == null)	return BooleanValue.FALSE;	      int offset = fieldOffset + 1;      if (offset < 1 || md.getColumnCount() < offset)	return BooleanValue.FALSE;      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 fieldMysqlType = md.getColumnTypeName(offset);      int fieldLength = md.getPrecision(offset);      int fieldScale = md.getScale(offset);      if (fieldTable == null || "".equals(fieldTable)) {        return fetchFieldImproved(env, md, offset);      }      String sql = "SHOW FULL COLUMNS FROM " + fieldTable + " LIKE \'" + fieldName + "\'";      MysqliResult metaResult;      metaResult = ((Mysqli) getConnection()).metaQuery(env,							sql,							catalogName);      if (metaResult == null) {        return fetchFieldImproved(env, md, offset);      }      return metaResult.fetchFieldImproved(env,					   fieldLength,					   fieldAlias,					   fieldName,					   fieldTable,					   jdbcType,					   fieldMysqlType,					   fieldScale);    } catch (SQLException e) {      log.log(Level.FINE, e.toString(), e);      return BooleanValue.FALSE;    }  }  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", new LongValue(0));      // "length" is the width of the field defined in the table      // declaration.      result.putField(env, "length", new LongValue(fieldLength));      //generate flags      long flags = 0;      result.putField(env, "flags", new LongValue(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", new LongValue(quercusType));      result.putField(env, "decimals", new LongValue(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", new LongValue(0));    } catch (SQLException e) {      log.log(Level.FINE, e.toString(), e);      return BooleanValue.FALSE;    }    return result;  }  /**   * Returns an object with the following fields:   *   * 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

⌨️ 快捷键说明

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