From 33bb440bd28d77e903a58f24afc585706e2d0f56 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Fri, 26 Jan 2018 15:45:46 -0500 Subject: [PATCH] SQLite improvements, remove nullable PDOStatement return values --- .gitignore | 2 +- src/ConnectionManager.php | 11 ++-- src/Drivers/AbstractDriver.php | 6 +-- src/Drivers/DriverInterface.php | 5 +- src/Drivers/Mysql/SQL.php | 24 +++++---- src/Drivers/Sqlite/SQL.php | 51 ++++++++++++------- src/Drivers/Sqlite/Util.php | 5 +- src/{ => Exception}/BadDBDriverException.php | 2 +- .../NonExistentConnectionException.php | 23 +++++++++ src/Exception/NotImplementedException.php | 23 +++++++++ src/QueryBuilder.php | 14 ++--- src/QueryBuilderInterface.php | 14 ++--- tests/BaseQueryBuilderTest.php | 2 +- tests/Drivers/SQLite/SQLiteDriverTest.php | 27 ++++------ tests/db_files/oci.sql | 0 tests/db_files/sqlite.sql | 2 +- 16 files changed, 134 insertions(+), 77 deletions(-) rename src/{ => Exception}/BadDBDriverException.php (95%) create mode 100644 src/Exception/NonExistentConnectionException.php create mode 100644 src/Exception/NotImplementedException.php create mode 100644 tests/db_files/oci.sql diff --git a/.gitignore b/.gitignore index c6b05f1..f771c09 100644 --- a/.gitignore +++ b/.gitignore @@ -130,7 +130,7 @@ $RECYCLE.BIN/ test_config.json index.html -tests/db_files/* +tests/db_files/*.db build/api/* build/coverage/* build/logs/* diff --git a/src/ConnectionManager.php b/src/ConnectionManager.php index b4daae3..8b699fd 100644 --- a/src/ConnectionManager.php +++ b/src/ConnectionManager.php @@ -15,7 +15,6 @@ namespace Query; use DomainException; -use InvalidArgumentException; /** * Connection manager class to manage connections for the @@ -97,7 +96,7 @@ final class ConnectionManager { * * @param string|array|object $name * @return QueryBuilderInterface - * @throws InvalidArgumentException + * @throws Exception\NonExistentConnectionException */ public function getConnection($name = ''): QueryBuilderInterface { @@ -112,14 +111,14 @@ final class ConnectionManager { } // You should actually connect before trying to get a connection... - throw new InvalidArgumentException('The specified connection does not exist'); + throw new Exception\NonExistentConnectionException('The specified connection does not exist'); } /** * Parse the passed parameters and return a connection * * @param object|array $params - * @throws BadDBDriverException + * @throws Exception\BadDBDriverException * @return QueryBuilderInterface */ public function connect($params): QueryBuilderInterface @@ -162,7 +161,7 @@ final class ConnectionManager { * * @param \stdClass $params * @return object|array - * @throws BadDBDriverException + * @throws Exception\BadDBDriverException */ public function parseParams($params): array { @@ -174,7 +173,7 @@ final class ConnectionManager { // Make sure the class exists if ( ! class_exists("\\Query\\Drivers\\{$dbtype}\\Driver")) { - throw new BadDBDriverException('Database driver does not exist, or is not supported'); + throw new Exception\BadDBDriverException('Database driver does not exist, or is not supported'); } // Set additional PDO options diff --git a/src/Drivers/AbstractDriver.php b/src/Drivers/AbstractDriver.php index 49d7195..3b8683e 100644 --- a/src/Drivers/AbstractDriver.php +++ b/src/Drivers/AbstractDriver.php @@ -199,7 +199,7 @@ abstract class AbstractDriver * @return PDOStatement | FALSE * @throws InvalidArgumentException */ - public function prepareQuery(string $sql, array $data): ?PDOStatement + public function prepareQuery(string $sql, array $data): PDOStatement { // Prepare the sql, save the statement for easy access later $this->statement = $this->prepare($sql); @@ -227,7 +227,7 @@ abstract class AbstractDriver * @throws InvalidArgumentException * @return PDOStatement */ - public function prepareExecute(string $sql, array $params): ?PDOStatement + public function prepareExecute(string $sql, array $params): PDOStatement { $this->statement = $this->prepareQuery($sql, $params); $this->statement->execute(); @@ -636,7 +636,7 @@ abstract class AbstractDriver * @param string $table * @return PDOStatement */ - public function truncate(string $table): ?PDOStatement + public function truncate(string $table): PDOStatement { $sql = $this->hasTruncate ? 'TRUNCATE TABLE ' diff --git a/src/Drivers/DriverInterface.php b/src/Drivers/DriverInterface.php index 836b7a3..64921ca 100644 --- a/src/Drivers/DriverInterface.php +++ b/src/Drivers/DriverInterface.php @@ -14,7 +14,6 @@ */ namespace Query\Drivers; -use PDO; use PDOStatement; /** @@ -40,7 +39,7 @@ interface DriverInterface { * @return \PDOStatement|null * @throws \InvalidArgumentException */ - public function prepareQuery(string $sql, array $data): ?PDOStatement; + public function prepareQuery(string $sql, array $data): PDOStatement; /** * Retrieve column information for the current database table @@ -162,7 +161,7 @@ interface DriverInterface { * @param array $params * @return PDOStatement */ - public function prepareExecute(string $sql, array $params): ?PDOStatement; + public function prepareExecute(string $sql, array $params): PDOStatement; /** * Method to simplify retrieving db results for meta-data queries diff --git a/src/Drivers/Mysql/SQL.php b/src/Drivers/Mysql/SQL.php index 934cc08..f857ce1 100644 --- a/src/Drivers/Mysql/SQL.php +++ b/src/Drivers/Mysql/SQL.php @@ -67,7 +67,10 @@ class SQL extends AbstractSQL { */ public function dbList(): string { - return "SHOW DATABASES WHERE `Database` NOT IN ('information_schema','mysql')"; + return <<getDriver()->quote($row[$i]); + $row[$i] = is_numeric($row[$i]) ? $row[$i] : $this->getDriver()->quote($row[$i]); } $rowString = 'INSERT INTO "'.$r['name'].'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');'; diff --git a/src/BadDBDriverException.php b/src/Exception/BadDBDriverException.php similarity index 95% rename from src/BadDBDriverException.php rename to src/Exception/BadDBDriverException.php index 9f784e9..fa9bc5c 100644 --- a/src/BadDBDriverException.php +++ b/src/Exception/BadDBDriverException.php @@ -12,7 +12,7 @@ * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link https://git.timshomepage.net/aviat4ion/Query */ -namespace Query; +namespace Query\Exception; use InvalidArgumentException; diff --git a/src/Exception/NonExistentConnectionException.php b/src/Exception/NonExistentConnectionException.php new file mode 100644 index 0000000..3ff0731 --- /dev/null +++ b/src/Exception/NonExistentConnectionException.php @@ -0,0 +1,23 @@ + + * @copyright 2012 - 2018 Timothy J. Warren + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @link https://git.timshomepage.net/aviat4ion/Query + */ +namespace Query\Exception; + +use InvalidArgumentException; + +/** + * Exception for missing database connection + */ +class NonExistentConnectionException extends InvalidArgumentException { +} \ No newline at end of file diff --git a/src/Exception/NotImplementedException.php b/src/Exception/NotImplementedException.php new file mode 100644 index 0000000..a8bcb46 --- /dev/null +++ b/src/Exception/NotImplementedException.php @@ -0,0 +1,23 @@ + + * @copyright 2012 - 2018 Timothy J. Warren + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @link https://git.timshomepage.net/aviat4ion/Query + */ +namespace Query\Exception; + +use BadMethodCallException; + +/** + * Exception for non-implemented method + */ +class NotImplementedException extends BadMethodCallException{ +} \ No newline at end of file diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 744352b..40b2562 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -648,7 +648,7 @@ class QueryBuilder implements QueryBuilderInterface { * @param int|bool $offset * @return PDOStatement */ - public function get(string $table='', $limit=FALSE, $offset=FALSE): ?PDOStatement + public function get(string $table='', $limit=FALSE, $offset=FALSE): PDOStatement { // Set the table if ( ! empty($table)) @@ -674,7 +674,7 @@ class QueryBuilder implements QueryBuilderInterface { * @param int|bool $offset * @return PDOStatement */ - public function getWhere(string $table, $where=[], $limit=FALSE, $offset=FALSE): ?PDOStatement + public function getWhere(string $table, $where=[], $limit=FALSE, $offset=FALSE): PDOStatement { // Create the where clause $this->where($where); @@ -725,7 +725,7 @@ class QueryBuilder implements QueryBuilderInterface { * @param mixed $data * @return PDOStatement */ - public function insert(string $table, $data=[]): ?PDOStatement + public function insert(string $table, $data=[]): PDOStatement { if ( ! empty($data)) { @@ -742,7 +742,7 @@ class QueryBuilder implements QueryBuilderInterface { * @param array $data * @return PDOStatement */ - public function insertBatch(string $table, $data=[]): ?PDOStatement + public function insertBatch(string $table, $data=[]): PDOStatement { // Get the generated values and sql string [$sql, $data] = $this->driver->insertBatch($table, $data); @@ -799,7 +799,7 @@ class QueryBuilder implements QueryBuilderInterface { * @param array $data * @return \PDOStatement|null */ - public function replace(string $table, $data=[]): ?PDOStatement + public function replace(string $table, $data=[]): PDOStatement { if ( ! empty($data)) { @@ -816,7 +816,7 @@ class QueryBuilder implements QueryBuilderInterface { * @param mixed $where * @return PDOStatement */ - public function delete(string $table, $where=''): ?PDOStatement + public function delete(string $table, $where=''): PDOStatement { // Set the where clause if ( ! empty($where)) @@ -1167,7 +1167,7 @@ class QueryBuilder implements QueryBuilderInterface { * @param int $totalTime * @return void */ - protected function _appendQuery(array $values = NULL, string $sql, int $totalTime) + protected function _appendQuery(array $values = NULL, string $sql, int $totalTime): void { $evals = \is_array($values) ? $values : []; $esql = str_replace('?', "%s", $sql); diff --git a/src/QueryBuilderInterface.php b/src/QueryBuilderInterface.php index 35e898e..123ad43 100644 --- a/src/QueryBuilderInterface.php +++ b/src/QueryBuilderInterface.php @@ -322,7 +322,7 @@ interface QueryBuilderInterface { * @param int|bool $offset * @return PDOStatement */ - public function get(string $table='', $limit=FALSE, $offset=FALSE): ?PDOStatement; + public function get(string $table='', $limit=FALSE, $offset=FALSE): PDOStatement; /** * Convenience method for get() with a where clause @@ -333,7 +333,7 @@ interface QueryBuilderInterface { * @param int|bool $offset * @return PDOStatement */ - public function getWhere(string $table, $where=[], $limit=FALSE, $offset=FALSE): ?PDOStatement; + public function getWhere(string $table, $where=[], $limit=FALSE, $offset=FALSE): PDOStatement; /** * Retrieve the number of rows in the selected table @@ -360,7 +360,7 @@ interface QueryBuilderInterface { * @param mixed $data * @return PDOStatement */ - public function insert(string $table, $data=[]): ?PDOStatement; + public function insert(string $table, $data=[]): PDOStatement; /** * Creates and executes a batch insertion query @@ -369,7 +369,7 @@ interface QueryBuilderInterface { * @param array $data * @return PDOStatement */ - public function insertBatch(string $table, $data=[]): ?PDOStatement; + public function insertBatch(string $table, $data=[]): PDOStatement; /** * Insertion with automatic overwrite, rather than attempted duplication @@ -378,7 +378,7 @@ interface QueryBuilderInterface { * @param array $data * @return PDOStatement */ - public function replace(string $table, $data=[]): ?PDOStatement; + public function replace(string $table, $data=[]): PDOStatement; /** * Creates an update clause, and executes it @@ -387,7 +387,7 @@ interface QueryBuilderInterface { * @param mixed $data * @return PDOStatement */ - public function update(string $table, $data=[]): ?PDOStatement; + public function update(string $table, $data=[]): PDOStatement; /** * Creates a batch update, and executes it. @@ -407,7 +407,7 @@ interface QueryBuilderInterface { * @param mixed $where * @return PDOStatement */ - public function delete(string $table, $where=''): ?PDOStatement; + public function delete(string $table, $where=''): PDOStatement; // -------------------------------------------------------------------------- // ! SQL Returning Methods diff --git a/tests/BaseQueryBuilderTest.php b/tests/BaseQueryBuilderTest.php index b935d24..fe27371 100644 --- a/tests/BaseQueryBuilderTest.php +++ b/tests/BaseQueryBuilderTest.php @@ -16,7 +16,7 @@ namespace Query\Tests; use BadMethodCallException; use PDO; -use Query\BadDBDriverException; +use Query\Exception\BadDBDriverException; /** * Query builder parent test class diff --git a/tests/Drivers/SQLite/SQLiteDriverTest.php b/tests/Drivers/SQLite/SQLiteDriverTest.php index 323c4d1..e262b43 100644 --- a/tests/Drivers/SQLite/SQLiteDriverTest.php +++ b/tests/Drivers/SQLite/SQLiteDriverTest.php @@ -16,6 +16,7 @@ namespace Query\Tests\Drivers\SQLite; use PDO; use Query\Drivers\Sqlite\Driver; +use Query\Exception\NotImplementedException; use Query\Tests\BaseDriverTest; /** @@ -83,7 +84,8 @@ SQL; { $sql = mb_trim(self::$db->getUtil()->backupStructure()); $expected = <<assertTrue(is_array(self::$db->getDbs())); + $this->assertTrue(\is_array(self::$db->getDbs())); } public function testGetSchemas() @@ -262,18 +264,6 @@ SQL; // ! SQL tests // -------------------------------------------------------------------------- - public function testNullMethods() - { - $sql = self::$db->getSQL()->functionList(); - $this->assertEqual(NULL, $sql); - - $sql = self::$db->getSQL()->procedureList(); - $this->assertEqual(NULL, $sql); - - $sql = self::$db->getSQL()->sequenceList(); - $this->assertEqual(NULL, $sql); - } - public function testGetSystemTables() { $sql = self::$db->getSystemTables(); @@ -282,16 +272,19 @@ SQL; public function testGetSequences() { - $this->assertNull(self::$db->getSequences()); + $sql = self::$db->getSequences(); + $this->assertEquals(['create_test'], $sql); } public function testGetFunctions() { - $this->assertNull(self::$db->getFunctions()); + $this->expectException(NotImplementedException::class); + self::$db->getFunctions(); } public function testGetProcedures() { - $this->assertNull(self::$db->getProcedures()); + $this->expectException(NotImplementedException::class); + self::$db->getProcedures(); } } diff --git a/tests/db_files/oci.sql b/tests/db_files/oci.sql new file mode 100644 index 0000000..e69de29 diff --git a/tests/db_files/sqlite.sql b/tests/db_files/sqlite.sql index babf32c..d3d572b 100644 --- a/tests/db_files/sqlite.sql +++ b/tests/db_files/sqlite.sql @@ -1,5 +1,5 @@ -- sample data to test SQLite -CREATE TABLE IF NOT EXISTS "create_test" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT); +CREATE TABLE IF NOT EXISTS "create_test" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "key" TEXT, "val" TEXT); CREATE TABLE IF NOT EXISTS "create_join" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT); CREATE TABLE IF NOT EXISTS "create_delete" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT);