From d3d009588d56512313362b086098fb754f15f2d0 Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 3 Jan 2011 23:06:48 -0500 Subject: [PATCH 1/5] Fixing failing tests because of incorrect types. --- .../cases/libs/model/datasources/dbo/dbo_mysql.test.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php b/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php index 1e4c0ed93..5ddce2069 100644 --- a/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php @@ -178,19 +178,19 @@ class DboMysqlTest extends CakeTestCase { $this->assertTrue((bool)$this->model->save(array('bool' => 5, 'small_int' => 5))); $result = $this->model->find('first'); - $this->assertIdentical($result['Tinyint']['bool'], '1'); + $this->assertIdentical($result['Tinyint']['bool'], true); $this->assertIdentical($result['Tinyint']['small_int'], '5'); $this->model->deleteAll(true); $this->assertTrue((bool)$this->model->save(array('bool' => 0, 'small_int' => 100))); $result = $this->model->find('first'); - $this->assertIdentical($result['Tinyint']['bool'], '0'); + $this->assertIdentical($result['Tinyint']['bool'], false); $this->assertIdentical($result['Tinyint']['small_int'], '100'); $this->model->deleteAll(true); $this->assertTrue((bool)$this->model->save(array('bool' => true, 'small_int' => 0))); $result = $this->model->find('first'); - $this->assertIdentical($result['Tinyint']['bool'], '1'); + $this->assertIdentical($result['Tinyint']['bool'], true); $this->assertIdentical($result['Tinyint']['small_int'], '0'); $this->model->deleteAll(true); From de6eda964e6fd100dc24cedc787fdd981372a5d9 Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 3 Jan 2011 23:34:22 -0500 Subject: [PATCH 2/5] Removing silent errors in PDO dbo's when a connection exception is created. Adding the host param for mysql. Adding a test for mysql missing the host param. Fixes #1427 --- cake/libs/model/datasources/dbo/dbo_mysql.php | 4 ++-- cake/libs/model/datasources/dbo/dbo_postgres.php | 2 +- cake/libs/model/datasources/dbo/dbo_sqlite.php | 5 ++--- .../libs/model/datasources/dbo/dbo_mysql.test.php | 14 ++++++++++++++ 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/cake/libs/model/datasources/dbo/dbo_mysql.php b/cake/libs/model/datasources/dbo/dbo_mysql.php index 98e2cf5d2..1cdb1ddc1 100644 --- a/cake/libs/model/datasources/dbo/dbo_mysql.php +++ b/cake/libs/model/datasources/dbo/dbo_mysql.php @@ -148,14 +148,14 @@ class DboMysql extends DboSource { $flags[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $config['encoding']; } $this->_connection = new PDO( - "mysql:{$config['host']};port={$config['port']};dbname={$config['database']}", + "mysql:host={$config['host']};port={$config['port']};dbname={$config['database']}", $config['login'], $config['password'], $flags ); $this->connected = true; } catch (PDOException $e) { - $this->errors[] = $e->getMessage(); + throw new MissingConnectionException(array('class' => $e->getMessage())); } $this->_useAlias = (bool)version_compare($this->getVersion(), "4.1", ">="); diff --git a/cake/libs/model/datasources/dbo/dbo_postgres.php b/cake/libs/model/datasources/dbo/dbo_postgres.php index 540ec0944..b6106d87b 100644 --- a/cake/libs/model/datasources/dbo/dbo_postgres.php +++ b/cake/libs/model/datasources/dbo/dbo_postgres.php @@ -130,7 +130,7 @@ class DboPostgres extends DboSource { $this->_execute('SET search_path TO ' . $config['schema']); } } catch (PDOException $e) { - $this->errors[] = $e->getMessage(); + throw new MissingConnectionException(array('class' => $e->getMessage())); } return $this->connected; diff --git a/cake/libs/model/datasources/dbo/dbo_sqlite.php b/cake/libs/model/datasources/dbo/dbo_sqlite.php index c09b175e2..ae5c25f11 100644 --- a/cake/libs/model/datasources/dbo/dbo_sqlite.php +++ b/cake/libs/model/datasources/dbo/dbo_sqlite.php @@ -114,9 +114,8 @@ class DboSqlite extends DboSource { $this->_connection = new PDO('sqlite:' . $config['database'], null, null, $flags); $this->_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->connected = true; - } - catch(PDOException $e) { - $this->errors[] = $e->getMessage(); + } catch(PDOException $e) { + throw new MissingConnectionException(array('class' => $e->getMessage())); } return $this->connected; } diff --git a/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php b/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php index 5ddce2069..ff462f86e 100644 --- a/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php @@ -3367,4 +3367,18 @@ class DboMysqlTest extends CakeTestCase { $this->assertTrue(Set::matches('/Comment[id=2]', $result)); $this->assertFalse(Set::matches('/Comment[id=10]', $result)); } + +/** + * @expectedException MissingConnectionException + * @return void + */ + function testExceptionOnBrokenConnection() { + $dbo = new DboMysql(array( + 'driver' => 'mysql', + 'host' => 'imaginary_host', + 'login' => 'mark', + 'password' => 'inyurdatabase', + 'database' => 'imaginary' + )); + } } From 665e560310f8bb848a4f2602185b5bd65dae2419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=CC=81=20Lorenzo=20Rodri=CC=81guez?= Date: Tue, 4 Jan 2011 17:00:10 -0430 Subject: [PATCH 3/5] Checking for null values before converting to boolean --- cake/libs/model/datasources/dbo/dbo_mysql.php | 2 +- cake/libs/model/datasources/dbo/dbo_postgres.php | 2 +- cake/libs/model/datasources/dbo/dbo_sqlite.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cake/libs/model/datasources/dbo/dbo_mysql.php b/cake/libs/model/datasources/dbo/dbo_mysql.php index 1cdb1ddc1..749b9d0a6 100644 --- a/cake/libs/model/datasources/dbo/dbo_mysql.php +++ b/cake/libs/model/datasources/dbo/dbo_mysql.php @@ -238,7 +238,7 @@ class DboMysql extends DboSource { foreach ($this->map as $col => $meta) { list($table, $column, $type) = $meta; $resultRow[$table][$column] = $row[$col]; - if ($type == 'boolean') { + if ($type == 'boolean' && !is_null($row[$col])) { $resultRow[$table][$column] = $this->boolean($resultRow[$table][$column]); } } diff --git a/cake/libs/model/datasources/dbo/dbo_postgres.php b/cake/libs/model/datasources/dbo/dbo_postgres.php index b6106d87b..9a397a0cc 100644 --- a/cake/libs/model/datasources/dbo/dbo_postgres.php +++ b/cake/libs/model/datasources/dbo/dbo_postgres.php @@ -707,7 +707,7 @@ class DboPostgres extends DboSource { switch ($type) { case 'bool': - $resultRow[$table][$column] = $this->boolean($row[$index]); + $resultRow[$table][$column] = is_null($row[$index]) ? null : $this->boolean($row[$index]); break; case 'binary': case 'bytea': diff --git a/cake/libs/model/datasources/dbo/dbo_sqlite.php b/cake/libs/model/datasources/dbo/dbo_sqlite.php index ae5c25f11..63e1a1e40 100644 --- a/cake/libs/model/datasources/dbo/dbo_sqlite.php +++ b/cake/libs/model/datasources/dbo/dbo_sqlite.php @@ -336,7 +336,7 @@ class DboSqlite extends DboSource { foreach ($this->map as $col => $meta) { list($table, $column, $tpye) = $meta; $resultRow[$table][$column] = $row[$col]; - if ($type === 'boolean') { + if ($type == 'boolean' && !is_null($row[$col])) { $resultRow[$table][$column] = $this->boolean($resultRow[$table][$column]); } } From 4948280ba89eddaed2930845ca2fdd80563c5698 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=CC=81=20Lorenzo=20Rodri=CC=81guez?= Date: Tue, 4 Jan 2011 17:25:23 -0430 Subject: [PATCH 4/5] Fixing typo in sqlite driver --- cake/libs/model/datasources/dbo/dbo_sqlite.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cake/libs/model/datasources/dbo/dbo_sqlite.php b/cake/libs/model/datasources/dbo/dbo_sqlite.php index 63e1a1e40..5f34023f3 100644 --- a/cake/libs/model/datasources/dbo/dbo_sqlite.php +++ b/cake/libs/model/datasources/dbo/dbo_sqlite.php @@ -334,7 +334,7 @@ class DboSqlite extends DboSource { if ($row = $this->_result->fetch()) { $resultRow = array(); foreach ($this->map as $col => $meta) { - list($table, $column, $tpye) = $meta; + list($table, $column, $type) = $meta; $resultRow[$table][$column] = $row[$col]; if ($type == 'boolean' && !is_null($row[$col])) { $resultRow[$table][$column] = $this->boolean($resultRow[$table][$column]); From 2b9c6b8609aecfec5d56b768583056e2afa76459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=CC=81=20Lorenzo=20Rodri=CC=81guez?= Date: Tue, 4 Jan 2011 17:44:33 -0430 Subject: [PATCH 5/5] optimizing sqlite driver to look for the "From" keyword in reverse order on the sql string --- cake/libs/model/datasources/dbo/dbo_sqlite.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cake/libs/model/datasources/dbo/dbo_sqlite.php b/cake/libs/model/datasources/dbo/dbo_sqlite.php index 5f34023f3..6819adf9a 100644 --- a/cake/libs/model/datasources/dbo/dbo_sqlite.php +++ b/cake/libs/model/datasources/dbo/dbo_sqlite.php @@ -284,7 +284,7 @@ class DboSqlite extends DboSource { // so try to figure it out based on the querystring $querystring = $results->queryString; if (stripos($querystring, 'SELECT') === 0) { - $last = stripos($querystring, 'FROM'); + $last = strripos($querystring, 'FROM'); if ($last !== false) { $selectpart = substr($querystring, 7, $last - 8); $selects = explode(',', $selectpart);