123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050 |
- <?php
- require_once 'common.php';
- class DB_mysql extends DB_common
- {
-
-
- public $phptype = 'mysql';
-
- public $dbsyntax = 'mysql';
-
- public $features = array(
- 'limit' => 'alter',
- 'new_link' => '4.2.0',
- 'numrows' => true,
- 'pconnect' => true,
- 'prepare' => false,
- 'ssl' => false,
- 'transactions' => true,
- );
-
- public $errorcode_map = array(
- 1004 => DB_ERROR_CANNOT_CREATE,
- 1005 => DB_ERROR_CANNOT_CREATE,
- 1006 => DB_ERROR_CANNOT_CREATE,
- 1007 => DB_ERROR_ALREADY_EXISTS,
- 1008 => DB_ERROR_CANNOT_DROP,
- 1022 => DB_ERROR_ALREADY_EXISTS,
- 1044 => DB_ERROR_ACCESS_VIOLATION,
- 1046 => DB_ERROR_NODBSELECTED,
- 1048 => DB_ERROR_CONSTRAINT,
- 1049 => DB_ERROR_NOSUCHDB,
- 1050 => DB_ERROR_ALREADY_EXISTS,
- 1051 => DB_ERROR_NOSUCHTABLE,
- 1054 => DB_ERROR_NOSUCHFIELD,
- 1061 => DB_ERROR_ALREADY_EXISTS,
- 1062 => DB_ERROR_ALREADY_EXISTS,
- 1064 => DB_ERROR_SYNTAX,
- 1091 => DB_ERROR_NOT_FOUND,
- 1100 => DB_ERROR_NOT_LOCKED,
- 1136 => DB_ERROR_VALUE_COUNT_ON_ROW,
- 1142 => DB_ERROR_ACCESS_VIOLATION,
- 1146 => DB_ERROR_NOSUCHTABLE,
- 1216 => DB_ERROR_CONSTRAINT,
- 1217 => DB_ERROR_CONSTRAINT,
- 1356 => DB_ERROR_DIVZERO,
- 1451 => DB_ERROR_CONSTRAINT,
- 1452 => DB_ERROR_CONSTRAINT,
- );
-
- public $connection;
-
- public $dsn = array();
-
- public $autocommit = true;
-
- public $transaction_opcount = 0;
-
- public $_db = '';
-
-
-
- public function __construct()
- {
- parent::__construct();
- }
-
-
-
- public function connect($dsn, $persistent = false)
- {
- if (!PEAR::loadExtension('mysql')) {
- return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
- }
- $this->dsn = $dsn;
- if ($dsn['dbsyntax']) {
- $this->dbsyntax = $dsn['dbsyntax'];
- }
- $params = array();
- if ($dsn['protocol'] && $dsn['protocol'] == 'unix') {
- $params[0] = ':' . $dsn['socket'];
- } else {
- $params[0] = $dsn['hostspec'] ? $dsn['hostspec']
- : 'localhost';
- if ($dsn['port']) {
- $params[0] .= ':' . $dsn['port'];
- }
- }
- $params[] = $dsn['username'] ? $dsn['username'] : null;
- $params[] = $dsn['password'] ? $dsn['password'] : null;
- if (!$persistent) {
- if (isset($dsn['new_link'])
- && ($dsn['new_link'] == 'true' || $dsn['new_link'] === true)) {
- $params[] = true;
- } else {
- $params[] = false;
- }
- }
- if (version_compare(phpversion(), '4.3.0', '>=')) {
- $params[] = isset($dsn['client_flags'])
- ? $dsn['client_flags'] : null;
- }
- $connect_function = $persistent ? 'mysql_pconnect' : 'mysql_connect';
- $ini = ini_get('track_errors');
- $php_errormsg = '';
- if ($ini) {
- $this->connection = @call_user_func_array(
- $connect_function,
- $params
- );
- } else {
- @ini_set('track_errors', 1);
- $this->connection = @call_user_func_array(
- $connect_function,
- $params
- );
- @ini_set('track_errors', $ini);
- }
- if (!$this->connection) {
- if (($err = @mysql_error()) != '') {
- return $this->raiseError(
- DB_ERROR_CONNECT_FAILED,
- null,
- null,
- null,
- $err
- );
- } else {
- return $this->raiseError(
- DB_ERROR_CONNECT_FAILED,
- null,
- null,
- null,
- $php_errormsg
- );
- }
- }
- if ($dsn['database']) {
- if (!@mysql_select_db($dsn['database'], $this->connection)) {
- return $this->mysqlRaiseError();
- }
- $this->_db = $dsn['database'];
- }
- return DB_OK;
- }
-
-
-
- public function mysqlRaiseError($errno = null)
- {
- if ($errno === null) {
- if ($this->options['portability'] & DB_PORTABILITY_ERRORS) {
- $this->errorcode_map[1022] = DB_ERROR_CONSTRAINT;
- $this->errorcode_map[1048] = DB_ERROR_CONSTRAINT_NOT_NULL;
- $this->errorcode_map[1062] = DB_ERROR_CONSTRAINT;
- } else {
-
- $this->errorcode_map[1022] = DB_ERROR_ALREADY_EXISTS;
- $this->errorcode_map[1048] = DB_ERROR_CONSTRAINT;
- $this->errorcode_map[1062] = DB_ERROR_ALREADY_EXISTS;
- }
- $errno = $this->errorCode(mysql_errno($this->connection));
- }
- return $this->raiseError(
- $errno,
- null,
- null,
- null,
- @mysql_errno($this->connection) . ' ** ' .
- @mysql_error($this->connection)
- );
- }
-
-
-
- public function disconnect()
- {
- $ret = @mysql_close($this->connection);
- $this->connection = null;
- return $ret;
- }
-
-
-
- public function simpleQuery($query)
- {
- $ismanip = $this->_checkManip($query);
- $this->last_query = $query;
- $query = $this->modifyQuery($query);
- if ($this->_db) {
- if (!@mysql_select_db($this->_db, $this->connection)) {
- return $this->mysqlRaiseError(DB_ERROR_NODBSELECTED);
- }
- }
- if (!$this->autocommit && $ismanip) {
- if ($this->transaction_opcount == 0) {
- $result = @mysql_query('SET AUTOCOMMIT=0', $this->connection);
- $result = @mysql_query('BEGIN', $this->connection);
- if (!$result) {
- return $this->mysqlRaiseError();
- }
- }
- $this->transaction_opcount++;
- }
- if (!$this->options['result_buffering']) {
- $result = @mysql_unbuffered_query($query, $this->connection);
- } else {
- $result = @mysql_query($query, $this->connection);
- }
- if (!$result) {
- return $this->mysqlRaiseError();
- }
- if (is_resource($result)) {
- return $result;
- }
- return DB_OK;
- }
-
-
-
- public function modifyQuery($query)
- {
- if ($this->options['portability'] & DB_PORTABILITY_DELETE_COUNT) {
-
-
- if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $query)) {
- $query = preg_replace(
- '/^\s*DELETE\s+FROM\s+(\S+)\s*$/',
- 'DELETE FROM \1 WHERE 1=1',
- $query
- );
- }
- }
- return $query;
- }
-
-
-
- public function nextResult($result)
- {
- return false;
- }
-
-
-
- public function fetchInto($result, &$arr, $fetchmode, $rownum = null)
- {
- if ($rownum !== null) {
- if (!@mysql_data_seek($result, $rownum)) {
- return null;
- }
- }
- if ($fetchmode & DB_FETCHMODE_ASSOC) {
- $arr = @mysql_fetch_array($result, MYSQL_ASSOC);
- if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE && $arr) {
- $arr = array_change_key_case($arr, CASE_LOWER);
- }
- } else {
- $arr = @mysql_fetch_row($result);
- }
- if (!$arr) {
- return null;
- }
- if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
-
- $this->_rtrimArrayValues($arr);
- }
- if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
- $this->_convertNullArrayValuesToEmpty($arr);
- }
- return DB_OK;
- }
-
-
-
- public function freeResult($result)
- {
- return is_resource($result) ? mysql_free_result($result) : false;
- }
-
-
-
- public function numCols($result)
- {
- $cols = @mysql_num_fields($result);
- if (!$cols) {
- return $this->mysqlRaiseError();
- }
- return $cols;
- }
-
-
-
- public function numRows($result)
- {
- $rows = @mysql_num_rows($result);
- if ($rows === null) {
- return $this->mysqlRaiseError();
- }
- return $rows;
- }
-
-
-
- public function autoCommit($onoff = false)
- {
-
-
- $this->autocommit = $onoff ? true : false;
- return DB_OK;
- }
-
-
-
- public function commit()
- {
- if ($this->transaction_opcount > 0) {
- if ($this->_db) {
- if (!@mysql_select_db($this->_db, $this->connection)) {
- return $this->mysqlRaiseError(DB_ERROR_NODBSELECTED);
- }
- }
- $result = @mysql_query('COMMIT', $this->connection);
- $result = @mysql_query('SET AUTOCOMMIT=1', $this->connection);
- $this->transaction_opcount = 0;
- if (!$result) {
- return $this->mysqlRaiseError();
- }
- }
- return DB_OK;
- }
-
-
-
- public function rollback()
- {
- if ($this->transaction_opcount > 0) {
- if ($this->_db) {
- if (!@mysql_select_db($this->_db, $this->connection)) {
- return $this->mysqlRaiseError(DB_ERROR_NODBSELECTED);
- }
- }
- $result = @mysql_query('ROLLBACK', $this->connection);
- $result = @mysql_query('SET AUTOCOMMIT=1', $this->connection);
- $this->transaction_opcount = 0;
- if (!$result) {
- return $this->mysqlRaiseError();
- }
- }
- return DB_OK;
- }
-
-
-
- public function affectedRows()
- {
- if ($this->_last_query_manip) {
- return @mysql_affected_rows($this->connection);
- } else {
- return 0;
- }
- }
-
-
-
- public function nextId($seq_name, $ondemand = true)
- {
- $seqname = $this->getSequenceName($seq_name);
- do {
- $repeat = 0;
- $this->pushErrorHandling(PEAR_ERROR_RETURN);
- $result = $this->query("UPDATE ${seqname} " .
- 'SET id=LAST_INSERT_ID(id+1)');
- $this->popErrorHandling();
- if ($result === DB_OK) {
-
- $id = @mysql_insert_id($this->connection);
- if ($id != 0) {
- return $id;
- }
-
-
-
- $result = $this->getOne("SELECT GET_LOCK('${seqname}_lock',10)");
- if (DB::isError($result)) {
- return $this->raiseError($result);
- }
- if ($result == 0) {
-
- return $this->mysqlRaiseError(DB_ERROR_NOT_LOCKED);
- }
-
- $result = $this->query("REPLACE INTO ${seqname} (id) VALUES (0)");
- if (DB::isError($result)) {
- return $this->raiseError($result);
- }
-
- $result = $this->getOne('SELECT RELEASE_LOCK('
- . "'${seqname}_lock')");
- if (DB::isError($result)) {
- return $this->raiseError($result);
- }
-
- return 1;
- } elseif ($ondemand && DB::isError($result) &&
- $result->getCode() == DB_ERROR_NOSUCHTABLE) {
-
- $result = $this->createSequence($seq_name);
- if (DB::isError($result)) {
- return $this->raiseError($result);
- } else {
- $repeat = 1;
- }
- } elseif (DB::isError($result) &&
- $result->getCode() == DB_ERROR_ALREADY_EXISTS) {
-
-
- $result = $this->_BCsequence($seqname);
- if (DB::isError($result)) {
- return $this->raiseError($result);
- }
- $repeat = 1;
- }
- } while ($repeat);
- return $this->raiseError($result);
- }
-
-
-
- public function createSequence($seq_name)
- {
- $seqname = $this->getSequenceName($seq_name);
- $res = $this->query('CREATE TABLE ' . $seqname
- . ' (id INTEGER UNSIGNED AUTO_INCREMENT NOT NULL,'
- . ' PRIMARY KEY(id))');
- if (DB::isError($res)) {
- return $res;
- }
-
- $res = $this->query("INSERT INTO ${seqname} (id) VALUES (0)");
- if (DB::isError($res)) {
- return $res;
- }
-
- return $this->query("UPDATE ${seqname} SET id = 0");
- }
-
-
-
- public function _BCsequence($seqname)
- {
-
-
-
- $result = $this->getOne("SELECT GET_LOCK('${seqname}_lock',10)");
- if (DB::isError($result)) {
- return $result;
- }
- if ($result == 0) {
-
-
- return $this->mysqlRaiseError(DB_ERROR_NOT_LOCKED);
- }
- $highest_id = $this->getOne("SELECT MAX(id) FROM ${seqname}");
- if (DB::isError($highest_id)) {
- return $highest_id;
- }
-
-
-
- $result = $this->query('DELETE FROM ' . $seqname
- . " WHERE id <> $highest_id");
- if (DB::isError($result)) {
- return $result;
- }
-
-
-
- $result = $this->getOne("SELECT RELEASE_LOCK('${seqname}_lock')");
- if (DB::isError($result)) {
- return $result;
- }
- return true;
- }
-
-
-
- public function dropSequence($seq_name)
- {
- return $this->query('DROP TABLE ' . $this->getSequenceName($seq_name));
- }
-
-
-
- public function quoteIdentifier($str)
- {
- return '`' . str_replace('`', '``', $str) . '`';
- }
-
-
-
- public function escapeSimple($str)
- {
- if (function_exists('mysql_real_escape_string')) {
- return @mysql_real_escape_string($str, $this->connection);
- } else {
- return @mysql_escape_string($str);
- }
- }
-
-
-
- public function modifyLimitQuery($query, $from, $count, $params = array())
- {
- if (DB::isManip($query) || $this->_next_query_manip) {
- return $query . " LIMIT $count";
- } else {
- return $query . " LIMIT $from, $count";
- }
- }
-
-
-
- public function errorNative()
- {
- return @mysql_errno($this->connection);
- }
-
-
-
- public function tableInfo($result, $mode = null)
- {
- if (is_string($result)) {
-
- if ($this->_db) {
- if (!@mysql_select_db($this->_db, $this->connection)) {
- return $this->mysqlRaiseError(DB_ERROR_NODBSELECTED);
- }
- }
-
- $id = @mysql_query(
- "SELECT * FROM $result LIMIT 0",
- $this->connection
- );
- $got_string = true;
- } elseif (isset($result->result)) {
-
- $id = $result->result;
- $got_string = false;
- } else {
-
- $id = $result;
- $got_string = false;
- }
- if (!is_resource($id)) {
- return $this->mysqlRaiseError(DB_ERROR_NEED_MORE_DATA);
- }
- if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
- $case_func = 'strtolower';
- } else {
- $case_func = 'strval';
- }
- $count = @mysql_num_fields($id);
- $res = array();
- if ($mode) {
- $res['num_fields'] = $count;
- }
- for ($i = 0; $i < $count; $i++) {
- $res[$i] = array(
- 'table' => $case_func(@mysql_field_table($id, $i)),
- 'name' => $case_func(@mysql_field_name($id, $i)),
- 'type' => @mysql_field_type($id, $i),
- 'len' => @mysql_field_len($id, $i),
- 'flags' => @mysql_field_flags($id, $i),
- );
- if ($mode & DB_TABLEINFO_ORDER) {
- $res['order'][$res[$i]['name']] = $i;
- }
- if ($mode & DB_TABLEINFO_ORDERTABLE) {
- $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
- }
- }
-
- if ($got_string) {
- @mysql_free_result($id);
- }
- return $res;
- }
-
-
-
- public function getSpecialQuery($type)
- {
- switch ($type) {
- case 'tables':
- return 'SHOW TABLES';
- case 'users':
- return 'SELECT DISTINCT User FROM mysql.user';
- case 'databases':
- return 'SHOW DATABASES';
- default:
- return null;
- }
- }
-
- }
|