X-Git-Url: http://git.osdn.net/view?p=nucleus-jp%2Fnucleus-next.git;a=blobdiff_plain;f=nucleus%2Flibs%2Fsql%2FMYSQLPDO.php;h=65bcb486802102e3781228a08b1dbb5e8cc6982b;hp=2142ff51633bba13bd0d22c8ef0b8224b22740df;hb=c90b0980cfa3e79cd4bc7eed551a64a5e2b02a5c;hpb=fe4e862f6b23596d657c5228588785dfe537fb8f diff --git a/nucleus/libs/sql/MYSQLPDO.php b/nucleus/libs/sql/MYSQLPDO.php index 2142ff5..65bcb48 100644 --- a/nucleus/libs/sql/MYSQLPDO.php +++ b/nucleus/libs/sql/MYSQLPDO.php @@ -1,3 +1,4 @@ +<<<<<<< HEAD dbcon = @call_user_func(MysqlPDO::$handler . 'connect', $host, $username, $password); + + if ( $this->dbcon == FALSE ) + { + throw new PDOException('Failed to connect to the server.', '01000'); + } + + // select database + if ( preg_match('/dbname=([^;]+)/', $dsn, $matches) ) + { + $dbname = $matches[1]; + if ( $dbname ) + { + if ( MysqlPDO::$handler == 'mysql_' ) + { + call_user_func(MysqlPDO::$handler . 'select_db', $dbname, $this->dbcon); + } + else + { + call_user_func(MysqlPDO::$handler . 'select_db', $this->dbcon, $dbname); + } + + // set use character + $charset = 'utf8'; + if ( is_array($driver_options) && array_key_exists(PDO::MYSQL_ATTR_INIT_COMMAND, $driver_options) ) + { + if ( preg_match('/SET\s+CHARACTER\s+SET\s+\'?([a-z0-9_-]+)\'?/', $driver_options[PDO::MYSQL_ATTR_INIT_COMMAND], $matches) ) + { + $charset = $matches[1]; + } + } + $server_info = call_user_func(MysqlPDO::$handler . 'get_server_info', $this->dbcon); + $mysql_version = preg_replace('/^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{1,2})(.*)$/', '$1.$2.$3', $server_info); + + if ( version_compare($mysql_version, '5.0.7', '>=') && function_exists(MysqlPDO::$handler . 'set_charset') ) + { + call_user_func(MysqlPDO::$handler . 'set_charset', $charset); + } + else if ( version_compare($mysql_version, '5.0.7', '<') ) + { + $this->exec("SET CHARACTER SET '{$charset}';"); + } + else + { + $this->exec("SET NAMES '{$charset}';"); + } + } + } + } + + /** + * Close the connection to the MySQL server. + */ + public function __destruct() + { + if ( $this->dbcon ) + { + @call_user_func(MysqlPDO::$handler . 'close', $this->dbcon); + } + } + + /** + * Not supported + */ + public function beginTransaction() + { + return FALSE; + } + + /** + * Not supported + */ + public function commit() + { + return FALSE; + } + + /** + * Fetch the SQLSTATE associated with the last operation. + * However, if successful '00000' Otherwise, the return to '01000'. + * @return string Error code + */ + public function errorCode() + { + $errno = call_user_func(MysqlPDO::$handler . 'errno', $this->dbcon); + return $errno === 0 ? '00000' : '01000'; + } + + /** + * To get extended error information associated with the last operation. + * Element 0: if successful '00000' Otherwise, the return to '01000' + * Element 1: The return value of mysql_errno() + * Element 2: The return value of mysql_error() + * @return array Array of error information + */ + public function errorInfo() + { + $errno = call_user_func(MysqlPDO::$handler . 'errno', $this->dbcon); + $error = call_user_func(MysqlPDO::$handler . 'error', $this->dbcon); + return array($errno === 0 ? '00000' : '01000', $errno, $error); + } + + public function exec($statement) + { + $result = @call_user_func(MysqlPDO::$handler . 'query', $statement, $this->dbcon); + if ( $result === TRUE ) + { + return @call_user_func(MysqlPDO::$handler . 'affected_rows', $this->dbcon); + } + else if ( is_resource($result) ) + { + return; + } + return FALSE; + } + + public function getAttribute($attribute) + { + switch ( $attribute ) + { + case PDO::ATTR_SERVER_VERSION: + return call_user_func(MysqlPDO::$handler . 'get_server_info', $this->dbcon); + break; + case PDO::ATTR_CLIENT_VERSION: + return call_user_func(MysqlPDO::$handler . 'get_client_info'); + break; + default: + return FALSE; + } + } + + public static function getAvailableDrivers() + { + return array('mysql'); + } + + /** + * Not supported + */ + public function inTransaction() + { + return FALSE; + } + + public function lastInsertId($name = null) + { + return call_user_func(MysqlPDO::$handler . 'insert_id', $this->dbcon); + } + + /** + * Not supported + */ + public function prepare($statement, $driver_options = array()) + { + return FALSE; + } + + public function query($statement) + { + $result = @call_user_func(MysqlPDO::$handler . 'query', $statement, $this->dbcon); + return ($result == FALSE) ? FALSE : new MysqlPDOStatement($statement, $result, $this->dbcon); + } + + public function quote($string, $parameter_type = PDO::PARAM_STR) + { + switch ( $parameter_type ) + { + case PDO::PARAM_NULL: + return 'null'; + case PDO::PARAM_BOOL: + return $string ? '1' : '0'; + default: + if ( $parameter_type == PDO::PARAM_INT && is_numeric($string) ) + { + return $string; + } + else + { + return '\'' . call_user_func(MysqlPDO::$handler . 'real_escape_string', $string) . '\''; + } + } + } + + /** + * Not supported + */ + public function rollBack() + { + return FALSE; + } + + /** + * Not supported + */ + public function setAttribute($attribute, $value) + { + return FALSE; + } +} + +/** + * MysqlPDOStatement class PDOStatement class like. + */ +class MysqlPDOStatement implements Iterator +{ + private $result; + private $dbcon; + private $_queryString = ''; + + private $def_fetch_mode = PDO::FETCH_BOTH; + private $def_col_num = 0; + private $def_class_name = 'stdClass'; + private $def_ctorargs = null; + private $bind_object = null; + + public function __get($name) + { + if ( $name == 'queryString' ) + { + return $this->_queryString; + } + } + + public function __construct($query, $result, $dbconnect = null) + { + $this->dbcon = $dbconnect; + $this->_queryString = $query; + $this->result = $result; + } + + public function __destruct() + { + $this->result = null; + } + + /** + * Not supported + */ + public function bindColumn($column, &$param, $type, $maxlen, $driverdata) + { + return FALSE; + } + + /** + * Not supported + */ + public function bindParam($parameter, &$variable, $data_type = PDO::PARAM_STR, $length, $driver_options) + { + return FALSE; + } + + /** + * Not supported + */ + public function bindValue($parameter, $value, $data_type = PDO::PARAM_STR) + { + return FALSE; + } + + public function closeCursor() + { + return call_user_func(MysqlPDO::$handler . 'free_result', $this->result); + } + + public function columnCount() + { + return call_user_func(MysqlPDO::$handler . 'num_fields', $this->result); + } + + /** + * Not supported + */ + public function debugDumpParams() + { + return; + } + + public function errorCode() + { + $errno = call_user_func(MysqlPDO::$handler . 'errno', $this->dbcon); + return $errno === 0 ? '00000' : '01000'; + } + + public function errorInfo() + { + $errno = call_user_func(MysqlPDO::$handler . 'errno', $this->dbcon); + $error = call_user_func(MysqlPDO::$handler . 'error', $this->dbcon); + return array($errno === 0 ? '00000' : '01000', $errno, $error); + } + + /** + * Not supported + */ + public function execute($input_parameters) + { + return FALSE; + } + + public function fetch($fetch_style = PDO::ATTR_DEFAULT_FETCH_MODE, $cursor_orientation = PDO::FETCH_ORI_NEXT, $cursor_offset = 0) + { + if ( !is_resource($this->result) || $cursor_orientation != PDO::FETCH_ORI_NEXT ) + { + return FALSE; + } + + if ( $fetch_style == PDO::ATTR_DEFAULT_FETCH_MODE ) + { + $fetch_style = $this->def_fetch_mode; + } + + switch ( $fetch_style ) + { + case PDO::FETCH_ASSOC: + return @call_user_func(MysqlPDO::$handler . 'fetch_array', $this->result, MYSQL_ASSOC); + case PDO::FETCH_BOTH: + return @call_user_func(MysqlPDO::$handler . 'fetch_array', $this->result, MYSQL_BOTH); + case PDO::FETCH_NUM: + return @call_user_func(MysqlPDO::$handler . 'fetch_array', $this->result, MYSQL_NUM); + case PDO::FETCH_OBJ: + return $this->fetchObject(); + case PDO::FETCH_CLASS: + return $this->fetchObject($this->def_class_name, $this->def_ctorargs); + case PDO::FETCH_COLUMN: + return $this->fetchColumn($this->def_col_num); + case PDO::FETCH_BOUND: + return FALSE; // Not supported + case PDO::FETCH_INTO: + return FALSE; // Not supported + case PDO::FETCH_LAZY: + return FALSE; // Not supported + default: + return FALSE; + } + } + + public function fetchAll($fetch_style = PDO::ATTR_DEFAULT_FETCH_MODE, $fetch_argument = null, $ctor_args = array()) + { + if ( $fetch_style == PDO::ATTR_DEFAULT_FETCH_MODE ) + { + $fetch_style = PDO::FETCH_BOTH; + } + + $ret = array(); + if ( ($fetch_style & PDO::FETCH_COLUMN) != 0 ) + { + if ( $fetch_style == PDO::FETCH_COLUMN ) + { + $column = $fetch_argument == null ? 0 : intval($fetch_argument); + while ( $row = $this->fetchColumn($column) ) + { + $ret[] = $row; + } + } + elseif ( ($fetch_style & PDO::FETCH_UNIQUE) != 0 ) + { + return FALSE; + } + elseif ( ($fetch_style & PDO::FETCH_GROUP) != 0 ) + { + return FALSE; + } + } + elseif ( $fetch_style == PDO::FETCH_CLASS ) + { + while ( $row = $this->fetchObject($fetch_argument, $ctor_args) ) + { + $ret[] = $row; + } + } + elseif ( $fetch_style == PDO::FETCH_FUNC ) + { + while ( $row = $this->fetch(PDO::FETCH_ASSOC) ) + { + $ret[] = call_user_func_array($fetch_argument, array_values($row)); + } + } + else + { + while ( $row = $this->fetch($fetch_style) ) + { + $ret[] = $row; + } + } + return $ret; + } + + public function fetchColumn($column_number = 0) + { + if ( $ret = $this->fetch(PDO::FETCH_NUM) ) + { + return $ret[$column_number]; + } + return FALSE; + } + + public function fetchObject($class_name = 'stdClass', $ctor_args = null) + { + if ( is_array($ctor_args) && !empty($ctor_args) ) + { + return @call_user_func(MysqlPDO::$handler . 'fetch_object', $this->result, $class_name, $ctor_args); + } + else + { + return @call_user_func(MysqlPDO::$handler . 'fetch_object', $this->result, $class_name); + } + } + + public function getAttribute($attribute) + { + switch ( $attribute ) + { + default: + return FALSE; + } + } + + /** + * Not supported + */ + public function getColumnMeta($column) + { + $result = array(); + if ( MysqlPDO::$handler == 'mysql_' ) + { + $result['name'] = @call_user_func(MysqlPDO::$handler . 'field_name', $this->result, $column); + } + return $result; + } + + /** + * Not supported + */ + public function nextRowset() + { + return FALSE; + } + + public function rowCount() + { + return @call_user_func(MysqlPDO::$handler . 'affected_rows', $this->dbcon); + } + + /** + * Not supported + */ + public function setAttribute($attribute, $value) + { + return FALSE; + } + + public function setFetchMode($mode, &$mode_argument, $ctorargs) + { + switch ( $mode ) + { + case PDO::FETCH_COLUMN: + $this->def_col_num = $mode_argument; + break; + case PDO::FETCH_CLASS: + $this->def_class_name = $mode_argument; + $this->def_ctorargs = $ctorargs; + break; + case PDO::FETCH_INTO: + $this->bind_object = &$mode_argument; + return FALSE; // Not supported + default: + $this->def_fetch_mode = $mode; + break; + } + return 1; + } + + // Iterator + private $iterator_value; + + function rewind() + {} + + function next() + {} + + function valid() + { + return ($this->iterator_value = $this->fetch()); + } + + function current() + { + return $this->iterator_value; + } + + function key() + { + return null; + } +} +>>>>>>> skinnable-master