This commit is contained in:
2021-08-03 11:44:58 +08:00
parent 3f9b8d8e8c
commit 72b8da4c7f
5 changed files with 115 additions and 105 deletions
+1 -1
View File
@@ -121,7 +121,7 @@ class ActiveQuery extends Component implements ISqlBuilder
*/
public function execute($sql, array $params = []): Command
{
return $this->modelClass::getDb()->createCommand($sql, $this->modelClass::getDbName(), $params);
return $this->modelClass::getDb()->createCommand($sql, $params);
}
+1 -1
View File
@@ -177,7 +177,7 @@ class ActiveRecord extends BaseActiveRecord
if (is_bool($create)) {
return false;
}
return static::getDb()->createCommand($create[0], static::getDbName(), $create[1])->exec();
return static::getDb()->createCommand($create[0], $create[1])->exec();
}
+10 -8
View File
@@ -90,6 +90,10 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
protected array $actions = [];
/** @var string */
private static string $connection = 'db';
/**
* @var Relation|null
*/
@@ -325,7 +329,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/
public static function find(): ActiveQuery
{
return new ActiveQuery(get_called_class());
return static::query();
}
/**
@@ -342,7 +346,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/
protected function getConnection()
{
return Config::get('connections.' . static::$ab_name, null, true);
return Config::get('connections.' . static::$connection, null, true);
}
@@ -445,7 +449,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
private function insert($param, $attributes): bool|static
{
[$sql, $param] = SqlBuilder::builder(static::find())->insert($param);
$dbConnection = static::getDb()->createCommand($sql, static::getDbName(), $param);
$dbConnection = static::getDb()->createCommand($sql, $param);
if (!($lastId = (int)$dbConnection->save(true, $this))) {
throw new Exception('保存失败.');
}
@@ -498,7 +502,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
if (is_bool($generate)) {
return $generate;
}
$command = static::getDb()->createCommand($generate[0], static::getDbName(), $generate[1]);
$command = static::getDb()->createCommand($generate[0], $generate[1]);
if ($command->save(false, $this)) {
return $this->refresh()->afterSave($fields, $param);
}
@@ -995,9 +999,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/
public static function setDatabaseConnect($dbName): Connection
{
static::$ab_name = $dbName;
return Snowflake::app()->db->get($dbName);
return Snowflake::app()->db->get(static::$connection = $dbName);
}
@@ -1007,7 +1009,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/
public static function getDbName(): string
{
return Config::get('databases.connections.' . static::$ab_name . '.database');
return Config::get('databases.connections.' . static::$connection . '.database');
}
+1 -2
View File
@@ -261,12 +261,11 @@ class Connection extends Component
/**
* @param null $sql
* @param string $dbname
* @param array $attributes
* @return Command
* @throws Exception
*/
public function createCommand($sql = null, string $dbname = '', array $attributes = []): Command
public function createCommand($sql = null, array $attributes = []): Command
{
$command = new Command(['db' => $this, 'sql' => $sql]);
return $command->bindValues($attributes);
+102 -93
View File
@@ -11,8 +11,9 @@ namespace Database;
use Database\Traits\QueryTrait;
use Exception;
use Snowflake\Abstracts\Config;
use Snowflake\Event;
use Snowflake\Snowflake;
use Snowflake\Exception\ConfigException;
/**
* Class Db
@@ -54,6 +55,7 @@ class Db implements ISqlBuilder
static::$_inTransaction = false;
}
/**
* @throws Exception
*/
@@ -67,6 +69,7 @@ class Db implements ISqlBuilder
static::$_inTransaction = false;
}
/**
* @param $table
*
@@ -74,11 +77,12 @@ class Db implements ISqlBuilder
*/
public static function table($table): Db|static
{
$db = new Db();
$db->from($table);
return $db;
$connection = new Db();
$connection->from($table);
return $connection;
}
/**
* @param string $column
* @param string $alias
@@ -114,16 +118,15 @@ class Db implements ISqlBuilder
/**
* @param Connection|null $db
* @param Connection|null $connection
* @return mixed
* @throws Exception
*/
public function get(Connection $db = NULL): mixed
public function get(Connection $connection = NULL): mixed
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
return $db->createCommand(SqlBuilder::builder($this)->one())
$connection = static::getDefaultConnection($connection);
return $connection->createCommand(SqlBuilder::builder($this)->one())
->all();
}
@@ -137,69 +140,70 @@ class Db implements ISqlBuilder
}
/**
* @param Connection|null $db
* @param Connection|null $connection
* @return mixed
* @throws Exception
*/
public function find(Connection $db = NULL): mixed
public function find(Connection $connection = NULL): mixed
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
return $db->createCommand(SqlBuilder::builder($this)->all())
$connection = static::getDefaultConnection($connection);
return $connection->createCommand(SqlBuilder::builder($this)->all())
->one();
}
/**
* @param Connection|NULL $db
* @param Connection|NULL $connection
* @return bool|int
* @throws Exception
*/
public function count(Connection $db = NULL): bool|int
public function count(Connection $connection = NULL): bool|int
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
return $db->createCommand(SqlBuilder::builder($this)->count())
$connection = static::getDefaultConnection($connection);
return $connection->createCommand(SqlBuilder::builder($this)->count())
->exec();
}
/**
* @param Connection|NULL $db
* @param Connection|NULL $connection
* @return bool|int
* @throws Exception
*/
public function exists(Connection $db = NULL): bool|int
public function exists(Connection $connection = NULL): bool|int
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
return $db->createCommand(SqlBuilder::builder($this)->one())
$connection = static::getDefaultConnection($connection);
return $connection->createCommand(SqlBuilder::builder($this)->one())
->fetchColumn();
}
/**
* @param string $sql
* @param array $attributes
* @param Connection|null $db
* @param Connection|null $connection
* @return array|bool|int|string|null
* @throws Exception
*/
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 $connection = NULL): int|bool|array|string|null
{
return $db->createCommand($sql, $db?->database, $attributes)->all();
$connection = static::getDefaultConnection($connection);
return $connection->createCommand($sql, $attributes)->all();
}
/**
* @param string $sql
* @param array $attributes
* @param Connection|NULL $db
* @param Connection|NULL $connection
* @return string|array|bool|int|null
* @throws Exception
*/
public static function findBySql(string $sql, array $attributes = [], Connection $db = NULL): string|array|bool|int|null
public static function findBySql(string $sql, array $attributes = [], Connection $connection = NULL): string|array|bool|int|null
{
return $db->createCommand($sql, $db?->database, $attributes)->one();
$connection = static::getDefaultConnection($connection);
return $connection->createCommand($sql, $attributes)->one();
}
/**
@@ -235,115 +239,120 @@ class Db implements ISqlBuilder
}
/**
* @param null $db
* @param Connection|null $connection
* @return bool|int
* @throws Exception
* @throws ConfigException
*/
public function delete($db = null): bool|int
public function delete(?Connection $connection = null): bool|int
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
$connection = static::getDefaultConnection($connection);
$query = $db->getBuild()->builder($this);
return $db->createCommand($query)->delete();
return $connection->createCommand($connection->getBuild()->builder($this))->delete();
}
/**
* @param $table
* @param null $db
* @param string $table
* @param null $connection
* @return bool|int
* @throws Exception
* @throws ConfigException
*/
public static function drop($table, $db = null): bool|int
public static function drop(string $table, $connection = null): bool|int
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
return $db->createCommand('DROP TABLE `' . $db->database . '`.' . $table)->delete();
$connection = static::getDefaultConnection($connection);
$sprint = sprintf('DROP TABLE `%s`.`%s`', $connection->database, $table);
return $connection->createCommand($sprint)->delete();
}
/**
* @param $table
* @param null $db
* @param string $table
* @param null $connection
* @return bool|int
* @throws Exception
*/
public static function truncate($table, $db = null): bool|int
public static function truncate(string $table, $connection = null): bool|int
{
$connection = static::getDefaultConnection($connection);
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
return $db->createCommand('TRUNCATE `' . $db->database . '`.' . $table)->exec();
$sprint = sprintf('TRUNCATE `%s`.`%s`', $connection->database, $table);
return $connection->createCommand($sprint)->exec();
}
/**
* @param $table
* @param Connection|NULL $db
* @param string $table
* @param Connection|NULL $connection
* @return mixed
* @throws Exception
* @throws ConfigException
*/
public static function showCreateSql($table, Connection $db = NULL): mixed
public static function showCreateSql(string $table, Connection $connection = NULL): mixed
{
$connection = static::getDefaultConnection($connection);
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
if (empty($table)) {
return null;
}
return $db->createCommand('SHOW CREATE TABLE `' . $db->database . '`.' . $table)->one();
$sprint = sprintf('SHOW CREATE TABLE `%s`.`%s`', $connection->database, $table);
return $connection->createCommand($sprint)->one();
}
/**
* @param $table
* @param Connection|NULL $db
* @param string $table
* @param Connection|NULL $connection
* @return bool|int|null
* @throws Exception
* @throws ConfigException
*/
public static function desc($table, Connection $db = NULL): bool|int|null
public static function desc(string $table, Connection $connection = NULL): bool|int|null
{
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
$connection = static::getDefaultConnection($connection);
if (empty($table)) {
return null;
}
return $db->createCommand('SHOW FULL FIELDS FROM `' . $db->database . '`.' . $table)->all();
$sprint = sprintf('SHOW FULL FIELDS FROM `%s`.`%s`', $connection->database, $table);
return $connection->createCommand($sprint)->all();
}
/**
* @param string $table
* @param Connection|NULL $db
* @param Connection|NULL $connection
* @return mixed
* @throws Exception
*/
public static function show(string $table, Connection $db = NULL): mixed
public static function show(string $table, Connection $connection = NULL): mixed
{
if (empty($table)) {
return null;
}
if (empty($db)) {
$db = Snowflake::app()->get('db');
}
$connection = static::getDefaultConnection($connection);
$table = [' const TABLE = \'select * from %s where REFERENCED_TABLE_NAME=%s\';'];
return $db->createCommand((new Query())
return $connection->createCommand((new Query())
->select('*')
->from('INFORMATION_SCHEMA.KEY_COLUMN_USAGE')
->where(['REFERENCED_TABLE_NAME' => $table])
->getSql())->one();
}
/**
* @param null|Connection $connection
* @param null $name
* @return mixed
* @throws ConfigException
* @throws Exception
*/
public static function getDefaultConnection(?Connection $connection, $name = null): Connection
{
if ($connection instanceof Connection) {
return $connection;
}
$databases = Config::get('databases.connections', []);
if (empty($databases) || !is_array($databases)) {
throw new Exception('Please configure the database link.');
}
if (!empty($name)) {
if (!isset($databases[$name])) {
throw new Exception('Please configure the database link.');
}
return $databases[$name];
}
return current($databases);
}
}