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 |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame^] | 24 | * @since Version 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/ |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame^] | 36 | * @since 2.1 |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 37 | */ |
| 38 | class CI_DB_pdo_result extends CI_DB_result { |
| 39 | |
| 40 | /** |
| 41 | * Number of rows in the result set |
| 42 | * |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 43 | * @return int |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 44 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 45 | public function num_rows() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 46 | { |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame^] | 47 | if (is_int($this->num_rows)) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 48 | { |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame^] | 49 | return $this->num_rows; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 50 | } |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame^] | 51 | elseif (count($this->result_array) > 0) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 52 | { |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame^] | 53 | return $this->num_rows = count($this->result_array); |
| 54 | } |
| 55 | elseif (count($this->result_object) > 0) |
| 56 | { |
| 57 | return $this->num_rows = count($this->result_object); |
| 58 | } |
| 59 | elseif (($this->num_rows = $this->restul_id->rowCount()) > 0) |
| 60 | { |
| 61 | return $this->num_rows; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 62 | } |
| 63 | |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame^] | 64 | return $this->num_rows = count($this->result_array()); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // -------------------------------------------------------------------- |
| 68 | |
| 69 | /** |
| 70 | * Number of fields in the result set |
| 71 | * |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 72 | * @return int |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 73 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 74 | public function num_fields() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 75 | { |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 76 | return $this->result_id->columnCount(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // -------------------------------------------------------------------- |
| 80 | |
| 81 | /** |
| 82 | * Fetch Field Names |
| 83 | * |
| 84 | * Generates an array of column names |
| 85 | * |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 86 | * @return bool |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 87 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 88 | public function list_fields() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 89 | { |
Andrey Andreev | 626f1a6 | 2012-07-04 22:38:52 +0300 | [diff] [blame] | 90 | $field_names = array(); |
| 91 | for ($i = 0, $c = $this->num_fields(); $i < $c; $i++) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 92 | { |
Andrey Andreev | 626f1a6 | 2012-07-04 22:38:52 +0300 | [diff] [blame] | 93 | $field_names[$i] = @$this->result_id->getColumnMeta(); |
| 94 | $field_names[$i] = $field_names[$i]['name']; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 95 | } |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 96 | |
Andrey Andreev | 626f1a6 | 2012-07-04 22:38:52 +0300 | [diff] [blame] | 97 | return $field_names; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // -------------------------------------------------------------------- |
| 101 | |
| 102 | /** |
| 103 | * Field data |
| 104 | * |
| 105 | * Generates an array of objects containing field meta-data |
| 106 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 107 | * @return array |
| 108 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 109 | public function field_data() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 110 | { |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 111 | $data = array(); |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 112 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 113 | try |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 114 | { |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 115 | if (strpos($this->result_id->queryString, 'PRAGMA') !== FALSE) |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 116 | { |
Taufan Aditya | 865ce1e | 2012-03-17 02:19:42 +0700 | [diff] [blame] | 117 | foreach ($this->result_array() as $field) |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 118 | { |
| 119 | preg_match('/([a-zA-Z]+)(\(\d+\))?/', $field['type'], $matches); |
| 120 | |
| 121 | $F = new stdClass(); |
| 122 | $F->name = $field['name']; |
| 123 | $F->type = ( ! empty($matches[1])) ? $matches[1] : NULL; |
| 124 | $F->default = NULL; |
| 125 | $F->max_length = ( ! empty($matches[2])) ? preg_replace('/[^\d]/', '', $matches[2]) : NULL; |
| 126 | $F->primary_key = (int) $field['pk']; |
| 127 | $F->pdo_type = NULL; |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 128 | |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 129 | $data[] = $F; |
| 130 | } |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | for($i = 0, $max = $this->num_fields(); $i < $max; $i++) |
| 135 | { |
| 136 | $field = $this->result_id->getColumnMeta($i); |
| 137 | |
| 138 | $F = new stdClass(); |
| 139 | $F->name = $field['name']; |
| 140 | $F->type = $field['native_type']; |
| 141 | $F->default = NULL; |
| 142 | $F->pdo_type = $field['pdo_type']; |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 143 | |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 144 | if ($field['precision'] < 0) |
| 145 | { |
| 146 | $F->max_length = NULL; |
| 147 | $F->primary_key = 0; |
| 148 | } |
| 149 | else |
| 150 | { |
Taufan Aditya | 4a80154 | 2012-03-18 01:00:36 +0700 | [diff] [blame] | 151 | $F->max_length = ($field['len'] > 255) ? 0 : $field['len']; |
Taufan Aditya | 865ce1e | 2012-03-17 02:19:42 +0700 | [diff] [blame] | 152 | $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] | 153 | } |
| 154 | |
| 155 | $data[] = $F; |
| 156 | } |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 157 | } |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 158 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 159 | return $data; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 160 | } |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 161 | catch (Exception $e) |
| 162 | { |
| 163 | if ($this->db->db_debug) |
| 164 | { |
| 165 | return $this->db->display_error('db_unsuported_feature'); |
| 166 | } |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 167 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 168 | return FALSE; |
| 169 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // -------------------------------------------------------------------- |
| 173 | |
| 174 | /** |
| 175 | * Free the result |
| 176 | * |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 177 | * @return void |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 178 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 179 | public function free_result() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 180 | { |
Timothy Warren | 6a450cf | 2011-08-23 12:46:11 -0400 | [diff] [blame] | 181 | if (is_object($this->result_id)) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 182 | { |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 183 | $this->result_id = FALSE; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // -------------------------------------------------------------------- |
| 188 | |
| 189 | /** |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 190 | * Result - associative array |
| 191 | * |
| 192 | * Returns the result set as an array |
| 193 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 194 | * @return array |
| 195 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 196 | protected function _fetch_assoc() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 197 | { |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 198 | return $this->result_id->fetch(PDO::FETCH_ASSOC); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // -------------------------------------------------------------------- |
| 202 | |
| 203 | /** |
| 204 | * Result - object |
| 205 | * |
| 206 | * Returns the result set as an object |
| 207 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 208 | * @return object |
| 209 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 210 | protected function _fetch_object() |
| 211 | { |
Taufan Aditya | 8ca31f3 | 2012-05-04 23:57:46 +0700 | [diff] [blame] | 212 | return $this->result_id->fetch(PDO::FETCH_OBJ); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | } |
| 216 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 217 | /* End of file pdo_result.php */ |
| 218 | /* Location: ./system/database/drivers/pdo/pdo_result.php */ |