@@ -17,12 +17,14 @@ abstract class Controller
1717{
1818 const DSN_CONFIG_FILE = 'config/Database.ini ' ;
1919 const DATABASE_SCHEMA_FILE = 'data/schema.sql ' ;
20+ const DATABASE_SCHEMA_FILE_MYSQL = 'data/schema_mysql.sql ' ;
2021 const INITIAL_DATA_FILE = 'data/initial_data.sql ' ;
2122 const SESSION_NAME = 'SURVEYFORMAPP ' ;
2223 const RUNTIME_EXCEPTION_VIEW = 'runtime_exception.php ' ;
2324
2425 protected $ config ;
2526 protected $ dsn ;
27+ protected $ driver = 'sqlite ' ;
2628 protected $ pdo ;
2729 protected $ viewVariables = [];
2830
@@ -227,19 +229,37 @@ protected function openDatabase()
227229 if (! isset ($ databaseConfig ['dsn ' ])) {
228230 throw new RuntimeException ("Database config parameter 'dsn' not found in config file: " . self ::DSN_CONFIG_FILE );
229231 }
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+ }
238256 }
239257 try {
240- $ this ->pdo = new PDO ($ databaseConfig ['dsn ' ]);
258+ $ this ->pdo = new PDO ($ databaseConfig ['dsn ' ], $ username , $ password );
241259 $ 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+ }
243263
244264 if (! $ this ->databaseTablesCreated ()) {
245265 $ this ->createDatabaseTables ();
@@ -254,11 +274,16 @@ protected function openDatabase()
254274 */
255275 protected function createDatabaseTables ()
256276 {
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 );
259284 }
260285 // Create tables
261- $ sql = file_get_contents (self :: DATABASE_SCHEMA_FILE );
286+ $ sql = file_get_contents ($ schemaFile );
262287 $ this ->pdo ->exec ($ sql );
263288
264289 // Load initial data
@@ -271,13 +296,25 @@ protected function createDatabaseTables()
271296 */
272297 protected function databaseTablesCreated ()
273298 {
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+ }
275304 $ stmt = $ this ->pdo ->prepare ($ sql );
276305 $ stmt ->execute ();
277306 $ stmt ->setFetchMode (PDO ::FETCH_NUM );
278307 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+ }
281318 }
282319 }
283320
0 commit comments