-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.php
157 lines (125 loc) · 3.91 KB
/
Database.php
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
class Database
{
private static $instance = null;
private $pdo, $query, $error = false, $results, $count;
private function __construct() {
// try {
// $this->pdo = new PDO("mysql:host=" . Config::get('mysql.host') . ";dbname=" . Config::get('mysql.database'), Config::get('mysql.username'), Config::get('mysql.password'));
// } catch (PDOException $exception) {
// die($exception->getMessage());
// }
try {
if (Config::get('dbdriver') == 'sqlite') {
$this->pdo = new PDO("sqlite:" . Config::get('sqlite.database'), '', '', [
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ
]);
} else if (Config::get('dbdriver') == 'mysql') {
$this->pdo = new PDO("mysql:host=" . Config::get('mysql.host') . ";dbname=" . Config::get('mysql.database') . "; charset=utf8", Config::get('mysql.username'), Config::get('mysql.password'), [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ
]);
}
} catch (PDOException $exception) {
die($exception->getMessage());
}
}
public static function getInstance() {
if(!isset(self::$instance)) {
self::$instance = new Database;
}
return self::$instance;
}
public function query($sql, $params = []) {
$this->error = false;
$this->query = $this->pdo->prepare($sql);
if(count($params)) {
$i = 1;
foreach($params as $param) {
$this->query->bindValue($i, $param);
$i++;
}
}
if(!$this->query->execute()) {
$this->error = true;
} else {
$this->results = $this->query->fetchAll(PDO::FETCH_OBJ);
$this->count = count($this->results);
}
return $this;
}
public function error() {
return $this->error;
}
public function results() {
//var_dump($this->results);
return $this->results;
}
public function count() {
return $this->count;
}
public function get($table, $where = []) {
return $this->action('SELECT *', $table, $where);
}
public function getAll($table) {
return $this->action('SELECT *', $table, ['1']);
}
public function getFields($fields, $table, $where = []) {
if (is_array($fields)) {
return $this->action('SELECT `' . implode('`, `', $fields) . '`', $table, $where);
} else if (is_string($fields)) {
return $this->action("SELECT `{$fields}`", $table, $where);
}
}
public function delete($table, $where = []) {
return $this->action('DELETE', $table, $where);
}
public function action($action, $table, $where = []) {
//var_dump($action);die;
if(count($where) === 3) {
$operators = ['=', '>', '<', '>=', '<='];
$field = $where[0];
$operator = $where[1];
$value = $where[2];
if(in_array($operator, $operators)) {
$sql = "{$action} FROM `{$table}` WHERE `{$field}` {$operator} ?";
//var_dump('3 - ' . $sql);
if(!$this->query($sql, [$value])->error()) { //true если есть ошибка
return $this;
}
}
} else if (count($where) === 1) {
$sql = "{$action} FROM `{$table}` WHERE {$where[0]}";
//var_dump('1 - '.$sql);
if(!$this->query($sql)->error()) { //true если есть ошибка
return $this;
}
}
return false;
}
public function insert($table, $fields = []) {
$values = '';
foreach($fields as $field) {
$values .= "?,";
}
$val = rtrim($values, ',');
$sql = "INSERT INTO `{$table}` (" . '`' . implode('`, `', array_keys($fields)) . '`' . ") VALUES ({$val})";
if($this->query($sql, $fields)->error()) return false;
return true;
}
public function update($table, $id, $fields = []) {
$set = '';
foreach($fields as $key => $field) {
$set .= "`{$key}` = ?,"; // username = ?, password = ?,
}
$set = rtrim($set, ','); // username = ?, password = ?
$sql = "UPDATE `{$table}` SET {$set} WHERE `id` = {$id}";
if($this->query($sql, $fields)->error()) return false;
return true;
}
public function first() {
return $this->results()[0];
}
}