支持多数据库

This commit is contained in:
zyimm 2023-01-11 16:22:10 +08:00
parent d1fa6e7419
commit d4f8912a0f
3 changed files with 36 additions and 30 deletions

View File

@ -101,7 +101,7 @@ class Table
*
* @return Table
*/
public function setName(string $name): Table
public function setName(string $name)
{
$this->name = $name;
return $this;
@ -124,7 +124,7 @@ class Table
*
* @return Table
*/
public function setOptions(array $options): Table
public function setOptions(array $options)
{
$this->options = $options;
return $this;
@ -147,7 +147,7 @@ class Table
*
* @return Table
*/
public function setAdapter(AdapterInterface $adapter): Table
public function setAdapter(AdapterInterface $adapter)
{
$this->adapter = $adapter;
return $this;
@ -190,7 +190,7 @@ class Table
*
* @return Table
*/
public function rename(string $newTableName): Table
public function rename(string $newTableName)
{
$this->getAdapter()->renameTable($this->getName(), $newTableName);
$this->setName($newTableName);
@ -206,7 +206,7 @@ class Table
* @return Table
* @deprecated
*/
public function setColumns(array $columns): Table
public function setColumns(array $columns)
{
return $this->setPendingColumns($columns);
}
@ -228,7 +228,7 @@ class Table
*
* @return Table
*/
public function setPendingColumns(array $columns): Table
public function setPendingColumns(array $columns)
{
$this->columns = $columns;
return $this;
@ -251,7 +251,7 @@ class Table
*
* @return Table
*/
public function setIndexes(array $indexes): Table
public function setIndexes(array $indexes)
{
$this->indexes = $indexes;
return $this;
@ -274,7 +274,7 @@ class Table
*
* @return Table
*/
public function setForeignKeys(array $foreignKeys): Table
public function setForeignKeys(array $foreignKeys)
{
$this->foreignKeys = $foreignKeys;
return $this;
@ -297,7 +297,7 @@ class Table
*
* @return Table
*/
public function setData(array $data): Table
public function setData(array $data)
{
$this->data = $data;
return $this;
@ -342,7 +342,7 @@ class Table
* @throws InvalidArgumentException
* @throws RuntimeException
*/
public function addColumn($columnName, string $type = null, array $options = []): Table
public function addColumn($columnName, string $type = null, array $options = [])
{
// we need an adapter set to add a column
if (null === $this->getAdapter()) {
@ -379,7 +379,7 @@ class Table
*
* @return Table
*/
public function removeColumn(string $columnName): Table
public function removeColumn(string $columnName)
{
$this->getAdapter()->dropColumn($this->getName(), $columnName);
return $this;
@ -393,7 +393,7 @@ class Table
*
* @return Table
*/
public function renameColumn(string $oldName, string $newName): Table
public function renameColumn(string $oldName, string $newName)
{
$this->getAdapter()->renameColumn($this->getName(), $oldName, $newName);
return $this;
@ -408,7 +408,7 @@ class Table
*
* @return Table
*/
public function changeColumn(string $columnName, $newColumnType, array $options = []): Table
public function changeColumn(string $columnName, $newColumnType, array $options = [])
{
// create a column object if one wasn't supplied
if (!$newColumnType instanceof Column) {
@ -451,7 +451,7 @@ class Table
*
* @return Table
*/
public function addIndex($columns, array $options = []): Table
public function addIndex($columns, array $options = [])
{
// create a new index object if strings or an array of strings is supplied
if (!$columns instanceof Index) {
@ -477,7 +477,7 @@ class Table
*
* @return Table
*/
public function removeIndex(array $columns, array $options = []): Table
public function removeIndex(array $columns, array $options = [])
{
$this->getAdapter()->dropIndex($this->getName(), $columns, $options);
return $this;
@ -490,7 +490,7 @@ class Table
*
* @return Table
*/
public function removeIndexByName(string $name): Table
public function removeIndexByName(string $name)
{
$this->getAdapter()->dropIndexByName($this->getName(), $name);
return $this;
@ -521,7 +521,7 @@ class Table
*
* @return Table
*/
public function addForeignKey($columns, $referencedTable, $referencedColumns = ['id'], array $options = []): Table
public function addForeignKey($columns, $referencedTable, $referencedColumns = ['id'], array $options = [])
{
if (is_string($referencedColumns)) {
$referencedColumns = [$referencedColumns]; // str to array
@ -548,7 +548,7 @@ class Table
*
* @return Table
*/
public function dropForeignKey($columns, string $constraint = null): Table
public function dropForeignKey($columns, string $constraint = null)
{
if (is_string($columns)) {
$columns = [$columns];
@ -583,7 +583,7 @@ class Table
*
* @return Table
*/
public function addTimestamps(string $createdAtColumnName = 'created_at', string $updatedAtColumnName = 'updated_at'): Table
public function addTimestamps(string $createdAtColumnName = 'created_at', string $updatedAtColumnName = 'updated_at')
{
$createdAtColumnName = is_null($createdAtColumnName) ? 'created_at' : $createdAtColumnName;
$updatedAtColumnName = is_null($updatedAtColumnName) ? 'updated_at' : $updatedAtColumnName;
@ -611,7 +611,7 @@ class Table
*
* @return Table
*/
public function insert(array $data): Table
public function insert(array $data)
{
// handle array of array situations
if (isset($data[0]) && is_array($data[0])) {

View File

@ -21,6 +21,11 @@ abstract class Command extends \think\console\Command
{
protected $dbConfig = null;
/**
* @var AdapterInterface
*/
private $adapter;
/**
* @param string|null $db_config
*
@ -29,9 +34,9 @@ abstract class Command extends \think\console\Command
*/
public function getAdapter(string $db_config = null): AdapterInterface
{
if (isset($this->adapter)) {
return $this->adapter;
}
// if (isset($this->adapter)) {
// return $this->adapter;
// }
$this->dbConfig = $db_config;
$options = $this->getDbConfig();
@ -43,7 +48,7 @@ abstract class Command extends \think\console\Command
$this->adapter = $adapter;
return $adapter;
return $this->adapter;
}
/**

View File

@ -53,11 +53,12 @@ abstract class Migrate extends Command
$startTime = time();
$direction = (MigrationInterface::UP === $direction) ? MigrationInterface::UP : MigrationInterface::DOWN;
$migration->setAdapter($this->getAdapter($migration->getDbConfig()));
// begin the transaction if the adapter supports it
if ($this->getAdapter()->hasTransactions()) {
$this->getAdapter()->beginTransaction();
if ($this->getAdapter($migration->getDbConfig())->hasTransactions()) {
$this->getAdapter($migration->getDbConfig())->beginTransaction();
}
// Run the migration
@ -81,13 +82,13 @@ abstract class Migrate extends Command
}
// commit the transaction if the adapter supports it
if ($this->getAdapter()->hasTransactions()) {
$this->getAdapter()->commitTransaction();
if ($this->getAdapter($migration->getDbConfig())->hasTransactions()) {
$this->getAdapter($migration->getDbConfig())->commitTransaction();
}
// Record it in the database
$this->getAdapter()
->migrated($migration, $direction, date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', time()));
$this->getAdapter($migration->getDbConfig())
->migrated($migration, $direction, date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', time()));
$end = microtime(true);