Skip to content

Commit

Permalink
fix: add SQLSRV\Forge::createDatabase()
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Mar 28, 2024
1 parent c8a92fb commit 41a48ec
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
51 changes: 50 additions & 1 deletion system/Database/SQLSRV/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
namespace CodeIgniter\Database\SQLSRV;

use CodeIgniter\Database\BaseConnection;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Database\Forge as BaseForge;
use Throwable;

/**
* Forge for SQLSRV
Expand Down Expand Up @@ -42,7 +44,7 @@ class Forge extends BaseForge
*
* @var string
*/
protected $createDatabaseIfStr = "DECLARE @DBName VARCHAR(255) = '%s'\nDECLARE @SQL VARCHAR(max) = 'IF DB_ID( ''' + @DBName + ''' ) IS NULL CREATE DATABASE ' + @DBName\nEXEC( @SQL )";
protected $createDatabaseIfStr = "DECLARE @DBName VARCHAR(255) = '%s'\nDECLARE @SQL VARCHAR(max) = 'IF DB_ID( ''' + @DBName + ''' ) IS NULL CREATE DATABASE %s'\nEXEC( @SQL )";

/**
* CREATE DATABASE IF statement
Expand Down Expand Up @@ -119,6 +121,53 @@ public function __construct(BaseConnection $db)
$this->dropIndexStr = 'DROP INDEX %s ON ' . $this->db->escapeIdentifiers($this->db->schema) . '.%s';
}

/**
* Create database
*
* @param bool $ifNotExists Whether to add IF NOT EXISTS condition
*
* @throws DatabaseException
*/
public function createDatabase(string $dbName, bool $ifNotExists = false): bool
{
if ($ifNotExists) {
$sql = sprintf(
$this->createDatabaseIfStr,
$dbName,
$this->db->escapeIdentifier($dbName)
);
} else {
$sql = sprintf(
$this->createDatabaseStr,
$this->db->escapeIdentifier($dbName)
);
}

try {
if (! $this->db->query($sql)) {
// @codeCoverageIgnoreStart
if ($this->db->DBDebug) {
throw new DatabaseException('Unable to create the specified database.');
}

return false;
// @codeCoverageIgnoreEnd
}

if (isset($this->db->dataCache['db_names'])) {
$this->db->dataCache['db_names'][] = $dbName;
}

return true;
} catch (Throwable $e) {
if ($this->db->DBDebug) {
throw new DatabaseException('Unable to create the specified database.', 0, $e);
}

return false; // @codeCoverageIgnore
}
}

/**
* CREATE TABLE attributes
*/
Expand Down
15 changes: 6 additions & 9 deletions tests/system/Database/Live/ForgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,14 @@ public function testDropDatabase(): void

public function testCreateDatabaseExceptionNoCreateStatement(): void
{
$this->setPrivateProperty($this->forge, 'createDatabaseStr', false);
if ($this->db->DBDriver !== 'OCI8') {
$this->markTestSkipped($this->db->DBDriver . ' does support drop database.');
}

if ($this->db->DBDriver === 'SQLite3') {
$databaseCreated = $this->forge->createDatabase('test_forge_database');
$this->assertTrue($databaseCreated);
} else {
$this->expectException(DatabaseException::class);
$this->expectExceptionMessage('This feature is not available for the database you are using.');
$this->expectException(DatabaseException::class);
$this->expectExceptionMessage('This feature is not available for the database you are using.');

$this->forge->createDatabase('test_forge_database');
}
$this->forge->createDatabase('test_forge_database');
}

public function testDropDatabaseExceptionNoDropStatement(): void
Expand Down

0 comments on commit 41a48ec

Please sign in to comment.