@@ -17,12 +17,14 @@ abstract class Controller
17
17
{
18
18
const DSN_CONFIG_FILE = 'config/Database.ini ' ;
19
19
const DATABASE_SCHEMA_FILE = 'data/schema.sql ' ;
20
+ const DATABASE_SCHEMA_FILE_MYSQL = 'data/schema_mysql.sql ' ;
20
21
const INITIAL_DATA_FILE = 'data/initial_data.sql ' ;
21
22
const SESSION_NAME = 'SURVEYFORMAPP ' ;
22
23
const RUNTIME_EXCEPTION_VIEW = 'runtime_exception.php ' ;
23
24
24
25
protected $ config ;
25
26
protected $ dsn ;
27
+ protected $ driver = 'sqlite ' ;
26
28
protected $ pdo ;
27
29
protected $ viewVariables = [];
28
30
@@ -227,19 +229,37 @@ protected function openDatabase()
227
229
if (! isset ($ databaseConfig ['dsn ' ])) {
228
230
throw new RuntimeException ("Database config parameter 'dsn' not found in config file: " . self ::DSN_CONFIG_FILE );
229
231
}
230
- if (! isset ($ databaseConfig ['filename ' ])) {
231
- throw new RuntimeException ("Database config parameter 'filename' not found in config file: " . self ::DSN_CONFIG_FILE );
232
- }
233
- if (! is_writable (dirname ($ databaseConfig ['filename ' ]))) {
234
- throw new RuntimeException ('Data directory not writable by web server: ' . dirname ($ databaseConfig ['filename ' ]) . '/ ' );
235
- }
236
- if (! is_writable (dirname ($ databaseConfig ['filename ' ])) || (file_exists ($ databaseConfig ['filename ' ]) && ! is_writable ($ databaseConfig ['filename ' ]))) {
237
- throw new RuntimeException ('Database file not writable by web server: ' . $ databaseConfig ['filename ' ]);
232
+
233
+ $ username = null ;
234
+ $ password = null ;
235
+
236
+ if (preg_match ('/^mysql/ ' , $ databaseConfig ['dsn ' ])) {
237
+ $ this ->driver = 'mysql ' ;
238
+ if (! isset ($ databaseConfig ['username ' ])) {
239
+ throw new RuntimeException ("Database config parameter 'username' not found in config file: " . self ::DSN_CONFIG_FILE );
240
+ }
241
+ if (! isset ($ databaseConfig ['password ' ])) {
242
+ throw new RuntimeException ("Database config parameter 'password' not found in config file: " . self ::DSN_CONFIG_FILE );
243
+ }
244
+ $ username = $ databaseConfig ['username ' ];
245
+ $ password = $ databaseConfig ['password ' ];
246
+ } else {
247
+ if (! isset ($ databaseConfig ['filename ' ])) {
248
+ throw new RuntimeException ("Database config parameter 'filename' not found in config file: " . self ::DSN_CONFIG_FILE );
249
+ }
250
+ if (! is_writable (dirname ($ databaseConfig ['filename ' ]))) {
251
+ throw new RuntimeException ('Data directory not writable by web server: ' . dirname ($ databaseConfig ['filename ' ]) . '/ ' );
252
+ }
253
+ if (! is_writable (dirname ($ databaseConfig ['filename ' ])) || (file_exists ($ databaseConfig ['filename ' ]) && ! is_writable ($ databaseConfig ['filename ' ]))) {
254
+ throw new RuntimeException ('Database file not writable by web server: ' . $ databaseConfig ['filename ' ]);
255
+ }
238
256
}
239
257
try {
240
- $ this ->pdo = new PDO ($ databaseConfig ['dsn ' ]);
258
+ $ this ->pdo = new PDO ($ databaseConfig ['dsn ' ], $ username , $ password );
241
259
$ this ->pdo ->setAttribute (PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
242
- $ this ->pdo ->exec ('PRAGMA foreign_keys = ON; ' );
260
+ if ($ this ->driver == 'sqlite ' ) {
261
+ $ this ->pdo ->exec ('PRAGMA foreign_keys = ON; ' );
262
+ }
243
263
244
264
if (! $ this ->databaseTablesCreated ()) {
245
265
$ this ->createDatabaseTables ();
@@ -254,11 +274,16 @@ protected function openDatabase()
254
274
*/
255
275
protected function createDatabaseTables ()
256
276
{
257
- if (! file_exists (self ::DATABASE_SCHEMA_FILE )) {
258
- throw new RuntimeException ('Database schema file not found: ' . self ::DATABASE_SCHEMA_FILE );
277
+ $ schemaFile = self ::DATABASE_SCHEMA_FILE ;
278
+ if ($ this ->driver == 'mysql ' ) {
279
+ $ schemaFile = self ::DATABASE_SCHEMA_FILE_MYSQL ;
280
+ }
281
+
282
+ if (! file_exists ($ schemaFile )) {
283
+ throw new RuntimeException ('Database schema file not found: ' . $ schemaFile );
259
284
}
260
285
// Create tables
261
- $ sql = file_get_contents (self :: DATABASE_SCHEMA_FILE );
286
+ $ sql = file_get_contents ($ schemaFile );
262
287
$ this ->pdo ->exec ($ sql );
263
288
264
289
// Load initial data
@@ -271,13 +296,25 @@ protected function createDatabaseTables()
271
296
*/
272
297
protected function databaseTablesCreated ()
273
298
{
274
- $ sql = "select count(*) from sqlite_master where type='table' and name='login' " ;
299
+ if ($ this ->driver == 'mysql ' ) {
300
+ $ sql = "show tables like 'login' " ;
301
+ } else {
302
+ $ sql = "select count(*) from sqlite_master where type='table' and name='login' " ;
303
+ }
275
304
$ stmt = $ this ->pdo ->prepare ($ sql );
276
305
$ stmt ->execute ();
277
306
$ stmt ->setFetchMode (PDO ::FETCH_NUM );
278
307
if ($ row = $ stmt ->fetch ()) {
279
- if ($ row [0 ] == 1 ) {
280
- return true ;
308
+ if ($ this ->driver == 'mysql ' ) {
309
+ // mysql
310
+ if ($ row [0 ] == 'login ' ) {
311
+ return true ;
312
+ }
313
+ } else {
314
+ // sqlite3
315
+ if ($ row [0 ] == 1 ) {
316
+ return true ;
317
+ }
281
318
}
282
319
}
283
320
0 commit comments