style fixes, create table fixes, MariaDB fixes
This commit is contained in:
parent
62102fbf9f
commit
4bf591804c
21 changed files with 250 additions and 136 deletions
|
@ -1,5 +1,5 @@
|
|||
<?php
|
||||
/* Destructr | https://gitlab.com/byjoby/destructr | MIT License */
|
||||
/* Destructr | https://github.com/jobyone/destructr | MIT License */
|
||||
namespace Destructr;
|
||||
|
||||
use \Flatrr\FlatArray;
|
||||
|
@ -15,7 +15,7 @@ class DSO extends FlatArray implements DSOInterface
|
|||
protected $changes;
|
||||
protected $removals;
|
||||
|
||||
public function __construct(array $data = null, DSOFactoryInterface &$factory = null)
|
||||
public function __construct(array $data = null, DSOFactoryInterface $factory = null)
|
||||
{
|
||||
$this->resetChanges();
|
||||
parent::__construct($data);
|
||||
|
@ -111,7 +111,7 @@ class DSO extends FlatArray implements DSOInterface
|
|||
}
|
||||
}
|
||||
|
||||
public function factory(DSOFactoryInterface &$factory = null) : ?DSOFactoryInterface
|
||||
public function factory(DSOFactoryInterface $factory = null) : ?DSOFactoryInterface
|
||||
{
|
||||
if ($factory) {
|
||||
$this->factory = $factory;
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
<?php
|
||||
/* Destructr | https://gitlab.com/byjoby/destructr | MIT License */
|
||||
/* Destructr | https://github.com/jobyone/destructr | MIT License */
|
||||
namespace Destructr;
|
||||
|
||||
interface DSOFactoryInterface
|
||||
{
|
||||
public function __construct(Drivers\DSODriverInterface &$driver, string $table);
|
||||
public function __construct(Drivers\DSODriverInterface $driver, string $table);
|
||||
|
||||
public function class(array $data) : ?string;
|
||||
|
||||
public function createTable() : bool;
|
||||
public function create(array $data = array()) : DSOInterface;
|
||||
public function read(string $value, string $field = 'dso.id', $deleted = false) : ?DSOInterface;
|
||||
public function insert(DSOInterface &$dso) : bool;
|
||||
public function update(DSOInterface &$dso, bool $sneaky = false) : bool;
|
||||
public function delete(DSOInterface &$dso, bool $permanent = false) : bool;
|
||||
public function insert(DSOInterface $dso) : bool;
|
||||
public function update(DSOInterface $dso, bool $sneaky = false) : bool;
|
||||
public function delete(DSOInterface $dso, bool $permanent = false) : bool;
|
||||
public function quote(string $str) : string;
|
||||
|
||||
public function search() : Search;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<?php
|
||||
/* Destructr | https://gitlab.com/byjoby/destructr | MIT License */
|
||||
/* Destructr | https://github.com/jobyone/destructr | MIT License */
|
||||
namespace Destructr;
|
||||
|
||||
use Flatrr\FlatArrayInterface;
|
||||
|
@ -11,8 +11,8 @@ use Flatrr\FlatArrayInterface;
|
|||
*/
|
||||
interface DSOInterface extends FlatArrayInterface
|
||||
{
|
||||
public function __construct(array $data = null, DSOFactoryInterface &$factory = null);
|
||||
public function factory(DSOFactoryInterface &$factory = null) : ?DSOFactoryInterface;
|
||||
public function __construct(array $data = null, DSOFactoryInterface $factory = null);
|
||||
public function factory(DSOFactoryInterface $factory = null) : ?DSOFactoryInterface;
|
||||
|
||||
public function set(string $name = null, $value, $force=false);
|
||||
|
||||
|
|
|
@ -1,15 +1,16 @@
|
|||
<?php
|
||||
/* Destructr | https://gitlab.com/byjoby/destructr | MIT License */
|
||||
/* Destructr | https://github.com/jobyone/destructr | MIT License */
|
||||
namespace Destructr;
|
||||
|
||||
class DriverFactory
|
||||
{
|
||||
public static $map = [
|
||||
'mariadb' => Drivers\MariaDBDriver::class,
|
||||
'mysql' => Drivers\MySQLDriver::class,
|
||||
'sqlite' => LegacyDrivers\SQLiteDriver::class
|
||||
'sqlite' => LegacyDrivers\SQLiteDriver::class,
|
||||
];
|
||||
|
||||
public static function factory(string $dsn, string $username=null, string $password=null, array $options=null, string $type = null) : ?Drivers\DSODriverInterface
|
||||
public static function factory(string $dsn, string $username = null, string $password = null, array $options = null, string $type = null): ?Drivers\DSODriverInterface
|
||||
{
|
||||
if (!$type) {
|
||||
$type = @array_shift(explode(':', $dsn, 2));
|
||||
|
@ -22,7 +23,7 @@ class DriverFactory
|
|||
}
|
||||
}
|
||||
|
||||
public static function factoryFromPDO(\PDO &$pdo, string $type = null) : ?Drivers\DSODriverInterface
|
||||
public static function factoryFromPDO(\PDO $pdo, string $type = null): ?Drivers\DSODriverInterface
|
||||
{
|
||||
if (!$type) {
|
||||
$type = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
<?php
|
||||
/* Destructr | https://gitlab.com/byjoby/destructr | MIT License */
|
||||
/* Destructr | https://github.com/jobyone/destructr | MIT License */
|
||||
namespace Destructr\Drivers;
|
||||
|
||||
use Destructr\DSOInterface;
|
||||
use Destructr\Search;
|
||||
use PDO;
|
||||
|
||||
//TODO: Caching? It should happen somewhere in this class I think.
|
||||
abstract class AbstractDriver implements DSODriverInterface
|
||||
|
@ -12,7 +13,7 @@ abstract class AbstractDriver implements DSODriverInterface
|
|||
public $pdo;
|
||||
const EXTENSIBLE_VIRTUAL_COLUMNS = true;
|
||||
|
||||
public function __construct(string $dsn=null, string $username=null, string $password=null, array $options=null)
|
||||
public function __construct(string $dsn = null, string $username = null, string $password = null, array $options = null)
|
||||
{
|
||||
if ($dsn) {
|
||||
if (!($pdo = new \PDO($dsn, $username, $password, $options))) {
|
||||
|
@ -22,9 +23,11 @@ abstract class AbstractDriver implements DSODriverInterface
|
|||
}
|
||||
}
|
||||
|
||||
public function &pdo(\PDO &$pdo=null) : ?\PDO
|
||||
public function pdo(\PDO $pdo = null): ?\PDO
|
||||
{
|
||||
if ($pdo) {
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
|
||||
$this->pdo = $pdo;
|
||||
}
|
||||
return $this->pdo;
|
||||
|
@ -50,38 +53,38 @@ abstract class AbstractDriver implements DSODriverInterface
|
|||
return $this->pdo->errorInfo();
|
||||
}
|
||||
|
||||
public function createTable(string $table, array $virtualColumns) : bool
|
||||
public function createTable(string $table, array $virtualColumns): bool
|
||||
{
|
||||
$sql = $this->sql_ddl([
|
||||
'table'=>$table,
|
||||
'virtualColumns'=>$virtualColumns
|
||||
'table' => $table,
|
||||
'virtualColumns' => $virtualColumns,
|
||||
]);
|
||||
return $this->pdo->exec($sql) !== false;
|
||||
}
|
||||
|
||||
public function update(string $table, DSOInterface $dso) : bool
|
||||
public function update(string $table, DSOInterface $dso): bool
|
||||
{
|
||||
if (!$dso->changes() && !$dso->removals()) {
|
||||
return true;
|
||||
}
|
||||
$s = $this->getStatement(
|
||||
'setJSON',
|
||||
['table'=>$table]
|
||||
['table' => $table]
|
||||
);
|
||||
return $s->execute([
|
||||
':dso_id' => $dso['dso.id'],
|
||||
':data' => json_encode($dso->get())
|
||||
':data' => json_encode($dso->get()),
|
||||
]);
|
||||
}
|
||||
|
||||
public function delete(string $table, DSOInterface $dso) : bool
|
||||
public function delete(string $table, DSOInterface $dso): bool
|
||||
{
|
||||
$s = $this->getStatement(
|
||||
'delete',
|
||||
['table'=>$table]
|
||||
['table' => $table]
|
||||
);
|
||||
return $s->execute([
|
||||
':dso_id' => $dso['dso.id']
|
||||
':dso_id' => $dso['dso.id'],
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -89,7 +92,7 @@ abstract class AbstractDriver implements DSODriverInterface
|
|||
{
|
||||
$s = $this->getStatement(
|
||||
'count',
|
||||
['table'=>$table,'search'=>$search]
|
||||
['table' => $table, 'search' => $search]
|
||||
);
|
||||
if (!$s->execute($params)) {
|
||||
return null;
|
||||
|
@ -101,7 +104,7 @@ abstract class AbstractDriver implements DSODriverInterface
|
|||
{
|
||||
$s = $this->getStatement(
|
||||
'select',
|
||||
['table'=>$table,'search'=>$search]
|
||||
['table' => $table, 'search' => $search]
|
||||
);
|
||||
if (!$s->execute($params)) {
|
||||
return [];
|
||||
|
@ -109,19 +112,19 @@ abstract class AbstractDriver implements DSODriverInterface
|
|||
return @$s->fetchAll(\PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
public function insert(string $table, DSOInterface $dso) : bool
|
||||
public function insert(string $table, DSOInterface $dso): bool
|
||||
{
|
||||
return $this->getStatement(
|
||||
'insert',
|
||||
['table'=>$table]
|
||||
['table' => $table]
|
||||
)->execute(
|
||||
[':data'=>json_encode($dso->get())]
|
||||
[':data' => json_encode($dso->get())]
|
||||
);
|
||||
}
|
||||
|
||||
protected function getStatement(string $type, $args=array()) : \PDOStatement
|
||||
protected function getStatement(string $type, $args = array()): \PDOStatement
|
||||
{
|
||||
$fn = 'sql_'.$type;
|
||||
$fn = 'sql_' . $type;
|
||||
if (!method_exists($this, $fn)) {
|
||||
throw new \Exception("Error getting SQL statement, driver doesn't have a method named $fn");
|
||||
}
|
||||
|
@ -129,7 +132,7 @@ abstract class AbstractDriver implements DSODriverInterface
|
|||
$stmt = $this->pdo->prepare($sql);
|
||||
if (!$stmt) {
|
||||
$this->lastPreparationErrorOn = $sql;
|
||||
throw new \Exception("Error preparing statement: ".implode(': ', $this->pdo->errorInfo()), 1);
|
||||
throw new \Exception("Error preparing statement: " . implode(': ', $this->pdo->errorInfo()), 1);
|
||||
}
|
||||
return $stmt;
|
||||
//TODO: turn this on someday and see if caching statements helps in the real world
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<?php
|
||||
/* Destructr | https://gitlab.com/byjoby/destructr | MIT License */
|
||||
/* Destructr | https://github.com/jobyone/destructr | MIT License */
|
||||
namespace Destructr\Drivers;
|
||||
|
||||
use Destructr\DSOInterface;
|
||||
|
@ -8,7 +8,7 @@ use Destructr\Search;
|
|||
interface DSODriverInterface
|
||||
{
|
||||
public function __construct(string $dsn=null, string $username=null, string $password=null, array $options=null);
|
||||
public function &pdo(\PDO &$pdo=null) : ?\PDO;
|
||||
public function pdo(\PDO $pdo=null) : ?\PDO;
|
||||
|
||||
public function createTable(string $table, array $virtualColumns) : bool;
|
||||
public function select(string $table, Search $search, array $params);
|
||||
|
|
108
src/Drivers/MariaDBDriver.php
Normal file
108
src/Drivers/MariaDBDriver.php
Normal file
|
@ -0,0 +1,108 @@
|
|||
<?php
|
||||
/* Destructr | https://github.com/jobyone/destructr | MIT License */
|
||||
namespace Destructr\Drivers;
|
||||
|
||||
/**
|
||||
* What this driver supports: MariaDB >= 10.2
|
||||
*/
|
||||
class MariaDBDriver 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_count($args)
|
||||
{
|
||||
//extract query parts from Search and expand paths
|
||||
$where = $this->expandPaths($args['search']->where());
|
||||
//select from
|
||||
$out = ["SELECT count(dso_id) FROM `{$args['table']}`"];
|
||||
//where statement
|
||||
if ($where !== null) {
|
||||
$out[] = "WHERE ".$where;
|
||||
}
|
||||
//return
|
||||
return implode(PHP_EOL, $out).';';
|
||||
}
|
||||
|
||||
protected function sql_ddl($args=array())
|
||||
{
|
||||
$out = [];
|
||||
$out[] = "CREATE TABLE IF NOT EXISTS `{$args['table']}` (";
|
||||
$lines = [];
|
||||
$lines[] = "`json_data` JSON DEFAULT NULL";
|
||||
foreach ($args['virtualColumns'] as $path => $col) {
|
||||
$line = "`{$col['name']}` {$col['type']} GENERATED ALWAYS AS (".$this->expandPath($path).")";
|
||||
if (@$col['primary']) {
|
||||
//this needs to be "PERSISTENT" for MariaDB -- I guess there are going to be two drivers now
|
||||
$line .= ' STORED';
|
||||
} else {
|
||||
$line .= ' VIRTUAL';
|
||||
}
|
||||
$lines[] = $line;
|
||||
}
|
||||
foreach ($args['virtualColumns'] as $path => $col) {
|
||||
if (@$col['primary']) {
|
||||
$lines[] = "UNIQUE KEY (`{$col['name']}`)";
|
||||
} elseif (@$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;';
|
||||
}
|
||||
}
|
|
@ -1,12 +1,9 @@
|
|||
<?php
|
||||
/* Destructr | https://gitlab.com/byjoby/destructr | MIT License */
|
||||
/* Destructr | https://github.com/jobyone/destructr | MIT License */
|
||||
namespace Destructr\Drivers;
|
||||
|
||||
/**
|
||||
* What this driver supports: MySQL and MariaDB databases new enough to support
|
||||
* JSON functions. This means:
|
||||
* * MySQL >= 5.7
|
||||
* * MariaDB >= 10.2
|
||||
* What this driver supports: MySQL >= 5.7
|
||||
*/
|
||||
class MySQLDriver extends AbstractDriver
|
||||
{
|
||||
|
@ -62,7 +59,7 @@ class MySQLDriver extends AbstractDriver
|
|||
protected function sql_ddl($args=array())
|
||||
{
|
||||
$out = [];
|
||||
$out[] = "CREATE TABLE `{$args['table']}` (";
|
||||
$out[] = "CREATE TABLE IF NOT EXISTS `{$args['table']}` (";
|
||||
$lines = [];
|
||||
$lines[] = "`json_data` JSON DEFAULT NULL";
|
||||
foreach ($args['virtualColumns'] as $path => $col) {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<?php
|
||||
/* Destructr | https://gitlab.com/byjoby/destructr | MIT License */
|
||||
/* Destructr | https://github.com/jobyone/destructr | MIT License */
|
||||
namespace Destructr;
|
||||
|
||||
use mofodojodino\ProfanityFilter\Check;
|
||||
|
@ -73,7 +73,7 @@ class Factory implements DSOFactoryInterface
|
|||
]
|
||||
];
|
||||
|
||||
public function __construct(Drivers\DSODriverInterface &$driver, string $table)
|
||||
public function __construct(Drivers\DSODriverInterface $driver, string $table)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
$this->table = $table;
|
||||
|
@ -84,7 +84,7 @@ class Factory implements DSOFactoryInterface
|
|||
return $this->driver->pdo()->quote($str);
|
||||
}
|
||||
|
||||
protected function hook_create(DSOInterface &$dso)
|
||||
protected function hook_create(DSOInterface $dso)
|
||||
{
|
||||
if (!$dso->get('dso.id')) {
|
||||
$dso->set('dso.id', static::generate_id(static::ID_CHARS, static::ID_LENGTH), true);
|
||||
|
@ -97,18 +97,27 @@ class Factory implements DSOFactoryInterface
|
|||
}
|
||||
}
|
||||
|
||||
protected function hook_update(DSOInterface &$dso)
|
||||
protected function hook_update(DSOInterface $dso)
|
||||
{
|
||||
$dso->set('dso.modified.date', time());
|
||||
$dso->set('dso.modified.user', ['ip'=>@$_SERVER['REMOTE_ADDR']]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Override this function to allow a factory to create different
|
||||
* sub-classes of DSO based on attributes of the given object's
|
||||
* data. For example, you could use a property like dso.class to
|
||||
* select a class from an associative array.
|
||||
*
|
||||
* @param array $data
|
||||
* @return string|null
|
||||
*/
|
||||
public function class(array $data) : ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function delete(DSOInterface &$dso, bool $permanent = false) : bool
|
||||
public function delete(DSOInterface $dso, bool $permanent = false) : bool
|
||||
{
|
||||
if ($permanent) {
|
||||
return $this->driver->delete($this->table, $dso);
|
||||
|
@ -117,7 +126,7 @@ class Factory implements DSOFactoryInterface
|
|||
return $this->update($dso, true);
|
||||
}
|
||||
|
||||
public function undelete(DSOInterface &$dso) : bool
|
||||
public function undelete(DSOInterface $dso) : bool
|
||||
{
|
||||
unset($dso['dso.deleted']);
|
||||
return $this->update($dso, true);
|
||||
|
@ -153,7 +162,7 @@ class Factory implements DSOFactoryInterface
|
|||
return @$vcols[$path]['name'];
|
||||
}
|
||||
|
||||
public function update(DSOInterface &$dso, bool $sneaky = false) : bool
|
||||
public function update(DSOInterface $dso, bool $sneaky = false) : bool
|
||||
{
|
||||
if (!$dso->changes() && !$dso->removals()) {
|
||||
return true;
|
||||
|
@ -221,7 +230,7 @@ class Factory implements DSOFactoryInterface
|
|||
return null;
|
||||
}
|
||||
|
||||
public function insert(DSOInterface &$dso) : bool
|
||||
public function insert(DSOInterface $dso) : bool
|
||||
{
|
||||
$this->hook_update($dso);
|
||||
$dso->hook_update();
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<?php
|
||||
/* Destructr | https://gitlab.com/byjoby/destructr | MIT License */
|
||||
/* Destructr | https://github.com/jobyone/destructr | MIT License */
|
||||
namespace Destructr\LegacyDrivers;
|
||||
|
||||
use Destructr\Drivers\AbstractDriver;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<?php
|
||||
/* Destructr | https://gitlab.com/byjoby/destructr | MIT License */
|
||||
/* Destructr | https://github.com/jobyone/destructr | MIT License */
|
||||
namespace Destructr\LegacyDrivers;
|
||||
|
||||
use Destructr\DSOInterface;
|
||||
|
@ -16,7 +16,7 @@ use Destructr\Factory;
|
|||
*/
|
||||
class SQLiteDriver extends AbstractLegacyDriver
|
||||
{
|
||||
public function &pdo(\PDO &$pdo=null) : ?\PDO
|
||||
public function pdo(\PDO $pdo=null) : ?\PDO
|
||||
{
|
||||
if ($pdo) {
|
||||
$this->pdo = $pdo;
|
||||
|
@ -73,7 +73,7 @@ class SQLiteDriver extends AbstractLegacyDriver
|
|||
protected function sql_ddl($args=array())
|
||||
{
|
||||
$out = [];
|
||||
$out[] = "CREATE TABLE `{$args['table']}` (";
|
||||
$out[] = "CREATE TABLE IF NOT EXISTS `{$args['table']}` (";
|
||||
$lines = [];
|
||||
$lines[] = "`json_data` TEXT DEFAULT NULL";
|
||||
foreach (Factory::CORE_VIRTUAL_COLUMNS as $path => $col) {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<?php
|
||||
/* Destructr | https://gitlab.com/byjoby/destructr | MIT License */
|
||||
/* Destructr | https://github.com/jobyone/destructr | MIT License */
|
||||
namespace Destructr;
|
||||
|
||||
use Destructr\DSOFactoryInterface;
|
||||
|
@ -13,7 +13,7 @@ class Search implements \Serializable
|
|||
protected $limit;
|
||||
protected $offset;
|
||||
|
||||
public function __construct(DSOFactoryInterface &$factory=null)
|
||||
public function __construct(DSOFactoryInterface $factory=null)
|
||||
{
|
||||
$this->factory = $factory;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<?php
|
||||
/* Destructr | https://gitlab.com/byjoby/destructr | MIT License */
|
||||
/* Destructr | https://github.com/jobyone/destructr | MIT License */
|
||||
declare(strict_types=1);
|
||||
namespace Destructr;
|
||||
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
<?php
|
||||
/* Destructr | https://gitlab.com/byjoby/destructr | MIT License */
|
||||
declare(strict_types=1);
|
||||
/* Destructr | https://github.com/jobyone/destructr | MIT License */
|
||||
declare (strict_types = 1);
|
||||
namespace Destructr\Drivers;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit\DbUnit\TestCaseTrait;
|
||||
use Destructr\DSO;
|
||||
use Destructr\Search;
|
||||
use Destructr\Factory;
|
||||
use PHPUnit\DbUnit\TestCaseTrait;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
abstract class AbstractDriverIntegrationTest extends TestCase
|
||||
{
|
||||
|
@ -16,7 +14,7 @@ abstract class AbstractDriverIntegrationTest extends TestCase
|
|||
public static function setUpBeforeClass()
|
||||
{
|
||||
$pdo = static::createPDO();
|
||||
$pdo->exec('DROP TABLE '.static::TEST_TABLE);
|
||||
$pdo->exec('DROP TABLE ' . static::TEST_TABLE);
|
||||
}
|
||||
|
||||
public function testCreateTable()
|
||||
|
@ -39,12 +37,12 @@ abstract class AbstractDriverIntegrationTest extends TestCase
|
|||
$startRowCount = $this->getConnection()->getRowCount(static::TEST_TABLE);
|
||||
$factory = $this->createFactory();
|
||||
//inserting a freshly created object should return true
|
||||
$o = $factory->create(['dso.id'=>'object-one']);
|
||||
$o = $factory->create(['dso.id' => 'object-one']);
|
||||
$this->assertTrue($o->insert());
|
||||
//inserting it a second time should not
|
||||
$this->assertFalse($o->insert());
|
||||
//there should now be one more row
|
||||
$this->assertEquals($startRowCount+1, $this->getConnection()->getRowCount(static::TEST_TABLE));
|
||||
$this->assertEquals($startRowCount + 1, $this->getConnection()->getRowCount(static::TEST_TABLE));
|
||||
}
|
||||
|
||||
public function testReadAndUpdate()
|
||||
|
@ -52,9 +50,9 @@ abstract class AbstractDriverIntegrationTest extends TestCase
|
|||
$startRowCount = $this->getConnection()->getRowCount(static::TEST_TABLE);
|
||||
$factory = $this->createFactory();
|
||||
//insert some new objects
|
||||
$a1 = $factory->create(['foo'=>'bar']);
|
||||
$a1 = $factory->create(['foo' => 'bar']);
|
||||
$a1->insert();
|
||||
$b1 = $factory->create(['foo.bar'=>'baz']);
|
||||
$b1 = $factory->create(['foo.bar' => 'baz']);
|
||||
$b1->insert();
|
||||
//read objects back out
|
||||
$a2 = $factory->read($a1['dso.id']);
|
||||
|
@ -77,7 +75,7 @@ abstract class AbstractDriverIntegrationTest extends TestCase
|
|||
$this->assertNotEquals($a1->get(), $a3->get());
|
||||
$this->assertNotEquals($b1->get(), $b3->get());
|
||||
//there should now be two more rows
|
||||
$this->assertEquals($startRowCount+2, $this->getConnection()->getRowCount(static::TEST_TABLE));
|
||||
$this->assertEquals($startRowCount + 2, $this->getConnection()->getRowCount(static::TEST_TABLE));
|
||||
}
|
||||
|
||||
public function testDelete()
|
||||
|
@ -85,17 +83,17 @@ abstract class AbstractDriverIntegrationTest extends TestCase
|
|||
$startRowCount = $this->getConnection()->getRowCount(static::TEST_TABLE);
|
||||
$factory = $this->createFactory();
|
||||
//insert some new objects
|
||||
$a1 = $factory->create(['testDelete'=>'undelete me']);
|
||||
$a1 = $factory->create(['testDelete' => 'undelete me']);
|
||||
$a1->insert();
|
||||
$b1 = $factory->create(['testDelete'=>'should be permanently deleted']);
|
||||
$b1 = $factory->create(['testDelete' => 'should be permanently deleted']);
|
||||
$b1->insert();
|
||||
//there should now be two more rows
|
||||
$this->assertEquals($startRowCount+2, $this->getConnection()->getRowCount(static::TEST_TABLE));
|
||||
$this->assertEquals($startRowCount + 2, $this->getConnection()->getRowCount(static::TEST_TABLE));
|
||||
//delete one permanently and the other not, both shoudl take effect immediately
|
||||
$a1->delete();
|
||||
$b1->delete(true);
|
||||
//there should now be only one more row
|
||||
$this->assertEquals($startRowCount+1, $this->getConnection()->getRowCount(static::TEST_TABLE));
|
||||
$this->assertEquals($startRowCount + 1, $this->getConnection()->getRowCount(static::TEST_TABLE));
|
||||
//a should be possible to read a back out with the right flags
|
||||
$this->assertNull($factory->read($a1['dso.id']));
|
||||
$this->assertNotNull($factory->read($a1['dso.id'], 'dso.id', true));
|
||||
|
@ -116,25 +114,25 @@ abstract class AbstractDriverIntegrationTest extends TestCase
|
|||
$factory->create([
|
||||
'testSearch' => 'a',
|
||||
'a' => '1',
|
||||
'b' => '2'
|
||||
'b' => '2',
|
||||
])->insert();
|
||||
$factory->create([
|
||||
'testSearch' => 'b',
|
||||
'a' => '2',
|
||||
'b' => '1'
|
||||
'b' => '1',
|
||||
])->insert();
|
||||
$factory->create([
|
||||
'testSearch' => 'c',
|
||||
'a' => '3',
|
||||
'b' => '4'
|
||||
'b' => '4',
|
||||
])->insert();
|
||||
$factory->create([
|
||||
'testSearch' => 'a',
|
||||
'a' => '4',
|
||||
'b' => '3'
|
||||
'b' => '3',
|
||||
])->insert();
|
||||
//there should now be four more rows
|
||||
$this->assertEquals($startRowCount+4, $this->getConnection()->getRowCount(static::TEST_TABLE));
|
||||
$this->assertEquals($startRowCount + 4, $this->getConnection()->getRowCount(static::TEST_TABLE));
|
||||
//TODO: test some searches
|
||||
}
|
||||
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
<?php
|
||||
/* Destructr | https://gitlab.com/byjoby/destructr | MIT License */
|
||||
declare(strict_types=1);
|
||||
/* Destructr | https://github.com/jobyone/destructr | MIT License */
|
||||
declare (strict_types = 1);
|
||||
namespace Destructr\Drivers;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit\DbUnit\TestCaseTrait;
|
||||
use Destructr\DSO;
|
||||
use Destructr\Search;
|
||||
use PHPUnit\DbUnit\TestCaseTrait;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* This class tests a factory in isolation. In the name of simplicity it's a bit
|
||||
|
@ -25,21 +25,21 @@ abstract class AbstractDriverTest extends TestCase
|
|||
*/
|
||||
protected $virtualColumns = [
|
||||
'dso.id' => [
|
||||
'name'=>'dso_id',
|
||||
'type'=>'VARCHAR(16)',
|
||||
'name' => 'dso_id',
|
||||
'type' => 'VARCHAR(16)',
|
||||
'index' => 'BTREE',
|
||||
'unique' => true
|
||||
'unique' => true,
|
||||
],
|
||||
'dso.type' => [
|
||||
'name'=>'dso_type',
|
||||
'type'=>'VARCHAR(30)',
|
||||
'index'=>'BTREE'
|
||||
'name' => 'dso_type',
|
||||
'type' => 'VARCHAR(30)',
|
||||
'index' => 'BTREE',
|
||||
],
|
||||
'dso.deleted' => [
|
||||
'name'=>'dso_deleted',
|
||||
'type'=>'BIGINT',
|
||||
'index'=>'BTREE'
|
||||
]
|
||||
'name' => 'dso_deleted',
|
||||
'type' => 'BIGINT',
|
||||
'index' => 'BTREE',
|
||||
],
|
||||
];
|
||||
|
||||
public function testCreateTable()
|
||||
|
@ -56,15 +56,15 @@ abstract class AbstractDriverTest extends TestCase
|
|||
$driver = $this->createDriver();
|
||||
$driver->createTable('testInsert', $this->virtualColumns);
|
||||
//test inserting an object
|
||||
$o = new DSO(['dso.id'=>'first-inserted']);
|
||||
$o = new DSO(['dso.id' => 'first-inserted']);
|
||||
$this->assertTrue($driver->insert('testInsert', $o));
|
||||
$this->assertEquals(1, $this->getConnection()->getRowCount('testInsert'));
|
||||
//test inserting a second object
|
||||
$o = new DSO(['dso.id'=>'second-inserted']);
|
||||
$o = new DSO(['dso.id' => 'second-inserted']);
|
||||
$this->assertTrue($driver->insert('testInsert', $o));
|
||||
$this->assertEquals(2, $this->getConnection()->getRowCount('testInsert'));
|
||||
//test inserting a second object with an existing id, it shouldn't work
|
||||
$o = new DSO(['dso.id'=>'first-inserted']);
|
||||
$o = new DSO(['dso.id' => 'first-inserted']);
|
||||
$this->assertFalse($driver->insert('testInsert', $o));
|
||||
$this->assertEquals(2, $this->getConnection()->getRowCount('testInsert'));
|
||||
}
|
||||
|
@ -97,12 +97,12 @@ abstract class AbstractDriverTest extends TestCase
|
|||
// search with no results, searching by virtual column
|
||||
$search = new Search();
|
||||
$search->where('`dso_type` = :param');
|
||||
$results = $driver->select('testSelect', $search, [':param'=>'type-none']);
|
||||
$results = $driver->select('testSelect', $search, [':param' => 'type-none']);
|
||||
$this->assertSame(0, count($results));
|
||||
// search with no results, searching by json field
|
||||
$search = new Search();
|
||||
$search->where('${foo} = :param');
|
||||
$results = $driver->select('testSelect', $search, [':param'=>'nonexistent foo value']);
|
||||
$results = $driver->select('testSelect', $search, [':param' => 'nonexistent foo value']);
|
||||
$this->assertSame(0, count($results));
|
||||
}
|
||||
|
||||
|
@ -113,11 +113,11 @@ abstract class AbstractDriverTest extends TestCase
|
|||
//set up dummy data
|
||||
$this->setup_testDelete();
|
||||
//try deleting an item
|
||||
$dso = new DSO(['dso.id'=>'item-a-1']);
|
||||
$dso = new DSO(['dso.id' => 'item-a-1']);
|
||||
$driver->delete('testDelete', $dso);
|
||||
$this->assertEquals(3, $this->getConnection()->getRowCount('testDelete'));
|
||||
//try deleting an item at the other end of the table
|
||||
$dso = new DSO(['dso.id'=>'item-b-2']);
|
||||
$dso = new DSO(['dso.id' => 'item-b-2']);
|
||||
$driver->delete('testDelete', $dso);
|
||||
$this->assertEquals(2, $this->getConnection()->getRowCount('testDelete'));
|
||||
}
|
||||
|
@ -126,24 +126,24 @@ abstract class AbstractDriverTest extends TestCase
|
|||
{
|
||||
$driver = $this->createDriver();
|
||||
$driver->insert('testDelete', new DSO([
|
||||
'dso'=>['id'=>'item-a-1','type'=>'type-a'],
|
||||
'foo'=>'bar',
|
||||
'sort'=>'a'
|
||||
'dso' => ['id' => 'item-a-1', 'type' => 'type-a'],
|
||||
'foo' => 'bar',
|
||||
'sort' => 'a',
|
||||
]));
|
||||
$driver->insert('testDelete', new DSO([
|
||||
'dso'=>['id'=>'item-a-2','type'=>'type-a'],
|
||||
'foo'=>'baz',
|
||||
'sort'=>'c'
|
||||
'dso' => ['id' => 'item-a-2', 'type' => 'type-a'],
|
||||
'foo' => 'baz',
|
||||
'sort' => 'c',
|
||||
]));
|
||||
$driver->insert('testDelete', new DSO([
|
||||
'dso'=>['id'=>'item-b-1','type'=>'type-b'],
|
||||
'foo'=>'buz',
|
||||
'sort'=>'b'
|
||||
'dso' => ['id' => 'item-b-1', 'type' => 'type-b'],
|
||||
'foo' => 'buz',
|
||||
'sort' => 'b',
|
||||
]));
|
||||
$driver->insert('testDelete', new DSO([
|
||||
'dso'=>['id'=>'item-b-2','type'=>'type-b','deleted'=>100],
|
||||
'foo'=>'quz',
|
||||
'sort'=>'d'
|
||||
'dso' => ['id' => 'item-b-2', 'type' => 'type-b', 'deleted' => 100],
|
||||
'foo' => 'quz',
|
||||
'sort' => 'd',
|
||||
]));
|
||||
}
|
||||
|
||||
|
@ -151,24 +151,24 @@ abstract class AbstractDriverTest extends TestCase
|
|||
{
|
||||
$driver = $this->createDriver();
|
||||
$driver->insert('testSelect', new DSO([
|
||||
'dso'=>['id'=>'item-a-1','type'=>'type-a'],
|
||||
'foo'=>'bar',
|
||||
'sort'=>'a'
|
||||
'dso' => ['id' => 'item-a-1', 'type' => 'type-a'],
|
||||
'foo' => 'bar',
|
||||
'sort' => 'a',
|
||||
]));
|
||||
$driver->insert('testSelect', new DSO([
|
||||
'dso'=>['id'=>'item-a-2','type'=>'type-a'],
|
||||
'foo'=>'baz',
|
||||
'sort'=>'c'
|
||||
'dso' => ['id' => 'item-a-2', 'type' => 'type-a'],
|
||||
'foo' => 'baz',
|
||||
'sort' => 'c',
|
||||
]));
|
||||
$driver->insert('testSelect', new DSO([
|
||||
'dso'=>['id'=>'item-b-1','type'=>'type-b'],
|
||||
'foo'=>'buz',
|
||||
'sort'=>'b'
|
||||
'dso' => ['id' => 'item-b-1', 'type' => 'type-b'],
|
||||
'foo' => 'buz',
|
||||
'sort' => 'b',
|
||||
]));
|
||||
$driver->insert('testSelect', new DSO([
|
||||
'dso'=>['id'=>'item-b-2','type'=>'type-b','deleted'=>100],
|
||||
'foo'=>'quz',
|
||||
'sort'=>'d'
|
||||
'dso' => ['id' => 'item-b-2', 'type' => 'type-b', 'deleted' => 100],
|
||||
'foo' => 'quz',
|
||||
'sort' => 'd',
|
||||
]));
|
||||
}
|
||||
|
||||
|
|
|
@ -1,15 +1,14 @@
|
|||
<?php
|
||||
/* Destructr | https://gitlab.com/byjoby/destructr | MIT License */
|
||||
declare(strict_types=1);
|
||||
/* Destructr | https://github.com/jobyone/destructr | MIT License */
|
||||
declare (strict_types = 1);
|
||||
namespace Destructr\Drivers\MySQL;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Destructr\Drivers\AbstractDriverIntegrationTest;
|
||||
use Destructr\Drivers\MySQLDriver;
|
||||
|
||||
class MySQLDriverIntegrationTest extends AbstractDriverIntegrationTest
|
||||
{
|
||||
const DRIVER_CLASS = \Destructr\Drivers\MySQLDriver::class;
|
||||
const DRIVER_CLASS = MySQLDriver::class;
|
||||
const DRIVER_DSN = 'mysql:host=127.0.0.1;dbname=test';
|
||||
const DRIVER_USERNAME = 'root';
|
||||
const DRIVER_PASSWORD = '';
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
<?php
|
||||
/* Destructr | https://gitlab.com/byjoby/destructr | MIT License */
|
||||
declare(strict_types=1);
|
||||
/* Destructr | https://github.com/jobyone/destructr | MIT License */
|
||||
declare (strict_types = 1);
|
||||
namespace Destructr\Drivers\MySQL;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Destructr\Drivers\AbstractDriverTest;
|
||||
use Destructr\Drivers\MySQLDriver;
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<?php
|
||||
/* Destructr | https://gitlab.com/byjoby/destructr | MIT License */
|
||||
/* Destructr | https://github.com/jobyone/destructr | MIT License */
|
||||
declare(strict_types=1);
|
||||
namespace Destructr;
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<?php
|
||||
/* Destructr | https://gitlab.com/byjoby/destructr | MIT License */
|
||||
/* Destructr | https://github.com/jobyone/destructr | MIT License */
|
||||
namespace Destructr;
|
||||
|
||||
class HarnessDriver implements Drivers\DSODriverInterface
|
||||
|
@ -14,7 +14,7 @@ class HarnessDriver implements Drivers\DSODriverInterface
|
|||
{
|
||||
}
|
||||
|
||||
public function &pdo(\PDO &$pdo=null) : ?\PDO {
|
||||
public function pdo(\PDO $pdo=null) : ?\PDO {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<?php
|
||||
/* Destructr | https://gitlab.com/byjoby/destructr | MIT License */
|
||||
/* Destructr | https://github.com/jobyone/destructr | MIT License */
|
||||
declare(strict_types=1);
|
||||
namespace Destructr\LegacyDrivers\SQLite;
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<?php
|
||||
/* Destructr | https://gitlab.com/byjoby/destructr | MIT License */
|
||||
/* Destructr | https://github.com/jobyone/destructr | MIT License */
|
||||
declare(strict_types=1);
|
||||
namespace Destructr\LegacyDrivers\SQLite;
|
||||
|
||||
|
|
Loading…
Reference in a new issue