@@ -32,7 +32,7 @@ |
||
32 | 32 | { |
33 | 33 | $ar = []; |
34 | 34 | if(!count($whereQuery)){ |
35 | - return $ar; |
|
35 | + return $ar; |
|
36 | 36 | } |
37 | 37 | |
38 | 38 | foreach ($whereQuery as $where) { |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | class WhereQueryParser |
14 | 14 | { |
15 | 15 | |
16 | - protected function prepareArrayForWhere($bindKey, $bindVal = null){ |
|
16 | + protected function prepareArrayForWhere($bindKey, $bindVal = null) { |
|
17 | 17 | $ar = $conditionAr = []; |
18 | 18 | // expecting a string like 'status = :status' |
19 | 19 | if ($this->checkWherePrepareUsed($bindKey)) { |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | public function parseWhereQuery($whereQuery = []) |
32 | 32 | { |
33 | 33 | $ar = []; |
34 | - if(!count($whereQuery)){ |
|
34 | + if (!count($whereQuery)) { |
|
35 | 35 | return $ar; |
36 | 36 | } |
37 | 37 |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | public function turnObject($destination, $sourceObject) |
29 | 29 | { |
30 | 30 | $destination = new $destination(); |
31 | - if(!is_object($sourceObject)){ |
|
31 | + if (!is_object($sourceObject)) { |
|
32 | 32 | return $destination; |
33 | 33 | } |
34 | 34 | |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | if ($destinationReflection->hasProperty($name)) { |
45 | 45 | $propDest = $destinationReflection->getProperty($name); |
46 | 46 | $propDest->setAccessible(true); |
47 | - $propDest->setValue($destination,$value); |
|
47 | + $propDest->setValue($destination, $value); |
|
48 | 48 | } else { |
49 | 49 | $destination->$name = $value; |
50 | 50 | } |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | public function turnObjects($destination, $data) |
67 | 67 | { |
68 | 68 | $destination = new $destination(); |
69 | - if((!is_null($data)) && count($data)){ |
|
69 | + if ((!is_null($data)) && count($data)) { |
|
70 | 70 | $destination->data = json_decode(json_encode($data, true)); |
71 | 71 | } |
72 | 72 |
@@ -26,14 +26,14 @@ |
||
26 | 26 | * @since v0.0.8 |
27 | 27 | */ |
28 | 28 | public function update( |
29 | - $row, |
|
30 | - $table, |
|
31 | - $where, |
|
32 | - $whereRaw, |
|
33 | - $whereIn, |
|
34 | - $whereNotIn, |
|
35 | - $whereNull, |
|
36 | - $whereNotNull |
|
29 | + $row, |
|
30 | + $table, |
|
31 | + $where, |
|
32 | + $whereRaw, |
|
33 | + $whereIn, |
|
34 | + $whereNotIn, |
|
35 | + $whereNull, |
|
36 | + $whereNotNull |
|
37 | 37 | ): bool |
38 | 38 | { |
39 | 39 | $wqb = new WhereQueryBuilder(); |
@@ -40,14 +40,14 @@ discard block |
||
40 | 40 | $query = "UPDATE ".$table." SET "; |
41 | 41 | $ar = []; |
42 | 42 | |
43 | - foreach($row as $key => $val){ |
|
43 | + foreach ($row as $key => $val) { |
|
44 | 44 | $ar[':'.$key] = $val; |
45 | - $query.= $this->quote($key)." =:".$key.","; |
|
45 | + $query .= $this->quote($key)." =:".$key.","; |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | $query = rtrim($query, ","); |
49 | 49 | |
50 | - try{ |
|
50 | + try { |
|
51 | 51 | $whereQuery = $wqb->buildAllWhereQuery( |
52 | 52 | $where, |
53 | 53 | $whereRaw, |
@@ -56,10 +56,10 @@ discard block |
||
56 | 56 | $whereNull, |
57 | 57 | $whereNotNull |
58 | 58 | ); |
59 | - $query.= " ".join(" ", $whereQuery); |
|
59 | + $query .= " ".join(" ", $whereQuery); |
|
60 | 60 | $stmt = Connection::get()->prepare($this->queryPrefix($query)); |
61 | 61 | $stmt->execute($ar); |
62 | - } catch(Exception $e){ |
|
62 | + } catch (Exception $e) { |
|
63 | 63 | throw new Exception($e->getMessage()); |
64 | 64 | } |
65 | 65 |
@@ -65,7 +65,7 @@ |
||
65 | 65 | } |
66 | 66 | |
67 | 67 | private function checkMultipleInsert($rows = []){ |
68 | - return is_array($rows) && isset($rows[0]) && is_array($rows[0]); |
|
68 | + return is_array($rows) && isset($rows[0]) && is_array($rows[0]); |
|
69 | 69 | } |
70 | 70 | /** |
71 | 71 | * It inserts the new rows |
@@ -16,55 +16,55 @@ discard block |
||
16 | 16 | */ |
17 | 17 | class InsertQueryBuilder extends QueryBuilder |
18 | 18 | { |
19 | - private function buildInsert($table, $obj){ |
|
19 | + private function buildInsert($table, $obj) { |
|
20 | 20 | $qb = new QueryBuilder(); |
21 | 21 | $query = "INSERT INTO ".$table." ("; |
22 | - foreach($obj as $key => $val){ |
|
23 | - $query.= $qb->quote($key).", "; |
|
22 | + foreach ($obj as $key => $val) { |
|
23 | + $query .= $qb->quote($key).", "; |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | $query = rtrim($query, ", "); |
27 | - $query.= ") VALUES "; |
|
27 | + $query .= ") VALUES "; |
|
28 | 28 | |
29 | 29 | return $query; |
30 | 30 | } |
31 | 31 | |
32 | - private function buildInsertPlaceholder($rows){ |
|
32 | + private function buildInsertPlaceholder($rows) { |
|
33 | 33 | $ar = []; |
34 | 34 | $query = "("; |
35 | 35 | |
36 | - foreach($rows as $key => $val){ |
|
36 | + foreach ($rows as $key => $val) { |
|
37 | 37 | $ar[$key] = $val; |
38 | - $query.= ":".$key.", "; |
|
38 | + $query .= ":".$key.", "; |
|
39 | 39 | } |
40 | 40 | |
41 | 41 | $query = rtrim($query, ", "); |
42 | - $query.=") "; |
|
42 | + $query .= ") "; |
|
43 | 43 | |
44 | 44 | return ['query' => $query, 'array' => $ar]; |
45 | 45 | } |
46 | 46 | |
47 | - private function buildInsertPlaceholders($rows){ |
|
47 | + private function buildInsertPlaceholders($rows) { |
|
48 | 48 | $bindAr = []; |
49 | 49 | $query = ""; |
50 | 50 | |
51 | - foreach($rows as $i => $row){ |
|
52 | - $query.="("; |
|
53 | - foreach($row as $key => $val){ |
|
54 | - $param = ":" . $key . $i; |
|
55 | - $query.= $param.", "; |
|
51 | + foreach ($rows as $i => $row) { |
|
52 | + $query .= "("; |
|
53 | + foreach ($row as $key => $val) { |
|
54 | + $param = ":".$key.$i; |
|
55 | + $query .= $param.", "; |
|
56 | 56 | $bindAr[$param] = $val; |
57 | 57 | } |
58 | 58 | |
59 | 59 | $query = rtrim($query, ", "); |
60 | - $query.="), "; |
|
60 | + $query .= "), "; |
|
61 | 61 | } |
62 | 62 | |
63 | 63 | $query = rtrim($query, ", "); |
64 | 64 | return ['query' => $query, 'array' => $bindAr]; |
65 | 65 | } |
66 | 66 | |
67 | - private function checkMultipleInsert($rows = []){ |
|
67 | + private function checkMultipleInsert($rows = []) { |
|
68 | 68 | return is_array($rows) && isset($rows[0]) && is_array($rows[0]); |
69 | 69 | } |
70 | 70 | /** |
@@ -83,21 +83,21 @@ discard block |
||
83 | 83 | $dataToBuild = $rows; |
84 | 84 | $methodToCall = 'buildInsertPlaceholder'; |
85 | 85 | |
86 | - if($this->checkMultipleInsert($rows)){ |
|
86 | + if ($this->checkMultipleInsert($rows)) { |
|
87 | 87 | $dataToBuild = $rows[0]; |
88 | 88 | $methodToCall = 'buildInsertPlaceholders'; |
89 | 89 | } |
90 | 90 | |
91 | 91 | $query = $this->buildInsert($table, $dataToBuild); |
92 | 92 | $dataRet = $this->$methodToCall($rows); |
93 | - $query.= $dataRet['query']; |
|
93 | + $query .= $dataRet['query']; |
|
94 | 94 | $bindAr = $dataRet['array']; |
95 | 95 | |
96 | - try{ |
|
96 | + try { |
|
97 | 97 | $stmt = $db->prepare($this->queryPrefix($query)); |
98 | 98 | |
99 | - if($this->checkMultipleInsert($rows)){ |
|
100 | - foreach($bindAr as $param => $val){ |
|
99 | + if ($this->checkMultipleInsert($rows)) { |
|
100 | + foreach ($bindAr as $param => $val) { |
|
101 | 101 | $stmt->bindValue($param, $val); |
102 | 102 | } |
103 | 103 | |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | |
108 | 108 | $stmt->execute($bindAr); |
109 | 109 | return $db->lastInsertId(); |
110 | - } catch(Exception $e){ |
|
110 | + } catch (Exception $e) { |
|
111 | 111 | throw new Exception($e->getMessage()); |
112 | 112 | } |
113 | 113 | } |
@@ -26,13 +26,13 @@ |
||
26 | 26 | * @since v0.0.8 |
27 | 27 | */ |
28 | 28 | public function delete( |
29 | - $table, |
|
30 | - $where, |
|
31 | - $whereRaw, |
|
32 | - $whereIn, |
|
33 | - $whereNotIn, |
|
34 | - $whereNull, |
|
35 | - $whereNotNull |
|
29 | + $table, |
|
30 | + $where, |
|
31 | + $whereRaw, |
|
32 | + $whereIn, |
|
33 | + $whereNotIn, |
|
34 | + $whereNull, |
|
35 | + $whereNotNull |
|
36 | 36 | ): bool |
37 | 37 | { |
38 | 38 | $wqb = new WhereQueryBuilder(); |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | class PrepareQueryBuilder extends QueryBuilder |
20 | 20 | { |
21 | 21 | private function checkCountable($results = null ): bool{ |
22 | - return (is_array($results) || is_object($results)) && count($results); |
|
22 | + return (is_array($results) || is_object($results)) && count($results); |
|
23 | 23 | } |
24 | 24 | |
25 | 25 | /** |
@@ -31,33 +31,33 @@ discard block |
||
31 | 31 | * @author RN Kushwaha <[email protected]> |
32 | 32 | * @since v0.0.5 |
33 | 33 | */ |
34 | - public function prepare($where, $className, $query, $fetchRows = 'all') |
|
35 | - { |
|
36 | - $wqp = new WhereQueryParser(); |
|
37 | - $util = new Utils(); |
|
38 | - $rows = null; |
|
39 | - |
|
40 | - try { |
|
41 | - $ar = $wqp->parseWhereQuery($where); |
|
42 | - $stmt = Connection::get()->prepare($this->queryPrefix($query)); |
|
43 | - $stmt->execute($ar); |
|
44 | - |
|
45 | - if ($fetchRows == 'first') { |
|
46 | - $results = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
47 | - } else{ |
|
48 | - $results = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
|
49 | - } |
|
50 | - |
|
51 | - if($this->checkCountable($results) ){ |
|
52 | - // now turn this stdClass object to the object type of calling model |
|
53 | - $rows = $util->turnObjects($className, $results); |
|
54 | - } |
|
55 | - |
|
56 | - return $rows; |
|
57 | - } catch (\PDOException $ex) { |
|
58 | - throw new \PDOException($ex->getMessage(), 1); |
|
59 | - } catch (Exception $e) { |
|
60 | - throw new Exception($e->getMessage(), 1); |
|
61 | - } |
|
62 | - } |
|
34 | + public function prepare($where, $className, $query, $fetchRows = 'all') |
|
35 | + { |
|
36 | + $wqp = new WhereQueryParser(); |
|
37 | + $util = new Utils(); |
|
38 | + $rows = null; |
|
39 | + |
|
40 | + try { |
|
41 | + $ar = $wqp->parseWhereQuery($where); |
|
42 | + $stmt = Connection::get()->prepare($this->queryPrefix($query)); |
|
43 | + $stmt->execute($ar); |
|
44 | + |
|
45 | + if ($fetchRows == 'first') { |
|
46 | + $results = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
47 | + } else{ |
|
48 | + $results = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
|
49 | + } |
|
50 | + |
|
51 | + if($this->checkCountable($results) ){ |
|
52 | + // now turn this stdClass object to the object type of calling model |
|
53 | + $rows = $util->turnObjects($className, $results); |
|
54 | + } |
|
55 | + |
|
56 | + return $rows; |
|
57 | + } catch (\PDOException $ex) { |
|
58 | + throw new \PDOException($ex->getMessage(), 1); |
|
59 | + } catch (Exception $e) { |
|
60 | + throw new Exception($e->getMessage(), 1); |
|
61 | + } |
|
62 | + } |
|
63 | 63 | } |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | */ |
19 | 19 | class PrepareQueryBuilder extends QueryBuilder |
20 | 20 | { |
21 | - private function checkCountable($results = null ): bool{ |
|
21 | + private function checkCountable($results = null): bool{ |
|
22 | 22 | return (is_array($results) || is_object($results)) && count($results); |
23 | 23 | } |
24 | 24 | |
@@ -44,11 +44,11 @@ discard block |
||
44 | 44 | |
45 | 45 | if ($fetchRows == 'first') { |
46 | 46 | $results = $stmt->fetch(\PDO::FETCH_ASSOC); |
47 | - } else{ |
|
47 | + } else { |
|
48 | 48 | $results = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
49 | 49 | } |
50 | 50 | |
51 | - if($this->checkCountable($results) ){ |
|
51 | + if ($this->checkCountable($results)) { |
|
52 | 52 | // now turn this stdClass object to the object type of calling model |
53 | 53 | $rows = $util->turnObjects($className, $results); |
54 | 54 | } |
@@ -23,16 +23,16 @@ |
||
23 | 23 | } |
24 | 24 | |
25 | 25 | public function buildQueryStrSingleFromArr($row = []){ |
26 | - $ar = []; |
|
27 | - $query = "UPDATE ".$this->table." SET "; |
|
28 | - foreach($row as $key => $val){ |
|
29 | - $ar[':'.$key] = $val; |
|
30 | - if($key == 'id') continue; |
|
31 | - $query.= $this->qb->quote($key)." =:".$key.","; |
|
32 | - } |
|
33 | - $query = rtrim($query, ","); |
|
26 | + $ar = []; |
|
27 | + $query = "UPDATE ".$this->table." SET "; |
|
28 | + foreach($row as $key => $val){ |
|
29 | + $ar[':'.$key] = $val; |
|
30 | + if($key == 'id') continue; |
|
31 | + $query.= $this->qb->quote($key)." =:".$key.","; |
|
32 | + } |
|
33 | + $query = rtrim($query, ","); |
|
34 | 34 | |
35 | - return ['ar' => $ar, 'query' => $query]; |
|
35 | + return ['ar' => $ar, 'query' => $query]; |
|
36 | 36 | } |
37 | 37 | public function createQuery($row){ |
38 | 38 | $ar = []; |
@@ -22,33 +22,33 @@ discard block |
||
22 | 22 | $this->qb = new QueryBuilder(); |
23 | 23 | } |
24 | 24 | |
25 | - public function buildQueryStrSingleFromArr($row = []){ |
|
25 | + public function buildQueryStrSingleFromArr($row = []) { |
|
26 | 26 | $ar = []; |
27 | 27 | $query = "UPDATE ".$this->table." SET "; |
28 | - foreach($row as $key => $val){ |
|
28 | + foreach ($row as $key => $val) { |
|
29 | 29 | $ar[':'.$key] = $val; |
30 | - if($key == 'id') continue; |
|
31 | - $query.= $this->qb->quote($key)." =:".$key.","; |
|
30 | + if ($key == 'id') continue; |
|
31 | + $query .= $this->qb->quote($key)." =:".$key.","; |
|
32 | 32 | } |
33 | 33 | $query = rtrim($query, ","); |
34 | 34 | |
35 | 35 | return ['ar' => $ar, 'query' => $query]; |
36 | 36 | } |
37 | - public function createQuery($row){ |
|
37 | + public function createQuery($row) { |
|
38 | 38 | $ar = []; |
39 | - if(isset($row) && isset($row->id) && $row->id > 0 ){ |
|
39 | + if (isset($row) && isset($row->id) && $row->id > 0) { |
|
40 | 40 | $mixedData = $this->buildQueryStrSingleFromArr($row); |
41 | - $query= $mixedData['query']." WHERE ".$this->qb->quote('id')."=:id"; |
|
41 | + $query = $mixedData['query']." WHERE ".$this->qb->quote('id')."=:id"; |
|
42 | 42 | |
43 | 43 | return ['query' => $query, 'data' => $mixedData['ar']]; |
44 | 44 | } |
45 | 45 | |
46 | 46 | $queryVal = ''; |
47 | 47 | $query = "INSERT INTO ".$this->table." ("; |
48 | - foreach($row as $key => $val){ |
|
49 | - $query.= $this->qb->quote($key).", "; |
|
48 | + foreach ($row as $key => $val) { |
|
49 | + $query .= $this->qb->quote($key).", "; |
|
50 | 50 | $ar[$key] = $val; |
51 | - $queryVal.= ":".$key.", "; |
|
51 | + $queryVal .= ":".$key.", "; |
|
52 | 52 | } |
53 | 53 | |
54 | 54 | $query = rtrim($query, ", ").") VALUES (".$queryVal.rtrim($query, ", ").") "; |
@@ -73,10 +73,10 @@ discard block |
||
73 | 73 | |
74 | 74 | list($query, $data) = $this->createQuery($row); |
75 | 75 | |
76 | - try{ |
|
76 | + try { |
|
77 | 77 | $stmt = Connection::get()->prepare($this->qb->queryPrefix($query)); |
78 | 78 | $stmt->execute($data); |
79 | - } catch(Exception $e){ |
|
79 | + } catch (Exception $e) { |
|
80 | 80 | throw new Exception($e->getMessage()); |
81 | 81 | } |
82 | 82 |
@@ -374,14 +374,14 @@ discard block |
||
374 | 374 | public function update($row) |
375 | 375 | { |
376 | 376 | $result = (new UpdateQueryBuilder())->update( |
377 | - $row, |
|
378 | - $this->table, |
|
379 | - $this->where, |
|
380 | - $this->whereRaw, |
|
381 | - $this->whereIn, |
|
382 | - $this->whereNotIn, |
|
383 | - $this->whereNull, |
|
384 | - $this->whereNotNull |
|
377 | + $row, |
|
378 | + $this->table, |
|
379 | + $this->where, |
|
380 | + $this->whereRaw, |
|
381 | + $this->whereIn, |
|
382 | + $this->whereNotIn, |
|
383 | + $this->whereNull, |
|
384 | + $this->whereNotNull |
|
385 | 385 | ); |
386 | 386 | |
387 | 387 | $this->reset(); |
@@ -420,13 +420,13 @@ discard block |
||
420 | 420 | public function delete() |
421 | 421 | { |
422 | 422 | $result = (new DeleteQueryBuilder())->delete( |
423 | - $this->table, |
|
424 | - $this->where, |
|
425 | - $this->whereRaw, |
|
426 | - $this->whereIn, |
|
427 | - $this->whereNotIn, |
|
428 | - $this->whereNull, |
|
429 | - $this->whereNotNull |
|
423 | + $this->table, |
|
424 | + $this->where, |
|
425 | + $this->whereRaw, |
|
426 | + $this->whereIn, |
|
427 | + $this->whereNotIn, |
|
428 | + $this->whereNull, |
|
429 | + $this->whereNotNull |
|
430 | 430 | ); |
431 | 431 | |
432 | 432 | $this->reset(); |
@@ -73,12 +73,12 @@ discard block |
||
73 | 73 | protected $offset; |
74 | 74 | protected $results; |
75 | 75 | |
76 | - private function getFields(array $args, bool $quote = true){ |
|
76 | + private function getFields(array $args, bool $quote = true) { |
|
77 | 77 | return (new QueryBuilder())->getFields($args, $quote); |
78 | 78 | } |
79 | 79 | |
80 | - private function validateArgsCount($noOfArgs){ |
|
81 | - if($noOfArgs<2 || $noOfArgs >3){ |
|
80 | + private function validateArgsCount($noOfArgs) { |
|
81 | + if ($noOfArgs < 2 || $noOfArgs > 3) { |
|
82 | 82 | throw new Exception('Where parameter contains invalid number of parameters', 1); |
83 | 83 | } |
84 | 84 | } |
@@ -138,7 +138,7 @@ discard block |
||
138 | 138 | $noOfArgs = func_num_args(); |
139 | 139 | $this->validateArgsCount($noOfArgs); |
140 | 140 | |
141 | - if($noOfArgs===2){ |
|
141 | + if ($noOfArgs === 2) { |
|
142 | 142 | $this->where = array_merge($this->where, [[$args[0], '=', $args[1]]]); |
143 | 143 | return $this; |
144 | 144 | } |
@@ -223,7 +223,7 @@ discard block |
||
223 | 223 | */ |
224 | 224 | protected function buildQuery() |
225 | 225 | { |
226 | - $query = (new QueryBuilder())->buildQuery([ |
|
226 | + $query = (new QueryBuilder())->buildQuery([ |
|
227 | 227 | 'table' => $this->table, |
228 | 228 | 'fields' => $this->fields, |
229 | 229 | 'join' => $this->join, |
@@ -334,7 +334,7 @@ discard block |
||
334 | 334 | $this->where('id', $id); |
335 | 335 | $row = $this->first(); |
336 | 336 | |
337 | - if($row == null ){ |
|
337 | + if ($row == null) { |
|
338 | 338 | throw new Exception("The record does not exists!"); |
339 | 339 | } |
340 | 340 | |
@@ -373,7 +373,7 @@ discard block |
||
373 | 373 | */ |
374 | 374 | public function update($row) |
375 | 375 | { |
376 | - $result = (new UpdateQueryBuilder())->update( |
|
376 | + $result = (new UpdateQueryBuilder())->update( |
|
377 | 377 | $row, |
378 | 378 | $this->table, |
379 | 379 | $this->where, |
@@ -401,9 +401,9 @@ discard block |
||
401 | 401 | $qb = new QueryBuilder(); |
402 | 402 | $query = "TRUNCATE ".$this->table; |
403 | 403 | |
404 | - try{ |
|
404 | + try { |
|
405 | 405 | Connection::get()->query($qb->queryPrefix($query)); |
406 | - } catch(Exception $e){ |
|
406 | + } catch (Exception $e) { |
|
407 | 407 | throw new Exception($e->getMessage()); |
408 | 408 | } |
409 | 409 | |
@@ -419,7 +419,7 @@ discard block |
||
419 | 419 | */ |
420 | 420 | public function delete() |
421 | 421 | { |
422 | - $result = (new DeleteQueryBuilder())->delete( |
|
422 | + $result = (new DeleteQueryBuilder())->delete( |
|
423 | 423 | $this->table, |
424 | 424 | $this->where, |
425 | 425 | $this->whereRaw, |