-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoftMapper.php
306 lines (260 loc) · 9.01 KB
/
SoftMapper.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/**
* Created by PhpStorm.
* User: Mohammed Elamin
* Date: 12/12/2018
* Time: 06:33
*/
require_once 'env.php';
class SoftMapper
{
/*PDO PHP DATA ----> instance for data base operations*/
/**
* @var PDO
*/
private $pdo;
/*specify the initial query for complex one*/
/**
* @var string
*/
private $builded_query = 'SELECT * FROM ';
/*specify selected columns selected by select fuction */
/**
* @var string
*/
private $selected_columns = '';
/*specify value for columns that used by prepared statement*/
/**
* @var
*/
private $query_columns_place_holder_array;
/*specify model for data */
/**
* @var
*/
public $table_name;
/*specify entity columns*/
/**
* @var
*/
public $columns;
/**
* @var bool
*/
private $update_switch = false;
/**
* DataBaseHandler constructor.
* @param string $host
* @param $dbname
* @param $user
* @param $password
*/
public function __construct()
{
/*data source name for php to know about data base tech and user data */
$dsn = 'mysql:host=' . host . ';dbname=' . dbname;
/*PDO initialization*/
$this->pdo = new PDO($dsn, user, password);
}
/**
* @return $this
* TODO:get all rows from table
*/
public function all()
{
$this->builded_query .= $this->table_name;
return $this;
}
/**
* @param $table
* @param array $columns
* @param string $aggregate_function
* @param string $aggregate_parameter
* @return $this
* TODO:select raws from table with aggregate functions as option parameter
*/
public function select($columns = array(), $aggregate_function = '', $aggregate_parameter = null)
{
// reset old query for selection
$this->builded_query = '';
// update with select
if (isset($columns)) {
$this->selected_columns .= implode(',', $columns);
if (isset($aggregate_function))
$this->builded_query .= 'SELECT' . "\t" . $this->selected_columns . ",\t" . $aggregate_function . '(' . $aggregate_parameter . ')' . "\t FROM \t" . $this->table_name;
else
$this->builded_query .= 'SELECT' . "\t" . $this->selected_columns . "\t FROM \t" . $this->table_name;
} else {
if (isset($aggregate_function))
$this->builded_query .= 'SELECT' . "\t" . $aggregate_function . '(' . $aggregate_parameter . ')' . "\t FROM \t" . $this->table_name;
}
$this->builded_query;
return $this;
}
/**
* @return array
* TODO:THIS METHOD FALLOWED WITH MOST METHODS INCLUDED HERE TO EXECUTE GENERATED QUERY
*/
public function getAll()
{
$stmt = $this->pdo->prepare($this->builded_query);
$stmt->execute($this->query_columns_place_holder_array);
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
return $result;
}
/**
* @return mixed
*/
public function get()
{
$stmt = $this->pdo->prepare($this->builded_query);
$stmt->execute($this->query_columns_place_holder_array);
$result = $stmt->fetch(PDO::FETCH_OBJ);
return $result;
}
/**
* @return bool
*/
public function execute()
{
$stmt = $this->pdo->prepare($this->builded_query);
$result = $stmt->execute($this->query_columns_place_holder_array);
return $result;
}
/**
* @param array $conditions
* @return $this
* TODO:add many or once condition for your built query
*/
public function where($conditions = array())
{
$this->query_columns_place_holder_array = array();
$where_query = "\t" . 'where' . "\t";
for ($i = 0; $i < sizeof($conditions); $i++) {
$values = $conditions[$i];
$index = $values[0];
$this->query_columns_place_holder_array[$index] = $values[2];
if (isset($values[3]))
$where_query .= "\t" . $values[0] . $values[1] . ' :' . $values[0] . "\t" . $values[3];
else
$where_query .= "\t" . $values[0] . $values[1] . ' :' . $values[0];
}
$this->builded_query .= $where_query;
$keys = array_keys($this->columns);
$values = array_values($this->columns);
if ($this->update_switch) {
// add update data to be added to prepared statment
for ($i = 0; $i < sizeof($this->columns); $i++)
$this->query_columns_place_holder_array['UP_' . $keys[$i]] = $values[$i];
print_r($this->query_columns_place_holder_array);
}
return $this;
}
/**
* @param $column_name
* @param $ordering_type
* @return $this
* TODO:add condition to query to be ordered with [ascending or descending]order
*/
public
function orderBy($column_name, $ordering_type)
{
$this->builded_query .= "\t" . 'order by' . "\t" . $column_name . "\t" . $ordering_type;
return $this;
}
/**
* @param $limitation_number
* @return $this
* TODO:limit numbers of selected row
*/
public
function limit($limitation_number)
{
$this->builded_query .= "\t" . 'LIMIT' . "\t" . $limitation_number;
return $this;
}
/**
* @param $grouper
* @return $this
* TODO:used with aggregated function to group some of raw and process functions on it
*/
public
function groupBy($grouper)
{
$this->builded_query .= "\t group by \t" . $grouper;
// echo $this->builded_query;
return $this;
}
/**
* @param array $conditions
* @return $this
* TODO:used as condition for aggregates functions
*/
public
function having($conditions = array())
{
$this->query_columns_place_holder_array = array();
$having_query = "\t" . 'HAVING' . "\t";
for ($i = 0; $i < sizeof($conditions); $i++) {
$values = $conditions[$i];
$index = $values[0];
$this->query_columns_place_holder_array[$index] = $values[2];
if (isset($values[3]))
$having_query .= "\t" . $values[0] . $values[1] . ' :' . $values[0] . "\t" . $values[3];
else
$having_query .= "\t" . $values[0] . $values[1] . ' :' . $values[0];
}
$this->builded_query .= $having_query;
return $this;
}
//
/**
*TODO:insert data
*/
public function insert()
{
$columns = array_keys($this->columns);
$values_indicator = array();
for ($i = 0; $i < sizeof($columns); $i++) {
array_push($values_indicator, ':' . $columns[$i]);
}
$values_pointers = implode(',', $values_indicator);
$columns_name = implode(',', $columns);
$query = 'INSERT INTO ' . $this->table_name . "\t" . '(' . $columns_name . ') ' . "values (" . $values_pointers . ')';
$stmt_insert = $this->pdo->prepare($query);
$insert_a_row = $stmt_insert->execute($this->columns);
return $insert_a_row;
}
/**
* @param $key
* @return mixed
*/
public function find($key)
{
return $result = $this->all()->where([['id', '=', $key]])->get();
}
/**
*TODO:delete data
*/
public function delete()
{
$this->builded_query = "DELETE FROM\t" . $this->table_name;
return $this;
}
/**
*TODO:update data
*/
public function update()
{
$keys = array_keys($this->columns);
$values = array_values($this->columns);
$keys_for_prepare = array();
for ($i = 0; $i < sizeof($keys); $i++) {
array_push($keys_for_prepare, $keys[$i] . "=:" . 'UP_' . $keys[$i] . "\t");
}
$query = 'UPDATE ' . $this->table_name . "\t" . 'SET ' . implode(',', $keys_for_prepare);
$this->builded_query = $query;
$this->update_switch = true;
return $this;
}
}