Skip to content

Commit 1f468dc

Browse files
committed
Add MySQL Support
1 parent 41a9197 commit 1f468dc

File tree

4 files changed

+101
-17
lines changed

4 files changed

+101
-17
lines changed

config/Database.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
; sqlite3
12
dsn = "sqlite:data/database.sqlite3"
23
filename = "data/database.sqlite3"
34

5+
; mysql
6+
;dsn = "mysql:host=localhost;dbname=php_survey_builder"
7+
;username = "user"
8+
;password = "yourpassword"

controllers/Controller.php

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

data/schema.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ CREATE TABLE survey_response (
3939
CREATE TABLE survey_answer (
4040
survey_answer_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
4141
survey_response_id INTEGER NOT NULL,
42-
question_id TEXT NOT NULL,
42+
question_id INTEGER NOT NULL,
4343
answer_value TEXT NOT NULL,
4444
FOREIGN KEY (survey_response_id) REFERENCES survey_response (survey_response_id) ON UPDATE CASCADE ON DELETE CASCADE,
4545
FOREIGN KEY (question_id) REFERENCES question (question_id) ON UPDATE CASCADE ON DELETE CASCADE

data/schema_mysql.sql

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
CREATE TABLE login ( login_id INTEGER NOT NULL PRIMARY KEY
2+
AUTO_INCREMENT, email TEXT NOT NULL, password TEXT NOT NULL,
3+
first_name TEXT NOT NULL, last_name TEXT NOT NULL );
4+
5+
CREATE TABLE survey (
6+
survey_id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
7+
survey_name TEXT NOT NULL
8+
);
9+
10+
CREATE TABLE question (
11+
question_id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
12+
survey_id INTEGER NOT NULL,
13+
question_type TEXT,
14+
question_text TEXT,
15+
is_required INTEGER,
16+
question_order INTEGER,
17+
FOREIGN KEY (survey_id) REFERENCES survey (survey_id) ON UPDATE CASCADE ON DELETE CASCADE
18+
);
19+
20+
CREATE TABLE choice (
21+
choice_id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
22+
question_id INTEGER NOT NULL,
23+
choice_text TEXT,
24+
choice_order INTEGER,
25+
FOREIGN KEY (question_id) REFERENCES question (question_id) ON UPDATE CASCADE ON DELETE CASCADE
26+
);
27+
28+
CREATE TABLE survey_response (
29+
survey_response_id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
30+
survey_id INTEGER NOT NULL,
31+
time_taken TEXT,
32+
FOREIGN KEY (survey_id) REFERENCES survey (survey_id) ON UPDATE CASCADE ON DELETE CASCADE
33+
);
34+
35+
CREATE TABLE survey_answer (
36+
survey_answer_id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
37+
survey_response_id INTEGER NOT NULL,
38+
question_id INTEGER NOT NULL,
39+
answer_value TEXT NOT NULL,
40+
FOREIGN KEY (survey_response_id) REFERENCES survey_response (survey_response_id) ON UPDATE CASCADE ON DELETE CASCADE,
41+
FOREIGN KEY (question_id) REFERENCES question (question_id) ON UPDATE CASCADE ON DELETE CASCADE
42+
);

0 commit comments

Comments
 (0)