Skip to content

Commit 41a48ec

Browse files
committed
fix: add SQLSRV\Forge::createDatabase()
1 parent c8a92fb commit 41a48ec

2 files changed

Lines changed: 56 additions & 10 deletions

File tree

‎system/Database/SQLSRV/Forge.php‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
namespace CodeIgniter\Database\SQLSRV;
1515

1616
use CodeIgniter\Database\BaseConnection;
17+
use CodeIgniter\Database\Exceptions\DatabaseException;
1718
use CodeIgniter\Database\Forge as BaseForge;
19+
use Throwable;
1820

1921
/**
2022
* Forge for SQLSRV
@@ -42,7 +44,7 @@ class Forge extends BaseForge
4244
*
4345
* @var string
4446
*/
45-
protected $createDatabaseIfStr = "DECLARE @DBName VARCHAR(255) = '%s'\nDECLARE @SQL VARCHAR(max) = 'IF DB_ID( ''' + @DBName + ''' ) IS NULL CREATE DATABASE ' + @DBName\nEXEC( @SQL )";
47+
protected $createDatabaseIfStr = "DECLARE @DBName VARCHAR(255) = '%s'\nDECLARE @SQL VARCHAR(max) = 'IF DB_ID( ''' + @DBName + ''' ) IS NULL CREATE DATABASE %s'\nEXEC( @SQL )";
4648

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

124+
/**
125+
* Create database
126+
*
127+
* @param bool $ifNotExists Whether to add IF NOT EXISTS condition
128+
*
129+
* @throws DatabaseException
130+
*/
131+
public function createDatabase(string $dbName, bool $ifNotExists = false): bool
132+
{
133+
if ($ifNotExists) {
134+
$sql = sprintf(
135+
$this->createDatabaseIfStr,
136+
$dbName,
137+
$this->db->escapeIdentifier($dbName)
138+
);
139+
} else {
140+
$sql = sprintf(
141+
$this->createDatabaseStr,
142+
$this->db->escapeIdentifier($dbName)
143+
);
144+
}
145+
146+
try {
147+
if (! $this->db->query($sql)) {
148+
// @codeCoverageIgnoreStart
149+
if ($this->db->DBDebug) {
150+
throw new DatabaseException('Unable to create the specified database.');
151+
}
152+
153+
return false;
154+
// @codeCoverageIgnoreEnd
155+
}
156+
157+
if (isset($this->db->dataCache['db_names'])) {
158+
$this->db->dataCache['db_names'][] = $dbName;
159+
}
160+
161+
return true;
162+
} catch (Throwable $e) {
163+
if ($this->db->DBDebug) {
164+
throw new DatabaseException('Unable to create the specified database.', 0, $e);
165+
}
166+
167+
return false; // @codeCoverageIgnore
168+
}
169+
}
170+
122171
/**
123172
* CREATE TABLE attributes
124173
*/

‎tests/system/Database/Live/ForgeTest.php‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,17 +155,14 @@ public function testDropDatabase(): void
155155

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

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

167-
$this->forge->createDatabase('test_forge_database');
168-
}
165+
$this->forge->createDatabase('test_forge_database');
169166
}
170167

171168
public function testDropDatabaseExceptionNoDropStatement(): void

0 commit comments

Comments
 (0)