blob: bac07378139ba18de7976dcb20148379a5d80780 [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 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, 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/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, 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()
Andrey Andreeva9194652017-09-27 18:57:46 +0300133 * @param int $type
Andrey Andreev9385cfe2017-09-13 13:17:28 +0300134 * @return string
135 */
Andrey Andreeva9194652017-09-27 18:57:46 +0300136 private static function _get_field_type($type)
Andrey Andreev9385cfe2017-09-13 13:17:28 +0300137 {
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
Andrey Andreeva9194652017-09-27 18:57:46 +0300167 return isset($map[$type]) ? $map[$type] : $type;
Andrey Andreev9385cfe2017-09-13 13:17:28 +0300168 }
169
170 // --------------------------------------------------------------------
171
172 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 * Free the result
174 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200175 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200176 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200177 public function free_result()
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 {
179 if (is_object($this->result_id))
180 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200181 $this->result_id->free();
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 $this->result_id = FALSE;
183 }
184 }
185
186 // --------------------------------------------------------------------
187
188 /**
189 * Data Seek
190 *
Andrey Andreev8f3566f2012-04-12 14:30:05 +0300191 * Moves the internal pointer to the desired offset. We call
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 * this internally before fetching results to make sure the
Andrey Andreev8463b912012-11-02 02:16:28 +0200193 * result set starts at zero.
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 *
Andrey Andreev8463b912012-11-02 02:16:28 +0200195 * @param int $n
Andrey Andreev8f3566f2012-04-12 14:30:05 +0300196 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 */
Andrey Andreev69edc432012-12-04 13:32:16 +0200198 public function data_seek($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200200 return $this->result_id->data_seek($n);
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 }
202
203 // --------------------------------------------------------------------
204
205 /**
206 * Result - associative array
207 *
208 * Returns the result set as an array
209 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 * @return array
211 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200212 protected function _fetch_assoc()
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200214 return $this->result_id->fetch_assoc();
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 // --------------------------------------------------------------------
218
219 /**
220 * Result - object
221 *
222 * Returns the result set as an object
223 *
Andrey Andreev8463b912012-11-02 02:16:28 +0200224 * @param string $class_name
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 * @return object
226 */
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300227 protected function _fetch_object($class_name = 'stdClass')
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300229 return $this->result_id->fetch_object($class_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 }
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232}