blob: 0b3d9c2b4a09aa2410e5502da6bd1a49172a36ac [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev1ff49e02012-01-27 11:30:41 +02008 *
Master Yodada60e9b2016-12-31 08:46:18 -08009 * Copyright (c) 2014 - 2017, British Columbia Institute of Technology
Andrey Andreev1ff49e02012-01-27 11:30:41 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * 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 Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * 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 Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Master Yodada60e9b2016-12-31 08:46:18 -080032 * @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.3.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * MySQLi Result Class
42 *
43 * This class extends the parent result class: CI_DB_result
44 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020045 * @package CodeIgniter
46 * @subpackage Drivers
Derek Allard2067d1a2008-11-13 22:59:24 +000047 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050048 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020049 * @link https://codeigniter.com/user_guide/database/
Derek Allard2067d1a2008-11-13 22:59:24 +000050 */
51class CI_DB_mysqli_result extends CI_DB_result {
Barry Mienydd671972010-10-04 16:33:58 +020052
Derek Allard2067d1a2008-11-13 22:59:24 +000053 /**
54 * Number of rows in the result set
55 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +020056 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +000057 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020058 public function num_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +000059 {
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030060 return is_int($this->num_rows)
61 ? $this->num_rows
62 : $this->num_rows = $this->result_id->num_rows;
Derek Allard2067d1a2008-11-13 22:59:24 +000063 }
Barry Mienydd671972010-10-04 16:33:58 +020064
Derek Allard2067d1a2008-11-13 22:59:24 +000065 // --------------------------------------------------------------------
66
67 /**
68 * Number of fields in the result set
69 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +020070 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +000071 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020072 public function num_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000073 {
Andrey Andreev992f1752012-03-19 16:58:43 +020074 return $this->result_id->field_count;
Derek Allard2067d1a2008-11-13 22:59:24 +000075 }
76
77 // --------------------------------------------------------------------
78
79 /**
80 * Fetch Field Names
81 *
82 * Generates an array of column names
83 *
Derek Allard2067d1a2008-11-13 22:59:24 +000084 * @return array
85 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020086 public function list_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
88 $field_names = array();
Chris Buckleyb835a4f2013-01-28 23:35:13 +000089 $this->result_id->field_seek(0);
Andrey Andreev992f1752012-03-19 16:58:43 +020090 while ($field = $this->result_id->fetch_field())
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
92 $field_names[] = $field->name;
93 }
Barry Mienydd671972010-10-04 16:33:58 +020094
Derek Allard2067d1a2008-11-13 22:59:24 +000095 return $field_names;
96 }
97
98 // --------------------------------------------------------------------
99
100 /**
101 * Field data
102 *
103 * Generates an array of objects containing field meta-data
104 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 * @return array
106 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200107 public function field_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 {
109 $retval = array();
Andrey Andreev992f1752012-03-19 16:58:43 +0200110 $field_data = $this->result_id->fetch_fields();
Andrey Andreeveffd0132012-03-02 12:34:54 +0200111 for ($i = 0, $c = count($field_data); $i < $c; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200112 {
Andrey Andreeveffd0132012-03-02 12:34:54 +0200113 $retval[$i] = new stdClass();
114 $retval[$i]->name = $field_data[$i]->name;
Andrey Andreev9385cfe2017-09-13 13:17:28 +0300115 $retval[$i]->type = static::_get_field_type($field_data[$i]->type);
Andrey Andreeveffd0132012-03-02 12:34:54 +0200116 $retval[$i]->max_length = $field_data[$i]->max_length;
Andrey Andreev9385cfe2017-09-13 13:17:28 +0300117 $retval[$i]->primary_key = (int) ($field_data[$i]->flags & MYSQLI_PRI_KEY_FLAG);
Andrey Andreev10ecc842012-11-16 01:06:20 +0200118 $retval[$i]->default = $field_data[$i]->def;
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 }
Barry Mienydd671972010-10-04 16:33:58 +0200120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 return $retval;
122 }
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200123
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 // --------------------------------------------------------------------
125
126 /**
Andrey Andreev9385cfe2017-09-13 13:17:28 +0300127 * Get field type
128 *
129 * Extracts field type info from the bitflags returned by
130 * mysqli_result::fetch_fields()
131 *
132 * @used-by CI_DB_mysqli_result::field_data()
133 * @param int $flags
134 * @return string
135 */
136 private static function _get_field_type($flags)
137 {
138 static $map;
139 isset($map) OR $map = array(
140 MYSQLI_TYPE_DECIMAL => 'decimal',
141 MYSQLI_TYPE_BIT => 'bit',
142 MYSQLI_TYPE_TINY => 'tinyint',
143 MYSQLI_TYPE_SHORT => 'smallint',
144 MYSQLI_TYPE_INT24 => 'mediumint',
145 MYSQLI_TYPE_LONG => 'int',
146 MYSQLI_TYPE_LONGLONG => 'bigint',
147 MYSQLI_TYPE_FLOAT => 'float',
148 MYSQLI_TYPE_DOUBLE => 'double',
149 MYSQLI_TYPE_TIMESTAMP => 'timestamp',
150 MYSQLI_TYPE_DATE => 'date',
151 MYSQLI_TYPE_TIME => 'time',
152 MYSQLI_TYPE_DATETIME => 'datetime',
153 MYSQLI_TYPE_YEAR => 'year',
154 MYSQLI_TYPE_NEWDATE => 'date',
155 MYSQLI_TYPE_INTERVAL => 'interval',
156 MYSQLI_TYPE_ENUM => 'enum',
157 MYSQLI_TYPE_SET => 'set',
158 MYSQLI_TYPE_TINY_BLOB => 'tinyblob',
159 MYSQLI_TYPE_MEDIUM_BLOB => 'mediumblob',
160 MYSQLI_TYPE_BLOB => 'blob',
161 MYSQLI_TYPE_LONG_BLOB => 'longblob',
162 MYSQLI_TYPE_STRING => 'char',
163 MYSQLI_TYPE_VAR_STRING => 'varchar',
164 MYSQLI_TYPE_GEOMETRY => 'geometry'
165 );
166
167 foreach ($map as $flag => $name)
168 {
169 if ($flags & $flag)
170 {
171 return $name;
172 }
173 }
174
175 return $flags;
176 }
177
178 // --------------------------------------------------------------------
179
180 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 * Free the result
182 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200183 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200184 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200185 public function free_result()
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 {
187 if (is_object($this->result_id))
188 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200189 $this->result_id->free();
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 $this->result_id = FALSE;
191 }
192 }
193
194 // --------------------------------------------------------------------
195
196 /**
197 * Data Seek
198 *
Andrey Andreev8f3566f2012-04-12 14:30:05 +0300199 * Moves the internal pointer to the desired offset. We call
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 * this internally before fetching results to make sure the
Andrey Andreev8463b912012-11-02 02:16:28 +0200201 * result set starts at zero.
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 *
Andrey Andreev8463b912012-11-02 02:16:28 +0200203 * @param int $n
Andrey Andreev8f3566f2012-04-12 14:30:05 +0300204 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 */
Andrey Andreev69edc432012-12-04 13:32:16 +0200206 public function data_seek($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200208 return $this->result_id->data_seek($n);
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 }
210
211 // --------------------------------------------------------------------
212
213 /**
214 * Result - associative array
215 *
216 * Returns the result set as an array
217 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 * @return array
219 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200220 protected function _fetch_assoc()
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200222 return $this->result_id->fetch_assoc();
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 // --------------------------------------------------------------------
226
227 /**
228 * Result - object
229 *
230 * Returns the result set as an object
231 *
Andrey Andreev8463b912012-11-02 02:16:28 +0200232 * @param string $class_name
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 * @return object
234 */
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300235 protected function _fetch_object($class_name = 'stdClass')
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300237 return $this->result_id->fetch_object($class_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 }
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240}