dvdoug /
DB
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | /** |
||
| 5 | * Database Access Layer. |
||
| 6 | * @author Doug Wright |
||
| 7 | */ |
||
| 8 | |||
| 9 | namespace DVDoug\DB; |
||
| 10 | |||
| 11 | use PDO; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * PDO-backed database statement. |
||
| 15 | * @author Doug Wright |
||
| 16 | */ |
||
| 17 | class PDOStatement extends \PDOStatement implements StatementInterface |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Used query string. |
||
| 21 | */ |
||
| 22 | public function getQueryString(): string |
||
| 23 | { |
||
| 24 | return $this->queryString; |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Fetches the next row(s) from the result set as an associative array. |
||
| 29 | * @param bool $aAllRows true to fetch all remaining rows, false for next row only |
||
| 30 | * @param bool $aGroupByFirstCol whether to group the results by the first column |
||
| 31 | * @return array|bool |
||
| 32 | */ |
||
| 33 | 24 | public function fetchAssoc($aAllRows = true, $aGroupByFirstCol = false) |
|
| 34 | { |
||
| 35 | 24 | if ($aAllRows) { |
|
| 36 | 24 | if ($aGroupByFirstCol) { |
|
| 37 | 24 | return $this->fetchAll(PDO::FETCH_ASSOC | PDO::FETCH_GROUP); |
|
| 38 | } else { |
||
| 39 | 16 | return $this->fetchAll(PDO::FETCH_ASSOC); |
|
| 40 | } |
||
| 41 | } else { |
||
| 42 | 16 | return $this->fetch(PDO::FETCH_ASSOC); |
|
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Fetches the next row(s) from a 2-column result set as a key=>value pairs. |
||
| 48 | * @param bool $aAllRows true to fetch all remaining rows, false for next row only |
||
| 49 | * @param bool $aGroupByFirstCol whether to group the results by the first column |
||
| 50 | * @return array|bool |
||
| 51 | */ |
||
| 52 | public function fetchKeyPair($aAllRows = true, $aGroupByFirstCol = false) |
||
| 53 | { |
||
| 54 | if ($aAllRows) { |
||
| 55 | if ($aGroupByFirstCol) { |
||
| 56 | return $this->fetchAll(PDO::FETCH_KEY_PAIR | PDO::FETCH_GROUP); |
||
| 57 | } else { |
||
| 58 | return $this->fetchAll(PDO::FETCH_KEY_PAIR); |
||
| 59 | } |
||
| 60 | } else { |
||
| 61 | return $this->fetch(PDO::FETCH_KEY_PAIR); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Fetches the next row(s) from the result set as an object. |
||
| 67 | * @param string $aClassName object will be created as instance of this type |
||
| 68 | * @param array $aConstructorArguments arguments to pass to constructor |
||
| 69 | * @param bool $aRunConstructorFirst whether the constructor should be run before/after the object's members are set |
||
| 70 | * @param bool $aAllRows true to fetch all remaining rows, false for next row only |
||
| 71 | * @param bool $aGroupByFirstCol whether to group the results by the first column |
||
| 72 | * @return array|bool |
||
| 73 | */ |
||
| 74 | public function fetchObj($aClassName = 'stdClass', $aConstructorArguments = [], $aRunConstructorFirst = true, $aAllRows = true, $aGroupByFirstCol = false) |
||
| 75 | { |
||
| 76 | if ($aRunConstructorFirst) { |
||
| 77 | $this->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, $aClassName, $aConstructorArguments); |
||
| 78 | } else { |
||
| 79 | $this->setFetchMode(PDO::FETCH_CLASS, $aClassName, $aConstructorArguments); |
||
| 80 | } |
||
| 81 | |||
| 82 | if ($aAllRows) { |
||
| 83 | if ($aGroupByFirstCol) { |
||
| 84 | return $this->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_GROUP); |
||
| 85 | } else { |
||
| 86 | return $this->fetchAll(PDO::FETCH_CLASS); |
||
| 87 | } |
||
| 88 | } else { |
||
| 89 | return $this->fetch(PDO::FETCH_CLASS); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Fetches the next row into the specified object. |
||
| 95 | * @param string $aObject object to update |
||
| 96 | * @return array|bool |
||
| 97 | */ |
||
| 98 | public function fetchIntoObj($aObject) |
||
| 99 | { |
||
| 100 | $this->setFetchMode(PDO::FETCH_INTO, $aObject); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 101 | |||
| 102 | return $this->fetch(PDO::FETCH_INTO); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Binds a value to a variable. |
||
| 107 | * @param string $aParam parameter name of the form :name |
||
| 108 | * @param mixed $aVariable mixed The variable to bind to the parameter |
||
| 109 | * @param int $aDataType data type for the parameter using the DatabaseInterface::PARAM_* constants |
||
| 110 | */ |
||
| 111 | public function bindParamToVariable($aParam, &$aVariable, $aDataType = DatabaseInterface::PARAM_IS_STR): bool |
||
| 112 | { |
||
| 113 | return $this->bindParam($aParam, $aVariable, $aDataType); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Binds a column to a variable. |
||
| 118 | * @param string $aColumn column name |
||
| 119 | * @param mixed $aVariable mixed The variable to bind to the result |
||
| 120 | * @param int $aDataType data type for the column using the DatabaseInterface::PARAM_* constants |
||
| 121 | */ |
||
| 122 | public function bindResultColumn($aColumn, &$aVariable, $aDataType = DatabaseInterface::PARAM_IS_STR): bool |
||
| 123 | { |
||
| 124 | return $this->bindColumn($aColumn, $aVariable, $aDataType); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Binds a value to a parameter. |
||
| 129 | * @param string $aParam parameter name of the form :name |
||
| 130 | * @param mixed $aValue mixed The value to bind to the parameter |
||
| 131 | * @param int $aDataType data type for the parameter using the DatabaseInterface::PARAM_* constants |
||
| 132 | */ |
||
| 133 | 20 | public function bindParamToValue($aParam, $aValue, $aDataType = DatabaseInterface::PARAM_IS_STR): bool |
|
| 134 | { |
||
| 135 | 20 | return $this->bindValue($aParam, $aValue, $aDataType); |
|
| 136 | } |
||
| 137 | } |
||
| 138 |