Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| adb95af124 | |||
| b554b53cfd | |||
| 0e0caaf8a9 |
+25
-5
@@ -11,7 +11,6 @@ namespace Database;
|
|||||||
|
|
||||||
use Database\Traits\QueryTrait;
|
use Database\Traits\QueryTrait;
|
||||||
use Exception;
|
use Exception;
|
||||||
use HttpServer\Http\Context;
|
|
||||||
use Snowflake\Event;
|
use Snowflake\Event;
|
||||||
use Snowflake\Snowflake;
|
use Snowflake\Snowflake;
|
||||||
|
|
||||||
@@ -93,6 +92,27 @@ class Db implements ISqlBuilder
|
|||||||
return 'ANY_VALUE(' . $column . ') as ' . $alias;
|
return 'ANY_VALUE(' . $column . ') as ' . $alias;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $column
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function increment(string $column): string
|
||||||
|
{
|
||||||
|
return '+ ' . $column;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $column
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function decrement(string $column): string
|
||||||
|
{
|
||||||
|
return '- ' . $column;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Connection|null $db
|
* @param Connection|null $db
|
||||||
* @return mixed
|
* @return mixed
|
||||||
@@ -167,19 +187,19 @@ class Db implements ISqlBuilder
|
|||||||
*/
|
*/
|
||||||
public static function findAllBySql(string $sql, array $attributes = [], Connection $db = NULL): int|bool|array|string|null
|
public static function findAllBySql(string $sql, array $attributes = [], Connection $db = NULL): int|bool|array|string|null
|
||||||
{
|
{
|
||||||
return $db->createCommand($sql, $attributes)->all();
|
return $db->createCommand($sql, $db?->database, $attributes)->all();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $sql
|
* @param string $sql
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
* @param Connection|NULL $db
|
* @param Connection|NULL $db
|
||||||
* @return array|mixed
|
* @return string|array|bool|int|null
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function findBySql(string $sql, array $attributes = [], Connection $db = NULL): mixed
|
public static function findBySql(string $sql, array $attributes = [], Connection $db = NULL): string|array|bool|int|null
|
||||||
{
|
{
|
||||||
return $db->createCommand($sql, $attributes)->one();
|
return $db->createCommand($sql, $db?->database, $attributes)->one();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+16
-17
@@ -52,19 +52,8 @@ class SqlBuilder extends Component
|
|||||||
public function update(array $attributes): bool|array
|
public function update(array $attributes): bool|array
|
||||||
{
|
{
|
||||||
[$string, $array] = $this->builderParams($attributes);
|
[$string, $array] = $this->builderParams($attributes);
|
||||||
if (empty($string) || empty($array)) {
|
|
||||||
return $this->addError('None data update.');
|
|
||||||
}
|
|
||||||
|
|
||||||
$condition = $this->conditionToString();
|
return $this->__updateBuilder($string, $array);
|
||||||
if (!empty($condition)) {
|
|
||||||
$condition = ' WHERE ' . $condition;
|
|
||||||
}
|
|
||||||
|
|
||||||
$update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $condition;
|
|
||||||
$update .= $this->builderLimit($this->query);
|
|
||||||
|
|
||||||
return [$update, $array];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -76,12 +65,22 @@ class SqlBuilder extends Component
|
|||||||
*/
|
*/
|
||||||
public function mathematics(array $attributes, string $opera = '+'): bool|array
|
public function mathematics(array $attributes, string $opera = '+'): bool|array
|
||||||
{
|
{
|
||||||
$string = $array = [];
|
$string = [];
|
||||||
|
|
||||||
foreach ($attributes as $attribute => $value) {
|
foreach ($attributes as $attribute => $value) {
|
||||||
$string[] = $attribute . '=' . $attribute . $opera . $value;
|
$string[] = $attribute . '=' . $attribute . $opera . $value;
|
||||||
}
|
}
|
||||||
|
return $this->__updateBuilder($string, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $string
|
||||||
|
* @param array $params
|
||||||
|
* @return array|bool
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function __updateBuilder(array $string, array $params): array|bool
|
||||||
|
{
|
||||||
if (empty($string)) {
|
if (empty($string)) {
|
||||||
return $this->addError('None data update.');
|
return $this->addError('None data update.');
|
||||||
}
|
}
|
||||||
@@ -92,9 +91,9 @@ class SqlBuilder extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
$update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $condition;
|
$update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $condition;
|
||||||
$update .= $this->builderLimit($this->query);
|
$update .= $this->builderLimit($this->query, false);
|
||||||
|
|
||||||
return [$update, []];
|
return [$update, $params];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -185,7 +184,7 @@ class SqlBuilder extends Component
|
|||||||
str_starts_with($value, '+ ') ||
|
str_starts_with($value, '+ ') ||
|
||||||
str_starts_with($value, '- ')
|
str_starts_with($value, '- ')
|
||||||
) {
|
) {
|
||||||
$keys[] = $key . '=' . $key . $value;
|
$keys[] = $key . '=' . $key . ' ' . $value;
|
||||||
} else {
|
} else {
|
||||||
$params[$key . $order] = $value;
|
$params[$key . $order] = $value;
|
||||||
$keys[] = $key . '=:' . $key . $order;
|
$keys[] = $key . '=:' . $key . $order;
|
||||||
|
|||||||
@@ -104,14 +104,15 @@ trait Builder
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ActiveQuery|Query $query
|
* @param ActiveQuery|Query $query
|
||||||
|
* @param bool $hasLimit
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
#[Pure] private function builderLimit(ActiveQuery|Query $query): string
|
#[Pure] private function builderLimit(ActiveQuery|Query $query, bool $hasLimit = true): string
|
||||||
{
|
{
|
||||||
if (!is_numeric($query->limit) || $query->limit < 1) {
|
if (!is_numeric($query->limit) || $query->limit < 1) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
if ($query->offset !== null) {
|
if ($query->offset !== null && $hasLimit) {
|
||||||
return ' LIMIT ' . $query->offset . ',' . $query->limit;
|
return ' LIMIT ' . $query->offset . ',' . $query->limit;
|
||||||
}
|
}
|
||||||
return ' LIMIT ' . $query->limit;
|
return ' LIMIT ' . $query->limit;
|
||||||
|
|||||||
Reference in New Issue
Block a user