| @@ 121-149 (lines=29) @@ | ||
| 118 | * @param string $className Class name if we want to create object |
|
| 119 | * @return array Collection of arrays or objects |
|
| 120 | */ |
|
| 121 | public function &fetch($sql, $className = null) |
|
| 122 | { |
|
| 123 | // Return value |
|
| 124 | $result = array(); |
|
| 125 | ||
| 126 | if (isset($this->driver)) { |
|
| 127 | // Store timestamp |
|
| 128 | $tsLast = microtime(true); |
|
| 129 | ||
| 130 | try { |
|
| 131 | // Perform database query |
|
| 132 | if (!isset($className)) { // Return array |
|
| 133 | $result = $this->driver->query($sql)->fetchAll(\PDO::FETCH_ASSOC); |
|
| 134 | } else { // Create object of passed class name |
|
| 135 | $result = $this->driver->query($sql)->fetchAll(\PDO::FETCH_CLASS, $className, array(&$this)); |
|
| 136 | } |
|
| 137 | } catch (\PDOException $e) { |
|
| 138 | echo("\n" . $sql . '-' . $e->getMessage()); |
|
| 139 | } |
|
| 140 | ||
| 141 | // Store queries count |
|
| 142 | $this->count++; |
|
| 143 | ||
| 144 | // Count elapsed time |
|
| 145 | $this->elapsed += microtime(true) - $tsLast; |
|
| 146 | } |
|
| 147 | ||
| 148 | return $result; |
|
| 149 | } |
|
| 150 | ||
| 151 | /** |
|
| 152 | * Special accelerated function to retrieve db record fields instead of objects |
|
| @@ 201-230 (lines=30) @@ | ||
| 198 | * @param string $className Class name if we want to create object |
|
| 199 | * @return array|object Record as array or object |
|
| 200 | */ |
|
| 201 | public function &fetchOne($sql, $className = null) |
|
| 202 | { |
|
| 203 | // Return value, configure to return correct type |
|
| 204 | $result = isset($className) ? new \stdClass() : array(); |
|
| 205 | ||
| 206 | if (isset($this->driver)) { |
|
| 207 | // Store timestamp |
|
| 208 | $tsLast = microtime(true); |
|
| 209 | ||
| 210 | try { |
|
| 211 | // Perform database query |
|
| 212 | if (!isset($className)) { // Return array |
|
| 213 | $result = $this->driver->query($sql)->fetch(\PDO::FETCH_ASSOC); |
|
| 214 | } else { // Create object of passed class name |
|
| 215 | $result = $this->driver->query($sql)->fetchObject($className, array(&$this)); |
|
| 216 | } |
|
| 217 | ||
| 218 | } catch (\PDOException $e) { |
|
| 219 | echo("\n" . $sql . '-' . $e->getMessage()); |
|
| 220 | } |
|
| 221 | ||
| 222 | // Store queries count |
|
| 223 | $this->count++; |
|
| 224 | ||
| 225 | // Count elapsed time |
|
| 226 | $this->elapsed += microtime(true) - $tsLast; |
|
| 227 | } |
|
| 228 | ||
| 229 | return $result; |
|
| 230 | } |
|
| 231 | ||
| 232 | /** |
|
| 233 | * Get collection record from database by its field value |
|