diff --git a/cake/libs/model/datasources/dbo/dbo_mysql.php b/cake/libs/model/datasources/dbo/dbo_mysql.php index 98e2cf5d2..749b9d0a6 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", ">="); @@ -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 540ec0944..9a397a0cc 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; @@ -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 c09b175e2..6819adf9a 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; } @@ -285,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); @@ -335,9 +334,9 @@ 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') { + if ($type == 'boolean' && !is_null($row[$col])) { $resultRow[$table][$column] = $this->boolean($resultRow[$table][$column]); } } 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..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 @@ -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); @@ -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' + )); + } }