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