o2system /
database
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of the O2System Framework package. |
||
| 4 | * |
||
| 5 | * For the full copyright and license information, please view the LICENSE |
||
| 6 | * file that was distributed with this source code. |
||
| 7 | * |
||
| 8 | * @author Steeve Andrian Salim |
||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
| 10 | */ |
||
| 11 | |||
| 12 | // ------------------------------------------------------------------------ |
||
| 13 | |||
| 14 | namespace O2System\Database\Sql\Drivers\MySql; |
||
| 15 | |||
| 16 | // ------------------------------------------------------------------------ |
||
| 17 | |||
| 18 | use O2System\Database\Sql\Abstracts\AbstractQueryBuilder; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Class QueryBuilder |
||
| 22 | * |
||
| 23 | * @package O2System\Database\Sql\Drivers\MySql |
||
| 24 | */ |
||
| 25 | class QueryBuilder extends AbstractQueryBuilder |
||
| 26 | {
|
||
| 27 | /** |
||
| 28 | * AbstractQueryBuilder::countAllResult |
||
| 29 | * |
||
| 30 | * Perform execution of count all result from Query Builder along with WHERE, LIKE, HAVING, GROUP BY, and LIMIT Sql |
||
| 31 | * statement. |
||
| 32 | * |
||
| 33 | * @param bool $reset Whether perform reset Query Builder or not |
||
| 34 | * |
||
| 35 | * @return int |
||
| 36 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
| 37 | * @throws \Psr\Cache\InvalidArgumentException |
||
| 38 | * @access public |
||
| 39 | */ |
||
| 40 | public function countAllResults($reset = true) |
||
| 41 | {
|
||
| 42 | // generate Sql statement |
||
| 43 | $sqlStatement = $this->getSqlStatement(); |
||
| 44 | |||
| 45 | if ($this->testMode) {
|
||
| 46 | return $sqlStatement; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 47 | } |
||
| 48 | |||
| 49 | $this->conn->query($sqlStatement, $this->builderCache->binds); |
||
| 50 | $result = $this->conn->query('SELECT FOUND_ROWS() AS numrows;');
|
||
| 51 | |||
| 52 | if ($reset === true) {
|
||
| 53 | $this->builderCache->reset(); |
||
| 54 | } |
||
| 55 | |||
| 56 | if ($result->count() == 0) {
|
||
| 57 | return 0; |
||
| 58 | } |
||
| 59 | |||
| 60 | return (int)$result->first()->numrows; |
||
|
0 ignored issues
–
show
The property
numrows does not exist on O2System\Database\DataObjects\Result\Row. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||
| 61 | } |
||
| 62 | |||
| 63 | //-------------------------------------------------------------------- |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Platform independent LIKE statement builder. |
||
| 67 | * |
||
| 68 | * @param string|null $prefix |
||
| 69 | * @param string $column |
||
| 70 | * @param string|null $not |
||
| 71 | * @param string $bind |
||
| 72 | * @param bool $caseSensitive |
||
| 73 | * |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | protected function platformPrepareLikeStatement( |
||
| 77 | $prefix = null, |
||
| 78 | $column, |
||
| 79 | $not = null, |
||
| 80 | $bind, |
||
| 81 | $caseSensitive = false |
||
| 82 | ) {
|
||
| 83 | $likeStatement = "{$prefix} {$column} {$not} LIKE :{$bind}";
|
||
| 84 | |||
| 85 | if ($caseSensitive === true) {
|
||
| 86 | $likeStatement = "{$prefix} LOWER({$column}) {$not} LIKE :{$bind}";
|
||
| 87 | } |
||
| 88 | |||
| 89 | return $likeStatement; |
||
| 90 | } |
||
| 91 | |||
| 92 | //-------------------------------------------------------------------- |
||
| 93 | |||
| 94 | /** |
||
| 95 | * QueryBuilder::platformInsertStatement |
||
| 96 | * |
||
| 97 | * Generates a platform-specific insert string from the supplied data. |
||
| 98 | * |
||
| 99 | * @param string $table Table name. |
||
| 100 | * @param array $keys Insert keys. |
||
| 101 | * @param array $values Insert values. |
||
| 102 | * |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | protected function platformInsertStatement($table, array $keys, array $values) |
||
| 106 | {
|
||
| 107 | return 'INSERT INTO ' |
||
| 108 | . $table |
||
| 109 | . ' ('
|
||
| 110 | . implode(', ', $keys)
|
||
| 111 | . ') VALUES ('
|
||
| 112 | . implode(', ', $values)
|
||
| 113 | . ')'; |
||
| 114 | } |
||
| 115 | |||
| 116 | //-------------------------------------------------------------------- |
||
| 117 | |||
| 118 | /** |
||
| 119 | * QueryBuilder::platformInsertBatchStatement |
||
| 120 | * |
||
| 121 | * @param string $table |
||
| 122 | * @param array $keys |
||
| 123 | * @param array $values |
||
| 124 | * |
||
| 125 | * @return mixed |
||
| 126 | */ |
||
| 127 | protected function platformInsertBatchStatement($table, array $keys, array $values) |
||
| 128 | {
|
||
| 129 | return 'INSERT INTO ' |
||
| 130 | . $table |
||
| 131 | . ' ('
|
||
| 132 | . implode(', ', $keys)
|
||
| 133 | . ') VALUES ' |
||
| 134 | . implode(', ', $values);
|
||
| 135 | } |
||
| 136 | |||
| 137 | //-------------------------------------------------------------------- |
||
| 138 | |||
| 139 | /** |
||
| 140 | * QueryBuilder::platformReplaceStatement |
||
| 141 | * |
||
| 142 | * Generates a platform-specific update string from the supplied data. |
||
| 143 | * |
||
| 144 | * @param string $table Table name. |
||
| 145 | * @param array $keys Insert keys. |
||
| 146 | * @param array $values Insert values. |
||
| 147 | * |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | protected function platformReplaceStatement($table, array $keys, array $values) |
||
| 151 | {
|
||
| 152 | return 'REPLACE INTO ' . $table . ' (' . implode(', ', $keys) . ') VALUES (' . implode(', ', $values) . ')';
|
||
| 153 | } |
||
| 154 | |||
| 155 | //-------------------------------------------------------------------- |
||
| 156 | |||
| 157 | /** |
||
| 158 | * QueryBuilder::platformUpdateStatement |
||
| 159 | * |
||
| 160 | * Generates a platform-specific update string from the supplied data. |
||
| 161 | * |
||
| 162 | * @param string $table Table name. |
||
| 163 | * @param array $sets An associative array of set values. |
||
| 164 | * sets[][field => value] |
||
| 165 | * |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | protected function platformUpdateStatement($table, array $sets) |
||
| 169 | {
|
||
| 170 | $columns = []; |
||
| 171 | |||
| 172 | foreach ($sets as $key => $val) {
|
||
| 173 | $columns[] = $key . ' = ' . $val; |
||
| 174 | } |
||
| 175 | |||
| 176 | return 'UPDATE ' . $table . ' SET ' . implode(', ', $columns)
|
||
| 177 | . $this->compileWhereHavingStatement('where')
|
||
| 178 | . $this->compileOrderByStatement() |
||
| 179 | . $this->compileLimitStatement(); |
||
| 180 | } |
||
| 181 | |||
| 182 | //-------------------------------------------------------------------- |
||
| 183 | |||
| 184 | /** |
||
| 185 | * QueryBuilder::platformUpdateBatchStatement |
||
| 186 | * |
||
| 187 | * Generates a platform-specific batch update string from the supplied data. |
||
| 188 | * |
||
| 189 | * @param string $table Table name |
||
| 190 | * @param array $values Update data |
||
| 191 | * @param string $index WHERE key |
||
| 192 | * |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | protected function platformUpdateBatchStatement($table, $values, $index) |
||
| 196 | {
|
||
| 197 | $ids = []; |
||
| 198 | $columns = []; |
||
| 199 | |||
| 200 | foreach ($values as $key => $value) {
|
||
| 201 | $ids[] = $value[ $index ]; |
||
| 202 | |||
| 203 | foreach (array_keys($value) as $field) {
|
||
| 204 | if ($field !== $index) {
|
||
| 205 | $columns[ $field ][] = 'WHEN ' . $index . ' = ' . $value[ $index ] . ' THEN ' . $value[ $field ]; |
||
| 206 | } |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | $cases = ''; |
||
| 211 | foreach ($columns as $key => $value) {
|
||
| 212 | $cases .= $key . " = CASE \n" |
||
| 213 | . implode("\n", $value) . "\n"
|
||
| 214 | . 'ELSE ' . $key . ' END, '; |
||
| 215 | } |
||
| 216 | |||
| 217 | $this->where($index . ' IN(' . implode(',', $ids) . ')', null, false);
|
||
| 218 | |||
| 219 | return 'UPDATE ' . $table . ' SET ' . substr($cases, 0, -2) . $this->compileWhereHavingStatement('where');
|
||
| 220 | } |
||
| 221 | |||
| 222 | //-------------------------------------------------------------------- |
||
| 223 | |||
| 224 | /** |
||
| 225 | * QueryBuilder::platformDeleteStatement |
||
| 226 | * |
||
| 227 | * Generates a platform-specific delete string from the supplied data |
||
| 228 | * |
||
| 229 | * @param string $table The table name. |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | protected function platformDeleteStatement($table) |
||
| 234 | {
|
||
| 235 | return 'DELETE FROM ' . $table |
||
| 236 | . $this->compileWhereHavingStatement('where')
|
||
| 237 | . $this->compileLimitStatement(); |
||
| 238 | } |
||
| 239 | |||
| 240 | //-------------------------------------------------------------------- |
||
| 241 | |||
| 242 | /** |
||
| 243 | * QueryBuilder::platformTruncateStatement |
||
| 244 | * |
||
| 245 | * Generates a platform-specific truncate statement. |
||
| 246 | * |
||
| 247 | * @param string $table The table name. |
||
| 248 | * |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | protected function platformTruncateStatement($table) |
||
| 252 | {
|
||
| 253 | return 'TRUNCATE ' . $table; |
||
| 254 | } |
||
| 255 | |||
| 256 | //-------------------------------------------------------------------- |
||
| 257 | |||
| 258 | /** |
||
| 259 | * QueryBuilder::compileSelectStatement |
||
| 260 | * |
||
| 261 | * Compile the SELECT statement |
||
| 262 | * |
||
| 263 | * Generates a query string based on which functions were used. |
||
| 264 | * Should not be called directly. |
||
| 265 | * |
||
| 266 | * @param bool $selectOverride |
||
| 267 | * |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | protected function compileSelectStatement($selectOverride = false) |
||
| 271 | {
|
||
| 272 | $sqlStatement = parent::compileSelectStatement($selectOverride); |
||
| 273 | |||
| 274 | if ($this->isSubQuery) {
|
||
| 275 | return $sqlStatement; |
||
| 276 | } elseif(strpos($sqlStatement, 'COUNT') !== false) {
|
||
| 277 | return $sqlStatement; |
||
| 278 | } |
||
| 279 | |||
| 280 | return str_replace('SELECT', 'SELECT SQL_CALC_FOUND_ROWS', $sqlStatement);
|
||
| 281 | } |
||
| 282 | } |
||
| 283 |