-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlexSQL.php
More file actions
256 lines (220 loc) · 6.84 KB
/
FlexSQL.php
File metadata and controls
256 lines (220 loc) · 6.84 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
class FlexSQL
{
private $pdo;
private $stmt;
private $joinClauses = [];
private $groupClauses = [];
private $orderClauses = [];
private $limitClause = '';
public function __construct($host, $db, $user, $pass)
{
$dsn = "mysql:host=$host;dbname=$db;charset=utf8mb4";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
// PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, // Object olarak çekmek için aşağıdaki kodu kapatıp bu kodu açmanız gerekli.
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$this->pdo = new PDO($dsn, $user, $pass, $options);
}
public function query($query, $params = [])
{
$this->stmt = $this->pdo->prepare($query);
$this->stmt->execute($params);
return $this;
}
public function delete($table, $where = "", $params = [])
{
$query = "DELETE FROM $table";
if (!empty($where)) {
$query .= " WHERE $where";
}
$this->stmt = $this->pdo->prepare($query);
$this->stmt->execute($params);
return $this;
}
public function select($table, $columns = "*", $where = "", $params = [], $limit = "")
{
$query = "SELECT $columns FROM $table";
if (!empty($where)) {
$query .= " WHERE $where";
}
if (!empty($limit)) {
$query .= " LIMIT $limit";
}
$this->stmt = $this->pdo->prepare($query);
$this->stmt->execute($params);
return $this;
}
public function insert($table, $data)
{
$columns = implode(", ", array_keys($data));
$values = implode(", ", array_fill(0, count($data), "?"));
$query = "INSERT INTO $table ($columns) VALUES ($values)";
$this->stmt = $this->pdo->prepare($query);
$this->stmt->execute(array_values($data));
return $this;
}
public function update($table, $data, $where = "", $params = [])
{
$setClause = implode(", ", array_map(function ($key) {
return "$key=?";
}, array_keys($data)));
$query = "UPDATE $table SET $setClause";
if (!empty($where)) {
$query .= " WHERE $where";
}
$params = array_merge(array_values($data), $params);
$this->stmt = $this->pdo->prepare($query);
$this->stmt->execute($params);
return $this;
}
public function fetch()
{
return $this->stmt->fetch();
}
public function fetchAll()
{
return $this->stmt->fetchAll();
}
public function rowCount()
{
return $this->stmt->rowCount();
}
public function lastInsertId()
{
return $this->pdo->lastInsertId();
}
public function beginTransaction()
{
return $this->pdo->beginTransaction();
}
public function commit()
{
return $this->pdo->commit();
}
public function rollBack()
{
return $this->pdo->rollBack();
}
public function quote($string)
{
return $this->pdo->quote($string);
}
private function resetQueryState()
{
$this->joinClauses = [];
$this->groupClauses = [];
$this->orderClauses = [];
$this->limitClause = '';
}
private function buildJoinClause()
{
$joinClause = '';
foreach ($this->joinClauses as $join) {
$joinClause .= " {$join['type']} JOIN {$join['table']} ON {$join['on']}";
}
return $joinClause;
}
public function join($table, $on, $type = 'INNER')
{
$this->joinClauses[] = [
'table' => $table,
'on' => $on,
'type' => $type,
];
return $this;
}
public function leftJoin($table, $on)
{
return $this->join($table, $on, 'LEFT');
}
public function rightJoin($table, $on)
{
return $this->join($table, $on, 'RIGHT');
}
public function fullOuterJoin($table, $on)
{
return $this->join($table, $on, 'FULL OUTER');
}
public function groupBy($columns)
{
if (!is_array($columns)) {
$columns = [$columns];
}
$this->groupClauses['columns'] = implode(',', $columns);
return $this;
}
public function having($having)
{
$this->groupClauses['having'] = $having;
return $this;
}
public function orderBy($table, $columns, $order = 'ASC')
{
if (!is_array($columns)) {
$columns = [$columns];
}
$order = strtoupper($order) === 'DESC' ? 'DESC' : 'ASC';
$orderBy = implode(', ', $columns);
$query = "SELECT * FROM $table ORDER BY $orderBy $order";
return $this->query($query)->fetchAll();
}
public function createTable($table, $columns)
{
$query = "CREATE TABLE $table ($columns)";
return $this->query($query)->rowCount();
}
public function dropTable($table)
{
$query = "DROP TABLE $table";
return $this->query($query)->rowCount();
}
public function updateTable($table, $column, $new_value, $where)
{
$query = "UPDATE $table SET $column = ? WHERE $where";
$params = [$new_value];
return $this->query($query, $params)->rowCount();
}
public function limit($limit)
{
$this->limitClause = "LIMIT " . intval($limit);
return $this;
}
public function mdelete($table, $where, $whereIn = null)
{
$query = "DELETE FROM $table WHERE $where";
if ($whereIn !== null && is_array($whereIn) && count($whereIn) > 1) { // $whereIn değişkeni kontrol ediliyor.
$whereInClause = implode(",", array_fill(0, count($whereIn[1]), "?"));
$query .= " AND $whereIn[0] IN ($whereInClause)";
}
$this->stmt = $this->pdo->prepare($query);
if ($whereIn !== null && is_array($whereIn) && count($whereIn) > 1) { // $whereIn değişkeni kontrol ediliyor.
$params = $whereIn[1];
if (isset($whereIn[2])) {
$params = array_merge($params, $whereIn[2]);
}
$this->stmt->execute($params);
} else {
$this->stmt->execute();
}
return $this;
}
public function minsert($table, $data, $columns = null)
{
if ($columns === null) {
$columns = array_keys(reset($data));
} elseif (is_string($columns)) {
$columns = explode(',', $columns);
}
$values = implode(", ", array_fill(0, count($columns), "?"));
$columns = implode(", ", $columns);
$query = "INSERT INTO $table ($columns) VALUES ($values)";
$this->stmt = $this->pdo->prepare($query);
foreach ($data as $row) {
$this->stmt->execute(array_values($row));
}
return $this;
}
}