commit | author | age
|
c9462d
|
1 |
<?php |
S |
2 |
// vim: set et ts=4 sw=4 fdm=marker: |
|
3 |
// +----------------------------------------------------------------------+ |
|
4 |
// | PHP versions 4 and 5 | |
|
5 |
// +----------------------------------------------------------------------+ |
|
6 |
// | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox, | |
|
7 |
// | Stig. S. Bakken, Lukas Smith | |
|
8 |
// | All rights reserved. | |
|
9 |
// +----------------------------------------------------------------------+ |
|
10 |
// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB | |
|
11 |
// | API as well as database abstraction for PHP applications. | |
|
12 |
// | This LICENSE is in the BSD license style. | |
|
13 |
// | | |
|
14 |
// | Redistribution and use in source and binary forms, with or without | |
|
15 |
// | modification, are permitted provided that the following conditions | |
|
16 |
// | are met: | |
|
17 |
// | | |
|
18 |
// | Redistributions of source code must retain the above copyright | |
|
19 |
// | notice, this list of conditions and the following disclaimer. | |
|
20 |
// | | |
|
21 |
// | Redistributions in binary form must reproduce the above copyright | |
|
22 |
// | notice, this list of conditions and the following disclaimer in the | |
|
23 |
// | documentation and/or other materials provided with the distribution. | |
|
24 |
// | | |
|
25 |
// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, | |
|
26 |
// | Lukas Smith nor the names of his contributors may be used to endorse | |
|
27 |
// | or promote products derived from this software without specific prior| |
|
28 |
// | written permission. | |
|
29 |
// | | |
|
30 |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
|
31 |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
|
32 |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
|
33 |
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
|
34 |
// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
|
35 |
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
|
36 |
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS| |
|
37 |
// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
|
38 |
// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
|
39 |
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY| |
|
40 |
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
|
41 |
// | POSSIBILITY OF SUCH DAMAGE. | |
|
42 |
// +----------------------------------------------------------------------+ |
|
43 |
// | Original QuerySim Concept & ColdFusion Author: Hal Helms | |
|
44 |
// | <hal.helms@teamallaire.com> | |
|
45 |
// | Bert Dawson <bdawson@redbanner.com> | |
|
46 |
// +----------------------------------------------------------------------+ |
|
47 |
// | Original PHP Author: Alan Richmond <arichmond@bigfoot.com> | |
|
48 |
// | David Huyck <b@bombusbee.com> | |
|
49 |
// +----------------------------------------------------------------------+ |
|
50 |
// | Special note concerning code documentation: | |
|
51 |
// | QuerySim was originally created for use during development of | |
|
52 |
// | applications built using the Fusebox framework. (www.fusebox.org) | |
|
53 |
// | Fusebox uses an XML style of documentation called Fusedoc. (Which | |
|
54 |
// | is admittedly not well suited to documenting classes and functions. | |
|
55 |
// | This short-coming is being addressed by the Fusebox community.) PEAR | |
|
56 |
// | uses a Javadoc style of documentation called PHPDoc. (www.phpdoc.de) | |
|
57 |
// | Since this class extension spans two groups of users, it is asked | |
|
58 |
// | that the members of each respect the documentation standard of the | |
|
59 |
// | other. So it is a further requirement that both documentation | |
|
60 |
// | standards be included and maintained. If assistance is required | |
|
61 |
// | please contact Alan Richmond. | |
|
62 |
// +----------------------------------------------------------------------+ |
|
63 |
// |
|
64 |
// $Id$ |
|
65 |
// |
|
66 |
|
|
67 |
/* |
|
68 |
<fusedoc fuse="querysim.php" language="PHP"> |
|
69 |
<responsibilities> |
|
70 |
I take information and turn it into a recordset that can be accessed |
|
71 |
through the PEAR MDB2 API. Based on Hal Helms' QuerySim.cfm ColdFusion |
|
72 |
custom tag available at halhelms.com. |
|
73 |
</responsibilities> |
|
74 |
<properties> |
|
75 |
<property name="API" value="PEAR MDB2" /> |
|
76 |
<property name="version" value="0.2.1" /> |
|
77 |
<property name="status" value="beta" /> |
|
78 |
<history author="Hal Helms" email="hal.helms@teamallaire.com" type="Create" /> |
|
79 |
<history author="Bert Dawson" email="bdawson@redbanner.com" type="Update"> |
|
80 |
Extensive revision that is backwardly compatible but eliminates the |
|
81 |
need for a separate .sim file. |
|
82 |
</history> |
|
83 |
<history author="Alan Richmond" email="arichmond@bigfoot.com" type="Create" date="10-July-2002"> |
|
84 |
Rewrote in PHP as an extention to the PEAR DB API. |
|
85 |
Functions supported: |
|
86 |
connect, disconnect, query, fetchRow, freeResult, |
|
87 |
numCols, numRows, getSpecialQuery |
|
88 |
David Huyck (bombusbee.com) added ability to escape special |
|
89 |
characters (i.e., delimiters) using a '\'. |
|
90 |
Extended PEAR DB options[] for adding incoming parameters. Added |
|
91 |
options: columnDelim, dataDelim, eolDelim |
|
92 |
</history> |
|
93 |
<history author="David Huyck" email="b@bombusbee.com" type="Update" date="19-July-2002"> |
|
94 |
Added the ability to set the QuerySim options at runtime. |
|
95 |
Default options are: |
|
96 |
'columnDelim' => ',', // Commas split the column names |
|
97 |
'dataDelim' => '|', // Pipes split the data fields |
|
98 |
'eolDelim' => chr(13).chr(10) // Carriage returns split the |
|
99 |
// lines of data |
|
100 |
Affected functions are: |
|
101 |
DB_querysim(): set the default options when the |
|
102 |
constructor method is called |
|
103 |
_parseQuerySim($query): altered the parsing of lines, column |
|
104 |
names, and data fields |
|
105 |
_empty2null: altered the way this function is called |
|
106 |
to simplify calling it |
|
107 |
</history> |
|
108 |
<history author="Alan Richmond" email="arichmond@bigfoot.com" type="Update" date="24-July-2002"> |
|
109 |
Added error catching for malformed QuerySim text. |
|
110 |
Bug fix _empty2null(): altered version was returning unmodified |
|
111 |
lineData. |
|
112 |
Cleanup: |
|
113 |
PEAR compliant formatting, finished PHPDocs and added 'out' to |
|
114 |
Fusedoc 'io'. |
|
115 |
Broke up _parseQuerySim() into _buildResult() and _parseOnDelim() |
|
116 |
to containerize duplicate parse code. |
|
117 |
</history> |
|
118 |
<history author="David Huyck" email="b@bombusbee.com" type="Update" date="25-July-2002"> |
|
119 |
Edited the _buildResult() and _parseOnDelim() functions to improve |
|
120 |
reliability of special character escaping. |
|
121 |
Re-introduced a custom setOption() method to throw an error when a |
|
122 |
person tries to set one of the delimiters to '\'. |
|
123 |
</history> |
|
124 |
<history author="Alan Richmond" email="arichmond@bigfoot.com" type="Update" date="27-July-2002"> |
|
125 |
Added '/' delimiter param to preg_quote() in _empty2null() and |
|
126 |
_parseOnDelim() so '/' can be used as a delimiter. |
|
127 |
Added error check for columnDelim == eolDelim or dataDelim == eolDelim. |
|
128 |
Renamed some variables for consistancy. |
|
129 |
</history> |
|
130 |
<history author="Alan Richmond" email="arichmond@bigfoot.com" type="Update" date="30-July-2002"> |
|
131 |
Removed protected function _empty2null(). Turns out preg_split() |
|
132 |
deals with empty elemants by making them zero length strings, just |
|
133 |
what they ended up being anyway. This should speed things up a little. |
|
134 |
Affected functions: |
|
135 |
_parseOnDelim() perform trim on line here, instead of in |
|
136 |
_empty2null(). |
|
137 |
_buildResult() remove call to _empty2null(). |
|
138 |
_empty2null() removed function. |
|
139 |
</history> |
|
140 |
<history author="Alan Richmond" email="arichmond@bigfoot.com" type="Update" date="1-Jan-2003"> |
|
141 |
Ported to PEAR MDB2. |
|
142 |
Methods supported: |
|
143 |
connect, query, getColumnNames, numCols, valid, fetch, |
|
144 |
numRows, free, fetchRow, nextResult, setLimit |
|
145 |
(inherited). |
|
146 |
</history> |
|
147 |
<history |
|
148 |
Removed array_change_key_case() work around for <4.2.0 in |
|
149 |
getColumnNames(), found it already done in MDB2/Common.php. |
|
150 |
</history> |
|
151 |
<history author="Alan Richmond" email="arichmond@bigfoot.com" type="Update" date="3-Feb-2003"> |
|
152 |
Changed default eolDelim to a *nix file eol, since we're trimming |
|
153 |
the result anyway, it makes no difference for Windows. Now only |
|
154 |
Mac file eols should need to be set (and other kinds of chars). |
|
155 |
</history> |
|
156 |
<note author="Alan Richmond"> |
|
157 |
Got WAY too long. See querysim_readme.txt for instructions and some |
|
158 |
examples. |
|
159 |
io section only documents elements of DB_result that DB_querysim uses, |
|
160 |
adds or changes; see MDB2 and MDB2_Driver_Common for more info. |
|
161 |
io section uses some elements that are not Fusedoc 2.0 compliant: |
|
162 |
object and resource. |
|
163 |
</note> |
|
164 |
</properties> |
|
165 |
<io> |
|
166 |
<in> |
|
167 |
<file path="MDB2/Common.php" action="require_once" /> |
|
168 |
</in> |
|
169 |
<out> |
|
170 |
<object name="MDB2_querysim" extends="MDB2_Driver_Common" instantiatedby="MDB2::connect()"> |
|
171 |
<resource type="file" name="connection" oncondition="source is external file" scope="class" /> |
|
172 |
<string name="phptype" default="querysim" /> |
|
173 |
<string name="dbsyntax" default="querysim" /> |
|
174 |
<array name="supported" comments="most of these don't actually do anything, they are enabled to simulate the option being available if checked"> |
|
175 |
<boolean name="sequences" default="true" /> |
|
176 |
<boolean name="indexes" default="true" /> |
|
177 |
<boolean name="affected_rows" default="true" /> |
|
178 |
<boolean name="summary_functions" default="true" /> |
|
179 |
<boolean name="order_by_text" default="true" /> |
|
180 |
<boolean name="current_id" default="true" /> |
|
181 |
<boolean name="limit_querys" default="true" comments="this one is functional" /> |
|
182 |
<boolean name="LOBs" default="true" /> |
|
183 |
<boolean name="replace" default="true" /> |
|
184 |
<boolean name="sub_selects" default="true" /> |
|
185 |
<boolean name="transactions" default="true" /> |
|
186 |
</array> |
|
187 |
<string name="last_query" comments="last value passed in with query()" /> |
|
188 |
<array name="options" comments="these can be changed at run time"> |
|
189 |
<string name="columnDelim" default="," /> |
|
190 |
<string name="dataDelim" default="|" /> |
|
191 |
<string name="eolDelim" default="chr(13).chr(10)" /> |
|
192 |
</array> |
|
193 |
</object> |
|
194 |
<array name="result" comments="the simulated record set returned by ::query()"> |
|
195 |
<array comments="columns"> |
|
196 |
<string comments="column name" /> |
|
197 |
</array> |
|
198 |
<array comments="data"> |
|
199 |
<array comments="row"> |
|
200 |
<string comments="data element" /> |
|
201 |
</array> |
|
202 |
</array> |
|
203 |
</array> |
|
204 |
</out> |
|
205 |
</io> |
|
206 |
</fusedoc> |
|
207 |
*/ |
|
208 |
|
|
209 |
/** |
|
210 |
* MDB2 QuerySim driver |
|
211 |
* |
|
212 |
* @package MDB2 |
|
213 |
* @category Database |
|
214 |
* @author Alan Richmond <arichmond@bigfoot.com> |
|
215 |
*/ |
|
216 |
class MDB2_Driver_querysim extends MDB2_Driver_Common |
|
217 |
{ |
|
218 |
// {{{ properties |
|
219 |
var $escape_quotes = "\\"; |
|
220 |
// }}} |
|
221 |
|
|
222 |
// {{{ constructor |
|
223 |
|
|
224 |
/** |
|
225 |
* Constructor |
|
226 |
*/ |
|
227 |
function __construct() |
|
228 |
{ |
|
229 |
parent::__construct(); |
|
230 |
|
|
231 |
$this->phptype = 'querysim'; |
|
232 |
$this->dbsyntax = 'querysim'; |
|
233 |
|
|
234 |
// Most of these are dummies to simulate availability if checked |
|
235 |
$this->supported['sequences'] = 'emulated'; |
|
236 |
$this->supported['indexes'] = true; |
|
237 |
$this->supported['affected_rows'] = false; |
|
238 |
$this->supported['summary_functions'] = false; |
|
239 |
$this->supported['order_by_text'] = false; |
|
240 |
$this->supported['current_id'] = 'emulated'; |
|
241 |
$this->supported['limit_queries'] = true;// this one is real |
|
242 |
$this->supported['LOBs'] = true; |
|
243 |
$this->supported['replace'] = 'emulated'; |
|
244 |
$this->supported['sub_selects'] = 'emulated'; |
|
245 |
$this->supported['transactions'] = false; |
|
246 |
$this->supported['auto_increment'] = false; |
|
247 |
$this->supported['primary_key'] = false; |
|
248 |
|
|
249 |
$this->options['columnDelim'] = ','; |
|
250 |
$this->options['dataDelim'] = '|'; |
|
251 |
$this->options['eolDelim'] = "\n"; |
|
252 |
} |
|
253 |
|
|
254 |
// }}} |
|
255 |
|
|
256 |
// {{{ connect() |
|
257 |
|
|
258 |
/** |
|
259 |
* Open a file or simulate a successful database connect |
|
260 |
* |
|
261 |
* @access public |
|
262 |
* |
|
263 |
* @return mixed MDB2_OK string on success, a MDB2 error object on failure |
|
264 |
*/ |
|
265 |
function connect() |
|
266 |
{ |
|
267 |
if (is_resource($this->connection)) { |
|
268 |
if ($this->connected_database_name == $this->database_name |
|
269 |
&& ($this->opened_persistent == $this->options['persistent']) |
|
270 |
) { |
|
271 |
return MDB2_OK; |
|
272 |
} |
|
273 |
if ($this->connected_database_name) { |
|
274 |
$this->_close($this->connection); |
|
275 |
} |
|
276 |
$this->disconnect(); |
|
277 |
} |
|
278 |
|
|
279 |
$connection = 1;// sim connect |
|
280 |
// if external, check file... |
|
281 |
if ($this->database_name) { |
|
282 |
$file = $this->database_name; |
|
283 |
if (!file_exists($file)) { |
|
284 |
return $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null, 'file not found'); |
|
285 |
} |
|
286 |
if (!is_file($file)) { |
|
287 |
return $this->raiseError(MDB2_ERROR_INVALID, null, null, 'not a file'); |
|
288 |
} |
|
289 |
if (!is_readable($file)) { |
|
290 |
return $this->raiseError(MDB2_ERROR_ACCESS_VIOLATION, null, null, |
|
291 |
'could not open file - check permissions'); |
|
292 |
} |
|
293 |
// ...and open if persistent |
|
294 |
if ($this->options['persistent']) { |
|
295 |
$connection = @fopen($file, 'r'); |
|
296 |
} |
|
297 |
} |
|
298 |
$this->connection = $connection; |
|
299 |
$this->connected_database_name = $this->database_name; |
|
300 |
$this->opened_persistent = $this->options['persistent']; |
|
301 |
$this->dbsyntax = $this->dsn['dbsyntax'] ? $this->dsn['dbsyntax'] : $this->phptype; |
|
302 |
|
|
303 |
return MDB2_OK; |
|
304 |
} |
|
305 |
// }}} |
|
306 |
|
|
307 |
// {{{ disconnect() |
|
308 |
|
|
309 |
/** |
|
310 |
* Log out and disconnect from the database. |
|
311 |
* |
|
312 |
* @return mixed true on success, false if not connected and error |
|
313 |
* object on error |
|
314 |
* @access public |
|
315 |
*/ |
|
316 |
function disconnect() |
|
317 |
{ |
|
318 |
if (is_resource($this->connection)) { |
|
319 |
if (($this->opened_persistent) && (is_resource($this->connection))) { |
|
320 |
if (!@fclose($this->connection)) { |
|
321 |
return $this->raiseError(); |
|
322 |
} |
|
323 |
} |
|
324 |
$this->connection = 0; |
|
325 |
} |
|
326 |
return MDB2_OK; |
|
327 |
} |
|
328 |
// }}} |
|
329 |
|
|
330 |
// {{{ _doQuery() |
|
331 |
|
|
332 |
/** |
|
333 |
* Execute a query |
|
334 |
* @param string $query query |
|
335 |
* @param boolean $isManip if the query is a manipulation query |
|
336 |
* @param resource $connection |
|
337 |
* @param string $database_name |
|
338 |
* @return result or error object |
|
339 |
* @access protected |
|
340 |
*/ |
|
341 |
function _doQuery($query, $isManip = false, $connection = null, $database_name = null) |
|
342 |
{ |
|
343 |
if ($isManip) { |
|
344 |
return $this->raiseError(MDB2_ERROR_UNSUPPORTED); |
|
345 |
} |
|
346 |
|
|
347 |
$this->last_query = $query; |
|
348 |
$this->debug($query, 'query'); |
|
349 |
if ($this->options['disable_query']) { |
|
350 |
return null; |
|
351 |
} |
|
352 |
|
|
353 |
$result = $this->_buildResult($query); |
|
354 |
if (PEAR::isError($result)) { |
|
355 |
return $result; |
|
356 |
} |
|
357 |
|
|
358 |
if ($limit > 0) { |
|
359 |
$result[1] = array_slice($result[1], $offset-1, $limit); |
|
360 |
} |
|
361 |
return $result; |
|
362 |
} |
|
363 |
// }}} |
|
364 |
|
|
365 |
// {{{ _readFile() |
|
366 |
|
|
367 |
/** |
|
368 |
* Read an external file |
|
369 |
* |
|
370 |
* @param string filepath/filename |
|
371 |
* |
|
372 |
* @access protected |
|
373 |
* |
|
374 |
* @return string the contents of a file |
|
375 |
*/ |
|
376 |
function _readFile() |
|
377 |
{ |
|
378 |
$buffer = ''; |
|
379 |
if ($this->opened_persistent) { |
|
380 |
while (!feof($this->connection)) { |
|
381 |
$buffer.= fgets($this->connection, 1024); |
|
382 |
} |
|
383 |
} else { |
|
384 |
$this->connection = @fopen($this->connected_database_name, 'r'); |
|
385 |
while (!feof($this->connection)) { |
|
386 |
$buffer.= fgets($this->connection, 1024); |
|
387 |
} |
|
388 |
$this->connection = @fclose($this->connection); |
|
389 |
} |
|
390 |
return $buffer; |
|
391 |
} |
|
392 |
// }}} |
|
393 |
|
|
394 |
// {{{ _buildResult() |
|
395 |
|
|
396 |
/** |
|
397 |
* Convert QuerySim text into an array |
|
398 |
* |
|
399 |
* @param string Text of simulated query |
|
400 |
* |
|
401 |
* @access protected |
|
402 |
* |
|
403 |
* @return multi-dimensional array containing the column names and data |
|
404 |
* from the QuerySim |
|
405 |
*/ |
|
406 |
function _buildResult($query) |
|
407 |
{ |
|
408 |
$eolDelim = $this->options['eolDelim']; |
|
409 |
$columnDelim = $this->options['columnDelim']; |
|
410 |
$dataDelim = $this->options['dataDelim']; |
|
411 |
|
|
412 |
$columnNames = array(); |
|
413 |
$data = array(); |
|
414 |
|
|
415 |
if ($columnDelim == $eolDelim) { |
|
416 |
return $this->raiseError(MDB2_ERROR_INVALID, null, null, |
|
417 |
'columnDelim and eolDelim must be different'); |
|
418 |
} elseif ($dataDelim == $eolDelim){ |
|
419 |
return $this->raiseError(MDB2_ERROR_INVALID, null, null, |
|
420 |
'dataDelim and eolDelim must be different'); |
|
421 |
} |
|
422 |
|
|
423 |
$query = trim($query); |
|
424 |
//tokenize escaped slashes |
|
425 |
$query = str_replace('\\\\', '[$double-slash$]', $query); |
|
426 |
|
|
427 |
if (!strlen($query)) { |
|
428 |
return $this->raiseError(MDB2_ERROR_SYNTAX, null, null, |
|
429 |
'empty querysim text'); |
|
430 |
} |
|
431 |
$lineData = $this->_parseOnDelim($query, $eolDelim); |
|
432 |
//kill the empty last row created by final eol char if it exists |
|
433 |
if (!strlen(trim($lineData[count($lineData) - 1]))) { |
|
434 |
unset($lineData[count($lineData) - 1]); |
|
435 |
} |
|
436 |
//populate columnNames array |
|
437 |
$thisLine = each($lineData); |
|
438 |
$columnNames = $this->_parseOnDelim($thisLine[1], $columnDelim); |
|
439 |
if ((in_array('', $columnNames)) || (in_array('NULL', $columnNames))) { |
|
440 |
return $this->raiseError(MDB2_ERROR_SYNTAX, null, null, |
|
441 |
'all column names must be defined'); |
|
442 |
} |
|
443 |
//replace double-slash tokens with single-slash |
|
444 |
$columnNames = str_replace('[$double-slash$]', '\\', $columnNames); |
|
445 |
$columnCount = count($columnNames); |
|
446 |
$rowNum = 0; |
|
447 |
//loop through data lines |
|
448 |
if (count($lineData) > 1) { |
|
449 |
while ($thisLine = each($lineData)) { |
|
450 |
$thisData = $this->_parseOnDelim($thisLine[1], $dataDelim); |
|
451 |
$thisDataCount = count($thisData); |
|
452 |
if ($thisDataCount != $columnCount) { |
|
453 |
$fileLineNo = $rowNum + 2; |
|
454 |
return $this->raiseError(MDB2_ERROR_SYNTAX, null, null, |
|
455 |
"number of data elements ($thisDataCount) in line $fileLineNo not equal to number of defined columns ($columnCount)"); |
|
456 |
} |
|
457 |
//loop through data elements in data line |
|
458 |
foreach ($thisData as $thisElement) { |
|
459 |
if (strtoupper($thisElement) == 'NULL'){ |
|
460 |
$thisElement = ''; |
|
461 |
} |
|
462 |
//replace double-slash tokens with single-slash |
|
463 |
$data[$rowNum][] = str_replace('[$double-slash$]', '\\', $thisElement); |
|
464 |
}//end foreach |
|
465 |
++$rowNum; |
|
466 |
}//end while |
|
467 |
}//end if |
|
468 |
return array($columnNames, $data); |
|
469 |
}//end function _buildResult() |
|
470 |
// }}} |
|
471 |
|
|
472 |
// {{{ _parseOnDelim() |
|
473 |
|
|
474 |
/** |
|
475 |
* Split QuerySim string into an array on a delimiter |
|
476 |
* |
|
477 |
* @param string $thisLine Text of simulated query |
|
478 |
* @param string $delim The delimiter to split on |
|
479 |
* |
|
480 |
* @access protected |
|
481 |
* |
|
482 |
* @return array containing parsed string |
|
483 |
*/ |
|
484 |
function _parseOnDelim($thisLine, $delim) |
|
485 |
{ |
|
486 |
$delimQuoted = preg_quote($delim, '/'); |
|
487 |
$thisLine = trim($thisLine); |
|
488 |
|
|
489 |
$parsed = preg_split('/(?<!\\\\)' .$delimQuoted. '/', $thisLine); |
|
490 |
//replaces escaped delimiters |
|
491 |
$parsed = preg_replace('/\\\\' .$delimQuoted. '/', $delim, $parsed); |
|
492 |
if ($delim != $this->options['eolDelim']) { |
|
493 |
//replaces escape chars |
|
494 |
$parsed = preg_replace('/\\\\/', '', $parsed); |
|
495 |
} |
|
496 |
return $parsed; |
|
497 |
} |
|
498 |
// }}} |
|
499 |
} |
|
500 |
|
|
501 |
class MDB2_Result_querysim extends MDB2_Result_Common |
|
502 |
{ |
|
503 |
// }}} |
|
504 |
// {{{ fetchRow() |
|
505 |
|
|
506 |
/** |
|
507 |
* Fetch a row and insert the data into an existing array. |
|
508 |
* |
|
509 |
* @param int $fetchmode how the array data should be indexed |
|
510 |
* @param int $rownum number of the row where the data can be found |
|
511 |
* @return int data array on success, a MDB2 error on failure |
|
512 |
* @access public |
|
513 |
*/ |
|
514 |
function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null) |
|
515 |
{ |
|
516 |
if (is_null($this->result)) { |
|
517 |
$err =& $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, |
|
518 |
'fetchRow: resultset has already been freed'); |
|
519 |
return $err; |
|
520 |
} |
|
521 |
if (!is_null($rownum)) { |
|
522 |
$seek = $this->seek($rownum); |
|
523 |
if (PEAR::isError($seek)) { |
|
524 |
return $seek; |
|
525 |
} |
|
526 |
} |
|
527 |
$target_rownum = $this->rownum + 1; |
|
528 |
if ($fetchmode == MDB2_FETCHMODE_DEFAULT) { |
|
529 |
$fetchmode = $this->db->fetchmode; |
|
530 |
} |
|
531 |
if (!isset($this->result[1][$target_rownum])) { |
|
532 |
$null = null; |
|
533 |
return $null; |
|
534 |
} |
|
535 |
$row = $this->result[1][$target_rownum]; |
|
536 |
// make row associative |
|
537 |
if ($fetchmode & MDB2_FETCHMODE_ASSOC) { |
|
538 |
$column_names = $this->getColumnNames(); |
|
539 |
foreach ($column_names as $name => $i) { |
|
540 |
$column_names[$name] = $row[$i]; |
|
541 |
} |
|
542 |
$row = $column_names; |
|
543 |
} |
|
544 |
if (($mode = ($this->db->options['portability'] & MDB2_PORTABILITY_RTRIM) |
|
545 |
+ ($this->db->options['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL)) |
|
546 |
) { |
|
547 |
$this->db->_fixResultArrayValues($row, $mode); |
|
548 |
} |
|
549 |
if (!empty($this->values)) { |
|
550 |
$this->_assignBindColumns($row); |
|
551 |
} |
|
552 |
if (!empty($this->types)) { |
|
553 |
$row = $this->db->datatype->convertResultRow($this->types, $row); |
|
554 |
} |
|
555 |
if ($fetchmode === MDB2_FETCHMODE_OBJECT) { |
|
556 |
$object_class = $this->db->options['fetch_class']; |
|
557 |
if ($object_class == 'stdClass') { |
|
558 |
$row = (object) $row; |
|
559 |
} else { |
|
560 |
$row = &new $object_class($row); |
|
561 |
} |
|
562 |
} |
|
563 |
++$this->rownum; |
|
564 |
return $row; |
|
565 |
} |
|
566 |
|
|
567 |
// }}} |
|
568 |
// {{{ _getColumnNames() |
|
569 |
|
|
570 |
/** |
|
571 |
* Retrieve the names of columns returned by the DBMS in a query result. |
|
572 |
* |
|
573 |
* @return mixed an associative array variable |
|
574 |
* that will hold the names of columns. The |
|
575 |
* indexes of the array are the column names |
|
576 |
* mapped to lower case and the values are the |
|
577 |
* respective numbers of the columns starting |
|
578 |
* from 0. Some DBMS may not return any |
|
579 |
* columns when the result set does not |
|
580 |
* contain any rows. |
|
581 |
* |
|
582 |
* a MDB2 error on failure |
|
583 |
* @access private |
|
584 |
*/ |
|
585 |
function _getColumnNames() |
|
586 |
{ |
|
587 |
if (is_null($this->result)) { |
|
588 |
return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, |
|
589 |
'getColumnNames: resultset has already been freed'); |
|
590 |
} |
|
591 |
$columns = array_flip($this->result[0]); |
|
592 |
if ($this->db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { |
|
593 |
$columns = array_change_key_case($columns, $this->db->options['field_case']); |
|
594 |
} |
|
595 |
return $columns; |
|
596 |
} |
|
597 |
|
|
598 |
// }}} |
|
599 |
// {{{ numCols() |
|
600 |
|
|
601 |
/** |
|
602 |
* Count the number of columns returned by the DBMS in a query result. |
|
603 |
* |
|
604 |
* @access public |
|
605 |
* @return mixed integer value with the number of columns, a MDB2 error |
|
606 |
* on failure |
|
607 |
*/ |
|
608 |
function numCols() |
|
609 |
{ |
|
610 |
if (is_null($this->result)) { |
|
611 |
return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, |
|
612 |
'numCols: resultset has already been freed'); |
|
613 |
} |
|
614 |
$cols = count($this->result[0]); |
|
615 |
return $cols; |
|
616 |
} |
|
617 |
} |
|
618 |
|
|
619 |
class MDB2_BufferedResult_querysim extends MDB2_Result_querysim |
|
620 |
{ |
|
621 |
// {{{ seek() |
|
622 |
|
|
623 |
/** |
|
624 |
* seek to a specific row in a result set |
|
625 |
* |
|
626 |
* @param int $rownum number of the row where the data can be found |
|
627 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
628 |
* @access public |
|
629 |
*/ |
|
630 |
function seek($rownum = 0) |
|
631 |
{ |
|
632 |
if (is_null($this->result)) { |
|
633 |
return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, |
|
634 |
'seek: resultset has already been freed'); |
|
635 |
} |
|
636 |
$this->rownum = $rownum - 1; |
|
637 |
return MDB2_OK; |
|
638 |
} |
|
639 |
|
|
640 |
// }}} |
|
641 |
// {{{ valid() |
|
642 |
|
|
643 |
/** |
|
644 |
* check if the end of the result set has been reached |
|
645 |
* |
|
646 |
* @return mixed true or false on sucess, a MDB2 error on failure |
|
647 |
* @access public |
|
648 |
*/ |
|
649 |
function valid() |
|
650 |
{ |
|
651 |
$numrows = $this->numRows(); |
|
652 |
if (PEAR::isError($numrows)) { |
|
653 |
return $numrows; |
|
654 |
} |
|
655 |
return $this->rownum < ($numrows - 1); |
|
656 |
} |
|
657 |
|
|
658 |
// }}} |
|
659 |
// {{{ numRows() |
|
660 |
|
|
661 |
/** |
|
662 |
* returns the number of rows in a result object |
|
663 |
* |
|
664 |
* @return mixed MDB2 Error Object or the number of rows |
|
665 |
* @access public |
|
666 |
*/ |
|
667 |
function numRows() |
|
668 |
{ |
|
669 |
if (is_null($this->result)) { |
|
670 |
return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, |
|
671 |
'numRows: resultset has already been freed'); |
|
672 |
} |
|
673 |
$rows = count($this->result[1]); |
|
674 |
return $rows; |
|
675 |
} |
|
676 |
} |
|
677 |
|
|
678 |
|
|
679 |
class MDB2_Statement_querysim extends MDB2_Statement_Common |
|
680 |
{ |
|
681 |
|
|
682 |
} |
|
683 |
|
|
684 |
?> |