paulgoughbooks_old
This repository has no backups
This repository's network speed is throttled to 100KB/sec
Upgrade your account to fix these warnings, or use backups.vc for automated backups
@ 2
paulgoughbooks_old / trunk / system / Database / Postgre / Builder.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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
<?php /** * CodeIgniter * * An open source application development framework for PHP * * This content is released under the MIT License (MIT) * * Copyright (c) 2014-2019 British Columbia Institute of Technology * Copyright (c) 2019-2020 CodeIgniter Foundation * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * * @package CodeIgniter * @author CodeIgniter Dev Team * @copyright 2019-2020 CodeIgniter Foundation * @license https://opensource.org/licenses/MIT MIT License * @link https://codeigniter.com * @since Version 4.0.0 * @filesource */ namespace CodeIgniter\Database\Postgre; use CodeIgniter\Database\BaseBuilder; use CodeIgniter\Database\Exceptions\DatabaseException; /** * Builder for Postgre */ class Builder extends BaseBuilder { /** * ORDER BY random keyword * * @var array */ protected $randomKeyword = [ 'RANDOM()', ]; /** * Specifies which sql statements * support the ignore option. * * @var array */ protected $supportedIgnoreStatements = [ 'insert' => 'ON CONFLICT DO NOTHING', ]; //-------------------------------------------------------------------- /** * Compile Ignore Statement * * Checks if the ignore option is supported by * the Database Driver for the specific statement. * * @param string $statement * * @return string */ protected function compileIgnore(string $statement) { $sql = parent::compileIgnore($statement); if (! empty($sql)) { $sql = ' ' . trim($sql); } return $sql; } //-------------------------------------------------------------------- /** * ORDER BY * * @param string $orderBy * @param string $direction ASC, DESC or RANDOM * @param boolean $escape * * @return BaseBuilder */ public function orderBy(string $orderBy, string $direction = '', bool $escape = null) { $direction = strtoupper(trim($direction)); if ($direction === 'RANDOM') { if (! is_float($orderBy) && ctype_digit((string) $orderBy)) { $orderBy = (float) ($orderBy > 1 ? "0.{$orderBy}" : $orderBy); } if (is_float($orderBy)) { $this->db->simpleQuery("SET SEED {$orderBy}"); } $orderBy = $this->randomKeyword[0]; $direction = ''; $escape = false; } return parent::orderBy($orderBy, $direction, $escape); } //-------------------------------------------------------------------- /** * Increments a numeric column by the specified value. * * @param string $column * @param integer $value * * @throws DatabaseException * * @return mixed */ public function increment(string $column, int $value = 1) { $column = $this->db->protectIdentifiers($column); $sql = $this->_update($this->QBFrom[0], [$column => "to_number({$column}, '9999999') + {$value}"]); return $this->db->query($sql, $this->binds, false); } //-------------------------------------------------------------------- /** * Decrements a numeric column by the specified value. * * @param string $column * @param integer $value * * @throws DatabaseException * * @return mixed */ public function decrement(string $column, int $value = 1) { $column = $this->db->protectIdentifiers($column); $sql = $this->_update($this->QBFrom[0], [$column => "to_number({$column}, '9999999') - {$value}"]); return $this->db->query($sql, $this->binds, false); } //-------------------------------------------------------------------- /** * Replace * * Compiles an replace into string and runs the query. * Because PostgreSQL doesn't support the replace into command, * we simply do a DELETE and an INSERT on the first key/value * combo, assuming that it's either the primary key or a unique key. * * @param array $set An associative array of insert values * * @return mixed * @throws DatabaseException * @internal param true $bool returns the generated SQL, false executes the query. */ public function replace(array $set = null) { if ($set !== null) { $this->set($set); } if (! $this->QBSet) { if (CI_DEBUG) { throw new DatabaseException('You must use the "set" method to update an entry.'); } // @codeCoverageIgnoreStart return false; // @codeCoverageIgnoreEnd } $table = $this->QBFrom[0]; $set = $this->binds; // We need to grab out the actual values from // the way binds are stored with escape flag. array_walk($set, function (&$item) { $item = $item[0]; }); $keys = array_keys($set); $values = array_values($set); $builder = $this->db->table($table); $exists = $builder->where("$keys[0] = $values[0]", null, false)->get()->getFirstRow(); if (empty($exists)) { $result = $builder->insert($set); } else { array_pop($set); $result = $builder->update($set, "$keys[0] = $values[0]"); } unset($builder); $this->resetWrite(); return $result; } //-------------------------------------------------------------------- /** * Delete * * Compiles a delete string and runs the query * * @param mixed $where * @param integer $limit * @param boolean $reset_data * * @return mixed * @throws DatabaseException * @internal param the $mixed where clause * @internal param the $mixed limit clause * @internal param $bool */ public function delete($where = '', int $limit = null, bool $reset_data = true) { if (! empty($limit) || ! empty($this->QBLimit)) { throw new DatabaseException('PostgreSQL does not allow LIMITs on DELETE queries.'); } return parent::delete($where, $limit, $reset_data); } //-------------------------------------------------------------------- /** * LIMIT string * * Generates a platform-specific LIMIT clause. * * @param string $sql SQL Query * * @return string */ protected function _limit(string $sql, bool $offsetIgnore = false): string { return $sql . ' LIMIT ' . $this->QBLimit . ($this->QBOffset ? " OFFSET {$this->QBOffset}" : ''); } //-------------------------------------------------------------------- /** * Update statement * * Generates a platform-specific update string from the supplied data * * @param string $table * @param array $values * * @return string * @throws DatabaseException * @internal param the $string table name * @internal param the $array update data */ protected function _update(string $table, array $values): string { if (! empty($this->QBLimit)) { throw new DatabaseException('Postgres does not support LIMITs with UPDATE queries.'); } $this->QBOrderBy = []; return parent::_update($table, $values); } //-------------------------------------------------------------------- /** * Update_Batch statement * * Generates a platform-specific batch update string from the supplied data * * @param string $table Table name * @param array $values Update data * @param string $index WHERE key * * @return string */ protected function _updateBatch(string $table, array $values, string $index): string { $ids = []; foreach ($values as $val) { $ids[] = $val[$index]; foreach (array_keys($val) as $field) { if ($field !== $index) { $final[$field][] = "WHEN {$val[$index]} THEN {$val[$field]}"; } } } $cases = ''; foreach ($final as $k => $v) { $cases .= "{$k} = (CASE {$index}\n" . implode("\n", $v) . "\nELSE {$k} END), "; } $this->where("{$index} IN(" . implode(',', $ids) . ')', null, false); return "UPDATE {$table} SET " . substr($cases, 0, -2) . $this->compileWhereHaving('QBWhere'); } //-------------------------------------------------------------------- /** * Delete statement * * Generates a platform-specific delete string from the supplied data * * @param string $table The table name * * @return string */ protected function _delete(string $table): string { $this->QBLimit = false; return parent::_delete($table); } //-------------------------------------------------------------------- /** * Truncate statement * * Generates a platform-specific truncate string from the supplied data * * If the database does not support the truncate() command, * then this method maps to 'DELETE FROM table' * * @param string $table The table name * * @return string */ protected function _truncate(string $table): string { return 'TRUNCATE ' . $table . ' RESTART IDENTITY'; } //-------------------------------------------------------------------- /** * Platform independent LIKE statement builder. * * In PostgreSQL, the ILIKE operator will perform case insensitive * searches according to the current locale. * * @see https://www.postgresql.org/docs/9.2/static/functions-matching.html * * @param string $prefix * @param string $column * @param string $not * @param string $bind * @param boolean $insensitiveSearch * * @return string $like_statement */ public function _like_statement(string $prefix = null, string $column, string $not = null, string $bind, bool $insensitiveSearch = false): string { $op = $insensitiveSearch === true ? 'ILIKE' : 'LIKE'; return "{$prefix} {$column} {$not} {$op} :{$bind}:"; } //-------------------------------------------------------------------- /** * JOIN * * Generates the JOIN portion of the query * * @param string $table * @param string $cond The join condition * @param string $type The type of join * @param boolean $escape Whether not to try to escape identifiers * * @return BaseBuilder */ public function join(string $table, string $cond, string $type = '', bool $escape = null) { if (! in_array('FULL OUTER', $this->joinTypes, true)) { $this->joinTypes = array_merge($this->joinTypes, ['FULL OUTER']); } return parent::join($table, $cond, $type, $escape); } //-------------------------------------------------------------------- } |
Commits for paulgoughbooks_old/trunk/system/Database/Postgre/Builder.php
Revision | Author | Commited | Message |
---|---|---|---|
2 | tporter | Tue 03 Nov, 2020 08:50:21 +0000 | Migration of Paul Goughs Books site to Codeignitor 4 |