Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 2.1.0 |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 39 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 40 | /** |
| 41 | * PDO Result Class |
| 42 | * |
| 43 | * This class extends the parent result class: CI_DB_result |
| 44 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 45 | * @package CodeIgniter |
| 46 | * @subpackage Drivers |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 47 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 48 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 49 | * @link https://codeigniter.com/user_guide/database/ |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 50 | */ |
| 51 | class CI_DB_pdo_result extends CI_DB_result { |
| 52 | |
| 53 | /** |
| 54 | * Number of rows in the result set |
| 55 | * |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 56 | * @return int |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 57 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 58 | public function num_rows() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 59 | { |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame] | 60 | if (is_int($this->num_rows)) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 61 | { |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame] | 62 | return $this->num_rows; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 63 | } |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame] | 64 | elseif (count($this->result_array) > 0) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 65 | { |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame] | 66 | return $this->num_rows = count($this->result_array); |
| 67 | } |
| 68 | elseif (count($this->result_object) > 0) |
| 69 | { |
| 70 | return $this->num_rows = count($this->result_object); |
| 71 | } |
Andrey Andreev | 7ca3613 | 2012-07-05 16:20:15 +0300 | [diff] [blame] | 72 | elseif (($num_rows = $this->result_id->rowCount()) > 0) |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame] | 73 | { |
Andrey Andreev | 7ca3613 | 2012-07-05 16:20:15 +0300 | [diff] [blame] | 74 | return $this->num_rows = $num_rows; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 75 | } |
| 76 | |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame] | 77 | return $this->num_rows = count($this->result_array()); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // -------------------------------------------------------------------- |
| 81 | |
| 82 | /** |
| 83 | * Number of fields in the result set |
| 84 | * |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 85 | * @return int |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 86 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 87 | public function num_fields() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 88 | { |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 89 | return $this->result_id->columnCount(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // -------------------------------------------------------------------- |
| 93 | |
| 94 | /** |
| 95 | * Fetch Field Names |
| 96 | * |
| 97 | * Generates an array of column names |
| 98 | * |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 99 | * @return bool |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 100 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 101 | public function list_fields() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 102 | { |
Andrey Andreev | 626f1a6 | 2012-07-04 22:38:52 +0300 | [diff] [blame] | 103 | $field_names = array(); |
| 104 | for ($i = 0, $c = $this->num_fields(); $i < $c; $i++) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 105 | { |
Andrey Andreev | 2bbbd1a | 2014-05-09 10:24:14 +0300 | [diff] [blame] | 106 | // Might trigger an E_WARNING due to not all subdrivers |
| 107 | // supporting getColumnMeta() |
Dionysis Arvanitis | 5df2f1b | 2014-07-05 12:14:56 +0300 | [diff] [blame] | 108 | $field_names[$i] = @$this->result_id->getColumnMeta($i); |
Andrey Andreev | 626f1a6 | 2012-07-04 22:38:52 +0300 | [diff] [blame] | 109 | $field_names[$i] = $field_names[$i]['name']; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 110 | } |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 111 | |
Andrey Andreev | 626f1a6 | 2012-07-04 22:38:52 +0300 | [diff] [blame] | 112 | return $field_names; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // -------------------------------------------------------------------- |
| 116 | |
| 117 | /** |
| 118 | * Field data |
| 119 | * |
| 120 | * Generates an array of objects containing field meta-data |
| 121 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 122 | * @return array |
| 123 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 124 | public function field_data() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 125 | { |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 126 | try |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 127 | { |
Andrey Andreev | 10ecc84 | 2012-11-16 01:06:20 +0200 | [diff] [blame] | 128 | $retval = array(); |
| 129 | |
| 130 | for ($i = 0, $c = $this->num_fields(); $i < $c; $i++) |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 131 | { |
Andrey Andreev | 10ecc84 | 2012-11-16 01:06:20 +0200 | [diff] [blame] | 132 | $field = $this->result_id->getColumnMeta($i); |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 133 | |
Andrey Andreev | 10ecc84 | 2012-11-16 01:06:20 +0200 | [diff] [blame] | 134 | $retval[$i] = new stdClass(); |
| 135 | $retval[$i]->name = $field['name']; |
| 136 | $retval[$i]->type = $field['native_type']; |
| 137 | $retval[$i]->max_length = ($field['len'] > 0) ? $field['len'] : NULL; |
| 138 | $retval[$i]->primary_key = (int) ( ! empty($field['flags']) && in_array('primary_key', $field['flags'], TRUE)); |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 139 | } |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 140 | |
Andrey Andreev | 10ecc84 | 2012-11-16 01:06:20 +0200 | [diff] [blame] | 141 | return $retval; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 142 | } |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 143 | catch (Exception $e) |
| 144 | { |
| 145 | if ($this->db->db_debug) |
| 146 | { |
Andrey Andreev | 8d3afde | 2012-11-06 12:53:47 +0200 | [diff] [blame] | 147 | return $this->db->display_error('db_unsupported_feature'); |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 148 | } |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 149 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 150 | return FALSE; |
| 151 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // -------------------------------------------------------------------- |
| 155 | |
| 156 | /** |
| 157 | * Free the result |
| 158 | * |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 159 | * @return void |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 160 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 161 | public function free_result() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 162 | { |
Timothy Warren | 6a450cf | 2011-08-23 12:46:11 -0400 | [diff] [blame] | 163 | if (is_object($this->result_id)) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 164 | { |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 165 | $this->result_id = FALSE; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // -------------------------------------------------------------------- |
| 170 | |
| 171 | /** |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 172 | * Result - associative array |
| 173 | * |
| 174 | * Returns the result set as an array |
| 175 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 176 | * @return array |
| 177 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 178 | protected function _fetch_assoc() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 179 | { |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 180 | return $this->result_id->fetch(PDO::FETCH_ASSOC); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // -------------------------------------------------------------------- |
| 184 | |
| 185 | /** |
| 186 | * Result - object |
| 187 | * |
| 188 | * Returns the result set as an object |
| 189 | * |
Andrey Andreev | 8463b91 | 2012-11-02 02:16:28 +0200 | [diff] [blame] | 190 | * @param string $class_name |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 191 | * @return object |
| 192 | */ |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame] | 193 | protected function _fetch_object($class_name = 'stdClass') |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 194 | { |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame] | 195 | return $this->result_id->fetchObject($class_name); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | } |