Skip to content

Commit 6d61b99

Browse files
committed
Add PostgreSQL Support
1 parent 1f468dc commit 6d61b99

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

config/Database.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ filename = "data/database.sqlite3"
66
;dsn = "mysql:host=localhost;dbname=php_survey_builder"
77
;username = "user"
88
;password = "yourpassword"
9+
10+
; postgresql
11+
;dsn = "pgsql:host=localhost;dbname=php_survey_builder"
12+
;username = "youruser"
13+
;password = "yourpassword"

controllers/Controller.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ abstract class Controller
1818
const DSN_CONFIG_FILE = 'config/Database.ini';
1919
const DATABASE_SCHEMA_FILE = 'data/schema.sql';
2020
const DATABASE_SCHEMA_FILE_MYSQL = 'data/schema_mysql.sql';
21+
const DATABASE_SCHEMA_FILE_POSTGRESQL = 'data/schema_postgresql.sql';
2122
const INITIAL_DATA_FILE = 'data/initial_data.sql';
2223
const SESSION_NAME = 'SURVEYFORMAPP';
2324
const RUNTIME_EXCEPTION_VIEW = 'runtime_exception.php';
@@ -233,8 +234,8 @@ protected function openDatabase()
233234
$username = null;
234235
$password = null;
235236

236-
if (preg_match('/^mysql/', $databaseConfig['dsn'])) {
237-
$this->driver = 'mysql';
237+
if (preg_match('/^(mysql|pgsql)/', $databaseConfig['dsn'], $matches)) {
238+
$this->driver = $matches[1];
238239
if (! isset($databaseConfig['username'])) {
239240
throw new RuntimeException("Database config parameter 'username' not found in config file: " . self::DSN_CONFIG_FILE);
240241
}
@@ -277,6 +278,8 @@ protected function createDatabaseTables()
277278
$schemaFile = self::DATABASE_SCHEMA_FILE;
278279
if ($this->driver == 'mysql') {
279280
$schemaFile = self::DATABASE_SCHEMA_FILE_MYSQL;
281+
} elseif ($this->driver == 'pgsql') {
282+
$schemaFile = self::DATABASE_SCHEMA_FILE_POSTGRESQL;
280283
}
281284

282285
if (! file_exists($schemaFile)) {
@@ -298,6 +301,8 @@ protected function databaseTablesCreated()
298301
{
299302
if ($this->driver == 'mysql') {
300303
$sql = "show tables like 'login'";
304+
} elseif ($this->driver == 'pgsql') {
305+
$sql = "select table_name from information_schema.tables where table_schema='public' and table_name='login'";
301306
} else {
302307
$sql = "select count(*) from sqlite_master where type='table' and name='login'";
303308
}
@@ -310,6 +315,11 @@ protected function databaseTablesCreated()
310315
if ($row[0] == 'login') {
311316
return true;
312317
}
318+
} elseif ($this->driver == 'pgsql') {
319+
// pgsql
320+
if ($row[0] == 'login') {
321+
return true;
322+
}
313323
} else {
314324
// sqlite3
315325
if ($row[0] == 1) {

data/schema_postgresql.sql

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

models/Survey.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,20 @@ public function getQuestions(PDO $pdo)
4040
*/
4141
public function getSurveyResponses(PDO $pdo)
4242
{
43+
$driver = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
44+
$groupConcatSql = 'group_concat';
45+
if ($driver == 'pgsql') {
46+
$groupConcatSql = 'string_agg';
47+
}
48+
4349
if (! empty($this->survey_id)) {
4450
if (empty($this->questions)) {
4551
$this->getQuestions($pdo);
4652
}
4753

4854
$questionSubSelects = [];
4955
foreach ($this->questions as $question) {
50-
$questionSubSelects[] = "(select group_concat(answer_value, ', ') from survey_answer sa
56+
$questionSubSelects[] = "(select $groupConcatSql(answer_value, ', ') from survey_answer sa
5157
where sa.survey_response_id = sr.survey_response_id and
5258
sa.question_id = :question_id_{$question->question_id}) as question_{$question->question_id}";
5359
$params["question_id_{$question->question_id}"] = $question->question_id;

0 commit comments

Comments
 (0)