改名
This commit is contained in:
@@ -121,7 +121,7 @@ class ActiveQuery extends Component implements ISqlBuilder
|
|||||||
*/
|
*/
|
||||||
public function execute($sql, array $params = []): Command
|
public function execute($sql, array $params = []): Command
|
||||||
{
|
{
|
||||||
return $this->modelClass::getDb()->createCommand($sql, $this->modelClass::getDbName(), $params);
|
return $this->modelClass::getDb()->createCommand($sql, $params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -177,7 +177,7 @@ class ActiveRecord extends BaseActiveRecord
|
|||||||
if (is_bool($create)) {
|
if (is_bool($create)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return static::getDb()->createCommand($create[0], static::getDbName(), $create[1])->exec();
|
return static::getDb()->createCommand($create[0], $create[1])->exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -90,6 +90,10 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
|||||||
protected array $actions = [];
|
protected array $actions = [];
|
||||||
|
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
private static string $connection = 'db';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Relation|null
|
* @var Relation|null
|
||||||
*/
|
*/
|
||||||
@@ -325,7 +329,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public static function find(): ActiveQuery
|
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()
|
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
|
private function insert($param, $attributes): bool|static
|
||||||
{
|
{
|
||||||
[$sql, $param] = SqlBuilder::builder(static::find())->insert($param);
|
[$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))) {
|
if (!($lastId = (int)$dbConnection->save(true, $this))) {
|
||||||
throw new Exception('保存失败.');
|
throw new Exception('保存失败.');
|
||||||
}
|
}
|
||||||
@@ -498,7 +502,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
|||||||
if (is_bool($generate)) {
|
if (is_bool($generate)) {
|
||||||
return $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)) {
|
if ($command->save(false, $this)) {
|
||||||
return $this->refresh()->afterSave($fields, $param);
|
return $this->refresh()->afterSave($fields, $param);
|
||||||
}
|
}
|
||||||
@@ -995,9 +999,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public static function setDatabaseConnect($dbName): Connection
|
public static function setDatabaseConnect($dbName): Connection
|
||||||
{
|
{
|
||||||
static::$ab_name = $dbName;
|
return Snowflake::app()->db->get(static::$connection = $dbName);
|
||||||
|
|
||||||
return Snowflake::app()->db->get($dbName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1007,7 +1009,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public static function getDbName(): string
|
public static function getDbName(): string
|
||||||
{
|
{
|
||||||
return Config::get('databases.connections.' . static::$ab_name . '.database');
|
return Config::get('databases.connections.' . static::$connection . '.database');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -261,12 +261,11 @@ class Connection extends Component
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param null $sql
|
* @param null $sql
|
||||||
* @param string $dbname
|
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
* @return Command
|
* @return Command
|
||||||
* @throws Exception
|
* @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]);
|
$command = new Command(['db' => $this, 'sql' => $sql]);
|
||||||
return $command->bindValues($attributes);
|
return $command->bindValues($attributes);
|
||||||
|
|||||||
+102
-93
@@ -11,8 +11,9 @@ namespace Database;
|
|||||||
|
|
||||||
use Database\Traits\QueryTrait;
|
use Database\Traits\QueryTrait;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Snowflake\Abstracts\Config;
|
||||||
use Snowflake\Event;
|
use Snowflake\Event;
|
||||||
use Snowflake\Snowflake;
|
use Snowflake\Exception\ConfigException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Db
|
* Class Db
|
||||||
@@ -54,6 +55,7 @@ class Db implements ISqlBuilder
|
|||||||
static::$_inTransaction = false;
|
static::$_inTransaction = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
@@ -67,6 +69,7 @@ class Db implements ISqlBuilder
|
|||||||
static::$_inTransaction = false;
|
static::$_inTransaction = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $table
|
* @param $table
|
||||||
*
|
*
|
||||||
@@ -74,11 +77,12 @@ class Db implements ISqlBuilder
|
|||||||
*/
|
*/
|
||||||
public static function table($table): Db|static
|
public static function table($table): Db|static
|
||||||
{
|
{
|
||||||
$db = new Db();
|
$connection = new Db();
|
||||||
$db->from($table);
|
$connection->from($table);
|
||||||
return $db;
|
return $connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @param string $alias
|
* @param string $alias
|
||||||
@@ -114,16 +118,15 @@ class Db implements ISqlBuilder
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Connection|null $db
|
* @param Connection|null $connection
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function get(Connection $db = NULL): mixed
|
public function get(Connection $connection = NULL): mixed
|
||||||
{
|
{
|
||||||
if (empty($db)) {
|
$connection = static::getDefaultConnection($connection);
|
||||||
$db = Snowflake::app()->get('db');
|
|
||||||
}
|
return $connection->createCommand(SqlBuilder::builder($this)->one())
|
||||||
return $db->createCommand(SqlBuilder::builder($this)->one())
|
|
||||||
->all();
|
->all();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,69 +140,70 @@ class Db implements ISqlBuilder
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Connection|null $db
|
* @param Connection|null $connection
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function find(Connection $db = NULL): mixed
|
public function find(Connection $connection = NULL): mixed
|
||||||
{
|
{
|
||||||
if (empty($db)) {
|
$connection = static::getDefaultConnection($connection);
|
||||||
$db = Snowflake::app()->get('db');
|
|
||||||
}
|
return $connection->createCommand(SqlBuilder::builder($this)->all())
|
||||||
return $db->createCommand(SqlBuilder::builder($this)->all())
|
|
||||||
->one();
|
->one();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Connection|NULL $db
|
* @param Connection|NULL $connection
|
||||||
* @return bool|int
|
* @return bool|int
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function count(Connection $db = NULL): bool|int
|
public function count(Connection $connection = NULL): bool|int
|
||||||
{
|
{
|
||||||
if (empty($db)) {
|
$connection = static::getDefaultConnection($connection);
|
||||||
$db = Snowflake::app()->get('db');
|
|
||||||
}
|
return $connection->createCommand(SqlBuilder::builder($this)->count())
|
||||||
return $db->createCommand(SqlBuilder::builder($this)->count())
|
|
||||||
->exec();
|
->exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Connection|NULL $db
|
* @param Connection|NULL $connection
|
||||||
* @return bool|int
|
* @return bool|int
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function exists(Connection $db = NULL): bool|int
|
public function exists(Connection $connection = NULL): bool|int
|
||||||
{
|
{
|
||||||
if (empty($db)) {
|
$connection = static::getDefaultConnection($connection);
|
||||||
$db = Snowflake::app()->get('db');
|
|
||||||
}
|
return $connection->createCommand(SqlBuilder::builder($this)->one())
|
||||||
return $db->createCommand(SqlBuilder::builder($this)->one())
|
|
||||||
->fetchColumn();
|
->fetchColumn();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $sql
|
* @param string $sql
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
* @param Connection|null $db
|
* @param Connection|null $connection
|
||||||
* @return array|bool|int|string|null
|
* @return array|bool|int|string|null
|
||||||
* @throws Exception
|
* @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 string $sql
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
* @param Connection|NULL $db
|
* @param Connection|NULL $connection
|
||||||
* @return string|array|bool|int|null
|
* @return string|array|bool|int|null
|
||||||
* @throws Exception
|
* @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
|
* @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)) {
|
$connection = static::getDefaultConnection($connection);
|
||||||
$db = Snowflake::app()->get('db');
|
|
||||||
}
|
|
||||||
|
|
||||||
$query = $db->getBuild()->builder($this);
|
return $connection->createCommand($connection->getBuild()->builder($this))->delete();
|
||||||
|
|
||||||
return $db->createCommand($query)->delete();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $table
|
* @param string $table
|
||||||
* @param null $db
|
* @param null $connection
|
||||||
* @return bool|int
|
* @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)) {
|
$connection = static::getDefaultConnection($connection);
|
||||||
$db = Snowflake::app()->get('db');
|
|
||||||
}
|
$sprint = sprintf('DROP TABLE `%s`.`%s`', $connection->database, $table);
|
||||||
return $db->createCommand('DROP TABLE `' . $db->database . '`.' . $table)->delete();
|
return $connection->createCommand($sprint)->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $table
|
* @param string $table
|
||||||
* @param null $db
|
* @param null $connection
|
||||||
* @return bool|int
|
* @return bool|int
|
||||||
* @throws Exception
|
* @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)) {
|
$sprint = sprintf('TRUNCATE `%s`.`%s`', $connection->database, $table);
|
||||||
$db = Snowflake::app()->get('db');
|
return $connection->createCommand($sprint)->exec();
|
||||||
}
|
|
||||||
|
|
||||||
return $db->createCommand('TRUNCATE `' . $db->database . '`.' . $table)->exec();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $table
|
* @param string $table
|
||||||
* @param Connection|NULL $db
|
* @param Connection|NULL $connection
|
||||||
* @return mixed
|
* @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)) {
|
$sprint = sprintf('SHOW CREATE TABLE `%s`.`%s`', $connection->database, $table);
|
||||||
$db = Snowflake::app()->get('db');
|
return $connection->createCommand($sprint)->one();
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (empty($table)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $db->createCommand('SHOW CREATE TABLE `' . $db->database . '`.' . $table)->one();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $table
|
* @param string $table
|
||||||
* @param Connection|NULL $db
|
* @param Connection|NULL $connection
|
||||||
* @return bool|int|null
|
* @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)) {
|
$connection = static::getDefaultConnection($connection);
|
||||||
$db = Snowflake::app()->get('db');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (empty($table)) {
|
$sprint = sprintf('SHOW FULL FIELDS FROM `%s`.`%s`', $connection->database, $table);
|
||||||
return null;
|
return $connection->createCommand($sprint)->all();
|
||||||
}
|
|
||||||
|
|
||||||
return $db->createCommand('SHOW FULL FIELDS FROM `' . $db->database . '`.' . $table)->all();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $table
|
* @param string $table
|
||||||
* @param Connection|NULL $db
|
* @param Connection|NULL $connection
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @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)) {
|
if (empty($table)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
$connection = static::getDefaultConnection($connection);
|
||||||
if (empty($db)) {
|
|
||||||
$db = Snowflake::app()->get('db');
|
|
||||||
}
|
|
||||||
|
|
||||||
$table = [' const TABLE = \'select * from %s where REFERENCED_TABLE_NAME=%s\';'];
|
$table = [' const TABLE = \'select * from %s where REFERENCED_TABLE_NAME=%s\';'];
|
||||||
|
return $connection->createCommand((new Query())
|
||||||
return $db->createCommand((new Query())
|
|
||||||
->select('*')
|
->select('*')
|
||||||
->from('INFORMATION_SCHEMA.KEY_COLUMN_USAGE')
|
->from('INFORMATION_SCHEMA.KEY_COLUMN_USAGE')
|
||||||
->where(['REFERENCED_TABLE_NAME' => $table])
|
->where(['REFERENCED_TABLE_NAME' => $table])
|
||||||
->getSql())->one();
|
->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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user