This commit is contained in:
as2252258@163.com
2021-07-03 14:14:46 +08:00
parent 7a39375657
commit de475736ba
5 changed files with 27 additions and 16 deletions
+1 -1
View File
@@ -132,7 +132,7 @@ class ActiveQuery extends Component implements ISqlBuilder
*/
public function execute($sql, $params = []): Command
{
return $this->modelClass::getDb()->createCommand($sql, $params);
return $this->modelClass::getDb()->createCommand($sql, $this->modelClass::getDbName(), $params);
}
+1 -1
View File
@@ -196,7 +196,7 @@ class ActiveRecord extends BaseActiveRecord
if (is_bool($create)) {
return false;
}
return static::getDb()->createCommand($create[0], $create[1])->exec();
return static::getDb()->createCommand($create[0], static::getDbName(), $create[1])->exec();
}
+19 -3
View File
@@ -433,7 +433,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
// $trance = $dbConnection->beginTransaction();
try {
if (!($lastId = (int)$dbConnection->createCommand($sql, $param)->save(true, $this))) {
if (!($lastId = (int)$dbConnection->createCommand($sql, static::getDbName(), $param)->save(true, $this))) {
throw new Exception('保存失败.');
}
// $trance->commit();
@@ -499,7 +499,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
}
// $trance = $command->beginTransaction();
if (!$command->createCommand(...$generate)->save(false, $this)) {
if (!$command->createCommand($generate[0], static::getDbName(), $generate[1])->save(false, $this)) {
// $trance->rollback();
return false;
}
@@ -769,6 +769,10 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
return true;
}
private static string $ab_name = '';
/**
* @return Connection
* @throws Exception
@@ -1032,9 +1036,21 @@ 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 string
*/
public static function getDbName()
{
return static::$ab_name;
}
/**
* @return Columns
* @throws Exception
@@ -1043,7 +1059,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
{
return static::getDb()->getSchema()
->getColumns()
->table(static::getTable());
->table(static::getDbName() . '.' . static::getTable());
}
/**
+5 -5
View File
@@ -263,14 +263,14 @@ class Connection extends Component
* @return Command
* @throws
*/
public function createCommand($sql = null, array $attributes = []): Command
public function createCommand($sql = null, $dbname = '', array $attributes = []): Command
{
$substr = strtoupper(substr($sql, 0, 6));
$sql = match ($substr) {
'SELECT', 'SHOW F', 'DELETE' => preg_replace('/FROM\s+(\w+)\s+/', 'FROM `' . $this->database . '`.$1 ', $sql),
'UPDATE' => preg_replace('/UPDATE\s+(\w+)\s+SET/', 'UPDATE `' . $this->database . '`.$1 SET', $sql),
'INSERT' => preg_replace('/INSERT INTO\s+(\w+)\s+/', 'INSERT INTO `' . $this->database . '`.$1', $sql),
'TRUNCA' => preg_replace('/TRUNCATE\s+(\w+)\s+/', 'TRUNCATE `' . $this->database . '`.$1', $sql),
'SELECT', 'SHOW F', 'DELETE' => preg_replace('/FROM\s+(\w+)\s+/', 'FROM `' . $dbname . '`.$1 ', $sql),
'UPDATE' => preg_replace('/UPDATE\s+(\w+)\s+SET/', 'UPDATE `' . $dbname . '`.$1 SET', $sql),
'INSERT' => preg_replace('/INSERT INTO\s+(\w+)\s+/', 'INSERT INTO `' . $dbname . '`.$1', $sql),
'TRUNCA' => preg_replace('/TRUNCATE\s+(\w+)\s+/', 'TRUNCATE `' . $dbname . '`.$1', $sql),
default => throw new Exception('database error')
};
$command = new Command(['db' => $this, 'sql' => $sql]);
-5
View File
@@ -62,11 +62,6 @@ class Service extends Component
$object = Snowflake::createObject($config);
}
unset($config['class']);
Snowflake::configure($object, $config);
return $this->_components[$id] = $object;
}