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 | * |
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 |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2014, 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 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 28 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 29 | /** |
| 30 | * PDO Result Class |
| 31 | * |
| 32 | * This class extends the parent result class: CI_DB_result |
| 33 | * |
| 34 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 35 | * @author EllisLab Dev Team |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 36 | * @link http://codeigniter.com/user_guide/database/ |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame] | 37 | * @since 2.1 |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 38 | */ |
| 39 | class CI_DB_pdo_result extends CI_DB_result { |
| 40 | |
| 41 | /** |
| 42 | * Number of rows in the result set |
| 43 | * |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 44 | * @return int |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 45 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 46 | public function num_rows() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 47 | { |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame] | 48 | if (is_int($this->num_rows)) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 49 | { |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame] | 50 | return $this->num_rows; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 51 | } |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame] | 52 | elseif (count($this->result_array) > 0) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 53 | { |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame] | 54 | return $this->num_rows = count($this->result_array); |
| 55 | } |
| 56 | elseif (count($this->result_object) > 0) |
| 57 | { |
| 58 | return $this->num_rows = count($this->result_object); |
| 59 | } |
Andrey Andreev | 7ca3613 | 2012-07-05 16:20:15 +0300 | [diff] [blame] | 60 | elseif (($num_rows = $this->result_id->rowCount()) > 0) |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame] | 61 | { |
Andrey Andreev | 7ca3613 | 2012-07-05 16:20:15 +0300 | [diff] [blame] | 62 | return $this->num_rows = $num_rows; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 63 | } |
| 64 | |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame] | 65 | return $this->num_rows = count($this->result_array()); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | // -------------------------------------------------------------------- |
| 69 | |
| 70 | /** |
| 71 | * Number of fields in the result set |
| 72 | * |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 73 | * @return int |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 74 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 75 | public function num_fields() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 76 | { |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 77 | return $this->result_id->columnCount(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // -------------------------------------------------------------------- |
| 81 | |
| 82 | /** |
| 83 | * Fetch Field Names |
| 84 | * |
| 85 | * Generates an array of column names |
| 86 | * |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 87 | * @return bool |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 88 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 89 | public function list_fields() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 90 | { |
Andrey Andreev | 626f1a6 | 2012-07-04 22:38:52 +0300 | [diff] [blame] | 91 | $field_names = array(); |
| 92 | for ($i = 0, $c = $this->num_fields(); $i < $c; $i++) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 93 | { |
Andrey Andreev | 2bbbd1a | 2014-05-09 10:24:14 +0300 | [diff] [blame] | 94 | // Might trigger an E_WARNING due to not all subdrivers |
| 95 | // supporting getColumnMeta() |
Dionysis Arvanitis | 5df2f1b | 2014-07-05 12:14:56 +0300 | [diff] [blame^] | 96 | $field_names[$i] = @$this->result_id->getColumnMeta($i); |
Andrey Andreev | 626f1a6 | 2012-07-04 22:38:52 +0300 | [diff] [blame] | 97 | $field_names[$i] = $field_names[$i]['name']; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 98 | } |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 99 | |
Andrey Andreev | 626f1a6 | 2012-07-04 22:38:52 +0300 | [diff] [blame] | 100 | return $field_names; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // -------------------------------------------------------------------- |
| 104 | |
| 105 | /** |
| 106 | * Field data |
| 107 | * |
| 108 | * Generates an array of objects containing field meta-data |
| 109 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 110 | * @return array |
| 111 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 112 | public function field_data() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 113 | { |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 114 | try |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 115 | { |
Andrey Andreev | 10ecc84 | 2012-11-16 01:06:20 +0200 | [diff] [blame] | 116 | $retval = array(); |
| 117 | |
| 118 | for ($i = 0, $c = $this->num_fields(); $i < $c; $i++) |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 119 | { |
Andrey Andreev | 10ecc84 | 2012-11-16 01:06:20 +0200 | [diff] [blame] | 120 | $field = $this->result_id->getColumnMeta($i); |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 121 | |
Andrey Andreev | 10ecc84 | 2012-11-16 01:06:20 +0200 | [diff] [blame] | 122 | $retval[$i] = new stdClass(); |
| 123 | $retval[$i]->name = $field['name']; |
| 124 | $retval[$i]->type = $field['native_type']; |
| 125 | $retval[$i]->max_length = ($field['len'] > 0) ? $field['len'] : NULL; |
| 126 | $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] | 127 | } |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 128 | |
Andrey Andreev | 10ecc84 | 2012-11-16 01:06:20 +0200 | [diff] [blame] | 129 | return $retval; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 130 | } |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 131 | catch (Exception $e) |
| 132 | { |
| 133 | if ($this->db->db_debug) |
| 134 | { |
Andrey Andreev | 8d3afde | 2012-11-06 12:53:47 +0200 | [diff] [blame] | 135 | return $this->db->display_error('db_unsupported_feature'); |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 136 | } |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 137 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 138 | return FALSE; |
| 139 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // -------------------------------------------------------------------- |
| 143 | |
| 144 | /** |
| 145 | * Free the result |
| 146 | * |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 147 | * @return void |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 148 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 149 | public function free_result() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 150 | { |
Timothy Warren | 6a450cf | 2011-08-23 12:46:11 -0400 | [diff] [blame] | 151 | if (is_object($this->result_id)) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 152 | { |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 153 | $this->result_id = FALSE; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // -------------------------------------------------------------------- |
| 158 | |
| 159 | /** |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 160 | * Result - associative array |
| 161 | * |
| 162 | * Returns the result set as an array |
| 163 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 164 | * @return array |
| 165 | */ |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 166 | protected function _fetch_assoc() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 167 | { |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 168 | return $this->result_id->fetch(PDO::FETCH_ASSOC); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | // -------------------------------------------------------------------- |
| 172 | |
| 173 | /** |
| 174 | * Result - object |
| 175 | * |
| 176 | * Returns the result set as an object |
| 177 | * |
Andrey Andreev | 8463b91 | 2012-11-02 02:16:28 +0200 | [diff] [blame] | 178 | * @param string $class_name |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 179 | * @return object |
| 180 | */ |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame] | 181 | protected function _fetch_object($class_name = 'stdClass') |
Andrey Andreev | be22c5b | 2012-03-20 23:01:53 +0200 | [diff] [blame] | 182 | { |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame] | 183 | return $this->result_id->fetchObject($class_name); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | } |
| 187 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 188 | /* End of file pdo_result.php */ |
| 189 | /* Location: ./system/database/drivers/pdo/pdo_result.php */ |