From cbdfb3f76e885c52534d4577f61a9d567ce47526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez?= Date: Sun, 17 Oct 2010 11:49:17 -0430 Subject: [PATCH] Removing all references to mysqli --- cake/console/libs/tasks/db_config.php | 2 +- .../skel/config/database.php.default | 3 +- .../libs/model/datasources/dbo/dbo_mysqli.php | 336 ------------------ .../libs/model/connection_manager.test.php | 1 - .../model/datasources/dbo/dbo_mysqli.test.php | 330 ----------------- 5 files changed, 2 insertions(+), 670 deletions(-) delete mode 100644 cake/libs/model/datasources/dbo/dbo_mysqli.php delete mode 100644 cake/tests/cases/libs/model/datasources/dbo/dbo_mysqli.test.php diff --git a/cake/console/libs/tasks/db_config.php b/cake/console/libs/tasks/db_config.php index c62cbe1f3..c58e34556 100644 --- a/cake/console/libs/tasks/db_config.php +++ b/cake/console/libs/tasks/db_config.php @@ -100,7 +100,7 @@ class DbConfigTask extends Shell { } } - $driver = $this->in('Driver:', array('db2', 'firebird', 'mssql', 'mysql', 'mysqli', 'odbc', 'oracle', 'postgres', 'sqlite', 'sybase'), 'mysql'); + $driver = $this->in('Driver:', array('db2', 'firebird', 'mssql', 'mysql', 'odbc', 'oracle', 'postgres', 'sqlite', 'sybase'), 'mysql'); $persistent = $this->in('Persistent Connection?', array('y', 'n'), 'n'); if (strtolower($persistent) == 'n') { diff --git a/cake/console/templates/skel/config/database.php.default b/cake/console/templates/skel/config/database.php.default index 2a605e498..8c7e39661 100644 --- a/cake/console/templates/skel/config/database.php.default +++ b/cake/console/templates/skel/config/database.php.default @@ -31,7 +31,6 @@ * * driver => The name of a supported driver; valid options are as follows: * mysql - MySQL 4 & 5, - * mysqli - MySQL 4 & 5 Improved Interface (PHP5 only), * sqlite - SQLite (PHP5 only), * postgres - PostgreSQL 7 and higher, * mssql - Microsoft SQL Server 2000 and higher, @@ -67,7 +66,7 @@ * 'public', DB2 defaults to empty. * * encoding => - * For MySQL, MySQLi, Postgres and DB2, specifies the character encoding to use when connecting to the + * For MySQL, Postgres and DB2, specifies the character encoding to use when connecting to the * database. Uses database default. * */ diff --git a/cake/libs/model/datasources/dbo/dbo_mysqli.php b/cake/libs/model/datasources/dbo/dbo_mysqli.php deleted file mode 100644 index 7c6cdfd5d..000000000 --- a/cake/libs/model/datasources/dbo/dbo_mysqli.php +++ /dev/null @@ -1,336 +0,0 @@ - true, - 'host' => 'localhost', - 'login' => 'root', - 'password' => '', - 'database' => 'cake', - 'port' => '3306' - ); - -/** - * Connects to the database using options in the given configuration array. - * - * @return boolean True if the database could be connected, else false - */ - function connect() { - $config = $this->config; - $this->connected = false; - - if (is_numeric($config['port'])) { - $config['socket'] = null; - } else { - $config['socket'] = $config['port']; - $config['port'] = null; - } - - $this->connection = mysqli_connect($config['host'], $config['login'], $config['password'], $config['database'], $config['port'], $config['socket']); - - if ($this->connection !== false) { - $this->connected = true; - } - - $this->_useAlias = (bool)version_compare(mysqli_get_server_info($this->connection), "4.1", ">="); - - if (!empty($config['encoding'])) { - $this->setEncoding($config['encoding']); - } - return $this->connected; - } - -/** - * Check that MySQLi is installed/enabled - * - * @return boolean - */ - function enabled() { - return extension_loaded('mysqli'); - } -/** - * Disconnects from database. - * - * @return boolean True if the database could be disconnected, else false - */ - function disconnect() { - if (isset($this->results) && is_resource($this->results)) { - mysqli_free_result($this->results); - } - $this->connected = !@mysqli_close($this->connection); - return !$this->connected; - } - -/** - * Executes given SQL statement. - * - * @param string $sql SQL statement - * @return resource Result resource identifier - */ - protected function _execute($sql) { - if (preg_match('/^\s*call/i', $sql)) { - return $this->_executeProcedure($sql); - } - return mysqli_query($this->connection, $sql); - } - -/** - * Executes given SQL statement (procedure call). - * - * @param string $sql SQL statement (procedure call) - * @return resource Result resource identifier for first recordset - */ - protected function _executeProcedure($sql) { - $answer = mysqli_multi_query($this->connection, $sql); - - $firstResult = mysqli_store_result($this->connection); - - if (mysqli_more_results($this->connection)) { - while ($lastResult = mysqli_next_result($this->connection)); - } - return $firstResult; - } - -/** - * Returns an array of sources (tables) in the database. - * - * @return array Array of tablenames in the database - */ - function listSources() { - $cache = parent::listSources(); - if ($cache !== null) { - return $cache; - } - $result = $this->_execute('SHOW TABLES FROM ' . $this->name($this->config['database']) . ';'); - - if (!$result) { - return array(); - } - - $tables = array(); - - while ($line = mysqli_fetch_row($result)) { - $tables[] = $line[0]; - } - parent::listSources($tables); - return $tables; - } - -/** - * Returns a quoted and escaped string of $data for use in an SQL statement. - * - * @param string $data String to be prepared for use in an SQL statement - * @param string $column The column into which this data will be inserted - * @param boolean $safe Whether or not numeric data should be handled automagically if no column data is provided - * @return string Quoted and escaped data - */ - function value($data, $column = null, $safe = false) { - $parent = parent::value($data, $column, $safe); - - if ($parent != null) { - return $parent; - } - if ($data === null || (is_array($data) && empty($data))) { - return 'NULL'; - } - if ($data === '' && $column !== 'integer' && $column !== 'float' && $column !== 'boolean') { - return "''"; - } - if (empty($column)) { - $column = $this->introspectType($data); - } - - switch ($column) { - case 'boolean': - return $this->boolean((bool)$data); - break; - case 'integer' : - case 'float' : - case null : - if ($data === '') { - return 'NULL'; - } - if ((is_int($data) || is_float($data) || $data === '0') || ( - is_numeric($data) && strpos($data, ',') === false && - $data[0] != '0' && strpos($data, 'e') === false)) { - return $data; - } - default: - $data = "'" . mysqli_real_escape_string($this->connection, $data) . "'"; - break; - } - - return $data; - } - -/** - * Returns a formatted error message from previous database operation. - * - * @return string Error message with error number - */ - function lastError() { - if (mysqli_errno($this->connection)) { - return mysqli_errno($this->connection).': '.mysqli_error($this->connection); - } - return null; - } - -/** - * Returns number of affected rows in previous database operation. If no previous operation exists, - * this returns false. - * - * @return integer Number of affected rows - */ - function lastAffected() { - if ($this->_result) { - return mysqli_affected_rows($this->connection); - } - return null; - } - -/** - * Returns number of rows in previous resultset. If no previous resultset exists, - * this returns false. - * - * @return integer Number of rows in resultset - */ - function lastNumRows() { - if ($this->hasResult()) { - return mysqli_num_rows($this->_result); - } - return null; - } - -/** - * Returns the ID generated from the previous INSERT operation. - * - * @param unknown_type $source - * @return in - */ - function lastInsertId($source = null) { - $id = $this->fetchRow('SELECT LAST_INSERT_ID() AS insertID', false); - if ($id !== false && !empty($id) && !empty($id[0]) && isset($id[0]['insertID'])) { - return $id[0]['insertID']; - } - return null; - } - -/** - * Enter description here... - * - * @param unknown_type $results - */ - function resultSet(&$results) { - if (isset($this->results) && is_resource($this->results) && $this->results != $results) { - mysqli_free_result($this->results); - } - $this->results =& $results; - $this->map = array(); - $numFields = mysqli_num_fields($results); - $index = 0; - $j = 0; - while ($j < $numFields) { - $column = mysqli_fetch_field_direct($results, $j); - if (!empty($column->table)) { - $this->map[$index++] = array($column->table, $column->name); - } else { - $this->map[$index++] = array(0, $column->name); - } - $j++; - } - } - -/** - * Fetches the next row from the current result set - * - * @return unknown - */ - function fetchResult() { - if ($row = mysqli_fetch_row($this->results)) { - $resultRow = array(); - foreach ($row as $index => $field) { - $table = $column = null; - if (count($this->map[$index]) === 2) { - list($table, $column) = $this->map[$index]; - } - $resultRow[$table][$column] = $row[$index]; - } - return $resultRow; - } - return false; - } - -/** - * Gets the database encoding - * - * @return string The database encoding - */ - function getEncoding() { - return mysqli_client_encoding($this->connection); - } - -/** - * Query charset by collation - * - * @param string $name Collation name - * @return string Character set name - */ - function getCharsetName($name) { - if ((bool)version_compare(mysqli_get_server_info($this->connection), "5", ">=")) { - $cols = $this->query('SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.COLLATIONS WHERE COLLATION_NAME= ' . $this->value($name) . ';'); - if (isset($cols[0]['COLLATIONS']['CHARACTER_SET_NAME'])) { - return $cols[0]['COLLATIONS']['CHARACTER_SET_NAME']; - } - } - return false; - } - -/** - * Checks if the result is valid - * - * @return boolean True if the result is valid, else false - */ - function hasResult() { - return is_object($this->_result); - } -} diff --git a/cake/tests/cases/libs/model/connection_manager.test.php b/cake/tests/cases/libs/model/connection_manager.test.php index d8e8b848f..04212fa4a 100644 --- a/cake/tests/cases/libs/model/connection_manager.test.php +++ b/cake/tests/cases/libs/model/connection_manager.test.php @@ -226,7 +226,6 @@ class ConnectionManagerTest extends CakeTestCase { function testLoadDataSource() { $connections = array( array('classname' => 'DboMysql', 'filename' => 'dbo' . DS . 'dbo_mysql'), - array('classname' => 'DboMysqli', 'filename' => 'dbo' . DS . 'dbo_mysqli'), array('classname' => 'DboMssql', 'filename' => 'dbo' . DS . 'dbo_mssql'), array('classname' => 'DboOracle', 'filename' => 'dbo' . DS . 'dbo_oracle'), ); diff --git a/cake/tests/cases/libs/model/datasources/dbo/dbo_mysqli.test.php b/cake/tests/cases/libs/model/datasources/dbo/dbo_mysqli.test.php deleted file mode 100644 index 3a162f9ee..000000000 --- a/cake/tests/cases/libs/model/datasources/dbo/dbo_mysqli.test.php +++ /dev/null @@ -1,330 +0,0 @@ -testing) { - $this->simulated[] = $sql; - return null; - } - return parent::_execute($sql); - } - -/** - * getLastQuery method - * - * @access public - * @return void - */ - function getLastQuery() { - return $this->simulated[count($this->simulated) - 1]; - } -} - -/** - * MysqliTestModel class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources - */ -class MysqliTestModel extends Model { - -/** - * name property - * - * @var string 'MysqlTestModel' - * @access public - */ - public $name = 'MysqliTestModel'; - -/** - * useTable property - * - * @var bool false - * @access public - */ - public $useTable = false; - -/** - * schema method - * - * @access public - * @return void - */ - function schema() { - return array( - 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), - 'client_id' => array('type' => 'integer', 'null' => '', 'default' => '0', 'length' => '11'), - 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), - 'login' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), - 'passwd' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'), - 'addr_1' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'), - 'addr_2' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '25'), - 'zip_code' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'city' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'country' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'phone' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'fax' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'url' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'), - 'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), - 'comments' => array('type' => 'text', 'null' => '1', 'default' => '', 'length' => ''), - 'last_login'=> array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''), - 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), - 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) - ); - } -} - -/** - * DboMysqliTest class - * - * @package cake - * @subpackage cake.tests.cases.libs.model.datasources.dbo - */ -class DboMysqliTest extends CakeTestCase { - public $fixtures = array('core.datatype'); -/** - * The Dbo instance to be tested - * - * @var DboSource - * @access public - */ - public $Dbo = null; - -/** - * Sets up a Dbo class instance for testing - * - */ - public function setUp() { - $this->Dbo = ConnectionManager::getDataSource('test'); - if ($this->Dbo->config['driver'] !== 'mysqli') { - $this->markTestSkipped('The MySQLi extension is not available.'); - } - $this->model = new MysqliTestModel(); - } - -/** - * Sets up a Dbo class instance for testing - * - */ - public function tearDown() { - unset($this->model); - ClassRegistry::flush(); - } - -/** - * testIndexDetection method - * - * @return void - */ - public function testIndexDetection() { - $this->Dbo->cacheSources = false; - - $name = $this->Dbo->fullTableName('simple'); - $this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));'); - $expected = array('PRIMARY' => array('column' => 'id', 'unique' => 1)); - $result = $this->Dbo->index($name, false); - $this->assertEqual($expected, $result); - $this->Dbo->query('DROP TABLE ' . $name); - - $name = $this->Dbo->fullTableName('with_a_key'); - $this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ));'); - $expected = array( - 'PRIMARY' => array('column' => 'id', 'unique' => 1), - 'pointless_bool' => array('column' => 'bool', 'unique' => 0), - ); - $result = $this->Dbo->index($name, false); - $this->assertEqual($expected, $result); - $this->Dbo->query('DROP TABLE ' . $name); - - $name = $this->Dbo->fullTableName('with_two_keys'); - $this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ));'); - $expected = array( - 'PRIMARY' => array('column' => 'id', 'unique' => 1), - 'pointless_bool' => array('column' => 'bool', 'unique' => 0), - 'pointless_small_int' => array('column' => 'small_int', 'unique' => 0), - ); - $result = $this->Dbo->index($name, false); - $this->assertEqual($expected, $result); - $this->Dbo->query('DROP TABLE ' . $name); - - $name = $this->Dbo->fullTableName('with_compound_keys'); - $this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ));'); - $expected = array( - 'PRIMARY' => array('column' => 'id', 'unique' => 1), - 'pointless_bool' => array('column' => 'bool', 'unique' => 0), - 'pointless_small_int' => array('column' => 'small_int', 'unique' => 0), - 'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0), - ); - $result = $this->Dbo->index($name, false); - $this->assertEqual($expected, $result); - $this->Dbo->query('DROP TABLE ' . $name); - - $name = $this->Dbo->fullTableName('with_multiple_compound_keys'); - $this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ), KEY `other_way` ( `small_int`, `bool` ));'); - $expected = array( - 'PRIMARY' => array('column' => 'id', 'unique' => 1), - 'pointless_bool' => array('column' => 'bool', 'unique' => 0), - 'pointless_small_int' => array('column' => 'small_int', 'unique' => 0), - 'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0), - 'other_way' => array('column' => array('small_int', 'bool'), 'unique' => 0), - ); - $result = $this->Dbo->index($name, false); - $this->assertEqual($expected, $result); - $this->Dbo->query('DROP TABLE ' . $name); - } - -/** - * testColumn method - * - * @return void - */ - public function testColumn() { - $result = $this->Dbo->column('varchar(50)'); - $expected = 'string'; - $this->assertEqual($result, $expected); - - $result = $this->Dbo->column('text'); - $expected = 'text'; - $this->assertEqual($result, $expected); - - $result = $this->Dbo->column('int(11)'); - $expected = 'integer'; - $this->assertEqual($result, $expected); - - $result = $this->Dbo->column('int(11) unsigned'); - $expected = 'integer'; - $this->assertEqual($result, $expected); - - $result = $this->Dbo->column('tinyint(1)'); - $expected = 'boolean'; - $this->assertEqual($result, $expected); - - $result = $this->Dbo->column('boolean'); - $expected = 'boolean'; - $this->assertEqual($result, $expected); - - $result = $this->Dbo->column('float'); - $expected = 'float'; - $this->assertEqual($result, $expected); - - $result = $this->Dbo->column('float unsigned'); - $expected = 'float'; - $this->assertEqual($result, $expected); - - $result = $this->Dbo->column('double unsigned'); - $expected = 'float'; - $this->assertEqual($result, $expected); - - $result = $this->Dbo->column('decimal(14,7) unsigned'); - $expected = 'float'; - $this->assertEqual($result, $expected); - } - -/** - * test transaction commands. - * - * @return void - */ - public function testTransactions() { - $this->Dbo->testing = false; - $result = $this->Dbo->begin($this->model); - $this->assertTrue($result); - - $log = $this->Dbo->getLog(); - $beginSqlCalls = Set::extract('/.[query=START TRANSACTION]', $log['log']); - $this->assertEqual(1, count($beginSqlCalls)); - - $result = $this->Dbo->commit($this->model); - $this->assertTrue($result); - } -/** - * test that float values are correctly identified - * - * @return void - */ - function testFloatParsing() { - $model =& new Model(array('ds' => 'test', 'table' => 'datatypes', 'name' => 'Datatype')); - $result = $this->Dbo->describe($model); - $this->assertEqual((string)$result['float_field']['length'], '5,2'); - } - -/** - * test that tableParameters like collation, charset and engine are functioning. - * - * @access public - * @return void - */ - function testReadTableParameters() { - $table = 'tinyint' . uniqid(); - $this->Dbo->cacheSources = $this->Dbo->testing = false; - $this->Dbo->query('CREATE TABLE ' . $this->Dbo->fullTableName($table) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;'); - $result = $this->Dbo->readTableParameters($table); - $expected = array( - 'charset' => 'utf8', - 'collate' => 'utf8_unicode_ci', - 'engine' => 'InnoDB'); - $this->assertEqual($result, $expected); - - $this->Dbo->query('DROP TABLE ' . $this->Dbo->fullTableName($table)); - $this->Dbo->query('CREATE TABLE ' . $this->Dbo->fullTableName($table) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id)) ENGINE=MyISAM DEFAULT CHARSET=cp1250 COLLATE=cp1250_general_ci;'); - $result = $this->Dbo->readTableParameters($table); - $expected = array( - 'charset' => 'cp1250', - 'collate' => 'cp1250_general_ci', - 'engine' => 'MyISAM'); - $this->assertEqual($result, $expected); - $this->Dbo->query('DROP TABLE ' . $this->Dbo->fullTableName($table)); - } -}