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