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/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/composer.json b/composer.json index 800d9ba..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", @@ -30,12 +30,12 @@ }, "autoload": { "psr-4": { - "Digraph\\Destructr\\": "src/" + "Destructr\\": "src/" } }, "autoload-dev": { "psr-4": { - "Digraph\\Destructr\\": "tests/" + "Destructr\\": "tests/" } } -} +} \ No newline at end of file diff --git a/src/DSO.php b/src/DSO.php index 8d731ad..870983f 100644 --- a/src/DSO.php +++ b/src/DSO.php @@ -1,8 +1,8 @@ */ -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..1de8136 100644 --- a/src/DSOInterface.php +++ b/src/DSOInterface.php @@ -1,8 +1,8 @@ =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;'; - } -} diff --git a/src/Factory.php b/src/Factory.php index 8ecf914..2c104b8 100644 --- a/src/Factory.php +++ b/src/Factory.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..da72140 100644 --- a/src/Search.php +++ b/src/Search.php @@ -1,15 +1,17 @@ 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() diff --git a/tests/DSOTest.php b/tests/DSOTest.php index 6538b08..67ef3f7 100644 --- a/tests/DSOTest.php +++ b/tests/DSOTest.php @@ -1,7 +1,7 @@