Compare commits

...

3 Commits

Author SHA1 Message Date
as2252258 adb95af124 改名 2021-07-07 14:11:17 +08:00
as2252258 b554b53cfd 改名 2021-07-07 14:07:27 +08:00
as2252258 0e0caaf8a9 改名 2021-07-07 14:04:05 +08:00
3 changed files with 329 additions and 309 deletions
+310 -290
View File
@@ -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;
@@ -21,309 +20,330 @@ use Snowflake\Snowflake;
*/ */
class Db implements ISqlBuilder class Db implements ISqlBuilder
{ {
use QueryTrait; use QueryTrait;
private static bool $_inTransaction = false; private static bool $_inTransaction = false;
/** /**
* @return bool * @return bool
*/ */
public static function transactionsActive(): bool public static function transactionsActive(): bool
{ {
return static::$_inTransaction === true; return static::$_inTransaction === true;
} }
/** /**
* @throws Exception * @throws Exception
*/ */
public static function beginTransaction() public static function beginTransaction()
{ {
static::$_inTransaction = true; static::$_inTransaction = true;
} }
/** /**
* @throws Exception * @throws Exception
*/ */
public static function commit() public static function commit()
{ {
if (!static::transactionsActive()) { if (!static::transactionsActive()) {
return; return;
} }
Event::trigger(Connection::TRANSACTION_COMMIT); Event::trigger(Connection::TRANSACTION_COMMIT);
Event::offName(Connection::TRANSACTION_COMMIT); Event::offName(Connection::TRANSACTION_COMMIT);
static::$_inTransaction = false; static::$_inTransaction = false;
} }
/** /**
* @throws Exception * @throws Exception
*/ */
public static function rollback() public static function rollback()
{ {
if (!static::transactionsActive()) { if (!static::transactionsActive()) {
return; return;
} }
Event::trigger(Connection::TRANSACTION_ROLLBACK); Event::trigger(Connection::TRANSACTION_ROLLBACK);
Event::offName(Connection::TRANSACTION_ROLLBACK); Event::offName(Connection::TRANSACTION_ROLLBACK);
static::$_inTransaction = false; static::$_inTransaction = false;
} }
/** /**
* @param $table * @param $table
* *
* @return static * @return static
*/ */
public static function table($table): Db|static public static function table($table): Db|static
{ {
$db = new Db(); $db = new Db();
$db->from($table); $db->from($table);
return $db; return $db;
} }
/** /**
* @param string $column * @param string $column
* @param string $alias * @param string $alias
* @return string * @return string
*/ */
public static function any_value(string $column, string $alias = ''): string public static function any_value(string $column, string $alias = ''): string
{ {
if (empty($alias)) { if (empty($alias)) {
$alias = $column . '_any_value'; $alias = $column . '_any_value';
} }
return 'ANY_VALUE(' . $column . ') as ' . $alias; return 'ANY_VALUE(' . $column . ') as ' . $alias;
} }
/**
* @param Connection|null $db
* @return mixed
* @throws Exception
*/
public function get(Connection $db = NULL): mixed
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
return $db->createCommand(SqlBuilder::builder($this)->one())
->all();
}
/**
* @param $column
* @return string
*/
public static function raw($column): string
{
return '`' . $column . '`';
}
/**
* @param Connection|null $db
* @return mixed
* @throws Exception
*/
public function find(Connection $db = NULL): mixed
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
return $db->createCommand(SqlBuilder::builder($this)->all())
->one();
}
/**
* @param Connection|NULL $db
* @return bool|int
* @throws Exception
*/
public function count(Connection $db = NULL): bool|int
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
return $db->createCommand(SqlBuilder::builder($this)->count())
->exec();
}
/**
* @param Connection|NULL $db
* @return bool|int
* @throws Exception
*/
public function exists(Connection $db = NULL): bool|int
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
return $db->createCommand(SqlBuilder::builder($this)->one())
->fetchColumn();
}
/**
* @param string $sql
* @param array $attributes
* @param Connection|null $db
* @return array|bool|int|string|null
* @throws Exception
*/
public static function findAllBySql(string $sql, array $attributes = [], Connection $db = NULL): int|bool|array|string|null
{
return $db->createCommand($sql, $attributes)->all();
}
/**
* @param string $sql
* @param array $attributes
* @param Connection|NULL $db
* @return array|mixed
* @throws Exception
*/
public static function findBySql(string $sql, array $attributes = [], Connection $db = NULL): mixed
{
return $db->createCommand($sql, $attributes)->one();
}
/**
* @param string $field
* @return array|null
* @throws Exception
*/
public function values(string $field): ?array
{
$data = $this->get();
if (empty($data) || empty($field)) {
return NULL;
}
$first = current($data);
if (!isset($first[$field])) {
return NULL;
}
return array_column($data, $field);
}
/**
* @param $field
* @return mixed
* @throws Exception
*/
public function value($field): mixed
{
$data = $this->find();
if (!empty($field) && isset($data[$field])) {
return $data[$field];
}
return $data;
}
/**
* @param null $db
* @return bool|int
* @throws Exception
*/
public function delete($db = null): bool|int
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
$query = $db->getBuild()->builder($this);
return $db->createCommand($query)->delete();
}
/**
* @param $table
* @param null $db
* @return bool|int
* @throws Exception
*/
public static function drop($table, $db = null): bool|int
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
return $db->createCommand('DROP TABLE ' . $table)->delete();
}
/**
* @param $table
* @param null $db
* @return bool|int
* @throws Exception
*/
public static function truncate($table, $db = null): bool|int
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
return $db->createCommand('TRUNCATE ' . $table)->exec();
}
/**
* @param $table
* @param Connection|NULL $db
* @return mixed
* @throws Exception
*/
public static function showCreateSql($table, Connection $db = NULL): mixed
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
if (empty($table)) { /**
return null; * @param string $column
} * @return string
*/
return $db->createCommand('SHOW CREATE TABLE ' . $table)->one(); public static function increment(string $column): string
} {
return '+ ' . $column;
/** }
* @param $table
* @param Connection|NULL $db
* @return bool|int|null
* @throws Exception
*/
public static function desc($table, Connection $db = NULL): bool|int|null
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
if (empty($table)) {
return null;
}
return $db->createCommand('SHOW FULL FIELDS FROM ' . $table)->all();
}
/** /**
* @param string $table * @param string $column
* @param Connection|NULL $db * @return string
* @return mixed */
* @throws Exception public static function decrement(string $column): string
*/ {
public static function show(string $table, Connection $db = NULL): mixed return '- ' . $column;
{ }
if (empty($table)) {
return null;
}
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
$table = [' const TABLE = \'select * from %s where REFERENCED_TABLE_NAME=%s\';']; /**
* @param Connection|null $db
* @return mixed
* @throws Exception
*/
public function get(Connection $db = NULL): mixed
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
return $db->createCommand(SqlBuilder::builder($this)->one())
->all();
}
return $db->createCommand((new Query()) /**
->select('*') * @param $column
->from('INFORMATION_SCHEMA.KEY_COLUMN_USAGE') * @return string
->where(['REFERENCED_TABLE_NAME' => $table]) */
->getSql())->one(); public static function raw($column): string
} {
return '`' . $column . '`';
}
/**
* @param Connection|null $db
* @return mixed
* @throws Exception
*/
public function find(Connection $db = NULL): mixed
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
return $db->createCommand(SqlBuilder::builder($this)->all())
->one();
}
/**
* @param Connection|NULL $db
* @return bool|int
* @throws Exception
*/
public function count(Connection $db = NULL): bool|int
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
return $db->createCommand(SqlBuilder::builder($this)->count())
->exec();
}
/**
* @param Connection|NULL $db
* @return bool|int
* @throws Exception
*/
public function exists(Connection $db = NULL): bool|int
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
return $db->createCommand(SqlBuilder::builder($this)->one())
->fetchColumn();
}
/**
* @param string $sql
* @param array $attributes
* @param Connection|null $db
* @return array|bool|int|string|null
* @throws Exception
*/
public static function findAllBySql(string $sql, array $attributes = [], Connection $db = NULL): int|bool|array|string|null
{
return $db->createCommand($sql, $db?->database, $attributes)->all();
}
/**
* @param string $sql
* @param array $attributes
* @param Connection|NULL $db
* @return string|array|bool|int|null
* @throws Exception
*/
public static function findBySql(string $sql, array $attributes = [], Connection $db = NULL): string|array|bool|int|null
{
return $db->createCommand($sql, $db?->database, $attributes)->one();
}
/**
* @param string $field
* @return array|null
* @throws Exception
*/
public function values(string $field): ?array
{
$data = $this->get();
if (empty($data) || empty($field)) {
return NULL;
}
$first = current($data);
if (!isset($first[$field])) {
return NULL;
}
return array_column($data, $field);
}
/**
* @param $field
* @return mixed
* @throws Exception
*/
public function value($field): mixed
{
$data = $this->find();
if (!empty($field) && isset($data[$field])) {
return $data[$field];
}
return $data;
}
/**
* @param null $db
* @return bool|int
* @throws Exception
*/
public function delete($db = null): bool|int
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
$query = $db->getBuild()->builder($this);
return $db->createCommand($query)->delete();
}
/**
* @param $table
* @param null $db
* @return bool|int
* @throws Exception
*/
public static function drop($table, $db = null): bool|int
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
return $db->createCommand('DROP TABLE ' . $table)->delete();
}
/**
* @param $table
* @param null $db
* @return bool|int
* @throws Exception
*/
public static function truncate($table, $db = null): bool|int
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
return $db->createCommand('TRUNCATE ' . $table)->exec();
}
/**
* @param $table
* @param Connection|NULL $db
* @return mixed
* @throws Exception
*/
public static function showCreateSql($table, Connection $db = NULL): mixed
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
if (empty($table)) {
return null;
}
return $db->createCommand('SHOW CREATE TABLE ' . $table)->one();
}
/**
* @param $table
* @param Connection|NULL $db
* @return bool|int|null
* @throws Exception
*/
public static function desc($table, Connection $db = NULL): bool|int|null
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
if (empty($table)) {
return null;
}
return $db->createCommand('SHOW FULL FIELDS FROM ' . $table)->all();
}
/**
* @param string $table
* @param Connection|NULL $db
* @return mixed
* @throws Exception
*/
public static function show(string $table, Connection $db = NULL): mixed
{
if (empty($table)) {
return null;
}
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
$table = [' const TABLE = \'select * from %s where REFERENCED_TABLE_NAME=%s\';'];
return $db->createCommand((new Query())
->select('*')
->from('INFORMATION_SCHEMA.KEY_COLUMN_USAGE')
->where(['REFERENCED_TABLE_NAME' => $table])
->getSql())->one();
}
} }
+16 -17
View File
@@ -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;
+3 -2
View File
@@ -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;