From 672f0b317776fdbaf39d0916321405f28da5694f Mon Sep 17 00:00:00 2001 From: Joby Elliott Date: Fri, 17 Aug 2018 12:12:21 -0600 Subject: [PATCH 1/3] cleanup pgsql needs to be developed off on its own feature branch --- benchmark/results.txt | 23 ----- benchmark/run.php | 143 ------------------------------- src/Drivers/PostgreSQLDriver.php | 87 ------------------- 3 files changed, 253 deletions(-) delete mode 100644 benchmark/results.txt delete mode 100644 benchmark/run.php delete mode 100644 src/Drivers/PostgreSQLDriver.php diff --git a/benchmark/results.txt b/benchmark/results.txt deleted file mode 100644 index 07f78d7..0000000 --- a/benchmark/results.txt +++ /dev/null @@ -1,23 +0,0 @@ -Date: 2018-08-10T03:16:29+01:00 -Machine: PAULZE -Ops per: 1000 -Each result is the average number of milliseconds it took to complete each operation. Lower is better. - -Digraph\Destructr\Drivers\MySQLDriver -insert: 30.19ms -update: 31.07ms -search vcol: 147.44ms -search json: 154.34ms - -Digraph\Destructr\LegacyDrivers\MySQL56Driver -insert: 32.89ms -update: 25.11ms -search vcol: 174.05ms -search json: 192.56ms - -Digraph\Destructr\LegacyDrivers\SQLiteDriver -insert: 140.28ms -update: 145.12ms -search vcol: 157.92ms -search json: 165.01ms - diff --git a/benchmark/run.php b/benchmark/run.php deleted file mode 100644 index 6eaf699..0000000 --- a/benchmark/run.php +++ /dev/null @@ -1,143 +0,0 @@ - $config) { - $driver = new $class(@$config['dsn'], @$config['username'], @$config['password'], @$config['options']); - $factory = new Factory($driver, $config['table']); - $factory->createTable(); - benchmark_empty($factory); - $out[] = $class; - $out[] = benchmark_insert($factory); - $out[] = benchmark_update($factory); - $out[] = benchmark_search_vcol($factory); - $out[] = benchmark_search_json($factory); - $out[]= ''; -} -$out[] = ''; - -file_put_contents(__DIR__.'/results.txt', implode(PHP_EOL, $out)); - -/** - * The classes and connection settings for benchmarking - */ -function drivers_list() -{ - @unlink(__DIR__.'/test.sqlite'); - $out = []; - $out[MySQLDriver::class] = [ - 'table' => 'benchmark57', - 'dsn' => 'mysql:host=127.0.0.1;dbname=phpunit', - 'username' => 'travis' - ]; - $out[MySQL56Driver::class] = [ - 'table' => 'benchmark56', - 'dsn' => 'mysql:host=127.0.0.1;dbname=phpunit', - 'username' => 'travis' - ]; - $out[SQLiteDriver::class] = [ - 'table' => 'benchmark', - 'dsn' => 'sqlite:'.__DIR__.'/test.sqlite' - ]; - return $out; -} - -/** - * Empties a table before beginning - */ -function benchmark_empty(&$factory) -{ - global $dsos; - $dsos = []; - foreach ($factory->search()->execute([], null) as $o) { - $o->delete(true); - } -} - -/** - * Benchmark insert operations - */ -function benchmark_insert(&$factory) -{ - global $dsos; - $start = microtime(true); - for ($i=0; $i < OPS_PER; $i++) { - $dsos[$i] = $factory->create( - [ - 'dso.id'=>'benchmark-'.$i, - 'dso.type'=>'benchmark-'.($i%2?'odd':'even'), - 'benchmark.mod'=>($i%2?'odd':'even') - ] - ); - $dsos[$i]->insert(); - } - $end = microtime(true); - $per = round(($end-$start)*100000/OPS_PER)/100; - return 'insert: '.$per.'ms'; -} - -/** - * Benchmark update operations - */ -function benchmark_update(&$factory) -{ - global $dsos; - $start = microtime(true); - for ($i=0; $i < OPS_PER; $i++) { - $dsos[$i]['benchmark.int'] = $i; - $dsos[$i]['benchmark.string'] = 'benchmark-'.$i; - $dsos[$i]->update(); - } - $end = microtime(true); - $per = round(($end-$start)*100000/OPS_PER)/100; - return 'update: '.$per.'ms'; -} - -/** - * Benchmark searching on a vcol - */ -function benchmark_search_vcol(&$factory) -{ - $start = microtime(true); - for ($i=0; $i < OPS_PER; $i++) { - $search = $factory->search(); - $search->where('${dso.type} = :type'); - $search->execute([':type'=>'benchmark-odd']); - } - $end = microtime(true); - $per = round(($end-$start)*100000/OPS_PER)/100; - return 'search vcol: '.$per.'ms'; -} - -/** - * Benchmark searching on a JSON value - */ -function benchmark_search_json(&$factory) -{ - $start = microtime(true); - for ($i=0; $i < OPS_PER; $i++) { - $search = $factory->search(); - $search->where('${benchmark.mod} = :type'); - $search->execute([':type'=>'even']); - } - $end = microtime(true); - $per = round(($end-$start)*100000/OPS_PER)/100; - return 'search json: '.$per.'ms'; -} diff --git a/src/Drivers/PostgreSQLDriver.php b/src/Drivers/PostgreSQLDriver.php deleted file mode 100644 index 1c30dc6..0000000 --- a/src/Drivers/PostgreSQLDriver.php +++ /dev/null @@ -1,87 +0,0 @@ -=9.3 - * - * Eventually, anyway. At the moment it's untested and probably doesn't work. - */ -class PostgreSQLDriver extends AbstractDriver -{ - /** - * Within the search we expand strings like ${dso.id} into JSON queries. - * Note that the Search will have already had these strings expanded into - * column names if there are virtual columns configured for them. That - * happens in the Factory before it gets here. - */ - protected function sql_select($args) - { - //extract query parts from Search and expand paths - $where = $this->expandPaths($args['search']->where()); - $order = $this->expandPaths($args['search']->order()); - $limit = $args['search']->limit(); - $offset = $args['search']->offset(); - //select from - $out = ["SELECT * FROM `{$args['table']}`"]; - //where statement - if ($where !== null) { - $out[] = "WHERE ".$where; - } - //order statement - if ($order !== null) { - $out[] = "ORDER BY ".$order; - } - //limit - if ($limit !== null) { - $out[] = "LIMIT ".$limit; - } - //offset - if ($offset !== null) { - $out[] = "OFFSET ".$offset; - } - //return - return implode(PHP_EOL, $out).';'; - } - - protected function sql_ddl($args=array()) - { - $out = []; - $out[] = "CREATE TABLE `{$args['table']}` ("; - $lines = []; - $lines[] = "`json_data` JSON DEFAULT NULL"; - foreach ($args['virtualColumns'] as $path => $col) { - $lines[] = "`{$col['name']}` {$col['type']} GENERATED ALWAYS AS (".$this->expandPath($path).") VIRTUAL"; - } - foreach ($args['virtualColumns'] as $path => $col) { - if (@$col['unique'] && $as = @$col['index']) { - $lines[] = "UNIQUE KEY `{$args['table']}_{$col['name']}_idx` (`{$col['name']}`) USING $as"; - } elseif ($as = @$col['index']) { - $lines[] = "KEY `{$args['table']}_{$col['name']}_idx` (`{$col['name']}`) USING $as"; - } - } - $out[] = implode(','.PHP_EOL, $lines); - $out[] = ") ENGINE=InnoDB DEFAULT CHARSET=utf8;"; - return implode(PHP_EOL, $out); - } - - protected function expandPath(string $path) : string - { - return "JSON_UNQUOTE(JSON_EXTRACT(`json_data`,'$.{$path}'))"; - } - - protected function sql_setJSON($args) - { - return 'UPDATE `'.$args['table'].'` SET `json_data` = :data WHERE `dso_id` = :dso_id;'; - } - - protected function sql_insert($args) - { - return "INSERT INTO `{$args['table']}` (`json_data`) VALUES (:data);"; - } - - protected function sql_delete($args) - { - return 'DELETE FROM `'.$args['table'].'` WHERE `dso_id` = :dso_id;'; - } -} From 1c2a355a5a4c6ff8e75fb07ceabdc3b151652862 Mon Sep 17 00:00:00 2001 From: Joby Elliott Date: Fri, 17 Aug 2018 16:32:26 -0600 Subject: [PATCH 2/3] changing namespace --- README.md | 4 ++-- composer.json | 4 ++-- src/DSO.php | 4 ++-- src/DSOFactoryInterface.php | 7 ++----- src/DSOInterface.php | 4 ++-- src/DriverFactory.php | 4 ++-- src/Drivers/AbstractDriver.php | 8 ++++---- src/Drivers/DSODriverInterface.php | 8 ++++---- src/Drivers/MySQLDriver.php | 4 ++-- src/Factory.php | 4 ++-- src/LegacyDrivers/AbstractLegacyDriver.php | 12 ++++++------ src/LegacyDrivers/MySQL56Driver.php | 4 ++-- src/LegacyDrivers/README.md | 4 ++-- src/LegacyDrivers/SQLiteDriver.php | 10 +++++----- src/Search.php | 8 ++++---- tests/DSOTest.php | 4 ++-- tests/Drivers/AbstractDriverTest.php | 8 ++++---- .../AbstractDriverIntegrationTest.php | 10 +++++----- .../IntegrationTests/MySQLDriverIntegrationTest.php | 6 +++--- tests/Drivers/MySQLDriverTest.php | 4 ++-- tests/FactoryTest.php | 4 ++-- tests/HarnessDriver.php | 4 ++-- .../IntegrationTests/SQLiteDriverIntegrationTest.php | 8 ++++---- tests/LegacyDrivers/MySQL56DriverTest.php | 8 ++++---- tests/LegacyDrivers/SQLiteDriverTest.php | 6 +++--- 25 files changed, 74 insertions(+), 77 deletions(-) diff --git a/README.md b/README.md index 9581cf5..502bae9 100644 --- a/README.md +++ b/README.md @@ -21,13 +21,13 @@ In order to read/write objects from a database table, you'll need to configure a // DriverFactory::factory() has the same arguments as PDO::__construct // You can also construct a driver directly, from a class in Drivers, // but for common databases DriverFactory::factory should pick the right class -$driver = \Digraph\DriverFactory::factory( +$driver = \Destructr\DriverFactory::factory( 'mysql:host=127.0.0.1', 'username', 'password' ); // Driver is then used to construct a Factory -$factory = new \Digraph\Destructr\Factory( +$factory = new \Destructr\Factory( $driver, //driver is used to manage connection and generate queries 'dso_objects' //all of a Factory's data is stored in a single table ); diff --git a/composer.json b/composer.json index 800d9ba..a0fe501 100644 --- a/composer.json +++ b/composer.json @@ -30,12 +30,12 @@ }, "autoload": { "psr-4": { - "Digraph\\Destructr\\": "src/" + "Destructr\\": "src/" } }, "autoload-dev": { "psr-4": { - "Digraph\\Destructr\\": "tests/" + "Destructr\\": "tests/" } } } diff --git a/src/DSO.php b/src/DSO.php index 8d731ad..ccd75dd 100644 --- a/src/DSO.php +++ b/src/DSO.php @@ -1,6 +1,6 @@ */ -namespace Digraph\Destructr; +/* Destructr | https://gitlab.com/byjoby/destructr | MIT License */ +namespace Destructr; interface DSOFactoryInterface { diff --git a/src/DSOInterface.php b/src/DSOInterface.php index 373eeee..ba54c93 100644 --- a/src/DSOInterface.php +++ b/src/DSOInterface.php @@ -1,6 +1,6 @@ pdo->sqliteCreateFunction( 'DESTRUCTR_JSON_EXTRACT', - '\\Digraph\\Destructr\\LegacyDrivers\\SQLiteDriver::JSON_EXTRACT', + '\\Destructr\\LegacyDrivers\\SQLiteDriver::JSON_EXTRACT', 2 ); } diff --git a/src/Search.php b/src/Search.php index 6e16f6e..e79854c 100644 --- a/src/Search.php +++ b/src/Search.php @@ -1,9 +1,9 @@ Date: Sat, 18 Aug 2018 09:45:55 -0600 Subject: [PATCH 3/3] updating deps --- composer.json | 6 ++--- src/DSO.php | 2 +- src/DSOInterface.php | 2 +- src/LegacyDrivers/AbstractLegacyDriver.php | 2 +- src/LegacyDrivers/SQLiteDriver.php | 1 - src/Search.php | 26 +++++++++++++++++----- 6 files changed, 26 insertions(+), 13 deletions(-) diff --git a/composer.json b/composer.json index a0fe501..c3118ff 100644 --- a/composer.json +++ b/composer.json @@ -7,8 +7,8 @@ "prefer-stable": true, "require": { "php": ">=7.1", - "digraphcms/utilities": "^0.6", - "mofodojodino/profanity-filter": "^1.3" + "mofodojodino/profanity-filter": "^1.3", + "byjoby/flatrr": "dev-master" }, "require-dev": { "phpunit/phpunit": "^7", @@ -38,4 +38,4 @@ "Destructr\\": "tests/" } } -} +} \ No newline at end of file diff --git a/src/DSO.php b/src/DSO.php index ccd75dd..870983f 100644 --- a/src/DSO.php +++ b/src/DSO.php @@ -2,7 +2,7 @@ /* Destructr | https://gitlab.com/byjoby/destructr | MIT License */ namespace Destructr; -use \Digraph\FlatArray\FlatArray; +use \Flatrr\FlatArray; /** * Interface for DeStructure Objects (DSOs). These are the class that is diff --git a/src/DSOInterface.php b/src/DSOInterface.php index ba54c93..1de8136 100644 --- a/src/DSOInterface.php +++ b/src/DSOInterface.php @@ -2,7 +2,7 @@ /* Destructr | https://gitlab.com/byjoby/destructr | MIT License */ namespace Destructr; -use Digraph\FlatArray\FlatArrayInterface; +use Flatrr\FlatArrayInterface; /** * Interface for DeStructure Objects (DSOs). These are the class that is diff --git a/src/LegacyDrivers/AbstractLegacyDriver.php b/src/LegacyDrivers/AbstractLegacyDriver.php index 9bcd125..e468eb1 100644 --- a/src/LegacyDrivers/AbstractLegacyDriver.php +++ b/src/LegacyDrivers/AbstractLegacyDriver.php @@ -6,7 +6,7 @@ use Destructr\Drivers\AbstractDriver; use Destructr\DSOInterface; use Destructr\Factory; use Destructr\Search; -use Digraph\FlatArray\FlatArray; +use Flatrr\FlatArray; /** * This driver is for supporting older SQL servers that don't have their own diff --git a/src/LegacyDrivers/SQLiteDriver.php b/src/LegacyDrivers/SQLiteDriver.php index 13da790..851f662 100644 --- a/src/LegacyDrivers/SQLiteDriver.php +++ b/src/LegacyDrivers/SQLiteDriver.php @@ -3,7 +3,6 @@ namespace Destructr\LegacyDrivers; use Destructr\DSOInterface; -use Digraph\FlatArray\FlatArray; use Destructr\Factory; /** diff --git a/src/Search.php b/src/Search.php index e79854c..da72140 100644 --- a/src/Search.php +++ b/src/Search.php @@ -7,9 +7,11 @@ use Destructr\Drivers\DSODriverInterface; class Search implements \Serializable { - use \Digraph\Utilities\ValueFunctionTrait; - protected $factory; + protected $where; + protected $order; + protected $limit; + protected $offset; public function __construct(DSOFactoryInterface &$factory=null) { @@ -23,22 +25,34 @@ class Search implements \Serializable public function where(string $set = null) : ?string { - return $this->valueFunction('where', $set); + if ($set !== null) { + $this->where = $set; + } + return $this->where; } public function order(string $set = null) : ?string { - return $this->valueFunction('order', $set); + if ($set !== null) { + $this->order = $set; + } + return $this->order; } public function limit(int $set = null) : ?int { - return $this->valueFunction('limit', $set); + if ($set !== null) { + $this->limit = $set; + } + return $this->limit; } public function offset(int $set = null) : ?int { - return $this->valueFunction('offset', $set); + if ($set !== null) { + $this->offset = $set; + } + return $this->offset; } public function serialize()