@@ -58,13 +58,13 @@ discard block |
||
| 58 | 58 | public function fetchRows(\Closure $callback = null) { |
| 59 | 59 | $statement = $this->createStatement(); |
| 60 | 60 | $data = $statement->fetchAll(\PDO::FETCH_ASSOC); |
| 61 | - if($this->preserveTypes) { |
|
| 61 | + if ($this->preserveTypes) { |
|
| 62 | 62 | $columnDefinitions = FieldTypeProvider::getFieldTypes($statement); |
| 63 | - foreach($data as &$row) { |
|
| 63 | + foreach ($data as &$row) { |
|
| 64 | 64 | $row = FieldValueConverter::convertValues($row, $columnDefinitions); |
| 65 | 65 | } |
| 66 | 66 | } |
| 67 | - if($callback !== null) { |
|
| 67 | + if ($callback !== null) { |
|
| 68 | 68 | $data = array_map($callback, $data); |
| 69 | 69 | } |
| 70 | 70 | $statement->closeCursor(); |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | * @return array[]|\Generator |
| 77 | 77 | */ |
| 78 | 78 | public function fetchRowsLazy(\Closure $callback = null) { |
| 79 | - if(version_compare(PHP_VERSION, '5.5', '<=')) { |
|
| 79 | + if (version_compare(PHP_VERSION, '5.5', '<=')) { |
|
| 80 | 80 | return $this->fetchRows($callback); |
| 81 | 81 | } |
| 82 | 82 | $statement = $this->createStatement(); |
@@ -90,11 +90,11 @@ discard block |
||
| 90 | 90 | public function fetchRow() { |
| 91 | 91 | $statement = $this->createStatement(); |
| 92 | 92 | $row = $statement->fetch(\PDO::FETCH_ASSOC); |
| 93 | - if(!is_array($row)) { |
|
| 93 | + if (!is_array($row)) { |
|
| 94 | 94 | $statement->closeCursor(); |
| 95 | 95 | return array(); |
| 96 | 96 | } |
| 97 | - if($this->preserveTypes) { |
|
| 97 | + if ($this->preserveTypes) { |
|
| 98 | 98 | $columnDefinitions = FieldTypeProvider::getFieldTypes($statement); |
| 99 | 99 | $row = FieldValueConverter::convertValues($row, $columnDefinitions); |
| 100 | 100 | } |
@@ -109,13 +109,13 @@ discard block |
||
| 109 | 109 | public function fetchKeyValue($treatValueAsArray = false) { |
| 110 | 110 | $rows = $this->fetchRows(); |
| 111 | 111 | $result = array(); |
| 112 | - if(!$treatValueAsArray) { |
|
| 113 | - foreach($rows as $row) { |
|
| 112 | + if (!$treatValueAsArray) { |
|
| 113 | + foreach ($rows as $row) { |
|
| 114 | 114 | list($key, $value) = array_values($row); |
| 115 | 115 | $result[$key] = $value; |
| 116 | 116 | } |
| 117 | 117 | } else { |
| 118 | - foreach($rows as $row) { |
|
| 118 | + foreach ($rows as $row) { |
|
| 119 | 119 | list($key) = array_values($row); |
| 120 | 120 | $result[$key] = $row; |
| 121 | 121 | } |
@@ -130,11 +130,11 @@ discard block |
||
| 130 | 130 | public function fetchGroups(array $fields) { |
| 131 | 131 | $rows = $this->fetchRows(); |
| 132 | 132 | $result = array(); |
| 133 | - foreach($rows as $row) { |
|
| 133 | + foreach ($rows as $row) { |
|
| 134 | 134 | $tmp = &$result; |
| 135 | - foreach($fields as $field) { |
|
| 135 | + foreach ($fields as $field) { |
|
| 136 | 136 | $value = $row[$field]; |
| 137 | - if(!array_key_exists($value, $tmp)) { |
|
| 137 | + if (!array_key_exists($value, $tmp)) { |
|
| 138 | 138 | $tmp[$value] = []; |
| 139 | 139 | } |
| 140 | 140 | $tmp = &$tmp[$value]; |
@@ -148,7 +148,7 @@ discard block |
||
| 148 | 148 | * @return string[] |
| 149 | 149 | */ |
| 150 | 150 | public function fetchArray() { |
| 151 | - return $this->fetchRows(function ($row) { |
|
| 151 | + return $this->fetchRows(function($row) { |
|
| 152 | 152 | reset($row); |
| 153 | 153 | return current($row); |
| 154 | 154 | }); |
@@ -162,10 +162,10 @@ discard block |
||
| 162 | 162 | $statement = $this->createStatement(); |
| 163 | 163 | $row = $statement->fetch(\PDO::FETCH_ASSOC); |
| 164 | 164 | $statement->closeCursor(); |
| 165 | - if(!is_array($row)) { |
|
| 165 | + if (!is_array($row)) { |
|
| 166 | 166 | return $default; |
| 167 | 167 | } |
| 168 | - if(!count($row)) { |
|
| 168 | + if (!count($row)) { |
|
| 169 | 169 | return null; |
| 170 | 170 | } |
| 171 | 171 | return array_shift($row); |
@@ -186,7 +186,7 @@ discard block |
||
| 186 | 186 | $query = $this->__toString(); |
| 187 | 187 | $statement = $db->prepare($query); |
| 188 | 188 | $statement->execute($this->values); |
| 189 | - if($this->getCalcFoundRows()) { |
|
| 189 | + if ($this->getCalcFoundRows()) { |
|
| 190 | 190 | $this->foundRows = (int) $db->query('SELECT FOUND_ROWS()')->fetchColumn(); |
| 191 | 191 | } |
| 192 | 192 | return $statement; |
@@ -21,7 +21,7 @@ |
||
| 21 | 21 | * @return string |
| 22 | 22 | */ |
| 23 | 23 | protected function buildLimit($query) { |
| 24 | - if($this->limit !== null) { |
|
| 24 | + if ($this->limit !== null) { |
|
| 25 | 25 | $query .= "LIMIT\n\t{$this->limit}\n"; |
| 26 | 26 | } |
| 27 | 27 | return $query; |
@@ -21,7 +21,7 @@ |
||
| 21 | 21 | * @return string |
| 22 | 22 | */ |
| 23 | 23 | protected function buildOffset($query) { |
| 24 | - if($this->offset !== null) { |
|
| 24 | + if ($this->offset !== null) { |
|
| 25 | 25 | $query .= "OFFSET\n\t{$this->offset}\n"; |
| 26 | 26 | } |
| 27 | 27 | return $query; |
@@ -13,9 +13,9 @@ discard block |
||
| 13 | 13 | * @return $this |
| 14 | 14 | */ |
| 15 | 15 | public function groupBy() { |
| 16 | - foreach(func_get_args() as $expression) { |
|
| 17 | - if(is_array($expression)) { |
|
| 18 | - if(!count($expression)) { |
|
| 16 | + foreach (func_get_args() as $expression) { |
|
| 17 | + if (is_array($expression)) { |
|
| 18 | + if (!count($expression)) { |
|
| 19 | 19 | continue; |
| 20 | 20 | } |
| 21 | 21 | $arguments = array( |
@@ -34,12 +34,12 @@ discard block |
||
| 34 | 34 | * @return string |
| 35 | 35 | */ |
| 36 | 36 | protected function buildGroups($query) { |
| 37 | - if(!count($this->groupBy)) { |
|
| 37 | + if (!count($this->groupBy)) { |
|
| 38 | 38 | return $query; |
| 39 | 39 | } |
| 40 | 40 | $query .= "GROUP BY\n"; |
| 41 | 41 | $arr = array(); |
| 42 | - foreach($this->groupBy as $expression) { |
|
| 42 | + foreach ($this->groupBy as $expression) { |
|
| 43 | 43 | $arr[] = "\t{$expression}"; |
| 44 | 44 | } |
| 45 | 45 | return $query.join(",\n", $arr)."\n"; |
@@ -10,17 +10,17 @@ |
||
| 10 | 10 | * @return string |
| 11 | 11 | */ |
| 12 | 12 | protected function buildTableName($alias, $name) { |
| 13 | - if(is_object($name)) { |
|
| 13 | + if (is_object($name)) { |
|
| 14 | 14 | $name = (string) $name; |
| 15 | 15 | $lines = explode("\n", $name); |
| 16 | - foreach($lines as &$line) { |
|
| 16 | + foreach ($lines as &$line) { |
|
| 17 | 17 | $line = "\t{$line}"; |
| 18 | 18 | } |
| 19 | 19 | $name = join("\n", $lines); |
| 20 | - $name = '(' . trim(rtrim(trim($name), ';')) . ')'; |
|
| 20 | + $name = '('.trim(rtrim(trim($name), ';')).')'; |
|
| 21 | 21 | } |
| 22 | 22 | $name = $this->aliasReplacer()->replace($name); |
| 23 | - if($alias !== null) { |
|
| 23 | + if ($alias !== null) { |
|
| 24 | 24 | return sprintf("%s %s", $name, $alias); |
| 25 | 25 | } |
| 26 | 26 | return $name; |
@@ -30,21 +30,21 @@ |
||
| 30 | 30 | * @return string |
| 31 | 31 | */ |
| 32 | 32 | protected function buildUnions($query) { |
| 33 | - $wrap = function ($query) { |
|
| 33 | + $wrap = function($query) { |
|
| 34 | 34 | $query = trim($query); |
| 35 | 35 | $query = join("\n\t", explode("\n", $query)); |
| 36 | 36 | return sprintf("(\n\t%s\n)", $query); |
| 37 | 37 | }; |
| 38 | 38 | $queries = [$wrap($query)]; |
| 39 | - foreach($this->unions as $unionQuery) { |
|
| 40 | - if($unionQuery[0] === 'ALL') { |
|
| 39 | + foreach ($this->unions as $unionQuery) { |
|
| 40 | + if ($unionQuery[0] === 'ALL') { |
|
| 41 | 41 | $queries[] = 'UNION ALL'; |
| 42 | 42 | } else { |
| 43 | 43 | $queries[] = 'UNION'; |
| 44 | 44 | } |
| 45 | 45 | $queries[] = $wrap($unionQuery[1]); |
| 46 | 46 | } |
| 47 | - if(count($queries) > 1) { |
|
| 47 | + if (count($queries) > 1) { |
|
| 48 | 48 | return join(" ", $queries); |
| 49 | 49 | } |
| 50 | 50 | return $query; |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | * @return $this |
| 16 | 16 | */ |
| 17 | 17 | protected function addTable($alias, $table = null) { |
| 18 | - if($table === null) { |
|
| 18 | + if ($table === null) { |
|
| 19 | 19 | list($alias, $table) = [$table, $alias]; |
| 20 | 20 | } |
| 21 | 21 | $this->tables[] = array('alias' => $alias, 'name' => $table); |
@@ -28,10 +28,10 @@ discard block |
||
| 28 | 28 | */ |
| 29 | 29 | protected function buildTables($query) { |
| 30 | 30 | $arr = array(); |
| 31 | - foreach($this->tables as $table) { |
|
| 31 | + foreach ($this->tables as $table) { |
|
| 32 | 32 | $arr[] = "\t".$this->buildTableName($table['alias'], $table['name']); |
| 33 | 33 | } |
| 34 | - if(count($arr)) { |
|
| 34 | + if (count($arr)) { |
|
| 35 | 35 | $query .= join(",\n", $arr)."\n"; |
| 36 | 36 | } |
| 37 | 37 | return $query; |
@@ -49,15 +49,15 @@ |
||
| 49 | 49 | */ |
| 50 | 50 | protected function buildJoins($query) { |
| 51 | 51 | $arr = array(); |
| 52 | - foreach($this->joinTables as $table) { |
|
| 52 | + foreach ($this->joinTables as $table) { |
|
| 53 | 53 | $join = $table['type']." JOIN\n"; |
| 54 | - $join .= "\t" . $this->buildTableName($table['alias'], $table['name']); |
|
| 55 | - if($table['expression']) { |
|
| 56 | - $join .= " ON " . $this->db()->quoteExpression($table['expression'], $table['arguments']); |
|
| 54 | + $join .= "\t".$this->buildTableName($table['alias'], $table['name']); |
|
| 55 | + if ($table['expression']) { |
|
| 56 | + $join .= " ON ".$this->db()->quoteExpression($table['expression'], $table['arguments']); |
|
| 57 | 57 | } |
| 58 | 58 | $arr[] = $join; |
| 59 | 59 | } |
| 60 | - if(count($arr)) { |
|
| 60 | + if (count($arr)) { |
|
| 61 | 61 | $query .= join("\n", $arr)."\n"; |
| 62 | 62 | } |
| 63 | 63 | return $query; |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | * @return $this |
| 30 | 30 | */ |
| 31 | 31 | public function from($alias, $table = null) { |
| 32 | - if($table !== null) { |
|
| 32 | + if ($table !== null) { |
|
| 33 | 33 | $this->aliases[] = $alias; |
| 34 | 34 | } |
| 35 | 35 | $this->addTable($alias, $table); |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | public function __toString() { |
| 44 | 44 | $query = "DELETE "; |
| 45 | 45 | $query .= join(', ', $this->aliases); |
| 46 | - $query = trim($query) . " FROM\n"; |
|
| 46 | + $query = trim($query)." FROM\n"; |
|
| 47 | 47 | $query = $this->buildTables($query); |
| 48 | 48 | $query = $this->buildJoins($query); |
| 49 | 49 | $query = $this->buildWhereConditions($query); |