modify
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -998,7 +1002,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
||||
public function offsetUnset(mixed $offset)
|
||||
{
|
||||
if (!isset($this->_attributes[$offset])
|
||||
&& !isset($this->_oldAttributes[$offset])) {
|
||||
&& !isset($this->_oldAttributes[$offset])) {
|
||||
return;
|
||||
}
|
||||
unset($this->_attributes[$offset]);
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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]);
|
||||
|
||||
Reference in New Issue
Block a user