Simplify Database into a pure PDO wrapper with a create() factory
Constructor now only accepts a pre-built PDO; connection setup and attribute configuration move into a new static create() method. Drops the unused Statement/Row helper classes and the broken sql_mode init command option. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
76e69d6054
commit
d9bc65418f
3 files changed
+23
-59
No files matched your search
+23
-18
@@ -12,31 +12,36 @@ class Database
|
||||
*/
|
||||
private $driver;
|
||||
|
||||
public function __construct(
|
||||
/**
|
||||
* @param PDO $pdo
|
||||
*/
|
||||
public function __construct($pdo)
|
||||
{
|
||||
$this->driver = $pdo;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $dsn
|
||||
* @param string|null $username
|
||||
* @param string|null $password
|
||||
* @param array|null $options
|
||||
* @return self
|
||||
*/
|
||||
public static function create(
|
||||
$dsn,
|
||||
$username = null,
|
||||
$password = null,
|
||||
$options = null
|
||||
) {
|
||||
if ($dsn instanceof PDO) {
|
||||
$this->driver = $dsn;
|
||||
} else {
|
||||
$this->driver = new PDO(
|
||||
$dsn,
|
||||
$username,
|
||||
$password,
|
||||
array_merge([
|
||||
PDO::MYSQL_ATTR_INIT_COMMAND, 'SET sql_mode="ANSI,TRADITIONAL"'
|
||||
], (array) $options)
|
||||
);
|
||||
}
|
||||
$pdo = new PDO($dsn, $username, $password, (array) $options);
|
||||
|
||||
$this->driver->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$this->driver->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
|
||||
$this->driver->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_CLASS);
|
||||
$this->driver->setAttribute(PDO::ATTR_STATEMENT_CLASS, [Statement::class]);
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
|
||||
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
|
||||
|
||||
$this->driver->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
|
||||
$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
|
||||
|
||||
return new self($pdo);
|
||||
}
|
||||
|
||||
public function driver()
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Nightmare\Database;
|
||||
|
||||
use ArrayObject;
|
||||
|
||||
class Row extends ArrayObject
|
||||
{
|
||||
/**
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($name)
|
||||
{
|
||||
return $this[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $val
|
||||
* @return void
|
||||
*/
|
||||
public function __set($name, $val)
|
||||
{
|
||||
$this[$name] = $val;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Nightmare\Database;
|
||||
|
||||
use PDOStatement;
|
||||
use PDO;
|
||||
|
||||
class Statement extends PDOStatement
|
||||
{
|
||||
protected function __construct()
|
||||
{
|
||||
$this->setFetchMode(PDO::FETCH_CLASS, Row::class);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user