3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2012 The Nucleus Group
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 * (see nucleus/documentation/index.html#license for more info)
13 * @license http://nucleuscms.org/license.txt GNU General Public License
14 * @copyright Copyright (C) 2012 The Nucleus Group
18 if ( !class_exists('PDO') )
22 * Dummy constant of the PDO class
33 const PARAM_INPUT_OUTPUT = 128; // orignal is undefined.
35 const FETCH_ASSOC = 2;
39 const FETCH_BOUND = 6;
40 const FETCH_COLUMN = 7;
41 const FETCH_CLASS = 8;
43 const FETCH_FUNC = 10;
44 const FETCH_NAMED = 11;
45 const FETCH_KEY_PAIR = 12;
46 const FETCH_GROUP = 65536;
47 const FETCH_UNIQUE = 196608;
48 const FETCH_CLASSTYPE = 262144;
49 const FETCH_SERIALIZE = 524288;
50 const FETCH_PROPS_LATE = 1048576;
51 const ATTR_AUTOCOMMIT = 0;
52 const ATTR_PREFETCH = 1;
53 const ATTR_TIMEOUT = 2;
54 const ATTR_ERRMODE = 3;
55 const ATTR_SERVER_VERSION = 4;
56 const ATTR_CLIENT_VERSION = 5;
57 const ATTR_SERVER_INFO = 6;
58 const ATTR_CONNECTION_STATUS = 7;
60 const ATTR_CURSOR_NAME = 9;
61 const ATTR_CURSOR = 10;
62 const ATTR_ORACLE_NULLS = 11;
63 const ATTR_PERSISTENT = 12;
64 const ATTR_STATEMENT_CLASS = 13;
65 const ATTR_FETCH_TABLE_NAMES = 14;
66 const ATTR_FETCH_CATALOG_NAMES = 15;
67 const ATTR_DRIVER_NAME = 16;
68 const ATTR_STRINGIFY_FETCHES = 17;
69 const ATTR_MAX_COLUMN_LEN = 18;
70 const ATTR_DEFAULT_FETCH_MODE = 19;
71 const ATTR_EMULATE_PREPARES = 20;
72 const ERRMODE_SILENT = 0;
73 const ERRMODE_WARNING = 1;
74 const ERRMODE_EXCEPTION = 2;
75 const CASE_NATURAL = 0;
78 const NULL_NATURAL = 0;
79 const NULL_EMPTY_STRING = 1;
80 const NULL_TO_STRING = 2;
81 const FETCH_ORI_NEXT = 0;
82 const FETCH_ORI_PRIOR = 1;
83 const FETCH_ORI_FIRST = 2;
84 const FETCH_ORI_LAST = 3;
85 const FETCH_ORI_ABS = 4;
86 const FETCH_ORI_REL = 5;
87 const CURSOR_FWDONLY = 0;
88 const CURSOR_SCROLL = 1;
89 // from here orignal is undefined.
90 const ERR_CANT_MAP = 0;
92 const ERR_CONSTRAINT = 0;
93 const ERR_NOT_FOUND = 0;
94 const ERR_ALREADY_EXISTS = 0;
95 const ERR_NOT_IMPLEMENTED = 0;
96 const ERR_MISMATCH = 0;
97 const ERR_TRUNCATED = 0;
98 const ERR_DISCONNECTED = 0;
99 const ERR_NO_PERM = 0;
101 const ERR_NONE = '00000';
102 const PARAM_EVT_ALLOC = 0;
103 const PARAM_EVT_FREE = 1;
104 const PARAM_EVT_EXEC_PRE = 2;
105 const PARAM_EVT_EXEC_POST = 3;
106 const PARAM_EVT_FETCH_PRE = 4;
107 const PARAM_EVT_FETCH_POST = 5;
108 const PARAM_EVT_NORMALIZE = 6;
110 const MYSQL_ATTR_INIT_COMMAND = 1002;
114 * PDOException class of dummy
116 class PDOException extends Exception
121 * MysqlPDO class that wraps the mysql_ or mysqli_ function like PDO class
125 // Prefix function name
126 public static $handler;
131 * Creates a PDO instance representing a connection to a MySQL database.
132 * @param string $dsn DSN
133 * @param string $username UserName
134 * @param string $password Password
135 * @param mixed $driver_options Options[optional]
136 * @throws PDOException Thrown when failed to connect to the database.
138 public function __construct($dsn, $username, $password, $driver_options = '')
140 // select use function
141 if ( function_exists('mysql_query') )
143 MysqlPDO::$handler = 'mysql_';
145 else if ( function_exists('mysqli_query') )
147 MysqlPDO::$handler = 'mysqli_';
151 throw new PDOException('Can not be found mysql_ or mysqli_ functions.', 'IM000');
154 if ( preg_match('/host=([^;]+)/', $dsn, $matches) )
160 throw new PDOException('Host has not been set.', '01000');
162 if ( preg_match('/port=([^;]+)/', $dsn, $matches) )
164 $host .= ':' . $matches[1];
168 $this->dbcon = @call_user_func(MysqlPDO::$handler . 'connect', $host, $username, $password);
170 if ( $this->dbcon == FALSE )
172 throw new PDOException('Failed to connect to the server.', '01000');
176 if ( preg_match('/dbname=([^;]+)/', $dsn, $matches) )
178 $dbname = $matches[1];
181 if ( MysqlPDO::$handler == 'mysql_' )
183 call_user_func(MysqlPDO::$handler . 'select_db', $dbname, $this->dbcon);
187 call_user_func(MysqlPDO::$handler . 'select_db', $this->dbcon, $dbname);
192 if ( is_array($driver_options) && array_key_exists(PDO::MYSQL_ATTR_INIT_COMMAND, $driver_options) )
194 if ( preg_match('/SET\s+CHARACTER\s+SET\s+\'?([a-z0-9_-]+)\'?/', $driver_options[PDO::MYSQL_ATTR_INIT_COMMAND], $matches) )
196 $charset = $matches[1];
199 $server_info = call_user_func(MysqlPDO::$handler . 'get_server_info', $this->dbcon);
200 $mysql_version = preg_replace('/^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{1,2})(.*)$/', '$1.$2.$3', $server_info);
202 if ( version_compare($mysql_version, '5.0.7', '>=') && function_exists(MysqlPDO::$handler . 'set_charset') )
204 call_user_func(MysqlPDO::$handler . 'set_charset', $charset);
206 else if ( version_compare($mysql_version, '5.0.7', '<') )
208 $this->exec("SET CHARACTER SET '{$charset}';");
212 $this->exec("SET NAMES '{$charset}';");
219 * Close the connection to the MySQL server.
221 public function __destruct()
225 @call_user_func(MysqlPDO::$handler . 'close', $this->dbcon);
232 public function beginTransaction()
240 public function commit()
246 * Fetch the SQLSTATE associated with the last operation.
247 * However, if successful '00000' Otherwise, the return to '01000'.
248 * @return string Error code
250 public function errorCode()
252 $errno = call_user_func(MysqlPDO::$handler . 'errno', $this->dbcon);
253 return $errno === 0 ? '00000' : '01000';
257 * To get extended error information associated with the last operation.
258 * Element 0: if successful '00000' Otherwise, the return to '01000'
259 * Element 1: The return value of mysql_errno()
260 * Element 2: The return value of mysql_error()
261 * @return array Array of error information
263 public function errorInfo()
265 $errno = call_user_func(MysqlPDO::$handler . 'errno', $this->dbcon);
266 $error = call_user_func(MysqlPDO::$handler . 'error', $this->dbcon);
267 return array($errno === 0 ? '00000' : '01000', $errno, $error);
270 public function exec($statement)
272 $result = @call_user_func(MysqlPDO::$handler . 'query', $statement, $this->dbcon);
273 if ( $result === TRUE )
275 return @call_user_func(MysqlPDO::$handler . 'affected_rows', $this->dbcon);
277 else if ( is_resource($result) )
284 public function getAttribute($attribute)
286 switch ( $attribute )
288 case PDO::ATTR_SERVER_VERSION:
289 return call_user_func(MysqlPDO::$handler . 'get_server_info', $this->dbcon);
291 case PDO::ATTR_CLIENT_VERSION:
292 return call_user_func(MysqlPDO::$handler . 'get_client_info');
299 public static function getAvailableDrivers()
301 return array('mysql');
307 public function inTransaction()
312 public function lastInsertId($name = null)
314 return call_user_func(MysqlPDO::$handler . 'insert_id', $this->dbcon);
320 public function prepare($statement, $driver_options = array())
325 public function query($statement)
327 $result = @call_user_func(MysqlPDO::$handler . 'query', $statement, $this->dbcon);
328 return ($result == FALSE) ? FALSE : new MysqlPDOStatement($statement, $result, $this->dbcon);
331 public function quote($string, $parameter_type = PDO::PARAM_STR)
333 switch ( $parameter_type )
335 case PDO::PARAM_NULL:
337 case PDO::PARAM_BOOL:
338 return $string ? '1' : '0';
340 if ( $parameter_type == PDO::PARAM_INT && is_numeric($string) )
346 return '\'' . call_user_func(MysqlPDO::$handler . 'real_escape_string', $string) . '\'';
354 public function rollBack()
362 public function setAttribute($attribute, $value)
369 * MysqlPDOStatement class PDOStatement class like.
371 class MysqlPDOStatement implements Iterator
375 private $_queryString = '';
377 private $def_fetch_mode = PDO::FETCH_BOTH;
378 private $def_col_num = 0;
379 private $def_class_name = 'stdClass';
380 private $def_ctorargs = null;
381 private $bind_object = null;
383 public function __get($name)
385 if ( $name == 'queryString' )
387 return $this->_queryString;
391 public function __construct($query, $result, $dbconnect = null)
393 $this->dbcon = $dbconnect;
394 $this->_queryString = $query;
395 $this->result = $result;
398 public function __destruct()
400 $this->result = null;
406 public function bindColumn($column, &$param, $type, $maxlen, $driverdata)
414 public function bindParam($parameter, &$variable, $data_type = PDO::PARAM_STR, $length, $driver_options)
422 public function bindValue($parameter, $value, $data_type = PDO::PARAM_STR)
427 public function closeCursor()
429 return call_user_func(MysqlPDO::$handler . 'free_result', $this->result);
432 public function columnCount()
434 return call_user_func(MysqlPDO::$handler . 'num_fields', $this->result);
440 public function debugDumpParams()
445 public function errorCode()
447 $errno = call_user_func(MysqlPDO::$handler . 'errno', $this->dbcon);
448 return $errno === 0 ? '00000' : '01000';
451 public function errorInfo()
453 $errno = call_user_func(MysqlPDO::$handler . 'errno', $this->dbcon);
454 $error = call_user_func(MysqlPDO::$handler . 'error', $this->dbcon);
455 return array($errno === 0 ? '00000' : '01000', $errno, $error);
461 public function execute($input_parameters)
466 public function fetch($fetch_style = PDO::ATTR_DEFAULT_FETCH_MODE, $cursor_orientation = PDO::FETCH_ORI_NEXT, $cursor_offset = 0)
468 if ( !is_resource($this->result) || $cursor_orientation != PDO::FETCH_ORI_NEXT )
473 if ( $fetch_style == PDO::ATTR_DEFAULT_FETCH_MODE )
475 $fetch_style = $this->def_fetch_mode;
478 switch ( $fetch_style )
480 case PDO::FETCH_ASSOC:
481 return @call_user_func(MysqlPDO::$handler . 'fetch_array', $this->result, MYSQL_ASSOC);
482 case PDO::FETCH_BOTH:
483 return @call_user_func(MysqlPDO::$handler . 'fetch_array', $this->result, MYSQL_BOTH);
485 return @call_user_func(MysqlPDO::$handler . 'fetch_array', $this->result, MYSQL_NUM);
487 return $this->fetchObject();
488 case PDO::FETCH_CLASS:
489 return $this->fetchObject($this->def_class_name, $this->def_ctorargs);
490 case PDO::FETCH_COLUMN:
491 return $this->fetchColumn($this->def_col_num);
492 case PDO::FETCH_BOUND:
493 return FALSE; // Not supported
494 case PDO::FETCH_INTO:
495 return FALSE; // Not supported
496 case PDO::FETCH_LAZY:
497 return FALSE; // Not supported
503 public function fetchAll($fetch_style = PDO::ATTR_DEFAULT_FETCH_MODE, $fetch_argument = null, $ctor_args = array())
505 if ( $fetch_style == PDO::ATTR_DEFAULT_FETCH_MODE )
507 $fetch_style = PDO::FETCH_BOTH;
511 if ( ($fetch_style & PDO::FETCH_COLUMN) != 0 )
513 if ( $fetch_style == PDO::FETCH_COLUMN )
515 $column = $fetch_argument == null ? 0 : intval($fetch_argument);
516 while ( $row = $this->fetchColumn($column) )
521 elseif ( ($fetch_style & PDO::FETCH_UNIQUE) != 0 )
525 elseif ( ($fetch_style & PDO::FETCH_GROUP) != 0 )
530 elseif ( $fetch_style == PDO::FETCH_CLASS )
532 while ( $row = $this->fetchObject($fetch_argument, $ctor_args) )
537 elseif ( $fetch_style == PDO::FETCH_FUNC )
539 while ( $row = $this->fetch(PDO::FETCH_ASSOC) )
541 $ret[] = call_user_func_array($fetch_argument, array_values($row));
546 while ( $row = $this->fetch($fetch_style) )
554 public function fetchColumn($column_number = 0)
556 if ( $ret = $this->fetch(PDO::FETCH_NUM) )
558 return $ret[$column_number];
563 public function fetchObject($class_name = 'stdClass', $ctor_args = null)
565 if ( is_array($ctor_args) && !empty($ctor_args) )
567 return @call_user_func(MysqlPDO::$handler . 'fetch_object', $this->result, $class_name, $ctor_args);
571 return @call_user_func(MysqlPDO::$handler . 'fetch_object', $this->result, $class_name);
575 public function getAttribute($attribute)
577 switch ( $attribute )
587 public function getColumnMeta($column)
590 if ( MysqlPDO::$handler == 'mysql_' )
592 $result['name'] = @call_user_func(MysqlPDO::$handler . 'field_name', $this->result, $column);
600 public function nextRowset()
605 public function rowCount()
607 return @call_user_func(MysqlPDO::$handler . 'affected_rows', $this->dbcon);
613 public function setAttribute($attribute, $value)
618 public function setFetchMode($mode, &$mode_argument, $ctorargs)
622 case PDO::FETCH_COLUMN:
623 $this->def_col_num = $mode_argument;
625 case PDO::FETCH_CLASS:
626 $this->def_class_name = $mode_argument;
627 $this->def_ctorargs = $ctorargs;
629 case PDO::FETCH_INTO:
630 $this->bind_object = &$mode_argument;
631 return FALSE; // Not supported
633 $this->def_fetch_mode = $mode;
640 private $iterator_value;
650 return ($this->iterator_value = $this->fetch());
655 return $this->iterator_value;