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