mysqlimodule.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,441 行 · 第 1/3 页

JAVA
1,441
字号
/* * 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.NotNull;import com.caucho.quercus.annotation.Optional;import com.caucho.quercus.annotation.Reference;import com.caucho.quercus.annotation.ReturnNullAsFalse;import com.caucho.quercus.env.*;import com.caucho.quercus.module.AbstractQuercusModule;import com.caucho.util.L10N;import com.caucho.util.Log;import java.util.logging.Level;import java.util.logging.Logger;/** * Quercus mysql routines. */public class MysqliModule extends AbstractQuercusModule {  private static final Logger log = Log.open(MysqliModule.class);  private static final L10N L = new L10N(MysqliModule.class);  public static final int MYSQLI_ASSOC = JdbcResultResource.FETCH_ASSOC;  public static final int MYSQLI_NUM = JdbcResultResource.FETCH_NUM;  public static final int MYSQLI_BOTH = JdbcResultResource.FETCH_BOTH;  public static final int MYSQLI_USE_RESULT = 0x0;  public static final int MYSQLI_STORE_RESULT = 0x1;  // Used by mysqli_fetch_field.  public static final int NOT_NULL_FLAG = 0x1;  public static final int PRI_KEY_FLAG = 0x2;  public static final int UNIQUE_KEY_FLAG = 0x4;  public static final int MULTIPLE_KEY_FLAG = 0x8;  public static final int BLOB_FLAG = 0x10;  public static final int UNSIGNED_FLAG = 0x20;  public static final int ZEROFILL_FLAG = 0x40;  public static final int BINARY_FLAG = 0x80;  // Sent to new clients  public static final int ENUM_FLAG = 0x100;  public static final int AUTO_INCREMENT_FLAG = 0x200;  public static final int TIMESTAMP_FLAG = 0x400;  public static final int SET_FLAG = 0x800;  public static final int NUM_FLAG = 0x8000;  public static final int PART_KEY_FLAG = 0x4000; //Intern: Part of some key???  public static final int GROUP_FLAG = 0x8000;    //Intern: Group field???  public static final int UNIQUE_FLAG = 0x10000;   //Intern: Used by sql_yacc???  public static final int BINCMP_FLAG = 0x20000;  //Intern: Used by sql_yacc???  // The following are numerical respresentations  // of types returned by mysqli_fetch_field.  // These are defined in mysql's mysql_com.h header.  public static final int MYSQLI_TYPE_DECIMAL = 0x0;  public static final int MYSQLI_TYPE_TINY = 0x1;  public static final int MYSQLI_TYPE_SHORT = 0x2;  public static final int MYSQLI_TYPE_LONG = 0x3;  public static final int MYSQLI_TYPE_FLOAT = 0x4;  public static final int MYSQLI_TYPE_DOUBLE = 0x5;  public static final int MYSQLI_TYPE_NULL = 0x6;  public static final int MYSQLI_TYPE_TIMESTAMP = 0x7;  public static final int MYSQLI_TYPE_LONGLONG = 0x8;  public static final int MYSQLI_TYPE_INT24 = 0x9;  public static final int MYSQLI_TYPE_DATE = 0xA;  public static final int MYSQLI_TYPE_TIME = 0xB;  public static final int MYSQLI_TYPE_DATETIME = 0xC;  public static final int MYSQLI_TYPE_YEAR = 0xD;  public static final int MYSQLI_TYPE_NEWDATE = 0xE;  // Mysql defines the constant MYSQL_TYPE_VARCHAR = 0xF  // but there is no MYSQLI_TYPE_VARCHAR flag.  public static final int MYSQLI_TYPE_BIT = 0x10;  public static final int MYSQLI_TYPE_NEWDECIMAL = 0xF6;  public static final int MYSQLI_TYPE_ENUM = 0xF7;  public static final int MYSQLI_TYPE_SET = 0xF8;  public static final int MYSQLI_TYPE_TINY_BLOB = 0xF9;  public static final int MYSQLI_TYPE_MEDIUM_BLOB = 0xFA;  public static final int MYSQLI_TYPE_LONG_BLOB = 0xFB;  public static final int MYSQLI_TYPE_BLOB = 0xFC;  public static final int MYSQLI_TYPE_VAR_STRING = 0xFD;  public static final int MYSQLI_TYPE_STRING = 0xFE;  public static final int MYSQLI_TYPE_GEOMETRY = 0xFF;  public static final int MYSQLI_TYPE_CHAR = MYSQLI_TYPE_TINY;  public static final int MYSQLI_TYPE_INTERVAL = MYSQLI_TYPE_ENUM;  public static final int MYSQL_CLIENT_COMPRESS = (1 << 5);  // The next constant is NOT exported by this module,  // but the PHP documentation states that 128 can be passed  // in the client_flags parameter to mysql_connect().  // This flag will be ignored by Mysqli.connectImpl().  private static final int MYSQL_CLIENT_LOCAL_FILES = (1 << 7);  public static final int MYSQL_CLIENT_IGNORE_SPACE = (1 << 8);  public static final int MYSQL_CLIENT_INTERACTIVE = (1 << 10);  public static final int MYSQL_CLIENT_SSL = (1 << 11);  // mysqli_options option flags  public static final int MYSQLI_READ_DEFAULT_GROUP = 0x0;  public static final int MYSQLI_READ_DEFAULT_FILE = 0x1;  public static final int MYSQLI_OPT_CONNECT_TIMEOUT = 0x2;  public static final int MYSQLI_OPT_LOCAL_INFILE = 0x3;  public static final int MYSQLI_INIT_COMMAND = 0x4;  public MysqliModule()  {  }  /**   * Returns true for the mysql extension.   */  public String []getLoadedExtensions()  {    return new String[] { "mysqli" };  }  /**   * Returns the number of affected rows.   */  public static int mysqli_affected_rows(@NotNull Mysqli conn)  {    if (conn == null)      return -1;    return conn.affected_rows();  }  /**   * Turns auto-commit on or off.   */  public static boolean mysqli_autocommit(@NotNull Mysqli conn, boolean mode)  {    if (conn == null)      return false;    return conn.autocommit(mode);  }  /**   * Deprecated alias for {@link #mysqli_stmt_bind_param}.   */  public static boolean mysqli_bind_param(Env env,                                          @NotNull MysqliStatement stmt,                                          StringValue types,                                          @Reference Value[] params)  {    return mysqli_stmt_bind_param(env, stmt, types, params);  }  /**   * Commits the current transaction for the supplied connection.   *   * returns true on success or false on failure   */  public static boolean mysqli_commit(@NotNull Mysqli conn)  {    if (conn == null)      return false;    return conn.commit();  }  /**   * Returns the client encoding.   */  public static Value mysqli_character_set_name(Env env, @NotNull Mysqli conn)  {    if (conn == null)      return NullValue.NULL;    return conn.character_set_name(env);  }  /**   * Alias for {@link #mysqli_character_set_name}.   */  public static Value mysqli_client_encoding(Env env, @NotNull Mysqli conn)  {    return mysqli_character_set_name(env, conn);  }  /**   * Closes a connection.   */  public static boolean mysqli_close(Env env, @NotNull Mysqli conn)  {    if (conn == null)      return false;    else if (! conn.isConnected()) {      env.warning(L.l("no MySQLi-Link resource supplied"));      return false;    }    return conn.close(env);  }  /**   * Returns a new connection.   */  @ReturnNullAsFalse  public static Mysqli mysqli_connect(Env env,              @Optional("localhost") StringValue host,              @Optional StringValue userName,              @Optional StringValue password,              @Optional StringValue dbname,              @Optional("3306") int port,              @Optional StringValue socket)    throws IllegalStateException  {    Mysqli mysqli = new Mysqli(env,                               host,                               userName,                               password,                               dbname,                               port,                               socket);    if (! mysqli.isConnected())      return null;    return mysqli;  }  /**   * Returns an error code value for the last call to mysqli_connect(),   * 0 for no previous error.   */  public static int mysqli_connect_errno(Env env)  {    Value value = (Value) env.getSpecialValue("mysqli.connectErrno");    if (value != null)      return value.toInt();    else      return 0;  }  /**   * Returns an error description for the last call to mysqli_connect(),   * "" for no previous error.   */  public static StringValue mysqli_connect_error(Env env)  {    Object error = env.getSpecialValue("mysqli.connectError");    if (error != null)      return env.createString(error.toString());    else      return env.getEmptyString();  }  /**   * Seeks the specified row.   *   * @param env the PHP executing environment   * @param result the mysqli_result   * @param rowNumber the row offset   * @return true on success or false if the row number   * does not exist. NULL is returned if an error occurred.   */  public static Value mysqli_data_seek(Env env,                                       @NotNull MysqliResult result,                                       int rowNumber)  {    if (result == null)      return NullValue.NULL;    if (result.seek(env, rowNumber)) {      return BooleanValue.TRUE;    } else {      env.warning(L.l("Offset {0} is invalid for MySQL (or the query data is unbuffered)",                      rowNumber));      return BooleanValue.FALSE;    }  }  /**   * Returns the error code for the most recent function call,   * 0 for no error.   */  public static Value mysqli_errno(@NotNull Mysqli conn)  {    if (conn == null)      return NullValue.NULL;    return LongValue.create(conn.errno());  }  /**   * Alias for {@link #mysqli_real_escape_string}   */  public static Value mysqli_escape_string(Env env,                                           @NotNull Mysqli conn,                                           StringValue unescapedString)  {    return mysqli_real_escape_string(env, conn, unescapedString);  }  /**   * Deprecated alias for {@link #mysqli_stmt_fetch}.   */  public static Value mysqli_fetch(Env env,                                   MysqliStatement stmt)  {    return mysqli_stmt_fetch(env, stmt);  }  /**   * Returns the field metadata.   *   */  public static Value mysqli_fetch_field_direct(Env env,                                                @NotNull MysqliResult result,                                                int fieldOffset)  {    if (result == null)      return BooleanValue.FALSE;    return result.fetch_field_direct(env, fieldOffset);  }  /**   * Returns the field metadata.   */  public static Value mysqli_fetch_field(Env env,                                         @NotNull MysqliResult result)  {    if (result == null)      return BooleanValue.FALSE;    return result.fetch_field(env);  }  /**   * Returns an array of field metadata.   */  public static Value mysqli_fetch_fields(Env env,                                          @NotNull MysqliResult result)  {    if (result == null)      return BooleanValue.FALSE;    return result.fetch_fields(env);  }  /**   * Returns an array of integers respresenting the size of each column   * FALSE if an error occurred.   *   * @param env the PHP executing environment   * @param result the mysqli_result   * @return true on success or false if an error occurred.   * NULL is returned if result is null.   */  public static Value mysqli_fetch_lengths(Env env,                                           @NotNull MysqliResult result)  {    if (result == null)      return NullValue.NULL;    return result.fetch_lengths();  }  /**   * Seeks to the specified field offset.   * If the next call to mysql_fetch_field() doesn't include   * a field offset, the field offset specified in   * mysqli_field_seek() will be returned.   */  public static boolean mysqli_field_seek(Env env,                                          @NotNull MysqliResult result,                                          int fieldOffset)  {    if (result == null)      return false;    return result.field_seek(env, fieldOffset);  }  /**   * Returns the position of the field cursor used for the last   * mysqli_fetch_field() call. This value can be used as an   * argument to mysqli_field_seek()   */  public static int mysqli_field_tell(Env env,              @NotNull MysqliResult result)  {    if (result == null)      return -1;    return result.field_tell(env);  }  /**   * Frees a mysqli result   */  public static boolean mysqli_free_result(@NotNull MysqliResult result)  {    if (result == null)      return false;    result.close();    return true;  }  /**   * Returns ID generated for an AUTO_INCREMENT column by the previous   * INSERT query on success, 0 if the previous query does not generate   * an AUTO_INCREMENT value, or FALSE if no MySQL connection was established   */  public static Value mysqli_insert_id(Env env,                                       @NotNull Mysqli conn)  {    if (conn == null)      return BooleanValue.FALSE;    return conn.insert_id(env);  }  /**   * Returns the number of fields from specified result set.   */  public static Value mysqli_num_fields(@NotNull MysqliResult result)  {    if (result == null)      return NullValue.NULL;    return LongValue.create(result.num_fields());  }  /**   * Executes one or multiple queires which are   * concatenated by a semicolon.   */  public static boolean mysqli_multi_query(Env env,                                           @NotNull Mysqli conn,                                           StringValue query)  {    if (conn == null)      return false;    return conn.multi_query(env, query);  }  /**

⌨️ 快捷键说明

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