@@ -104,6 +104,20 @@ class Forge
104104 */
105105 protected $ createDatabaseStr = 'CREATE DATABASE %s ' ;
106106
107+ /**
108+ * CREATE DATABASE IF statement
109+ *
110+ * @var string
111+ */
112+ protected $ createDatabaseIfStr = null ;
113+
114+ /**
115+ * CHECK DATABASE EXIST statement
116+ *
117+ * @var string
118+ */
119+ protected $ checkDatabaseExistStr = null ;
120+
107121 /**
108122 * DROP DATABASE statement
109123 *
@@ -199,13 +213,23 @@ public function getConnection()
199213 /**
200214 * Create database
201215 *
202- * @param string $db_name
216+ * @param string $dbName
217+ * @param boolean $ifNotExists Whether to add IF NOT EXISTS condition
203218 *
204219 * @return boolean
205220 * @throws \CodeIgniter\Database\Exceptions\DatabaseException
206221 */
207- public function createDatabase (string $ db_name ): bool
222+ public function createDatabase (string $ dbName , bool $ ifNotExists = false ): bool
208223 {
224+ if ($ ifNotExists && $ this ->createDatabaseIfStr === null )
225+ {
226+ if ($ this ->databaseExists ($ dbName ))
227+ {
228+ return true ;
229+ }
230+ $ ifNotExists = false ;
231+ }
232+
209233 if ($ this ->createDatabaseStr === false )
210234 {
211235 if ($ this ->db ->DBDebug )
@@ -215,7 +239,7 @@ public function createDatabase(string $db_name): bool
215239
216240 return false ;
217241 }
218- elseif (! $ this ->db ->query (sprintf ($ this ->createDatabaseStr , $ db_name , $ this ->db ->charset , $ this ->db ->DBCollat ))
242+ elseif (! $ this ->db ->query (sprintf ($ ifNotExists ? $ this ->createDatabaseIfStr : $ this -> createDatabaseStr , $ dbName , $ this ->db ->charset , $ this ->db ->DBCollat ))
219243 )
220244 {
221245 if ($ this ->db ->DBDebug )
@@ -228,23 +252,48 @@ public function createDatabase(string $db_name): bool
228252
229253 if (! empty ($ this ->db ->dataCache ['db_names ' ]))
230254 {
231- $ this ->db ->dataCache ['db_names ' ][] = $ db_name ;
255+ $ this ->db ->dataCache ['db_names ' ][] = $ dbName ;
232256 }
233257
234258 return true ;
235259 }
236260
237261 //--------------------------------------------------------------------
238262
263+ /**
264+ * Determine if a database exists
265+ *
266+ * @param string $dbName
267+ *
268+ * @return boolean
269+ * @throws \CodeIgniter\Database\Exceptions\DatabaseException
270+ */
271+ private function databaseExists (string $ dbName ): bool
272+ {
273+ if ($ this ->checkDatabaseExistStr === null )
274+ {
275+ if ($ this ->db ->DBDebug )
276+ {
277+ throw new DatabaseException ('This feature is not available for the database you are using. ' );
278+ }
279+
280+ return false ;
281+ }
282+
283+ return $ this ->db ->query ($ this ->checkDatabaseExistStr , $ dbName )->getRow () !== null ;
284+ }
285+
286+ //--------------------------------------------------------------------
287+
239288 /**
240289 * Drop database
241290 *
242- * @param string $db_name
291+ * @param string $dbName
243292 *
244293 * @return boolean
245294 * @throws \CodeIgniter\Database\Exceptions\DatabaseException
246295 */
247- public function dropDatabase (string $ db_name ): bool
296+ public function dropDatabase (string $ dbName ): bool
248297 {
249298 if ($ this ->dropDatabaseStr === false )
250299 {
@@ -255,7 +304,7 @@ public function dropDatabase(string $db_name): bool
255304
256305 return false ;
257306 }
258- elseif (! $ this ->db ->query (sprintf ($ this ->dropDatabaseStr , $ db_name )))
307+ elseif (! $ this ->db ->query (sprintf ($ this ->dropDatabaseStr , $ dbName )))
259308 {
260309 if ($ this ->db ->DBDebug )
261310 {
@@ -267,7 +316,7 @@ public function dropDatabase(string $db_name): bool
267316
268317 if (! empty ($ this ->db ->dataCache ['db_names ' ]))
269318 {
270- $ key = array_search (strtolower ($ db_name ), array_map ('strtolower ' , $ this ->db ->dataCache ['db_names ' ]), true );
319+ $ key = array_search (strtolower ($ dbName ), array_map ('strtolower ' , $ this ->db ->dataCache ['db_names ' ]), true );
271320 if ($ key !== false )
272321 {
273322 unset($ this ->db ->dataCache ['db_names ' ][$ key ]);
0 commit comments