-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResult.php
More file actions
201 lines (179 loc) · 4.49 KB
/
Copy pathResult.php
File metadata and controls
201 lines (179 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* This file is for connecting to a database server
* with the odbc driver installed
*
* @package CodeIgniter
* @author Ronny García Zambrano <rsgarcia0203>
* @copyright Copyright (c) 2022, Ronny García
* @license https://opensource.org/licenses/MIT MIT License
* @link https://github.com/rsgarcia0203/ODBCDriver
* @since Version 4.0.1
*/
namespace CodeIgniter\Database\ODBC;
use CodeIgniter\Database\BaseResult;
use CodeIgniter\Entity\Entity;
use stdClass;
/**
* ODBC Result Class
*
* This class extends the parent result class: CI_DB_result
*
* @package CodeIgniter
* @subpackage Drivers
* @category Database
* @author Ronny García <rsgarcia0203>
* @link https://github.com/rsgarcia0203/ODBCDriver
*/
class Result extends BaseResult
{
/**
* Gets the number of fields in the result set.
*
* @return int
*/
public function getFieldCount(): int
{
return odbc_num_fields($this->resultID);
}
/**
* Generates an array of column names in the result set.
*
* @return array
*/
public function getFieldNames(): array
{
return array_map(fn ($fieldIndex) => odbc_field_name($this->resultID, $fieldIndex), range(1, $this->getFieldCount()));
}
/**
* Generates an array of objects representing field meta-data.
*
* @return array
*/
public function getFieldData(): array
{
return array_map(fn ($fieldIndex) => (object) [
'name' => odbc_field_name($this->resultID, $fieldIndex),
'type' => odbc_field_type($this->resultID, $fieldIndex),
'max_length' => odbc_field_len($this->resultID, $fieldIndex),
], range(1, $this->getFieldCount()));
}
/**
* Frees the current result
*
* @return void
*/
public function freeResult()
{
if (is_resource($this->resultID))
{
odbc_free_result($this->resultID);
$this->resultID = FALSE;
}
}
/**
* Moves the internal pointer to the desired offset. This is called
* internally before fetching results to make sure the result set
* starts at zero.
*
* @return false
*/
public function dataSeek(int $n = 0)
{
// Don´t support Data Seek in ODBC
return false;
}
/**
* Returns the result set as an array.
*
* Overridden by driver classes.
*
* @return mixed
*/
protected function fetchAssoc()
{
return odbc_fetch_array($this->resultID);
}
/**
* Returns the result set as an object.
*
* Overridden by child classes.
*
* @return bool|Entity|object
*/
protected function fetchObject(string $className = 'stdClass')
{
$row = odbc_fetch_object($this->resultID);
if ($className === 'stdClass' OR ! $row)
{
return $row;
}
if (is_subclass_of($className, Entity::class)) {
return (new $className())->setAttributes((array) $row);
}
$instance = new $className();
foreach (get_object_vars($row) as $key => $value)
{
$instance->{$key} = $value;
}
return $instance;
}
}
// --------------------------------------------------------------------
if ( ! function_exists('odbc_fetch_array'))
{
/**
* ODBC Fetch array
*
* Emulates the native odbc_fetch_array() function when
* it is not available (odbc_fetch_array() requires unixODBC)
*
* @param resource &$result
* @param int $rownumber
* @return array|bool
*/
function odbc_fetch_array(&$result, $rownumber = 1)
{
$rs = array();
if ( ! odbc_fetch_into($result, $rs, $rownumber))
{
return FALSE;
}
$rs_assoc = array();
foreach ($rs as $k => $v)
{
$field_name = odbc_field_name($result, $k+1);
$rs_assoc[$field_name] = $v;
}
return $rs_assoc;
}
}
// --------------------------------------------------------------------
if ( ! function_exists('odbc_fetch_object'))
{
/**
* ODBC Fetch object
*
* Emulates the native odbc_fetch_object() function when
* it is not available.
*
* @param resource &$result
* @param int $rownumber
* @return object|bool
*/
function odbc_fetch_object(&$result, $rownumber = 1)
{
$rs = array();
if ( ! odbc_fetch_into($result, $rs, $rownumber))
{
return FALSE;
}
$rs_object = new stdClass();
foreach ($rs as $k => $v)
{
$field_name = odbc_field_name($result, $k+1);
$rs_object->$field_name = $v;
}
return $rs_object;
}
}