1<?php
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP
6 *
7 * This content is released under the MIT License (MIT)
8 *
9 * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
10 *
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:
17 *
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
31 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
32 * @copyright	Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33 * @license	https://opensource.org/licenses/MIT	MIT License
34 * @link	https://codeigniter.com
35 * @since	Version 1.4.1
36 * @filesource
37 */
38defined('BASEPATH') OR exit('No direct script access allowed');
39
40/**
41 * oci8 Result Class
42 *
43 * This class extends the parent result class: CI_DB_result
44 *
45 * @category	Database
46 * @author		EllisLab Dev Team
47 * @link		https://codeigniter.com/user_guide/database/
48 */
49class CI_DB_oci8_result extends CI_DB_result {
50
51	/**
52	 * Statement ID
53	 *
54	 * @var	resource
55	 */
56	public $stmt_id;
57
58	/**
59	 * Cursor ID
60	 *
61	 * @var	resource
62	 */
63	public $curs_id;
64
65	/**
66	 * Limit used flag
67	 *
68	 * @var	bool
69	 */
70	public $limit_used;
71
72	/**
73	 * Commit mode flag
74	 *
75	 * @var	int
76	 */
77	public $commit_mode;
78
79	// --------------------------------------------------------------------
80
81	/**
82	 * Class constructor
83	 *
84	 * @param	object	&$driver_object
85	 * @return	void
86	 */
87	public function __construct(&$driver_object)
88	{
89		parent::__construct($driver_object);
90
91		$this->stmt_id = $driver_object->stmt_id;
92		$this->curs_id = $driver_object->curs_id;
93		$this->limit_used = $driver_object->limit_used;
94		$this->commit_mode =& $driver_object->commit_mode;
95		$driver_object->stmt_id = FALSE;
96	}
97
98	// --------------------------------------------------------------------
99
100	/**
101	 * Number of fields in the result set
102	 *
103	 * @return	int
104	 */
105	public function num_fields()
106	{
107		$count = oci_num_fields($this->stmt_id);
108
109		// if we used a limit we subtract it
110		return ($this->limit_used) ? $count - 1 : $count;
111	}
112
113	// --------------------------------------------------------------------
114
115	/**
116	 * Fetch Field Names
117	 *
118	 * Generates an array of column names
119	 *
120	 * @return	array
121	 */
122	public function list_fields()
123	{
124		$field_names = array();
125		for ($c = 1, $fieldCount = $this->num_fields(); $c <= $fieldCount; $c++)
126		{
127			$field_names[] = oci_field_name($this->stmt_id, $c);
128		}
129		return $field_names;
130	}
131
132	// --------------------------------------------------------------------
133
134	/**
135	 * Field data
136	 *
137	 * Generates an array of objects containing field meta-data
138	 *
139	 * @return	array
140	 */
141	public function field_data()
142	{
143		$retval = array();
144		for ($c = 1, $fieldCount = $this->num_fields(); $c <= $fieldCount; $c++)
145		{
146			$F		= new stdClass();
147			$F->name	= oci_field_name($this->stmt_id, $c);
148			$F->type	= oci_field_type($this->stmt_id, $c);
149			$F->max_length	= oci_field_size($this->stmt_id, $c);
150
151			$retval[] = $F;
152		}
153
154		return $retval;
155	}
156
157	// --------------------------------------------------------------------
158
159	/**
160	 * Free the result
161	 *
162	 * @return	void
163	 */
164	public function free_result()
165	{
166		if (is_resource($this->result_id))
167		{
168			oci_free_statement($this->result_id);
169			$this->result_id = FALSE;
170		}
171
172		if (is_resource($this->stmt_id))
173		{
174			oci_free_statement($this->stmt_id);
175		}
176
177		if (is_resource($this->curs_id))
178		{
179			oci_cancel($this->curs_id);
180			$this->curs_id = NULL;
181		}
182	}
183
184	// --------------------------------------------------------------------
185
186	/**
187	 * Result - associative array
188	 *
189	 * Returns the result set as an array
190	 *
191	 * @return	array
192	 */
193	protected function _fetch_assoc()
194	{
195		$id = ($this->curs_id) ? $this->curs_id : $this->stmt_id;
196		return oci_fetch_assoc($id);
197	}
198
199	// --------------------------------------------------------------------
200
201	/**
202	 * Result - object
203	 *
204	 * Returns the result set as an object
205	 *
206	 * @param	string	$class_name
207	 * @return	object
208	 */
209	protected function _fetch_object($class_name = 'stdClass')
210	{
211		$row = ($this->curs_id)
212			? oci_fetch_object($this->curs_id)
213			: oci_fetch_object($this->stmt_id);
214
215		if ($class_name === 'stdClass' OR ! $row)
216		{
217			return $row;
218		}
219
220		$class_name = new $class_name();
221		foreach ($row as $key => $value)
222		{
223			$class_name->$key = $value;
224		}
225
226		return $class_name;
227	}
228
229}
230