eee
This commit is contained in:
+23
-8
@@ -30,6 +30,14 @@ use ReturnTypeWillChange;
|
||||
use ReflectionException;
|
||||
use validator\Validator;
|
||||
|
||||
|
||||
enum Driver
|
||||
{
|
||||
case Mysql;
|
||||
case Pgsql;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Class BOrm
|
||||
*
|
||||
@@ -139,7 +147,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
*/
|
||||
public function clean(): void
|
||||
{
|
||||
$this->_attributes = [];
|
||||
$this->_attributes = [];
|
||||
$this->_oldAttributes = [];
|
||||
}
|
||||
|
||||
@@ -449,7 +457,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
*/
|
||||
private function insert(): bool|static
|
||||
{
|
||||
$sql = SqlBuilder::builder($query = static::query())->insert($this->_attributes);
|
||||
$sql = SqlBuilder::builder($query = static::query())->insert($this->_attributes);
|
||||
$lastId = $this->getConnection()->createCommand($sql, $query->params)->exec();
|
||||
if ($lastId === FALSE) {
|
||||
return FALSE;
|
||||
@@ -512,7 +520,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
protected function arrayIntersect(array $params): array
|
||||
{
|
||||
$condition = [];
|
||||
$oldPrams = [];
|
||||
$oldPrams = [];
|
||||
foreach ($this->_oldAttributes as $key => $attribute) {
|
||||
if (!array_key_exists($key, $params) || $params[$key] == $attribute) {
|
||||
$condition[$key] = $attribute;
|
||||
@@ -547,7 +555,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
*/
|
||||
public function populates(array $value): static
|
||||
{
|
||||
$this->_attributes = $value;
|
||||
$this->_attributes = $value;
|
||||
$this->_oldAttributes = $value;
|
||||
$this->setIsNowExample();
|
||||
return $this;
|
||||
@@ -635,7 +643,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
*/
|
||||
public function getTable(): string
|
||||
{
|
||||
$connection = $this->getConnection();
|
||||
$connection = $this->getConnection();
|
||||
$tablePrefix = $connection->tablePrefix;
|
||||
if (empty($this->table)) {
|
||||
throw new Exception('You need add static method `tableName` and return table name.');
|
||||
@@ -644,6 +652,13 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
if (!empty($tablePrefix) && !str_starts_with($table, $tablePrefix)) {
|
||||
$table = $tablePrefix . $table;
|
||||
}
|
||||
|
||||
$driver = strtolower($connection->driver ?? 'mysql');
|
||||
if (in_array($driver, ['pgsql', 'postgresql'])) {
|
||||
// PostgreSQL 使用双引号,并且 schema.table 格式
|
||||
return '"' . $connection->database . '"."' . $table . '"';
|
||||
}
|
||||
// MySQL 使用反引号
|
||||
return '`' . $connection->database . '`.' . $table;
|
||||
}
|
||||
|
||||
@@ -678,7 +693,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
public function refresh(): static
|
||||
{
|
||||
$this->_oldAttributes = $this->_attributes;
|
||||
$this->isNewExample = FALSE;
|
||||
$this->isNewExample = FALSE;
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -853,8 +868,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
*/
|
||||
public static function populate(array $data): static
|
||||
{
|
||||
$model = new static();
|
||||
$model->_attributes = $data;
|
||||
$model = new static();
|
||||
$model->_attributes = $data;
|
||||
$model->_oldAttributes = $data;
|
||||
$model->setIsNowExample();
|
||||
return $model;
|
||||
|
||||
+16
-1
@@ -5,6 +5,7 @@ namespace Database\Base;
|
||||
class PDO extends \PDO
|
||||
{
|
||||
|
||||
readonly public string $driver;
|
||||
|
||||
/**
|
||||
* @param string $database
|
||||
@@ -12,6 +13,7 @@ class PDO extends \PDO
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
* @param array $options
|
||||
* @param string $driver
|
||||
*/
|
||||
public function __construct(
|
||||
readonly public string $database,
|
||||
@@ -19,10 +21,23 @@ class PDO extends \PDO
|
||||
readonly public string $username,
|
||||
readonly public string $password,
|
||||
readonly public array $options,
|
||||
string $driver = 'mysql',
|
||||
)
|
||||
{
|
||||
parent::__construct('mysql:dbname=' . $this->database . ';host=' . $this->host, $this->username, $this->password, $this->options);
|
||||
$this->driver = strtolower($driver);
|
||||
$dsn = $this->buildDsn();
|
||||
parent::__construct($dsn, $this->username, $this->password, $this->options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
private function buildDsn(): string
|
||||
{
|
||||
return match ($this->driver) {
|
||||
'pgsql', 'postgresql' => 'pgsql:host=' . $this->host . ';dbname=' . $this->database,
|
||||
default => 'mysql:dbname=' . $this->database . ';host=' . $this->host,
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user