Complex classes like Db often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Db, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | abstract class Db { |
||
| 16 | use Utils\FetchModeTrait; |
||
| 17 | |||
| 18 | const QUERY_DEFINE = 'define'; |
||
| 19 | const QUERY_READ = 'read'; |
||
| 20 | const QUERY_WRITE = 'write'; |
||
| 21 | |||
| 22 | const INDEX_PK = 'primary'; |
||
| 23 | const INDEX_IX = 'index'; |
||
| 24 | const INDEX_UNIQUE = 'unique'; |
||
| 25 | |||
| 26 | const OPTION_REPLACE = 'replace'; |
||
| 27 | const OPTION_IGNORE = 'ignore'; |
||
| 28 | const OPTION_UPSERT = 'upsert'; |
||
| 29 | const OPTION_TRUNCATE = 'truncate'; |
||
| 30 | const OPTION_DROP = 'drop'; |
||
| 31 | const OPTION_FETCH_MODE = 'fetchMode'; |
||
| 32 | |||
| 33 | const OP_EQ = '='; |
||
| 34 | const OP_GT = '>'; |
||
| 35 | const OP_GTE = '>='; |
||
| 36 | const OP_IN = '$in'; |
||
| 37 | const OP_LIKE = '$like'; |
||
| 38 | const OP_LT = '<'; |
||
| 39 | const OP_LTE = '<='; |
||
| 40 | const OP_NEQ = '<>'; |
||
| 41 | |||
| 42 | const OP_AND = '$and'; |
||
| 43 | const OP_OR = '$or'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string[] Maps PDO drivers to db classes. |
||
| 47 | */ |
||
| 48 | private static $drivers = [ |
||
| 49 | 'mysql' => MySqlDb::class, |
||
| 50 | 'sqlite' => SqliteDb::class |
||
| 51 | ]; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var array The canonical database types. |
||
| 55 | */ |
||
| 56 | private static $types = [ |
||
| 57 | // String |
||
| 58 | 'char' => ['type' => 'string', 'length' => true], |
||
| 59 | 'varchar' => ['type' => 'string', 'length' => true], |
||
| 60 | 'tinytext' => ['type' => 'string', 'schema' => ['maxLength' => 255]], |
||
| 61 | 'text' => ['type' => 'string', 'schema' => ['maxLength' => 65535]], |
||
| 62 | 'mediumtext' => ['type' => 'string', 'schema' => ['maxLength' => 16777215]], |
||
| 63 | 'longtext' => ['type' => 'string', 'schema' => ['maxLength' => 4294967295]], |
||
| 64 | 'binary' => ['type' => 'string', 'length' => true], |
||
| 65 | 'varbinary' => ['type' => 'string', 'length' => true], |
||
| 66 | |||
| 67 | // Boolean |
||
| 68 | 'bool' => ['type' => 'boolean'], |
||
| 69 | |||
| 70 | // Integer |
||
| 71 | 'byte' => ['type' => 'integer', 'schema' => ['maximum' => 127, 'minimum' => -128]], |
||
| 72 | 'short' => ['type' => 'integer', 'schema' => ['maximum' => 32767, 'minimum' => -32768]], |
||
| 73 | 'int' => ['type' => 'integer', 'schema' => ['maximum' => 2147483647, 'minimum' => -2147483648]], |
||
| 74 | 'long' => ['type' => 'integer'], |
||
| 75 | |||
| 76 | // Number |
||
| 77 | 'float' => ['type' => 'number'], |
||
| 78 | 'double' => ['type' => 'number'], |
||
| 79 | 'decimal' => ['type' => 'number', 'precision' => true], |
||
| 80 | 'numeric' => ['type' => 'number', 'precision' => true], |
||
| 81 | |||
| 82 | // Date/Time |
||
| 83 | 'datetime' => ['type' => 'datetime'], |
||
| 84 | 'timestamp' => ['type' => 'datetime'], |
||
| 85 | |||
| 86 | // Enum |
||
| 87 | 'enum' => ['type' => 'string', 'enum' => true], |
||
| 88 | |||
| 89 | // Schema types |
||
| 90 | 'string' => 'varchar', |
||
| 91 | 'boolean' => 'bool', |
||
| 92 | 'integer' => 'int', |
||
| 93 | 'number' => 'float', |
||
| 94 | |||
| 95 | // Other aliases |
||
| 96 | 'character' => 'char', |
||
| 97 | 'tinyint' => 'byte', |
||
| 98 | 'int8' => 'byte', |
||
| 99 | 'smallint' => 'short', |
||
| 100 | 'int16' => 'short', |
||
| 101 | 'int32' => 'int', |
||
| 102 | 'bigint' => 'long', |
||
| 103 | 'int64' => 'long', |
||
| 104 | 'real' => 'double' |
||
| 105 | ]; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var string The database prefix. |
||
| 109 | */ |
||
| 110 | private $px = ''; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var array A cached copy of the table schemas indexed by lowercase name. |
||
| 114 | */ |
||
| 115 | private $tables = []; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var array|null A cached copy of the table names indexed by lowercase name. |
||
| 119 | */ |
||
| 120 | private $tableNames = null; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var \PDO |
||
| 124 | */ |
||
| 125 | private $pdo; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Initialize an instance of the {@link MySqlDb} class. |
||
| 129 | * |
||
| 130 | * @param PDO $pdo The connection to the database. |
||
| 131 | * @param string $px The database prefix. |
||
| 132 | */ |
||
| 133 | public function __construct(PDO $pdo, $px = '') { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get the name of the class that handles a database driver. |
||
| 143 | * |
||
| 144 | * @param string|PDO $driver The name of the driver or a database connection. |
||
| 145 | * @return null|string Returns the driver classname or **null** if one isn't found. |
||
| 146 | */ |
||
| 147 | public static function driverClass($driver) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Add a table to the database. |
||
| 160 | * |
||
| 161 | * @param array $tableDef The table definition. |
||
| 162 | * @param array $options An array of additional options when adding the table. |
||
| 163 | */ |
||
| 164 | abstract protected function createTableDb(array $tableDef, array $options = []); |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Alter a table in the database. |
||
| 168 | * |
||
| 169 | * When altering a table you pass an array with three optional keys: add, drop, and alter. |
||
| 170 | * Each value is consists of a table definition in a format that would be passed to {@link Db::setTableDef()}. |
||
| 171 | * |
||
| 172 | * @param array $alterDef The alter definition. |
||
| 173 | * @param array $options An array of additional options when adding the table. |
||
| 174 | */ |
||
| 175 | abstract protected function alterTableDb(array $alterDef, array $options = []); |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Drop a table. |
||
| 179 | * |
||
| 180 | * @param string $table The name of the table to drop. |
||
| 181 | * @param array $options An array of additional options when adding the table. |
||
| 182 | */ |
||
| 183 | 7 | final public function dropTable($table, array $options = []) { |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Perform the actual table drop. |
||
| 193 | * |
||
| 194 | * @param string $table The name of the table to drop. |
||
| 195 | * @param array $options An array of additional options when adding the table. |
||
| 196 | */ |
||
| 197 | abstract protected function dropTableDb($table, array $options = []); |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get the names of all the tables in the database. |
||
| 201 | * |
||
| 202 | * @return string[] Returns an array of table names without prefixes. |
||
| 203 | */ |
||
| 204 | 2 | final public function fetchTableNames() { |
|
| 205 | 2 | if ($this->tableNames !== null) { |
|
| 206 | 2 | return array_values($this->tableNames); |
|
| 207 | } |
||
| 208 | |||
| 209 | 2 | $names = $this->fetchTableNamesDb(); |
|
| 210 | |||
| 211 | 2 | $this->tableNames = []; |
|
| 212 | 2 | foreach ($names as $name) { |
|
| 213 | 2 | $name = $this->stripPrefix($name); |
|
| 214 | 2 | $this->tableNames[strtolower($name)] = $name; |
|
| 215 | } |
||
| 216 | |||
| 217 | 2 | return array_values($this->tableNames); |
|
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Fetch the table names from the underlying database layer. |
||
| 222 | * |
||
| 223 | * The driver should return all table names. It doesn't have to strip the prefix. |
||
| 224 | * |
||
| 225 | * @return string[] |
||
| 226 | */ |
||
| 227 | abstract protected function fetchTableNamesDb(); |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Get a table definition. |
||
| 231 | * |
||
| 232 | * @param string $table The name of the table. |
||
| 233 | * @return array|null Returns the table definition or null if the table does not exist. |
||
| 234 | */ |
||
| 235 | 66 | final public function fetchTableDef($table) { |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Fetch the table definition from the database. |
||
| 260 | * |
||
| 261 | * @param string $table The name of the table to get. |
||
| 262 | * @return array|null Returns the table def or **null** if the table doesn't exist. |
||
| 263 | */ |
||
| 264 | abstract protected function fetchTableDefDb($table); |
||
| 265 | |||
| 266 | |||
| 267 | /** |
||
| 268 | * Get the column definitions for a table. |
||
| 269 | * |
||
| 270 | * @param string $table The name of the table to get the columns for. |
||
| 271 | * @return array|null Returns an array of column definitions. |
||
| 272 | */ |
||
| 273 | 1 | final public function fetchColumnDefs($table) { |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Get the column definitions from the database. |
||
| 291 | * |
||
| 292 | * @param string $table The name of the table to fetch the columns for. |
||
| 293 | * @return array|null |
||
| 294 | */ |
||
| 295 | abstract protected function fetchColumnDefsDb($table); |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Get the canonical type based on a type string. |
||
| 299 | * |
||
| 300 | * @param string $type A type string. |
||
| 301 | * @return array|null Returns the type schema array or **null** if a type isn't found. |
||
| 302 | */ |
||
| 303 | 51 | public static function typeDef($type) { |
|
| 304 | // Check for the unsigned signifier. |
||
| 305 | 51 | $unsigned = null; |
|
| 306 | 51 | if ($type[0] === 'u') { |
|
| 307 | 6 | $unsigned = true; |
|
| 308 | 6 | $type = substr($type, 1); |
|
| 309 | 49 | } elseif (preg_match('`(.+)\s+unsigned`i', $type, $m)) { |
|
| 310 | 2 | $unsigned = true; |
|
| 311 | 2 | $type = $m[1]; |
|
| 312 | } |
||
| 313 | |||
| 314 | // Remove brackets from the type. |
||
| 315 | 51 | $brackets = null; |
|
| 316 | 51 | if (preg_match('`^(.*)\((.*)\)$`', $type, $m)) { |
|
| 317 | 35 | $brackets = $m[2]; |
|
| 318 | 35 | $type = $m[1]; |
|
| 319 | } |
||
| 320 | |||
| 321 | // Look for the type. |
||
| 322 | 51 | $type = strtolower($type); |
|
| 323 | 51 | if (isset(self::$types[$type])) { |
|
| 324 | 51 | $row = self::$types[$type]; |
|
| 325 | 51 | $dbtype = $type; |
|
| 326 | |||
| 327 | // Resolve an alias. |
||
| 328 | 51 | if (is_string($row)) { |
|
| 329 | 2 | $dbtype = $row; |
|
| 330 | 51 | $row = self::$types[$row]; |
|
| 331 | } |
||
| 332 | } else { |
||
| 333 | return null; |
||
| 334 | } |
||
| 335 | |||
| 336 | // Now that we have a type row we can build a schema for it. |
||
| 337 | $schema = [ |
||
| 338 | 51 | 'type' => $row['type'], |
|
| 339 | 51 | 'dbtype' => $dbtype |
|
| 340 | ]; |
||
| 341 | |||
| 342 | 51 | if (!empty($row['schema'])) { |
|
| 343 | 40 | $schema += $row['schema']; |
|
| 344 | } |
||
| 345 | |||
| 346 | 51 | if ($row['type'] === 'integer' && $unsigned) { |
|
| 347 | 6 | $schema['unsigned'] = true; |
|
| 348 | |||
| 349 | 6 | if (!empty($schema['maximum'])) { |
|
| 350 | 6 | $schema['maximum'] = $schema['maximum'] * 2 + 1; |
|
| 351 | 6 | $schema['minimum'] = 0; |
|
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | 51 | if (!empty($row['length'])) { |
|
| 356 | 28 | $schema['maxLength'] = (int)$brackets ?: 255; |
|
| 357 | } |
||
| 358 | |||
| 359 | 51 | if (!empty($row['precision'])) { |
|
| 360 | 2 | $parts = array_map('trim', explode(',', $brackets)); |
|
| 361 | 2 | $schema['precision'] = (int)$parts[0]; |
|
| 362 | 2 | if (isset($parts[1])) { |
|
| 363 | 2 | $schema['scale'] = (int)$parts[1]; |
|
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | 51 | if (!empty($row['enum'])) { |
|
| 368 | 1 | $enum = explode(',', $brackets); |
|
| 369 | 1 | $schema['enum'] = array_map(function ($str) { |
|
| 370 | 1 | return trim($str, "'\" \t\n\r\0\x0B"); |
|
| 371 | 1 | }, $enum); |
|
| 372 | } |
||
| 373 | |||
| 374 | 51 | return $schema; |
|
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Get the database type string from a type definition. |
||
| 379 | * |
||
| 380 | * This is the opposite of {@link Db::typeDef()}. |
||
| 381 | * |
||
| 382 | * @param array $typeDef The type definition array. |
||
| 383 | * @return string Returns a db type string. |
||
| 384 | */ |
||
| 385 | 30 | protected static function dbType(array $typeDef) { |
|
| 386 | 30 | $dbtype = $typeDef['dbtype']; |
|
| 387 | |||
| 388 | 30 | if (!empty($typeDef['maxLength'])) { |
|
| 389 | 16 | $dbtype .= "({$typeDef['maxLength']})"; |
|
| 390 | 28 | } elseif (!empty($typeDef['unsigned'])) { |
|
| 391 | $dbtype = 'u'.$dbtype; |
||
| 392 | 28 | } elseif (!empty($typeDef['precision'])) { |
|
| 393 | $dbtype .= "({$typeDef['precision']}"; |
||
| 394 | if (!empty($typeDef['scale'])) { |
||
| 395 | $dbtype .= ",{$typeDef['scale']}"; |
||
| 396 | } |
||
| 397 | $dbtype .= ')'; |
||
| 398 | 28 | } elseif (!empty($typeDef['enum'])) { |
|
| 399 | $parts = array_map(function ($str) { |
||
| 400 | return "'{$str}'"; |
||
| 401 | }, $typeDef['enum']); |
||
| 402 | $dbtype .= '('.implode(',', $parts).')'; |
||
| 403 | } |
||
| 404 | 30 | return $dbtype; |
|
| 405 | } |
||
| 406 | |||
| 407 | |||
| 408 | /** |
||
| 409 | * Get the native database type based on a type schema. |
||
| 410 | * |
||
| 411 | * The default implementation of this method returns the canonical db types. Individual database classes will have |
||
| 412 | * to override to provide any differences. |
||
| 413 | * |
||
| 414 | * @param array $type The type schema. |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | abstract protected function nativeDbType(array $type); |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Set a table definition to the database. |
||
| 421 | * |
||
| 422 | * @param array $tableDef The table definition. |
||
| 423 | * @param array $options An array of additional options when adding the table. |
||
| 424 | */ |
||
| 425 | 66 | final public function defineTable(array $tableDef, array $options = []) { |
|
| 503 | |||
| 504 | /** |
||
| 505 | * Find the primary key in an array of indexes. |
||
| 506 | * |
||
| 507 | * @param array $indexes The indexes to search. |
||
| 508 | * @return array|null Returns the primary key or **null** if there isn't one. |
||
| 509 | */ |
||
| 510 | 53 | protected function findPrimaryKeyIndex(array $indexes) { |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Move the primary key index into the correct place for database drivers. |
||
| 521 | * |
||
| 522 | * @param string $tableName The name of the table. |
||
| 523 | * @param array &$tableDef The table definition. |
||
| 524 | * @param array|null $curTableDef The current database table def used to resolve conflicts in some names. |
||
| 525 | * @throws \Exception Throws an exception when there is a mismatch between the primary index and the primary key |
||
| 526 | * defined on the columns themselves. |
||
| 527 | */ |
||
| 528 | 66 | private function fixIndexes($tableName, array &$tableDef, $curTableDef = null) { |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Get the database prefix. |
||
| 576 | * |
||
| 577 | * @return string Returns the current db prefix. |
||
| 578 | */ |
||
| 579 | 2 | public function getPx() { |
|
| 582 | |||
| 583 | /** |
||
| 584 | * Set the database prefix. |
||
| 585 | * |
||
| 586 | * @param string $px The new database prefix. |
||
| 587 | */ |
||
| 588 | public function setPx($px) { |
||
| 589 | $this->px = $px; |
||
| 590 | } |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Compare two index definitions to see if they have the same columns and same type. |
||
| 594 | * |
||
| 595 | * @param array $a The first index. |
||
| 596 | * @param array $b The second index. |
||
| 597 | * @return int Returns an integer less than, equal to, or greater than zero if {@link $a} is |
||
| 598 | * considered to be respectively less than, equal to, or greater than {@link $b}. |
||
| 599 | */ |
||
| 600 | 45 | private function indexCompare(array $a, array $b) { |
|
| 612 | |||
| 613 | /** |
||
| 614 | * Get data from the database. |
||
| 615 | * |
||
| 616 | * @param string|Identifier $table The name of the table to get the data from. |
||
| 617 | * @param array $where An array of where conditions. |
||
| 618 | * @param array $options An array of additional options. |
||
| 619 | * @return \PDOStatement Returns the result set. |
||
| 620 | */ |
||
| 621 | abstract public function get($table, array $where, array $options = []); |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Get a single row from the database. |
||
| 625 | * |
||
| 626 | * This is a convenience method that calls {@link Db::get()} and shifts off the first row. |
||
| 627 | * |
||
| 628 | * @param string|Identifier $table The name of the table to get the data from. |
||
| 629 | * @param array $where An array of where conditions. |
||
| 630 | * @param array $options An array of additional options. |
||
| 631 | * @return array|object|null Returns the row or false if there is no row. |
||
| 632 | */ |
||
| 633 | 16 | final public function getOne($table, array $where, array $options = []) { |
|
| 639 | |||
| 640 | /** |
||
| 641 | * Insert a row into a table. |
||
| 642 | * |
||
| 643 | * @param string $table The name of the table to insert into. |
||
| 644 | * @param array $row The row of data to insert. |
||
| 645 | * @param array $options An array of options for the insert. |
||
| 646 | * |
||
| 647 | * Db::OPTION_IGNORE |
||
| 648 | * : Whether or not to ignore inserts that lead to a duplicate key. *default false* |
||
| 649 | * Db::OPTION_REPLACE |
||
| 650 | * : Whether or not to replace duplicate keys. *default false* |
||
| 651 | * Db::OPTION_UPSERT |
||
| 652 | * : Whether or not to update the existing data when duplicate keys exist. |
||
| 653 | * |
||
| 654 | * @return mixed Returns the id of the inserted record, **true** if the table doesn't have an auto increment, or **false** otherwise. |
||
| 655 | * @see Db::load() |
||
| 656 | */ |
||
| 657 | abstract public function insert($table, array $row, array $options = []); |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Load many rows into a table. |
||
| 661 | * |
||
| 662 | * @param string $table The name of the table to insert into. |
||
| 663 | * @param \Traversable|array $rows A dataset to insert. |
||
| 664 | * Note that all rows must contain the same columns. |
||
| 665 | * The first row will be looked at for the structure of the insert and the rest of the rows will use this structure. |
||
| 666 | * @param array $options An array of options for the inserts. See {@link Db::insert()} for details. |
||
| 667 | * @see Db::insert() |
||
| 668 | */ |
||
| 669 | public function load($table, $rows, array $options = []) { |
||
| 674 | |||
| 675 | |||
| 676 | /** |
||
| 677 | * Update a row or rows in a table. |
||
| 678 | * |
||
| 679 | * @param string $table The name of the table to update. |
||
| 680 | * @param array $set The values to set. |
||
| 681 | * @param array $where The where filter for the update. |
||
| 682 | * @param array $options An array of options for the update. |
||
| 683 | * @return int Returns the number of affected rows. |
||
| 684 | */ |
||
| 685 | abstract public function update($table, array $set, array $where, array $options = []); |
||
| 686 | |||
| 687 | /** |
||
| 688 | * Delete rows from a table. |
||
| 689 | * |
||
| 690 | * @param string $table The name of the table to delete from. |
||
| 691 | * @param array $where The where filter of the delete. |
||
| 692 | * @param array $options An array of options. |
||
| 693 | * |
||
| 694 | * Db:OPTION_TRUNCATE |
||
| 695 | * : Truncate the table instead of deleting rows. In this case {@link $where} must be blank. |
||
| 696 | * @return int Returns the number of affected rows. |
||
| 697 | */ |
||
| 698 | abstract public function delete($table, array $where, array $options = []); |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Reset the internal table definition cache. |
||
| 702 | * |
||
| 703 | * @return $this |
||
| 704 | */ |
||
| 705 | 10 | public function reset() { |
|
| 710 | |||
| 711 | /** |
||
| 712 | * Build a standardized index name from an index definition. |
||
| 713 | * |
||
| 714 | * @param string $tableName The name of the table the index is in. |
||
| 715 | * @param array $indexDef The index definition. |
||
| 716 | * @return string Returns the index name. |
||
| 717 | */ |
||
| 718 | 64 | protected function buildIndexName($tableName, array $indexDef) { |
|
| 731 | |||
| 732 | /** |
||
| 733 | * Execute a query that fetches data. |
||
| 734 | * |
||
| 735 | * @param string $sql The query to execute. |
||
| 736 | * @param array $params Input parameters for the query. |
||
| 737 | * @param array $options Additional options. |
||
| 738 | * @return \PDOStatement Returns the result of the query. |
||
| 739 | * @throws \PDOException Throws an exception if something went wrong during the query. |
||
| 740 | */ |
||
| 741 | 89 | protected function query($sql, array $params = [], array $options = []) { |
|
| 763 | |||
| 764 | /** |
||
| 765 | * Query the database and return a row count. |
||
| 766 | * |
||
| 767 | * @param string $sql The query to execute. |
||
| 768 | * @param array $params Input parameters for the query. |
||
| 769 | * @param array $options Additional options. |
||
| 770 | * @return int |
||
| 771 | */ |
||
| 772 | 38 | protected function queryModify($sql, array $params = [], array $options = []) { |
|
| 777 | |||
| 778 | /** |
||
| 779 | * Query the database and return the ID of the record that was inserted. |
||
| 780 | * |
||
| 781 | * @param string $sql The query to execute. |
||
| 782 | * @param array $params Input parameters for the query. |
||
| 783 | * @param array $options Additional options. |
||
| 784 | * @return mixed Returns the record ID. |
||
| 785 | */ |
||
| 786 | 17 | protected function queryID($sql, array $params = [], array $options = []) { |
|
| 793 | |||
| 794 | /** |
||
| 795 | * Query the database for a database define. |
||
| 796 | * |
||
| 797 | * @param string $sql The query to execute. |
||
| 798 | * @param array $options Additional options. |
||
| 799 | */ |
||
| 800 | 30 | protected function queryDefine($sql, array $options = []) { |
|
| 804 | |||
| 805 | /** |
||
| 806 | * Safely get a value out of an array. |
||
| 807 | * |
||
| 808 | * This function will always return a value even if the array key doesn't exist. |
||
| 809 | * The self::val() function is one of the biggest workhorses of Vanilla and shows up a lot throughout other code. |
||
| 810 | * It's much preferable to use this function if your not sure whether or not an array key exists rather than |
||
| 811 | * using @ error suppression. |
||
| 812 | * |
||
| 813 | * This function uses optimizations found in the [facebook libphputil library](https://github.com/facebook/libphutil). |
||
| 814 | * |
||
| 815 | * @param string|int $key The array key. |
||
| 816 | * @param array|object $array The array to get the value from. |
||
| 817 | * @param mixed $default The default value to return if the key doesn't exist. |
||
| 818 | * @return mixed The item from the array or `$default` if the array key doesn't exist. |
||
| 819 | * @category Array Functions |
||
| 820 | */ |
||
| 821 | 84 | protected static function val($key, $array, $default = null) { |
|
| 844 | |||
| 845 | /** |
||
| 846 | * Escape an identifier. |
||
| 847 | * |
||
| 848 | * @param string|Literal $identifier The identifier to escape. |
||
| 849 | * @return string Returns the field properly escaped. |
||
| 850 | */ |
||
| 851 | 93 | public function escape($identifier) { |
|
| 857 | |||
| 858 | /** |
||
| 859 | * Escape a a like string so that none of its characters work as wildcards. |
||
| 860 | * |
||
| 861 | * @param string $str The string to escape. |
||
| 862 | * @return string Returns an escaped string. |
||
| 863 | */ |
||
| 864 | 2 | protected function escapeLike($str) { |
|
| 867 | |||
| 868 | /** |
||
| 869 | * Prefix a table name. |
||
| 870 | * |
||
| 871 | * @param string|Identifier $table The name of the table to prefix. |
||
| 872 | * @param bool $escape Whether or not to escape the output. |
||
| 873 | * @return string Returns a full table name. |
||
| 874 | */ |
||
| 875 | 93 | protected function prefixTable($table, $escape = true) { |
|
| 883 | |||
| 884 | /** |
||
| 885 | * Strip the database prefix off a table name. |
||
| 886 | * |
||
| 887 | * @param string $table The name of the table to strip. |
||
| 888 | * @return string Returns the table name stripped of the prefix. |
||
| 889 | */ |
||
| 890 | 2 | protected function stripPrefix($table) { |
|
| 897 | |||
| 898 | /** |
||
| 899 | * Optionally quote a where value. |
||
| 900 | * |
||
| 901 | * @param mixed $value The value to quote. |
||
| 902 | * @param string $column The column being operated on. It must already be quoted. |
||
| 903 | * @return string Returns the value, optionally quoted. |
||
| 904 | * @internal param bool $quote Whether or not to quote the value. |
||
| 905 | */ |
||
| 906 | 42 | public function quote($value, $column = '') { |
|
| 914 | |||
| 915 | /** |
||
| 916 | * Gets the {@link PDO} object for this connection. |
||
| 917 | * |
||
| 918 | * @return \PDO |
||
| 919 | */ |
||
| 920 | 93 | public function getPDO() { |
|
| 923 | |||
| 924 | /** |
||
| 925 | * Set the connection to the database. |
||
| 926 | * |
||
| 927 | * @param PDO $pdo The new connection to the database. |
||
| 928 | * @return $this |
||
| 929 | */ |
||
| 930 | public function setPDO(PDO $pdo) { |
||
| 934 | } |
||
| 935 |
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@returndoc comment to communicate to implementors of these methods what they are expected to return.