Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 23 | * @link http://codeigniter.com |
Timothy Warren | 018af7a | 2011-09-07 12:07:35 -0400 | [diff] [blame] | 24 | * @since Version 2.1.0 |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 25 | * @filesource |
| 26 | */ |
| 27 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 28 | /** |
| 29 | * PDO Result Class |
| 30 | * |
| 31 | * This class extends the parent result class: CI_DB_result |
| 32 | * |
| 33 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 35 | * @link http://codeigniter.com/user_guide/database/ |
| 36 | */ |
| 37 | class CI_DB_pdo_result extends CI_DB_result { |
| 38 | |
| 39 | /** |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 40 | * @var bool Hold the flag whether a result handler already fetched before |
| 41 | */ |
| 42 | protected $is_fetched = FALSE; |
| 43 | |
| 44 | /** |
| 45 | * @var mixed Hold the fetched assoc array of a result handler |
| 46 | */ |
| 47 | protected $result_assoc; |
| 48 | |
| 49 | /** |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 50 | * Number of rows in the result set |
| 51 | * |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 52 | * @return int |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 53 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 54 | public function num_rows() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 55 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 56 | if (empty($this->result_id) OR ! is_object($this->result_id)) |
| 57 | { |
| 58 | // invalid result handler |
| 59 | return 0; |
| 60 | } |
| 61 | elseif (($num_rows = $this->result_id->rowCount()) && $num_rows > 0) |
| 62 | { |
| 63 | // If rowCount return something, we're done. |
| 64 | return $num_rows; |
| 65 | } |
| 66 | |
| 67 | // Fetch the result, instead perform another extra query |
| 68 | return ($this->is_fetched && is_array($this->result_assoc)) ? count($this->result_assoc) : count($this->result_assoc()); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Fetch the result handler |
| 73 | * |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 74 | * @return mixed |
| 75 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 76 | public function result_assoc() |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 77 | { |
| 78 | // If the result already fetched before, use that one |
| 79 | if (count($this->result_array) > 0 OR $this->is_fetched) |
| 80 | { |
| 81 | return $this->result_array(); |
| 82 | } |
| 83 | |
| 84 | // Define the output |
| 85 | $output = array('assoc', 'object'); |
| 86 | |
| 87 | // Fetch the result |
| 88 | foreach ($output as $type) |
| 89 | { |
| 90 | // Define the method and handler |
| 91 | $res_method = '_fetch_'.$type; |
| 92 | $res_handler = 'result_'.$type; |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 93 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 94 | $this->$res_handler = array(); |
| 95 | $this->_data_seek(0); |
| 96 | |
| 97 | while ($row = $this->$res_method()) |
| 98 | { |
| 99 | $this->{$res_handler}[] = $row; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // Save this as buffer and marked the fetch flag |
| 104 | $this->result_array = $this->result_assoc; |
| 105 | $this->is_fetched = TRUE; |
| 106 | |
| 107 | return $this->result_assoc; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // -------------------------------------------------------------------- |
| 111 | |
| 112 | /** |
| 113 | * Number of fields in the result set |
| 114 | * |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 115 | * @return int |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 116 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 117 | public function num_fields() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 118 | { |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 119 | return $this->result_id->columnCount(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // -------------------------------------------------------------------- |
| 123 | |
| 124 | /** |
| 125 | * Fetch Field Names |
| 126 | * |
| 127 | * Generates an array of column names |
| 128 | * |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 129 | * @return bool |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 130 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 131 | public function list_fields() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 132 | { |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 133 | if ($this->db->db_debug) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 134 | { |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 135 | return $this->db->display_error('db_unsuported_feature'); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 136 | } |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 137 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 138 | return FALSE; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // -------------------------------------------------------------------- |
| 142 | |
| 143 | /** |
| 144 | * Field data |
| 145 | * |
| 146 | * Generates an array of objects containing field meta-data |
| 147 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 148 | * @return array |
| 149 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 150 | public function field_data() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 151 | { |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 152 | $data = array(); |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 153 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 154 | try |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 155 | { |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 156 | if (strpos($this->result_id->queryString, 'PRAGMA') !== FALSE) |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 157 | { |
Taufan Aditya | 865ce1e | 2012-03-17 02:19:42 +0700 | [diff] [blame] | 158 | foreach ($this->result_array() as $field) |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 159 | { |
| 160 | preg_match('/([a-zA-Z]+)(\(\d+\))?/', $field['type'], $matches); |
| 161 | |
| 162 | $F = new stdClass(); |
| 163 | $F->name = $field['name']; |
| 164 | $F->type = ( ! empty($matches[1])) ? $matches[1] : NULL; |
| 165 | $F->default = NULL; |
| 166 | $F->max_length = ( ! empty($matches[2])) ? preg_replace('/[^\d]/', '', $matches[2]) : NULL; |
| 167 | $F->primary_key = (int) $field['pk']; |
| 168 | $F->pdo_type = NULL; |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 169 | |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 170 | $data[] = $F; |
| 171 | } |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | for($i = 0, $max = $this->num_fields(); $i < $max; $i++) |
| 176 | { |
| 177 | $field = $this->result_id->getColumnMeta($i); |
| 178 | |
| 179 | $F = new stdClass(); |
| 180 | $F->name = $field['name']; |
| 181 | $F->type = $field['native_type']; |
| 182 | $F->default = NULL; |
| 183 | $F->pdo_type = $field['pdo_type']; |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 184 | |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 185 | if ($field['precision'] < 0) |
| 186 | { |
| 187 | $F->max_length = NULL; |
| 188 | $F->primary_key = 0; |
| 189 | } |
| 190 | else |
| 191 | { |
Taufan Aditya | 4a80154 | 2012-03-18 01:00:36 +0700 | [diff] [blame] | 192 | $F->max_length = ($field['len'] > 255) ? 0 : $field['len']; |
Taufan Aditya | 865ce1e | 2012-03-17 02:19:42 +0700 | [diff] [blame] | 193 | $F->primary_key = (int) ( ! empty($field['flags']) && in_array('primary_key', $field['flags'])); |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | $data[] = $F; |
| 197 | } |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 198 | } |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 199 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 200 | return $data; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 201 | } |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 202 | catch (Exception $e) |
| 203 | { |
| 204 | if ($this->db->db_debug) |
| 205 | { |
| 206 | return $this->db->display_error('db_unsuported_feature'); |
| 207 | } |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 208 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 209 | return FALSE; |
| 210 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // -------------------------------------------------------------------- |
| 214 | |
| 215 | /** |
| 216 | * Free the result |
| 217 | * |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 218 | * @return void |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 219 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 220 | public function free_result() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 221 | { |
Timothy Warren | 6a450cf | 2011-08-23 12:46:11 -0400 | [diff] [blame] | 222 | if (is_object($this->result_id)) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 223 | { |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 224 | $this->result_id = FALSE; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | // -------------------------------------------------------------------- |
| 229 | |
| 230 | /** |
| 231 | * Data Seek |
| 232 | * |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 233 | * Moves the internal pointer to the desired offset. We call |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 234 | * this internally before fetching results to make sure the |
| 235 | * result set starts at zero |
| 236 | * |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 237 | * @return bool |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 238 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 239 | protected function _data_seek($n = 0) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 240 | { |
| 241 | return FALSE; |
| 242 | } |
| 243 | |
| 244 | // -------------------------------------------------------------------- |
| 245 | |
| 246 | /** |
| 247 | * Result - associative array |
| 248 | * |
| 249 | * Returns the result set as an array |
| 250 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 251 | * @return array |
| 252 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 253 | protected function _fetch_assoc() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 254 | { |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 255 | return $this->result_id->fetch(PDO::FETCH_ASSOC); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | // -------------------------------------------------------------------- |
| 259 | |
| 260 | /** |
| 261 | * Result - object |
| 262 | * |
| 263 | * Returns the result set as an object |
| 264 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 265 | * @return object |
| 266 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame^] | 267 | protected function _fetch_object() |
| 268 | { |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 269 | return $this->result_id->fetchObject(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | } |
| 273 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 274 | /* End of file pdo_result.php */ |
| 275 | /* Location: ./system/database/drivers/pdo/pdo_result.php */ |