Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| be3fc004d5 | |||
| bbe0631f5b | |||
| bf5d988ba4 | |||
| 4fc9b42540 | |||
| e5453807f2 | |||
| 5a6f6da70a |
+43
-1
@@ -73,6 +73,48 @@ class ActiveQuery extends QueryTrait implements ISqlBuilder
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $column
|
||||||
|
* @param int|float $amount
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function increment(string $column, int|float $amount = 1): bool
|
||||||
|
{
|
||||||
|
return (bool)$this->buildCommand($this->builder->mathematics([$column => $amount]))->exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $attributes
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function increments(array $attributes): bool
|
||||||
|
{
|
||||||
|
return (bool)$this->buildCommand($this->builder->mathematics($attributes))->exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $column
|
||||||
|
* @param int|float $amount
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function decrement(string $column, int|float $amount = 1): bool
|
||||||
|
{
|
||||||
|
return (bool)$this->buildCommand($this->builder->mathematics([$column => $amount], '-'))->exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $attributes
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function decrements(array $attributes): bool
|
||||||
|
{
|
||||||
|
return (bool)$this->buildCommand($this->builder->mathematics($attributes, '-'))->exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
@@ -95,7 +137,7 @@ class ActiveQuery extends QueryTrait implements ISqlBuilder
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (Context::inCoroutine()) {
|
if (Context::inCoroutine()) {
|
||||||
Coroutine::create(fn() => $closure($data));
|
Coroutine::create(fn () => $closure($data));
|
||||||
} else {
|
} else {
|
||||||
call_user_func($closure, $data);
|
call_user_func($closure, $data);
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-6
@@ -223,8 +223,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
|||||||
*/
|
*/
|
||||||
public function getPrimaryValue(): ?int
|
public function getPrimaryValue(): ?int
|
||||||
{
|
{
|
||||||
if ($this->hasPrimary()) {
|
if ($this->hasPrimary() && isset($this->_oldAttributes[$this->getPrimary()])) {
|
||||||
return (int)$this->_oldAttributes[$this->getPrimary()] ?? null;
|
return (int)$this->_oldAttributes[$this->getPrimary()];
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -242,7 +242,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
|||||||
$query = new ActiveQuery($model);
|
$query = new ActiveQuery($model);
|
||||||
$query->from($model->getTable())->alias('t1');
|
$query->from($model->getTable())->alias('t1');
|
||||||
if (is_numeric($param)) {
|
if (is_numeric($param)) {
|
||||||
$query->where([$model->getPrimary() => $param]);
|
$query->where([$model->getPrimary() => +$param]);
|
||||||
} else if (is_array($param)) {
|
} else if (is_array($param)) {
|
||||||
$query->where($param);
|
$query->where($param);
|
||||||
} else {
|
} else {
|
||||||
@@ -442,10 +442,13 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
|||||||
if ($lastId === false) {
|
if ($lastId === false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if ($this->hasPrimary()) {
|
if (!$this->hasPrimary()) {
|
||||||
$this->_attributes[$this->getPrimary()] = $lastId;
|
return $this->refresh()->afterSave($this->_attributes, []);
|
||||||
}
|
}
|
||||||
return $this;
|
|
||||||
|
$this->_attributes[$this->getPrimary()] = $lastId;
|
||||||
|
|
||||||
|
return $this->refresh()->afterSave($this->_attributes, [$this->getPrimary() => $lastId]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -655,6 +658,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
|||||||
public function refresh(): static
|
public function refresh(): static
|
||||||
{
|
{
|
||||||
$this->_oldAttributes = $this->_attributes;
|
$this->_oldAttributes = $this->_attributes;
|
||||||
|
$this->isNewExample = false;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-2
@@ -109,6 +109,7 @@ class Command extends Component
|
|||||||
{
|
{
|
||||||
$client = $this->connection->getConnection();
|
$client = $this->connection->getConnection();
|
||||||
try {
|
try {
|
||||||
|
$startTime = microtime(true);
|
||||||
if (($prepare = $client->prepare($this->sql)) === false) {
|
if (($prepare = $client->prepare($this->sql)) === false) {
|
||||||
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $client->errorInfo()[2]);
|
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $client->errorInfo()[2]);
|
||||||
}
|
}
|
||||||
@@ -118,7 +119,7 @@ class Command extends Component
|
|||||||
$result = $method == 'rowCount' ? $prepare->rowCount() : $prepare->{$method}(PDO::FETCH_ASSOC);
|
$result = $method == 'rowCount' ? $prepare->rowCount() : $prepare->{$method}(PDO::FETCH_ASSOC);
|
||||||
$prepare->closeCursor();
|
$prepare->closeCursor();
|
||||||
|
|
||||||
$this->connection->println($this->sql, $this->params);
|
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
} catch (Throwable $throwable) {
|
} catch (Throwable $throwable) {
|
||||||
@@ -151,6 +152,7 @@ class Command extends Component
|
|||||||
{
|
{
|
||||||
$client = $this->connection->getConnection();
|
$client = $this->connection->getConnection();
|
||||||
try {
|
try {
|
||||||
|
$startTime = microtime(true);
|
||||||
if (($prepare = $client->prepare($this->sql)) === false) {
|
if (($prepare = $client->prepare($this->sql)) === false) {
|
||||||
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
|
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
|
||||||
}
|
}
|
||||||
@@ -161,7 +163,7 @@ class Command extends Component
|
|||||||
|
|
||||||
$result = $client->lastInsertId();
|
$result = $client->lastInsertId();
|
||||||
|
|
||||||
$this->connection->println($this->sql, $this->params);
|
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
|
||||||
|
|
||||||
return $result == 0 ? $prepare->rowCount() : (int)$result;
|
return $result == 0 ? $prepare->rowCount() : (int)$result;
|
||||||
} catch (Throwable $throwable) {
|
} catch (Throwable $throwable) {
|
||||||
|
|||||||
+12
-10
@@ -79,14 +79,16 @@ class Connection extends Component
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param float $startTime
|
||||||
|
* @param float $endTime
|
||||||
* @param string $sql
|
* @param string $sql
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function println(string $sql, array $params = []): void
|
public function println(float $startTime, float $endTime, string $sql, array $params = []): void
|
||||||
{
|
{
|
||||||
if (is_callable($this->_println)) {
|
if (is_callable($this->_println)) {
|
||||||
call_user_func($this->_println, $sql, $params);
|
call_user_func($this->_println, $startTime, $endTime, $sql, $params);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,7 +115,7 @@ class Connection extends Component
|
|||||||
*/
|
*/
|
||||||
public function tick(): void
|
public function tick(): void
|
||||||
{
|
{
|
||||||
$this->timerId = Timer::tick($this->tick_time, fn() => $this->checkClientHealth($this->pool()));
|
$this->timerId = Timer::tick($this->tick_time, fn () => $this->checkClientHealth($this->pool()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -349,13 +351,13 @@ class Connection extends Component
|
|||||||
{
|
{
|
||||||
$pdo = new PDO($this->database, $this->cds, $this->username, $this->password, [
|
$pdo = new PDO($this->database, $this->cds, $this->username, $this->password, [
|
||||||
// $pdo = new \PDO('mysql:dbname=' . $this->database . ';host=' . $this->cds, $this->username, $this->password, [
|
// $pdo = new \PDO('mysql:dbname=' . $this->database . ';host=' . $this->cds, $this->username, $this->password, [
|
||||||
\PDO::ATTR_CASE => \PDO::CASE_NATURAL,
|
\PDO::ATTR_CASE => \PDO::CASE_NATURAL,
|
||||||
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
|
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
|
||||||
\PDO::ATTR_ORACLE_NULLS => \PDO::NULL_NATURAL,
|
\PDO::ATTR_ORACLE_NULLS => \PDO::NULL_NATURAL,
|
||||||
\PDO::ATTR_STRINGIFY_FETCHES => false,
|
\PDO::ATTR_STRINGIFY_FETCHES => false,
|
||||||
\PDO::ATTR_EMULATE_PREPARES => true,
|
\PDO::ATTR_EMULATE_PREPARES => true,
|
||||||
\PDO::ATTR_TIMEOUT => $this->timeout,
|
\PDO::ATTR_TIMEOUT => $this->timeout,
|
||||||
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->charset
|
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->charset
|
||||||
]);
|
]);
|
||||||
foreach ($this->attributes as $key => $attribute) {
|
foreach ($this->attributes as $key => $attribute) {
|
||||||
$pdo->setAttribute($key, $attribute);
|
$pdo->setAttribute($key, $attribute);
|
||||||
|
|||||||
Reference in New Issue
Block a user