mysqliresult.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 936 行 · 第 1/2 页
JAVA
936 行
* 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", 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; 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", 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 the following field flags: not_null, primary_key, multiple_key, blob, * unsigned zerofill, binary, enum, auto_increment and timestamp * <p/> * it does not return the MySQL / PHP flag unique_key * <p/> * MysqlModule generates a special result set with the appropriate values * * @return the field flags */ public Value getFieldFlagsImproved(Env env, int jdbcType, String mysqlType) { try { StringBuilder flags = new StringBuilder(); // php/142r if (! _rs.next()) return BooleanValue.FALSE; if (! isInResultString(4, "YES")) flags.append("not_null"); if (isInResultString(5, "PRI")) { if (flags.length() > 0) flags.append(' '); flags.append("primary_key"); } else { if (isInResultString(5, "MUL")) { if (flags.length() > 0) flags.append(' '); flags.append("multiple_key"); } } final boolean isTimestamp = (jdbcType == Types.TIMESTAMP) && mysqlType.equals("TIMESTAMP"); if (isInResultString(2, "blob") || (jdbcType == Types.LONGVARCHAR)) { if (flags.length() > 0) flags.append(' '); flags.append("blob"); } if (isInResultString(2, "unsigned") || (jdbcType == Types.BIT && mysqlType.equals("BIT")) || isTimestamp) { if (flags.length() > 0) flags.append(' '); flags.append("unsigned"); } if (isInResultString(2, "zerofill") || isTimestamp) { if (flags.length() > 0) flags.append(' '); flags.append("zerofill"); } if (isInResultString(3, "bin") || (jdbcType == Types.BINARY) || (jdbcType == Types.LONGVARBINARY) || (jdbcType == Types.VARBINARY) || (jdbcType == Types.TIME) || isTimestamp || isInResultString(2, "date")) { if (flags.length() > 0) flags.append(' '); flags.append("binary"); } if (isInResultString(2, "enum")) { if (flags.length() > 0) flags.append(' '); flags.append("enum"); } if (isInResultString(2, "set")) { if (flags.length() > 0) flags.append(' '); flags.append("set"); } if (isInResultString(7, "auto_increment")) { if (flags.length() > 0) flags.append(' '); flags.append("auto_increment"); } if (isTimestamp) { if (flags.length() > 0) flags.append(' '); flags.append("timestamp"); } return env.createString(flags.toString()); } catch (SQLException e) { log.log(Level.FINE, e.toString(), e); return BooleanValue.FALSE; } } /** * Get Mysql type string * * @param fieldOffset the field number (0-based) * @return the Mysql type */ protected String getMysqlType(int fieldOffset) { try { ResultSetMetaData md = getMetaData(); return md.getColumnTypeName(fieldOffset + 1); } catch (Exception e) { log.log(Level.FINE, e.toString(), e); return null; } } /** * Given the JDBC type of the field at the given offset, * return a PHP type string. */ protected String getFieldType(int fieldOffset, int jdbcType) { if (jdbcType == Types.TIMESTAMP) { // The Mysql types DATETIME and TIMESTAMP both map to Types.TIMESTAMP String mysqlType = getMysqlType(fieldOffset); if (mysqlType.equals("TIMESTAMP")) { return TIMESTAMP; } else { return DATETIME; } } else if (jdbcType == Types.DATE) { // The Mysql types DATE and YEAR both map to Types.DATE String mysqlType = getMysqlType(fieldOffset); if (mysqlType.equals("YEAR")) { return YEAR; } else { return DATE; } } else { return super.getFieldType(fieldOffset, jdbcType); } } /** * @see Value fetchFieldDirect * * increments the fieldOffset counter by one; * * @param env the PHP executing environment * @return */ protected Value fetchNextField(Env env) { int fieldOffset = getFieldOffset(); Value result = fetchFieldDirect(env, fieldOffset); setFieldOffset(fieldOffset + 1); return result; } /** * Sets the field metadata cursor to the * given offset. The next call to * mysqli_fetch_field() will retrieve the * field definition of the column associated * with that offset. * * @param env the PHP executing environment * @return previous value of field cursor */ public boolean field_seek(Env env, int offset) { boolean success = setFieldOffset(offset); if (! success) env.invalidArgument("field", offset); return success; } /** * Get current field offset of a result pointer. * * @param env the PHP executing environment * @return current offset of field cursor */ public int field_tell(Env env) { return getFieldOffset(); } /** * Closes the result. */ public void free() { close(); } /** * Closes the result */ public void free_result() { close(); } /** * * @param env the PHP executing environment * @return array of fieldDirect objects */ public Value getFieldDirectArray(Env env) { ArrayValue array = new ArrayValueImpl(); try { int numColumns = getMetaData().getColumnCount(); for (int i = 0; i < numColumns; i++) { array.put(fetchFieldDirect(env, i)); } return array; } catch (SQLException e) { log.log(Level.FINE, e.toString(), e); return BooleanValue.FALSE; } } /** * Get the number of fields in the result set. * * @return the number of columns in the result set */ public int num_fields() { return getFieldCount(); } /** * Get the number of rows in the result set. * * @return the number of rows in the result set */ public Value num_rows() { int numRows = getNumRows(); if (numRows < 0) { return BooleanValue.FALSE; } return LongValue.create(numRows); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?