mysqlmodule.java

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

JAVA
1,189
字号
/* * 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 Scott Ferguson */package com.caucho.quercus.lib.db;import com.caucho.quercus.annotation.NotNull;import com.caucho.quercus.annotation.Optional;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.sql.Connection;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;/** * PHP mysql routines. */public class MysqlModule extends AbstractQuercusModule {  private static final Logger log = Log.open(MysqlModule.class);  private static final L10N L = new L10N(MysqlModule.class);  public static final int MYSQL_ASSOC = JdbcResultResource.FETCH_ASSOC;  public static final int MYSQL_NUM = JdbcResultResource.FETCH_NUM;  public static final int MYSQL_BOTH = JdbcResultResource.FETCH_BOTH;  public static final int MYSQL_USE_RESULT = 0x0;  public static final int MYSQL_STORE_RESULT = 0x1;  public MysqlModule()  {  }  /**   * Returns true for the mysql extension.   */  public String []getLoadedExtensions()  {    return new String[] { "mysql" };  }  /**   * Returns the number of affected rows.   */  public static int mysql_affected_rows(Env env, @Optional Mysqli conn)  {    if (conn == null)      conn = getConnection(env);    return conn.affected_rows();  }  /**   * Get information about the most recent query.   */  public static Value mysql_info(Env env, @Optional Mysqli conn)  {    if (conn == null)      conn = getConnection(env);    return conn.info(env);  }  /**   * Change the logged in user of the current active connection.   * This function is deprecated and was removed from PHP in PHP 3.0.14.   */  public static boolean mysql_change_user(Env env,                               StringValue user,                               StringValue pass,                               @Optional StringValue database,                               @Optional Mysqli conn)  {    return false;  }  /**   * Returns the client encoding   */  public static StringValue mysql_client_encoding(Env env, @Optional Mysqli conn)  {    if (conn == null)      conn = getConnection(env);    return conn.client_encoding(env);  }  /**   * Closes a mysql connection.   */  public static boolean mysql_close(Env env, @Optional Mysqli conn)  {    Mysqli envConn = (Mysqli) env.getSpecialValue("caucho.mysql");    if (conn == null) {      if (envConn == null) {        // php/1435        env.warning(L.l("no MySQL-Link resource supplied"));        return false;      }      conn = envConn;    }    if (conn == envConn)      env.removeSpecialValue("caucho.mysql");    if (conn != null) {      conn.close(env);      return true;    }    else      return true;  }  /**   * Creates a database.   */  public static boolean mysql_create_db(Env env,                                 @NotNull StringValue name,                                 @Optional Mysqli conn)  {    if (name.length() == 0)      return false;    if (conn == null)      conn = getConnection(env);    Statement stmt = null;    // XXX: move implementation    try {      try {        Connection sqlConn = conn.validateConnection().getConnection(env);                if (sqlConn == null)          return false;                stmt = sqlConn.createStatement();        stmt.setEscapeProcessing(false);        stmt.executeUpdate("CREATE DATABASE " + name.toString());      } finally {        if (stmt != null)          stmt.close();      }    } catch (SQLException e) {      log.log(Level.FINE, e.toString(), e);      return false;    }    return true;  }  /**   * Moves the intenal row pointer of the MySQL result to the   * specified row number, 0 based.   */  public static boolean mysql_data_seek(Env env,                                        @NotNull MysqliResult result,                                        int rowNumber)  {    if (result == null)      return false;    if (result.seek(env, rowNumber)) {      return true;    } else {      env.warning(L.l("Offset {0} is invalid for MySQL (or the query data is unbuffered)",                      rowNumber));      return false;    }  }  /**   * Retrieves the database name after a call to mysql_list_dbs()   */  public static Value mysql_db_name(Env env,                             @NotNull MysqliResult result,                             int row,                             @Optional("0") Value field)  {    if (result == null)      return BooleanValue.FALSE;    return mysql_result(env, result, row, field);  }  /**   * Deprecated alias for mysql_db_name   */  public static Value mysql_dbname(Env env,                             @NotNull MysqliResult result,                             int row)  {    return mysql_db_name(env, result, row,                         env.createString("0"));  }  /**   * Returns the value of one field in the result set. FALSE on failure.   */  public static Value mysql_result(Env env,                            @NotNull MysqliResult result,                            int row,                            @Optional("0") Value field)  {    if (result == null)      return BooleanValue.FALSE;    return result.getResultField(env, row, field);  }  /**   * Drops a database.   */  public static boolean mysql_drop_db(Env env,                               @NotNull StringValue databaseName,                               @Optional Mysqli conn)  {    if (databaseName.length() == 0)      return false;    Value value = mysql_query(env,      env.createString("DROP DATABASE " + databaseName),      conn);    return (value != null && value.toBoolean());  }  /**   * Deprecated alias for mysql_drop_db.   */  public static boolean mysql_dropdb(Env env,                             @NotNull StringValue databaseName,                             @Optional Mysqli conn)  {    return mysql_drop_db(env, databaseName, conn);  }  /**   * Returns the error number of the most recent error   */  public static int mysql_errno(Env env, @Optional Mysqli conn)  {    if (conn == null)      conn = getConnection(env);    StringValue error = conn.error(env);    int errno = conn.errno();    if (errno != 0)      return errno;    else if (error.length() != 0)      return 2006; // mysql has gone away    else      return 0;  }  /**   * Returns the most recent error.   */  public static StringValue mysql_error(Env env, @Optional Mysqli conn)  {    if (conn == null)      conn = getConnection(env);    return conn.error(env);  }  /**   * Deprecated, mysql_real_escape_string() should be used instead.   *   * @see StringValue MysqlModule.mysql_real_escape_string(String, Mysqli)   *   * @return the escaped string   */  public static StringValue mysql_escape_string(Env env, Value val)  {    StringValue unescapedString = val.toStringValue();        StringValue sb = unescapedString.createStringBuilder();        int len = unescapedString.length();    for (int i = 0; i < len; i++) {      char ch = unescapedString.charAt(i);      switch(ch) {        case 0:          sb.append('\\');          sb.append(0);          break;        case '\n':          sb.append('\\');          sb.append('n');          break;        case '\r':          sb.append('\\');          sb.append('r');          break;        case '\\':          sb.append('\\');          sb.append('\\');          break;        case '\'':          sb.append('\\');          sb.append('\'');          break;        case '"':          sb.append('\\');          sb.append('"');          break;        case 0x1A:          sb.append('\\');          sb.append('Z');          break;        default:          sb.append(ch);      }    }        return sb;  }  /**   * Escapes special characters.   *   * @see StringValue MysqliModule.mysqli_real_escape_string(JdbcConnectionResource, String)   *   * @return the escaped string   */  public static StringValue mysql_real_escape_string(Env env,                                                     Value val,                                                     @Optional Mysqli conn)  {    StringValue unescapedString = val.toStringValue();        if (conn == null)      conn = getConnection(env);    return conn.real_escape_string(unescapedString);  }  /**   * Returns a row from the connection   */  public static Value mysql_fetch_array(Env env,                                 @NotNull MysqliResult result,                                 @Optional("MYSQL_BOTH") int type)  {    if (result == null)      return BooleanValue.FALSE;    Value value = result.fetch_array(env, type);    if (value != null)      return value;

⌨️ 快捷键说明

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