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 | * |
| 5 | * An open source application development framework for PHP 5.1.6 or newer |
| 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 | { |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 163 | for($i = 0; $i < $this->num_fields(); $i++) |
| 164 | { |
| 165 | $data[] = $this->result_id->getColumnMeta($i); |
| 166 | } |
| 167 | |
| 168 | return $data; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 169 | } |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 170 | catch (Exception $e) |
| 171 | { |
| 172 | if ($this->db->db_debug) |
| 173 | { |
| 174 | return $this->db->display_error('db_unsuported_feature'); |
| 175 | } |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame^] | 176 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 177 | return FALSE; |
| 178 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // -------------------------------------------------------------------- |
| 182 | |
| 183 | /** |
| 184 | * Free the result |
| 185 | * |
| 186 | * @return null |
| 187 | */ |
| 188 | function free_result() |
| 189 | { |
Timothy Warren | 6a450cf | 2011-08-23 12:46:11 -0400 | [diff] [blame] | 190 | if (is_object($this->result_id)) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 191 | { |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 192 | $this->result_id = FALSE; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // -------------------------------------------------------------------- |
| 197 | |
| 198 | /** |
| 199 | * Data Seek |
| 200 | * |
| 201 | * Moves the internal pointer to the desired offset. We call |
| 202 | * this internally before fetching results to make sure the |
| 203 | * result set starts at zero |
| 204 | * |
| 205 | * @access private |
| 206 | * @return array |
| 207 | */ |
| 208 | function _data_seek($n = 0) |
| 209 | { |
| 210 | return FALSE; |
| 211 | } |
| 212 | |
| 213 | // -------------------------------------------------------------------- |
| 214 | |
| 215 | /** |
| 216 | * Result - associative array |
| 217 | * |
| 218 | * Returns the result set as an array |
| 219 | * |
| 220 | * @access private |
| 221 | * @return array |
| 222 | */ |
| 223 | function _fetch_assoc() |
| 224 | { |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 225 | return $this->result_id->fetch(PDO::FETCH_ASSOC); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // -------------------------------------------------------------------- |
| 229 | |
| 230 | /** |
| 231 | * Result - object |
| 232 | * |
| 233 | * Returns the result set as an object |
| 234 | * |
| 235 | * @access private |
| 236 | * @return object |
| 237 | */ |
| 238 | function _fetch_object() |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 239 | { |
| 240 | return $this->result_id->fetchObject(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | } |
| 244 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 245 | /* End of file pdo_result.php */ |
| 246 | /* Location: ./system/database/drivers/pdo/pdo_result.php */ |