-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbinterface.php
More file actions
105 lines (80 loc) · 3.13 KB
/
dbinterface.php
File metadata and controls
105 lines (80 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
* Created by PhpStorm.
* User: mzalm
* Date: 2018/04/18
* Time: 9:41
*/
require_once "dbconfig.php";
class dbInterface
{
private static $instance = NULL;
private $dbConn;
private $projectsList = 'projects';
private $logFields = "id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, log_date TIMESTAMP, log_data LONGTEXT NOT NULL";
private $projectsListFields = "id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, proj_name VARCHAR(30) NOT NULL";
private function __construct() {
try {
$this->dbConn = new PDO("mysql:host=". DB_SERVER . ";dbname=" . DB_NAME, DB_USER, DB_PASSWD);
$this->dbConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
echo "Connection failed: " . $e->getMessage();
die();
}
$this->assertTable($this->projectsList, $this->projectsListFields);
}
private function assertTable ($name, $fields) {
$stmt = "CREATE TABLE IF NOT EXISTS " . $name . " ( " . $fields . " ) CHARACTER SET utf8 COLLATE utf8_unicode_ci";
$query = $this->dbConn->prepare($stmt);
$query->execute();
}
public static function getInstance() {
static $instance = null;
if (self::$instance === null){
$instance = new dbinterface();
}
return $instance;
}
public function makeLog($table, $data) {
$this->assertTable($table, $this->logFields);
$stmt = "INSERT INTO " . $table . " (log_data) VALUES (:data)";
$query = $this->dbConn->prepare($stmt);
$query->bindParam(':data',$data );
$query->execute();
}
public function pullLog($table) {
$this->assertTable($table, $this->logFields);
$query = $this->dbConn->prepare("SELECT log_date, log_data FROM " . $table);
$query->execute();
$query->setFetchMode(PDO::FETCH_ASSOC);
$result = $query->fetchAll();
return $result;
}
public function makeProject($project) {
$this->assertTable($this->projectsList, $this->projectsListFields);
$stmt = "INSERT INTO " . $this->projectsList . " (proj_name) VALUES (:data)";
$query = $this->dbConn->prepare($stmt);
$query->bindParam(':data', $project);
$query->execute();
$this->assertTable($project, $this->logFields);
}
public function killProject($project) {
$this->assertTable($this->projectsList, $this->projectsListFields);
$stmt = "DROP TABLE " . $project;
$query = $this->dbConn->prepare($stmt);
$query->execute();
$stmt = "DELETE FROM " . $this->projectsList . " WHERE proj_name=:project";
$query = $this->dbConn->prepare($stmt);
$query->bindParam(':project', $project);
$query->execute();
}
public function pullProjects() {
$this->assertTable($this->projectsList, $this->projectsListFields);
$query = $this->dbConn->prepare("SELECT proj_name FROM " . $this->projectsList);
$query->execute();
$query->setFetchMode(PDO::FETCH_ASSOC);
$result = $query->fetchAll();
return $result;
}
}