-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDb.php
191 lines (148 loc) · 4.04 KB
/
Db.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
class Db {
private $db;
public function __construct($dbhost, $dbuser, $dbpw, $dbname) {
$this->db = new PDO(
'mysql:host=' . $dbhost . ';dbname='. $dbname . ';set=utf8mb4',
$dbuser,
$dbpw
);
}
/***To retrieve results from queries :
foreach($result as $res) {
var_dump($res);
}
***/
public function select($tableName, $rows = NULL, $conditions = NULL, $orderBy = NULL, $limit = NULL) {
$query = $this->selectQuery($tableName, $rows);
$query .= $this->whereQuery($conditions);
$query .= $this->orderByQuery($orderBy);
$query .= $this->limitQuery($limit);
/*var_dump($query);*/
return $this->execute($query);
}
public function insert($tableName, $values) {
// ROWS SELECTION
$length = count($values);
$rows = "";
$insertedValues = "";
foreach ($values as $key => $value) {
$rows .= "$key";
var_dump(gettype($value));
$insertedValues .= (is_string($value)) ? '"'.$value.'"' : "$value" ;
if(--$length) { // if is not last iteration
$rows .= ",";
$insertedValues .= ",";
}
}
$query = "INSERT INTO $tableName ($rows) VALUES ($insertedValues)";
$this->execute($query);
}
public function update($tableName, $changes, $conditions) {
$query = "UPDATE $tableName SET ";
// UPDATE
$length = count($changes);
foreach ($changes as $key => $value) {
$query .= "$key=" . '"' . "$value" . '" ';
if(--$length) { // if is not last iteration
$query .= ", ";
}
}
// WHERE
$query .= $this->whereQuery($conditions);
/*var_dump($query);*/
$this->execute($query);
}
public function delete($tableName, $conditions) {
$query = "DELETE FROM $tableName " . $this->whereQuery($conditions);
$this->execute($query);
}
// Precize which table !
public function leftJoin($tableName, $joinTable, $joins, $rows = NULL, $conditions = NULL, $orderBy = NULL, $limit = NULL) {
$query = $this->selectQuery($tableName, $rows);
$query .= "LEFT JOIN $joinTable ON ";
$length = count($joins);
foreach ($joins as $key => $value) {
$query .= "$tableName.$key=$joinTable.$value ";
if(--$length) { // if is not last iteration
$query .= "AND ";
}
}
$query .= $this->whereQuery($conditions);
$query .= $this->orderByQuery($orderBy);
$query .= $this->limitQuery($limit);
/*var_dump($query);*/
return $this->execute($query);
}
private function execute($query) {
return $this->db->query($query);
}
private function selectQuery($tableName, $rows) {
$query = "SELECT ";
// ROWS SELECTION
if(isset($rows)) {
$length = count($rows);
foreach ($rows as $row) {
$query .= $row;
if(--$length) { // if is not last iteration
$query .= ",";
}
}
} else {
$query .= "* ";
}
// TABLENAME
$query .= " FROM $tableName ";
return $query;
}
// @TODO to do better
// first OR second => array(id => array(first, second))
// first AND second => array(x => first, x => second)
private function whereQuery($conditions) {
$query = "";
if(isset($conditions)) {
$query = "WHERE ";
if(isset($conditions)) {
$length = count($conditions);
foreach ($conditions as $key => $value) {
// check whether there is a AND or OR to process @TODO MAKE BETTER
$innerLength = count($value);
if($innerLength > 1) {
foreach ($value as $v) {
$query .= "$key=" . '"' . "$v" . '" ';
if(--$innerLength) { // if is not last iteration
$query .= "OR ";
}
--$length;
}
}
else {
/*var_dump($value);*/
$query .= (strcmp($value[0],"IS NOT NULL")==0) ? "$key IS NOT NULL " : "$key=" . '"' . "$value[0]" . '" ';
if(--$length) { // if is not last iteration
$query .= "AND ";
}
}
}
}
}
/*var_dump($query);*/
return $query;
}
private function orderByQuery($orderBy = NULL) {
$query = "";
if(isset($orderBy)) {
$query .= "ORDER BY $orderBy ";
}
return $query;
}
private function limitQuery($limit = NULL) {
$query = "";
if(isset($limit)) {
$query .= "LIMIT $limit ";
} else {
/*$query .= "LIMIT 4";*/
}
return $query;
}
}