Allow instantiation with existing PDOs
This commit is contained in:
parent
7f70b8839e
commit
385f6c2b89
4 changed files with 46 additions and 16 deletions
|
@ -21,4 +21,19 @@ class DriverFactory
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function factoryFromPDO(\PDO &$pdo, string $type = null) : ?Drivers\DSODriverInterface
|
||||||
|
{
|
||||||
|
if (!$type) {
|
||||||
|
$type = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
|
||||||
|
}
|
||||||
|
$type = strtolower($type);
|
||||||
|
if ($class = @static::$map[$type]) {
|
||||||
|
$f = new $class();
|
||||||
|
$f->pdo($pdo);
|
||||||
|
return $f;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,13 +12,24 @@ abstract class AbstractDriver implements DSODriverInterface
|
||||||
public $pdo;
|
public $pdo;
|
||||||
const EXTENSIBLE_VIRTUAL_COLUMNS = true;
|
const EXTENSIBLE_VIRTUAL_COLUMNS = true;
|
||||||
|
|
||||||
public function __construct(string $dsn, 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 (!$this->pdo = new \PDO($dsn, $username, $password, $options)) {
|
if ($dsn) {
|
||||||
throw new \Exception("Error creating PDO connection");
|
if (!($pdo = new \PDO($dsn, $username, $password, $options))) {
|
||||||
|
throw new \Exception("Error creating PDO connection");
|
||||||
|
}
|
||||||
|
$this->pdo($pdo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function &pdo(\PDO &$pdo=null) : ?\PDO
|
||||||
|
{
|
||||||
|
if ($pdo) {
|
||||||
|
$this->pdo = $pdo;
|
||||||
|
}
|
||||||
|
return $this->pdo;
|
||||||
|
}
|
||||||
|
|
||||||
protected function expandPaths($value)
|
protected function expandPaths($value)
|
||||||
{
|
{
|
||||||
if ($value === null) {
|
if ($value === null) {
|
||||||
|
|
|
@ -7,7 +7,8 @@ use Destructr\Search;
|
||||||
|
|
||||||
interface DSODriverInterface
|
interface DSODriverInterface
|
||||||
{
|
{
|
||||||
public function __construct(string $dsn, string $username=null, string $password=null, array $options=null);
|
public function __construct(string $dsn=null, string $username=null, string $password=null, array $options=null);
|
||||||
|
public function &pdo(\PDO &$pdo=null) : ?\PDO;
|
||||||
|
|
||||||
public function createTable(string $table, array $virtualColumns) : bool;
|
public function createTable(string $table, array $virtualColumns) : bool;
|
||||||
public function select(string $table, Search $search, array $params);
|
public function select(string $table, Search $search, array $params);
|
||||||
|
|
|
@ -16,19 +16,22 @@ use Destructr\Factory;
|
||||||
*/
|
*/
|
||||||
class SQLiteDriver extends AbstractLegacyDriver
|
class SQLiteDriver extends AbstractLegacyDriver
|
||||||
{
|
{
|
||||||
public function __construct(string $dsn, string $username=null, string $password=null, array $options=null)
|
public function &pdo(\PDO &$pdo=null) : ?\PDO
|
||||||
{
|
{
|
||||||
parent::__construct($dsn, $username, $password, $options);
|
if ($pdo) {
|
||||||
/*
|
$this->pdo = $pdo;
|
||||||
What we're doing here is adding a custom function to SQLite so that it
|
/*
|
||||||
can extract JSON values. It's not fast, but it does let us use JSON
|
What we're doing here is adding a custom function to SQLite so that it
|
||||||
fairly seamlessly.
|
can extract JSON values. It's not fast, but it does let us use JSON
|
||||||
*/
|
fairly seamlessly.
|
||||||
$this->pdo->sqliteCreateFunction(
|
*/
|
||||||
'DESTRUCTR_JSON_EXTRACT',
|
$this->pdo->sqliteCreateFunction(
|
||||||
'\\Destructr\\LegacyDrivers\\SQLiteDriver::JSON_EXTRACT',
|
'DESTRUCTR_JSON_EXTRACT',
|
||||||
2
|
'\\Destructr\\LegacyDrivers\\SQLiteDriver::JSON_EXTRACT',
|
||||||
);
|
2
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return $this->pdo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function JSON_EXTRACT($json, $path)
|
public static function JSON_EXTRACT($json, $path)
|
||||||
|
|
Loading…
Reference in a new issue