Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 50f2db7a2d | |||
| a538c39837 | |||
| 8c98ce8c7f | |||
| f8718fa4de | |||
| ffae51bccf | |||
| 714df3dda4 | |||
| f3a7789184 | |||
| e186ebd54c | |||
| 4d7c9fdd17 | |||
| 2ea8e8276b | |||
| d2bdb4355f | |||
| f7e88e6c18 | |||
| 67b961ce6e | |||
| 81d0e09c65 | |||
| 022f3936c4 | |||
| cf468b8a24 |
@@ -237,7 +237,6 @@ class ActiveQuery extends Component implements ISqlBuilder
|
|||||||
*/
|
*/
|
||||||
public function count(): int
|
public function count(): int
|
||||||
{
|
{
|
||||||
$this->select = ['COUNT(*)'];
|
|
||||||
$data = $this->execute($this->builder->count())->one();
|
$data = $this->execute($this->builder->count())->one();
|
||||||
if ($data && is_array($data)) {
|
if ($data && is_array($data)) {
|
||||||
return (int)array_shift($data);
|
return (int)array_shift($data);
|
||||||
|
|||||||
+10
-9
@@ -13,12 +13,10 @@ defined('SAVE_FAIL') or define('SAVE_FAIL', 3227);
|
|||||||
defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a new model, but the data cannot be empty.');
|
defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a new model, but the data cannot be empty.');
|
||||||
|
|
||||||
|
|
||||||
use Annotation\Inject;
|
|
||||||
use ArrayAccess;
|
use ArrayAccess;
|
||||||
use Closure;
|
use Closure;
|
||||||
use Database\ActiveQuery;
|
use Database\ActiveQuery;
|
||||||
use Database\Connection;
|
use Database\Connection;
|
||||||
use Database\HasCount;
|
|
||||||
use Database\HasMany;
|
use Database\HasMany;
|
||||||
use Database\HasOne;
|
use Database\HasOne;
|
||||||
use Database\ModelInterface;
|
use Database\ModelInterface;
|
||||||
@@ -98,12 +96,6 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
|
|||||||
protected string $connection = 'db';
|
protected string $connection = 'db';
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected array $rules = [];
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
@@ -119,6 +111,15 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
@@ -611,7 +612,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
|
|||||||
if (!is_null($data)) {
|
if (!is_null($data)) {
|
||||||
$this->_attributes = merge($this->_attributes, $data);
|
$this->_attributes = merge($this->_attributes, $data);
|
||||||
}
|
}
|
||||||
if (!$this->validator($this->rules) || !$this->beforeSave($this)) {
|
if (!$this->validator($this->rules()) || !$this->beforeSave($this)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
[$change, $condition, $fields] = $this->separation();
|
[$change, $condition, $fields] = $this->separation();
|
||||||
|
|||||||
@@ -20,9 +20,8 @@ class HashCondition extends Condition
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
foreach ($this->value as $key => $value) {
|
foreach ($this->value as $key => $value) {
|
||||||
if ($value === null) {
|
if (is_null($value)) continue;
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$array[] = sprintf("%s = '%s'", $key, addslashes($value));
|
$array[] = sprintf("%s = '%s'", $key, addslashes($value));
|
||||||
}
|
}
|
||||||
return implode(' AND ', $array);
|
return implode(' AND ', $array);
|
||||||
|
|||||||
+12
-1
@@ -108,6 +108,17 @@ class Connection extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $config
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function configure($config): static
|
||||||
|
{
|
||||||
|
Kiri::configure($this, $config);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
@@ -141,7 +152,7 @@ class Connection extends Component
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
* @throws \Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function getSchema(): Schema
|
public function getSchema(): Schema
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,13 +6,13 @@ namespace Database;
|
|||||||
|
|
||||||
use Annotation\Inject;
|
use Annotation\Inject;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Server\Events\OnWorkerStart;
|
|
||||||
use Kiri\Abstracts\Config;
|
use Kiri\Abstracts\Config;
|
||||||
use Kiri\Abstracts\Providers;
|
use Kiri\Abstracts\Providers;
|
||||||
use Kiri\Application;
|
use Kiri\Application;
|
||||||
use Kiri\Events\EventProvider;
|
use Kiri\Events\EventProvider;
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
use Kiri\Kiri;
|
use Kiri\Kiri;
|
||||||
|
use Server\Events\OnWorkerStart;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class DatabasesProviders
|
* Class DatabasesProviders
|
||||||
@@ -53,11 +53,9 @@ class DatabasesProviders extends Providers
|
|||||||
*/
|
*/
|
||||||
public function get($name): Connection
|
public function get($name): Connection
|
||||||
{
|
{
|
||||||
$application = Kiri::app();
|
$config = $this->_settings($this->getConfig($name));
|
||||||
if (!$application->has('databases.' . $name)) {
|
|
||||||
$application->set('databases.' . $name, $this->_settings($this->getConfig($name)));
|
return Kiri::getDi()->get(Connection::class)->configure($config);
|
||||||
}
|
|
||||||
return $application->get('databases.' . $name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -71,11 +69,10 @@ class DatabasesProviders extends Providers
|
|||||||
if (empty($databases)) {
|
if (empty($databases)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$application = Kiri::app();
|
$connection = Kiri::getDi()->get(Connection::class);
|
||||||
foreach ($databases as $name => $database) {
|
foreach ($databases as $database) {
|
||||||
/** @var Connection $connection */
|
/** @var Connection $connection */
|
||||||
$application->set('databases.' . $name, $this->_settings($database));
|
$connection->configure($database)->fill();
|
||||||
$application->get('databases.' . $name)->fill();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-4
@@ -334,7 +334,7 @@ class Model extends Base\Model
|
|||||||
*/
|
*/
|
||||||
public function hasOne(string $modelName, $foreignKey, $localKey): HasOne|ActiveQuery
|
public function hasOne(string $modelName, $foreignKey, $localKey): HasOne|ActiveQuery
|
||||||
{
|
{
|
||||||
if (($value = $this->getAttribute($localKey)) === null) {
|
if (($value = $this->{$localKey}) === null) {
|
||||||
throw new Exception("Need join table primary key.");
|
throw new Exception("Need join table primary key.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -353,7 +353,7 @@ class Model extends Base\Model
|
|||||||
*/
|
*/
|
||||||
public function hasCount($modelName, $foreignKey, $localKey): ActiveQuery|HasCount
|
public function hasCount($modelName, $foreignKey, $localKey): ActiveQuery|HasCount
|
||||||
{
|
{
|
||||||
if (($value = $this->getAttribute($localKey)) === null) {
|
if (($value = $this->{$localKey}) === null) {
|
||||||
throw new Exception("Need join table primary key.");
|
throw new Exception("Need join table primary key.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -372,7 +372,7 @@ class Model extends Base\Model
|
|||||||
*/
|
*/
|
||||||
public function hasMany($modelName, $foreignKey, $localKey): ActiveQuery|HasMany
|
public function hasMany($modelName, $foreignKey, $localKey): ActiveQuery|HasMany
|
||||||
{
|
{
|
||||||
if (($value = $this->getAttribute($localKey)) === null) {
|
if (($value = $this->{$localKey}) === null) {
|
||||||
throw new Exception("Need join table primary key.");
|
throw new Exception("Need join table primary key.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -390,7 +390,7 @@ class Model extends Base\Model
|
|||||||
*/
|
*/
|
||||||
public function hasIn($modelName, $foreignKey, $localKey): ActiveQuery|HasMany
|
public function hasIn($modelName, $foreignKey, $localKey): ActiveQuery|HasMany
|
||||||
{
|
{
|
||||||
if (($value = $this->getAttribute($localKey)) === null) {
|
if (($value = $this->{$localKey}) === null) {
|
||||||
throw new Exception("Need join table primary key.");
|
throw new Exception("Need join table primary key.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -316,7 +316,7 @@ class Columns extends Component
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $column
|
* @param array $column
|
||||||
* @param $table
|
* @param $table
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
|
|||||||
+7
-5
@@ -240,7 +240,7 @@ class SqlBuilder extends Component
|
|||||||
if (empty($this->query->from) && !empty($this->query->modelClass)) {
|
if (empty($this->query->from) && !empty($this->query->modelClass)) {
|
||||||
$this->query->from($this->query->getTable());
|
$this->query->from($this->query->getTable());
|
||||||
}
|
}
|
||||||
return $this->_prefix();
|
return $this->_prefix(false, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -256,14 +256,15 @@ class SqlBuilder extends Component
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param bool $hasOrder
|
* @param bool $hasOrder
|
||||||
|
* @param bool $isCount
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function _prefix(bool $hasOrder = false): string
|
private function _prefix(bool $hasOrder = false, bool $isCount = false): string
|
||||||
{
|
{
|
||||||
$select = '';
|
$select = '';
|
||||||
if (!empty($this->query->from)) {
|
if (!empty($this->query->from)) {
|
||||||
$select = $this->_selectPrefix();
|
$select = $this->_selectPrefix($isCount);
|
||||||
}
|
}
|
||||||
$select = $this->_wherePrefix($select);
|
$select = $this->_wherePrefix($select);
|
||||||
if (!empty($this->query->attributes) && is_array($this->query->attributes)) {
|
if (!empty($this->query->attributes) && is_array($this->query->attributes)) {
|
||||||
@@ -302,12 +303,13 @@ class SqlBuilder extends Component
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param bool $isCount
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function _selectPrefix(): string
|
private function _selectPrefix(bool $isCount): string
|
||||||
{
|
{
|
||||||
$select = $this->builderSelect($this->query->select) . ' FROM ' . $this->tableName();
|
$select = $this->builderSelect($this->query->select, $isCount) . ' FROM ' . $this->tableName();
|
||||||
if (!empty($this->query->alias)) {
|
if (!empty($this->query->alias)) {
|
||||||
$select .= $this->builderAlias($this->query->alias);
|
$select .= $this->builderAlias($this->query->alias);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,10 +61,14 @@ trait Builder
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param null $select
|
* @param null $select
|
||||||
|
* @param bool $isCount
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
#[Pure] private function builderSelect($select = NULL): string
|
#[Pure] private function builderSelect($select = NULL, bool $isCount = false): string
|
||||||
{
|
{
|
||||||
|
if ($isCount) {
|
||||||
|
return "SELECT COUNT(*)";
|
||||||
|
}
|
||||||
if (empty($select)) {
|
if (empty($select)) {
|
||||||
return "SELECT *";
|
return "SELECT *";
|
||||||
}
|
}
|
||||||
@@ -129,6 +133,8 @@ trait Builder
|
|||||||
if (empty($where)) return '';
|
if (empty($where)) return '';
|
||||||
if (is_string($where)) return $where;
|
if (is_string($where)) return $where;
|
||||||
foreach ($where as $key => $value) {
|
foreach ($where as $key => $value) {
|
||||||
|
if (is_null($value)) continue;
|
||||||
|
|
||||||
$_value = $this->resolveCondition($key, $value, $_tmp);
|
$_value = $this->resolveCondition($key, $value, $_tmp);
|
||||||
|
|
||||||
if (empty($_value)) continue;
|
if (empty($_value)) continue;
|
||||||
@@ -210,6 +216,8 @@ trait Builder
|
|||||||
{
|
{
|
||||||
$_array = [];
|
$_array = [];
|
||||||
foreach ($condition as $key => $value) {
|
foreach ($condition as $key => $value) {
|
||||||
|
if (is_null($value)) continue;
|
||||||
|
|
||||||
$value = is_numeric($value) ? $value : '\'' . $value . '\'';
|
$value = is_numeric($value) ? $value : '\'' . $value . '\'';
|
||||||
if (!is_numeric($key)) {
|
if (!is_numeric($key)) {
|
||||||
$_array[] = sprintf('%s = %s', $key, $value);
|
$_array[] = sprintf('%s = %s', $key, $value);
|
||||||
|
|||||||
@@ -923,6 +923,7 @@ trait QueryTrait
|
|||||||
private function addArray(array $array): static
|
private function addArray(array $array): static
|
||||||
{
|
{
|
||||||
foreach ($array as $key => $value) {
|
foreach ($array as $key => $value) {
|
||||||
|
if (is_null($value)) continue;
|
||||||
if (is_numeric($key)) {
|
if (is_numeric($key)) {
|
||||||
[$column, $opera, $value] = $this->opera(...$value);
|
[$column, $opera, $value] = $this->opera(...$value);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user