This commit is contained in:
xl
2023-11-14 15:01:30 +08:00
parent a1238613f9
commit 53dbf7a9b2
12 changed files with 174 additions and 248 deletions
+3 -15
View File
@@ -51,27 +51,17 @@ class ActiveQuery extends Component implements ISqlBuilder
public function __construct($model) public function __construct($model)
{ {
$this->modelClass = $model; $this->modelClass = $model;
$this->builder = SqlBuilder::builder($this); $this->builder = SqlBuilder::builder($this);
parent::__construct(); parent::__construct();
} }
/** /**
* 清除不完整数据 * @param string $key
*/ * @param mixed $value
public function clear(): void
{
$this->db = NULL;
$this->useCache = FALSE;
}
/**
* @param $key
* @param $value
* @return $this * @return $this
*/ */
public function addParam($key, $value): static public function addParam(string $key, mixed $value): static
{ {
$this->attributes[$key] = $value; $this->attributes[$key] = $value;
return $this; return $this;
@@ -89,9 +79,7 @@ class ActiveQuery extends Component implements ISqlBuilder
{ {
$page = max(1, $page); $page = max(1, $page);
$size = max(1, $size); $size = max(1, $size);
$offset = ($page - 1) * $size; $offset = ($page - 1) * $size;
$count = $this->count(); $count = $this->count();
$lists = $this->offset($offset)->limit($size)->get()->toArray(); $lists = $this->offset($offset)->limit($size)->get()->toArray();
return [ return [
+2 -9
View File
@@ -232,29 +232,23 @@ class BackupCommand extends Command
public function buildLine($key, $percent): string public function buildLine($key, $percent): string
{ {
$repeatTimes = 100; $repeatTimes = 100;
if ($percent > 100) { if ($percent > 100) $percent = 100;
$percent = 100;
}
if ($percent > 0) { if ($percent > 0) {
$hasColor = str_repeat('■', $percent); $hasColor = str_repeat('■', $percent);
} else { } else {
$hasColor = ''; $hasColor = '';
} }
if ($repeatTimes - $percent > 0) { if ($repeatTimes - $percent > 0) {
$noColor = str_repeat(' ', $repeatTimes - $percent); $noColor = str_repeat(' ', $repeatTimes - $percent);
} else { } else {
$noColor = ''; $noColor = '';
} }
$buffer = "[{$hasColor}{$noColor}]";
if ($percent !== 100) { if ($percent !== 100) {
$percentString = sprintf("[ %s %-6s]", $key, $percent . '%'); $percentString = sprintf("[ %s %-6s]", $key, $percent . '%');
} else { } else {
$percentString = sprintf("[ %s %-5s]", $key, 'OK');; $percentString = sprintf("[ %s %-5s]", $key, 'OK');;
} }
return $percentString . "[{$hasColor}{$noColor}]" . "\r";
return $percentString . $buffer . "\r";
} }
/** /**
@@ -290,7 +284,6 @@ class BackupCommand extends Command
$this->outputProgress(true); $this->outputProgress(true);
return; return;
} }
$this->outputProgress(true); $this->outputProgress(true);
usleep(50000); usleep(50000);
} }
+1
View File
@@ -1,6 +1,7 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace Database\Base; namespace Database\Base;
+2 -7
View File
@@ -432,10 +432,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
private function insert(): bool|static private function insert(): bool|static
{ {
[$sql, $param] = SqlBuilder::builder(static::query())->insert($this->_attributes); [$sql, $param] = SqlBuilder::builder(static::query())->insert($this->_attributes);
$connection = $this->getConnection(); $connection = $this->getConnection();
$dbConnection = $connection->createCommand($sql, $param); $dbConnection = $connection->createCommand($sql, $param);
$lastId = $dbConnection->save(); $lastId = $dbConnection->save();
if ($lastId === false) { if ($lastId === false) {
return false; return false;
@@ -595,7 +593,6 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
public function getTable(): string public function getTable(): string
{ {
$connection = static::getConnection(); $connection = static::getConnection();
$tablePrefix = $connection->tablePrefix; $tablePrefix = $connection->tablePrefix;
if (empty($this->table)) { if (empty($this->table)) {
throw new Exception('You need add static method `tableName` and return table name.'); throw new Exception('You need add static method `tableName` and return table name.');
@@ -762,8 +759,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
*/ */
#[ReturnTypeWillChange] public function offsetUnset(mixed $offset) #[ReturnTypeWillChange] public function offsetUnset(mixed $offset)
{ {
if (!isset($this->_attributes[$offset]) if (!isset($this->_attributes[$offset]) && !isset($this->_oldAttributes[$offset])) {
&& !isset($this->_oldAttributes[$offset])) {
return; return;
} }
unset($this->_attributes[$offset]); unset($this->_attributes[$offset]);
@@ -786,8 +782,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
*/ */
public function getColumns(): Columns public function getColumns(): Columns
{ {
return $this->getConnection()->getSchema()->getColumns() return $this->getConnection()->getSchema()->getColumns()->table($this->getTable());
->table($this->getTable());
} }
-12
View File
@@ -24,20 +24,8 @@ use Throwable;
class Command extends Component class Command extends Component
{ {
const RETRY_NAME = 'db:retry:count';
/**
*
*/
const DB_ERROR_MESSAGE = 'The system is busy, please try again later.';
/** @var Connection */
public Connection $connection; public Connection $connection;
/** @var ?string */
public ?string $sql = ''; public ?string $sql = '';
/** @var array */
public array $params = []; public array $params = [];
-5
View File
@@ -16,12 +16,7 @@ abstract class Condition extends Component
protected string $column = ''; protected string $column = '';
protected string $opera = '='; protected string $opera = '=';
/** @var array|mixed */
protected mixed $value; protected mixed $value;
const INT_TYPE = ['bit', 'bool', 'tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'float', 'double', 'decimal', 'timestamp'];
protected array $attributes = []; protected array $attributes = [];
abstract public function builder(); abstract public function builder();
+7 -19
View File
@@ -45,27 +45,19 @@ class Connection extends Component
public string $password = ''; public string $password = '';
public string $username = ''; public string $username = '';
public string $charset = 'utf-8'; public string $charset = 'utf-8';
public string $tablePrefix = ''; public string $tablePrefix = '';
public string $database = ''; public string $database = '';
public int $timeout = 30; public int $timeout = 30;
public int $waite_time = 3; public int $waite_time = 3;
public int $tick_time = 60; public int $tick_time = 60;
public int $idle_count = 3; public int $idle_count = 3;
public array $pool = ['max' => 10, 'min' => 1]; public array $pool = ['max' => 10, 'min' => 1];
private int $storey = 0; private int $storey = 0;
protected int $timerId = -1; protected int $timerId = -1;
const ERROR_MSG = 'Failed to rollback transaction: connection was exists.';
/** /**
* @var bool * @var bool
* enable database cache * enable database cache
@@ -182,10 +174,7 @@ class Connection extends Component
public function getSchema(): Schema public function getSchema(): Schema
{ {
if ($this->_schema === null) { if ($this->_schema === null) {
$this->_schema = Kiri::createObject([ $this->_schema = Kiri::createObject(['class' => Schema::class, 'db' => $this]);
'class' => Schema::class,
'db' => $this
]);
} }
return $this->_schema; return $this->_schema;
} }
@@ -279,7 +268,7 @@ class Connection extends Component
/** @var PDO $pdo */ /** @var PDO $pdo */
$pdo = Context::get($this->cds); $pdo = Context::get($this->cds);
if ($pdo === null) { if ($pdo === null) {
throw new Exception('Failed to rollback transaction: connection was exists.'); throw new Exception(self::ERROR_MSG);
} }
if ($this->inTransaction()) { if ($this->inTransaction()) {
$pdo->rollback(); $pdo->rollback();
@@ -298,7 +287,7 @@ class Connection extends Component
if ($this->storey == 0) { if ($this->storey == 0) {
$pdo = Context::get($this->cds); $pdo = Context::get($this->cds);
if ($pdo === null) { if ($pdo === null) {
throw new Exception('Failed to commit transaction: connection was exists.'); throw new Exception(self::ERROR_MSG);
} }
if ($this->inTransaction()) { if ($this->inTransaction()) {
$pdo->commit(); $pdo->commit();
@@ -334,8 +323,7 @@ class Connection extends Component
*/ */
public function createCommand($sql = null, array $attributes = []): Command public function createCommand($sql = null, array $attributes = []): Command
{ {
$command = new Command(['connection' => $this, 'sql' => $sql]); return (new Command(['connection' => $this, 'sql' => $sql]))->bindValues($attributes);
return $command->bindValues($attributes);
} }
-1
View File
@@ -11,7 +11,6 @@ namespace Database;
use Exception; use Exception;
use Kiri;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
+1 -21
View File
@@ -25,31 +25,11 @@ use Kiri\Core\Json;
class Columns extends Component class Columns extends Component
{ {
/**
* @var array
* field types
*/
private array $columns = []; private array $columns = [];
/**
* @var string
* tableName
*/
public string $table = ''; public string $table = '';
/**
* @var array
* field primary key
*/
private array $_primary = []; private array $_primary = [];
/**
* @var array
* by mysql field auto_increment
*/
private array $_auto_increment = []; private array $_auto_increment = [];
private array $_fields = []; private array $_fields = [];
+1 -2
View File
@@ -197,8 +197,7 @@ class SqlBuilder extends Component
if (is_null($value)) { if (is_null($value)) {
return $keys; return $keys;
} }
if (is_string($value) && (str_starts_with($value, '+ ') || if (is_string($value) && (str_starts_with($value, '+ ') || str_starts_with($value, '- '))) {
str_starts_with($value, '- '))) {
$keys[] = $key . '=' . $key . ' ' . $value; $keys[] = $key . '=' . $key . ' ' . $value;
} else { } else {
$this->query->pushParam($value); $this->query->pushParam($value);