| 1 |
lars |
1 |
<?php
|
|
|
2 |
// +----------------------------------------------------------------------+
|
|
|
3 |
// | PHP Version 4 |
|
|
|
4 |
// +----------------------------------------------------------------------+
|
|
|
5 |
// | Copyright (c) 1998-2004 Manuel Lemos, Paul Cooper |
|
|
|
6 |
// | All rights reserved. |
|
|
|
7 |
// +----------------------------------------------------------------------+
|
|
|
8 |
// | MDB is a merge of PEAR DB and Metabases that provides a unified DB |
|
|
|
9 |
// | API as well as database abstraction for PHP applications. |
|
|
|
10 |
// | This LICENSE is in the BSD license style. |
|
|
|
11 |
// | |
|
|
|
12 |
// | Redistribution and use in source and binary forms, with or without |
|
|
|
13 |
// | modification, are permitted provided that the following conditions |
|
|
|
14 |
// | are met: |
|
|
|
15 |
// | |
|
|
|
16 |
// | Redistributions of source code must retain the above copyright |
|
|
|
17 |
// | notice, this list of conditions and the following disclaimer. |
|
|
|
18 |
// | |
|
|
|
19 |
// | Redistributions in binary form must reproduce the above copyright |
|
|
|
20 |
// | notice, this list of conditions and the following disclaimer in the |
|
|
|
21 |
// | documentation and/or other materials provided with the distribution. |
|
|
|
22 |
// | |
|
|
|
23 |
// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
|
|
|
24 |
// | Lukas Smith nor the names of his contributors may be used to endorse |
|
|
|
25 |
// | or promote products derived from this software without specific prior|
|
|
|
26 |
// | written permission. |
|
|
|
27 |
// | |
|
|
|
28 |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
|
|
29 |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
|
|
30 |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
|
|
31 |
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|
|
|
32 |
// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
|
|
33 |
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
|
|
34 |
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
|
|
|
35 |
// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
|
|
|
36 |
// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
|
|
37 |
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
|
|
|
38 |
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
|
|
39 |
// | POSSIBILITY OF SUCH DAMAGE. |
|
|
|
40 |
// +----------------------------------------------------------------------+
|
|
|
41 |
// | Author: Paul Cooper <pgc@ucecom.com> |
|
|
|
42 |
// +----------------------------------------------------------------------+
|
|
|
43 |
//
|
|
|
44 |
// $Id: MDB_usage_testcase.php,v 1.28.4.13 2004/03/31 16:01:56 lsmith Exp $
|
|
|
45 |
|
|
|
46 |
class MDB_Usage_TestCase extends PHPUnit_TestCase {
|
|
|
47 |
//contains the dsn of the database we are testing
|
|
|
48 |
var $dsn;
|
|
|
49 |
//contains the options that should be used during testing
|
|
|
50 |
var $options;
|
|
|
51 |
//contains the name of the database we are testing
|
|
|
52 |
var $database;
|
|
|
53 |
//contains the MDB object of the db once we have connected
|
|
|
54 |
var $db;
|
|
|
55 |
// contains field names from the test table
|
|
|
56 |
var $fields;
|
|
|
57 |
// contains the types of the fields from the test table
|
|
|
58 |
var $types;
|
|
|
59 |
|
|
|
60 |
function MDB_Usage_TestCase($name) {
|
|
|
61 |
$this->PHPUnit_TestCase($name);
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
function setUp() {
|
|
|
65 |
$this->dsn = $GLOBALS['dsn'];
|
|
|
66 |
$this->options = $GLOBALS['options'];
|
|
|
67 |
$this->database = $GLOBALS['database'];
|
|
|
68 |
$this->db =& MDB::connect($this->dsn, $this->options);
|
|
|
69 |
if (MDB::isError($this->db)) {
|
|
|
70 |
$this->assertTrue(false, 'Could not connect to database in setUp');
|
|
|
71 |
exit;
|
|
|
72 |
}
|
|
|
73 |
$this->db->setDatabase($this->database);
|
|
|
74 |
$this->fields = array(
|
|
|
75 |
'user_name',
|
|
|
76 |
'user_password',
|
|
|
77 |
'subscribed',
|
|
|
78 |
'user_id',
|
|
|
79 |
'quota',
|
|
|
80 |
'weight',
|
|
|
81 |
'access_date',
|
|
|
82 |
'access_time',
|
|
|
83 |
'approved'
|
|
|
84 |
);
|
|
|
85 |
$this->types = array(
|
|
|
86 |
'text',
|
|
|
87 |
'text',
|
|
|
88 |
'boolean',
|
|
|
89 |
'integer',
|
|
|
90 |
'decimal',
|
|
|
91 |
'float',
|
|
|
92 |
'date',
|
|
|
93 |
'time',
|
|
|
94 |
'timestamp'
|
|
|
95 |
);
|
|
|
96 |
$this->clearTables();
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
function tearDown() {
|
|
|
100 |
$this->clearTables();
|
|
|
101 |
unset($this->dsn);
|
|
|
102 |
if (!MDB::isError($this->db)) {
|
|
|
103 |
$this->db->disconnect();
|
|
|
104 |
}
|
|
|
105 |
unset($this->db);
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
function methodExists($name) {
|
|
|
109 |
if (array_key_exists(strtolower($name), array_change_key_case(array_flip(get_class_methods($this->db))))) {
|
|
|
110 |
return true;
|
|
|
111 |
}
|
|
|
112 |
$this->assertTrue(false, 'method '. $name.' not implemented in '.get_class($this->db));
|
|
|
113 |
return false;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
function clearTables() {
|
|
|
117 |
if (MDB::isError($this->db->query('DELETE FROM users'))) {
|
|
|
118 |
$this->assertTrue(false, 'Error deleting from table users');
|
|
|
119 |
}
|
|
|
120 |
if (MDB::isError($this->db->query('DELETE FROM files'))) {
|
|
|
121 |
$this->assertTrue(false, 'Error deleting from table users');
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
function supported($feature) {
|
|
|
126 |
if (!$this->db->support($feature)) {
|
|
|
127 |
$this->assertTrue(false, 'This database does not support '.$feature);
|
|
|
128 |
return false;
|
|
|
129 |
}
|
|
|
130 |
return true;
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
function insertTestValues($prepared_query, &$data) {
|
|
|
134 |
for ($i = 0; $i < count($this->fields); $i++) {
|
|
|
135 |
$func = 'setParam'.$this->types[$i];
|
|
|
136 |
$this->db->$func($prepared_query, ($i + 1), $data[$this->fields[$i]]);
|
|
|
137 |
}
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
function verifyFetchedValues(&$result, $rownum, &$data) {
|
|
|
141 |
$row = $this->db->fetchInto($result, $rownum);
|
|
|
142 |
for ($i = 0; $i < count($this->fields); $i++) {
|
|
|
143 |
$type = $this->types[$i];
|
|
|
144 |
if ($this->types[$i] == 'float') {
|
|
|
145 |
$delta = 0.0000000001;
|
|
|
146 |
} else {
|
|
|
147 |
$delta = 0;
|
|
|
148 |
}
|
|
|
149 |
$value = $row[$i];
|
|
|
150 |
$field = $this->fields[$i];
|
|
|
151 |
$this->assertEquals($data[$field], $value, "the value retrieved for field \"$field\" ($value) using type $type doesn't match what was stored ($data[$field]).", $delta);
|
|
|
152 |
}
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
/**
|
|
|
156 |
* Test typed data storage and retrieval
|
|
|
157 |
*
|
|
|
158 |
* This tests typed data storage and retrieval by executing a single
|
|
|
159 |
* prepared query and then selecting the data back from the database
|
|
|
160 |
* and comparing the results
|
|
|
161 |
*/
|
|
|
162 |
function testStorage() {
|
|
|
163 |
$row = 1234;
|
|
|
164 |
$data = array();
|
|
|
165 |
$data['user_name'] = "user_$row";
|
|
|
166 |
$data['user_password'] = 'somepassword';
|
|
|
167 |
$data['subscribed'] = (boolean)($row % 2);
|
|
|
168 |
$data['user_id'] = $row;
|
|
|
169 |
$data['quota'] = strval($row/100);
|
|
|
170 |
$data['weight'] = sqrt($row);
|
|
|
171 |
$data['access_date'] = MDB_Date::mdbToday();
|
|
|
172 |
$data['access_time'] = MDB_Date::mdbTime();
|
|
|
173 |
$data['approved'] = MDB_Date::mdbNow();
|
|
|
174 |
|
|
|
175 |
$prepared_query = $this->db->prepareQuery('INSERT INTO users (user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', $this->types);
|
|
|
176 |
|
|
|
177 |
$this->insertTestValues($prepared_query, $data);
|
|
|
178 |
|
|
|
179 |
$result = $this->db->executeQuery($prepared_query);
|
|
|
180 |
|
|
|
181 |
$this->db->freePreparedQuery($prepared_query);
|
|
|
182 |
|
|
|
183 |
if (MDB::isError($result)) {
|
|
|
184 |
$this->assertTrue(false, 'Error executing prepared query'.$result->getMessage());
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
$result = $this->db->query('SELECT user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved FROM users', $this->types);
|
|
|
188 |
|
|
|
189 |
if (MDB::isError($result)) {
|
|
|
190 |
$this->assertTrue(false, 'Error selecting from users'.$result->getMessage());
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
$this->verifyFetchedValues($result, 0, $data);
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
/**
|
|
|
197 |
* Test bulk fetch
|
|
|
198 |
*
|
|
|
199 |
* This test bulk fetching of result data by using a prepared query to
|
|
|
200 |
* insert an number of rows of data and then retrieving the data columns
|
|
|
201 |
* one by one
|
|
|
202 |
*/
|
|
|
203 |
function testBulkFetch() {
|
|
|
204 |
$data = array();
|
|
|
205 |
$total_rows = 5;
|
|
|
206 |
|
|
|
207 |
$prepared_query = $this->db->prepareQuery('INSERT INTO users (user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', $this->types);
|
|
|
208 |
|
|
|
209 |
for ($row = 0; $row < $total_rows; $row++) {
|
|
|
210 |
$data[$row]['user_name'] = "user_$row";
|
|
|
211 |
$data[$row]['user_password'] = 'somepassword';
|
|
|
212 |
$data[$row]['subscribed'] = (boolean)($row % 2);
|
|
|
213 |
$data[$row]['user_id'] = $row;
|
|
|
214 |
$data[$row]['quota'] = sprintf("%.2f",strval(1+($row+1)/100));
|
|
|
215 |
$data[$row]['weight'] = sqrt($row);
|
|
|
216 |
$data[$row]['access_date'] = MDB_Date::mdbToday();
|
|
|
217 |
$data[$row]['access_time'] = MDB_Date::mdbTime();
|
|
|
218 |
$data[$row]['approved'] = MDB_Date::mdbNow();
|
|
|
219 |
|
|
|
220 |
$this->insertTestValues($prepared_query, $data[$row]);
|
|
|
221 |
|
|
|
222 |
$result = $this->db->executeQuery($prepared_query, $this->types);
|
|
|
223 |
|
|
|
224 |
if (MDB::isError($result)) {
|
|
|
225 |
$this->assertTrue(false, 'Error executing prepared query'.$result->getMessage());
|
|
|
226 |
}
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
$this->db->freePreparedQuery($prepared_query);
|
|
|
230 |
|
|
|
231 |
$total_fields = count($this->fields);
|
|
|
232 |
for ($i = 0; $i < $total_fields; $i++) {
|
|
|
233 |
$field = $this->fields[$i];
|
|
|
234 |
for ($row = 0; $row < $total_rows; $row++) {
|
|
|
235 |
$value = $this->db->queryOne('SELECT '.$field.' FROM users WHERE user_id='.$row, $this->types[$i]);
|
|
|
236 |
if (MDB::isError($value)) {
|
|
|
237 |
$this->assertTrue(false, 'Error fetching row '.$row.' for field '.$field.' of type '.$this->types[$i]);
|
|
|
238 |
} else {
|
|
|
239 |
$this->assertEquals(strval(trim($value)), strval($data[$row][$field]), 'the query field '.$field.' of type '.$this->types[$i].' for row '.$row.' was returned in "'.$value.'" unlike "'.$data[$row][$field].'" as expected');
|
|
|
240 |
}
|
|
|
241 |
}
|
|
|
242 |
}
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
/**
|
|
|
246 |
* Test prepared queries
|
|
|
247 |
*
|
|
|
248 |
* Tests prepared queries, making sure they correctly deal with ?, !, and '
|
|
|
249 |
*/
|
|
|
250 |
function testPreparedQueries() {
|
|
|
251 |
$question_value = $this->db->getTextValue('Does this work?');
|
|
|
252 |
|
|
|
253 |
$prepared_query = $this->db->prepareQuery("INSERT INTO users (user_name, user_password, user_id) VALUES (?, $question_value, 1)", array('text'));
|
|
|
254 |
|
|
|
255 |
$this->db->setParamText($prepared_query, 1, 'Sure!');
|
|
|
256 |
|
|
|
257 |
$result = $this->db->executeQuery($prepared_query);
|
|
|
258 |
|
|
|
259 |
$this->db->freePreparedQuery($prepared_query);
|
|
|
260 |
|
|
|
261 |
if (MDB::isError($result)) {
|
|
|
262 |
$error = $result->getMessage();
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
$this->assertTrue(!MDB::isError($result), 'Could not execute prepared query with a text value with a question mark. Error: ');
|
|
|
266 |
|
|
|
267 |
$question_value = $this->db->getTextValue("Wouldn't it be great if this worked too?");
|
|
|
268 |
|
|
|
269 |
$prepared_query = $this->db->prepareQuery("INSERT INTO users (user_name, user_password, user_id) VALUES (?, $question_value, 2)", array('text'));
|
|
|
270 |
|
|
|
271 |
$this->db->setParamText($prepared_query, 1, 'For Sure!');
|
|
|
272 |
|
|
|
273 |
$result = $this->db->executeQuery($prepared_query);
|
|
|
274 |
|
|
|
275 |
$this->db->freePreparedQuery($prepared_query);
|
|
|
276 |
|
|
|
277 |
if (MDB::isError($result)) {
|
|
|
278 |
$error = $result->getMessage();
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
$this->assertTrue(!MDB::isError($result), 'Could not execute prepared query with a text value with a quote character before a question mark. Error: ');
|
|
|
282 |
|
|
|
283 |
}
|
|
|
284 |
|
|
|
285 |
/**
|
|
|
286 |
* Test retrieval of result metadata
|
|
|
287 |
*
|
|
|
288 |
* This tests the result metadata by executing a prepared_query and
|
|
|
289 |
* select the data, and checking the result contains the correct
|
|
|
290 |
* number of columns and that the column names are in the correct order
|
|
|
291 |
*/
|
|
|
292 |
function testMetadata() {
|
|
|
293 |
$row = 1234;
|
|
|
294 |
$data = array();
|
|
|
295 |
$data['user_name'] = "user_$row";
|
|
|
296 |
$data['user_password'] = 'somepassword';
|
|
|
297 |
$data['subscribed'] = (boolean)($row % 2);
|
|
|
298 |
$data['user_id'] = $row;
|
|
|
299 |
$data['quota'] = strval($row/100);
|
|
|
300 |
$data['weight'] = sqrt($row);
|
|
|
301 |
$data['access_date'] = MDB_Date::mdbToday();
|
|
|
302 |
$data['access_time'] = MDB_Date::mdbTime();
|
|
|
303 |
$data['approved'] = MDB_Date::mdbNow();
|
|
|
304 |
|
|
|
305 |
$prepared_query = $this->db->prepareQuery('INSERT INTO users (user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', $this->types);
|
|
|
306 |
|
|
|
307 |
$this->insertTestValues($prepared_query, $data);
|
|
|
308 |
|
|
|
309 |
$result = $this->db->executeQuery($prepared_query);
|
|
|
310 |
|
|
|
311 |
$this->db->freePreparedQuery($prepared_query);
|
|
|
312 |
|
|
|
313 |
if (MDB::isError($result)) {
|
|
|
314 |
$this->assertTrue(false, 'Error executing prepared query'.$result->getMessage());
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
$result = $this->db->query('SELECT user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved FROM users', $this->types);
|
|
|
318 |
|
|
|
319 |
if (MDB::isError($result)) {
|
|
|
320 |
$this->assertTrue(false, 'Error selecting from users'.$result->getMessage());
|
|
|
321 |
}
|
|
|
322 |
|
|
|
323 |
$numcols = $this->db->numCols($result);
|
|
|
324 |
|
|
|
325 |
$this->assertEquals($numcols, count($this->fields), "The query result returned a number of $numcols columns unlike ".count($this->fields) .' as expected');
|
|
|
326 |
|
|
|
327 |
$column_names = $this->db->getColumnNames($result);
|
|
|
328 |
for ($column = 0; $column < $numcols; $column++) {
|
|
|
329 |
$this->assertEquals($column_names[$this->fields[$column]], $column, "The query result column \"".$this->fields[$column]."\" was returned in position ".$column_names[$this->fields[$column]]." unlike $column as expected");
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
}
|
|
|
333 |
|
|
|
334 |
/**
|
|
|
335 |
* Test storage and retrieval of nulls
|
|
|
336 |
*
|
|
|
337 |
* This tests null storage and retrieval by successively inserting,
|
|
|
338 |
* selecting, and testing a number of null / not null values
|
|
|
339 |
*/
|
|
|
340 |
function testNulls() {
|
|
|
341 |
$test_values = array(
|
|
|
342 |
array('test', false),
|
|
|
343 |
array('NULL', false),
|
|
|
344 |
array('null', false),
|
|
|
345 |
array('', false),
|
|
|
346 |
array(null, true)
|
|
|
347 |
);
|
|
|
348 |
|
|
|
349 |
for ($test_value = 0; $test_value <= count($test_values); $test_value++) {
|
|
|
350 |
if ($test_value == count($test_values)) {
|
|
|
351 |
$value = 'NULL';
|
|
|
352 |
$is_null = true;
|
|
|
353 |
} else {
|
|
|
354 |
$value = $this->db->getTextValue($test_values[$test_value][0]);
|
|
|
355 |
$is_null = $test_values[$test_value][1];
|
|
|
356 |
}
|
|
|
357 |
|
|
|
358 |
$this->clearTables();
|
|
|
359 |
|
|
|
360 |
$result = $this->db->query("INSERT INTO users (user_name,user_password,user_id) VALUES ($value,$value,0)");
|
|
|
361 |
|
|
|
362 |
if (MDB::isError($result)) {
|
|
|
363 |
$this->assertTrue(false, 'Error executing insert query'.$result->getMessage());
|
|
|
364 |
}
|
|
|
365 |
|
|
|
366 |
$result = $this->db->query('SELECT user_name,user_password FROM users', array('text', 'text'));
|
|
|
367 |
|
|
|
368 |
if (MDB::isError($result)) {
|
|
|
369 |
$this->assertTrue(false, 'Error executing select query'.$result->getMessage());
|
|
|
370 |
}
|
|
|
371 |
|
|
|
372 |
$this->assertTrue(!$this->db->endOfResult($result), 'The query result seems to have reached the end of result earlier than expected');
|
|
|
373 |
|
|
|
374 |
if ($is_null) {
|
|
|
375 |
$error_message = 'A query result column is not NULL unlike what was expected';
|
|
|
376 |
} else {
|
|
|
377 |
$error_message = 'A query result column is NULL even though it was expected to be "' . $test_values[$test_value][0] . '"';
|
|
|
378 |
}
|
|
|
379 |
|
|
|
380 |
$value = $this->db->resultIsNull($result, 0, 0);
|
|
|
381 |
$this->assertTrue(($value == $is_null), $error_message);
|
|
|
382 |
|
|
|
383 |
$value = $this->db->resultIsNull($result, 0, 1);
|
|
|
384 |
$this->assertTrue(($value == $is_null), $error_message);
|
|
|
385 |
|
|
|
386 |
$this->assertTrue($this->db->endOfResult($result), 'the query result did not seem to have reached the end of result as expected after testing only if columns are NULLs');
|
|
|
387 |
|
|
|
388 |
$this->db->freeResult($result);
|
|
|
389 |
}
|
|
|
390 |
}
|
|
|
391 |
|
|
|
392 |
/**
|
|
|
393 |
* Tests escaping of text values with special characters
|
|
|
394 |
*
|
|
|
395 |
*/
|
|
|
396 |
function testEscapeSequences() {
|
|
|
397 |
$test_strings = array(
|
|
|
398 |
"'",
|
|
|
399 |
"\"",
|
|
|
400 |
"\\",
|
|
|
401 |
"%",
|
|
|
402 |
"_",
|
|
|
403 |
"''",
|
|
|
404 |
"\"\"",
|
|
|
405 |
"\\\\",
|
|
|
406 |
"\\'\\'",
|
|
|
407 |
"\\\"\\\""
|
|
|
408 |
);
|
|
|
409 |
|
|
|
410 |
for($string = 0; $string < count($test_strings); $string++) {
|
|
|
411 |
$this->clearTables();
|
|
|
412 |
|
|
|
413 |
$value = $this->db->getTextValue($test_strings[$string]);
|
|
|
414 |
|
|
|
415 |
$result = $this->db->query("INSERT INTO users (user_name,user_password,user_id) VALUES ($value,$value,0)");
|
|
|
416 |
|
|
|
417 |
if (MDB::isError($result)) {
|
|
|
418 |
$this->assertTrue(false, 'Error executing insert query'.$result->getMessage());
|
|
|
419 |
}
|
|
|
420 |
|
|
|
421 |
$result = $this->db->query('SELECT user_name,user_password FROM users', array('text', 'text'));
|
|
|
422 |
|
|
|
423 |
if (MDB::isError($result)) {
|
|
|
424 |
$this->assertTrue(false, 'Error executing select query'.$result->getMessage());
|
|
|
425 |
}
|
|
|
426 |
|
|
|
427 |
$this->assertTrue(!$this->db->endOfResult($result), 'The query result seems to have reached the end of result earlier than expected');
|
|
|
428 |
|
|
|
429 |
$value = $this->db->fetch($result, 0, 'user_name');
|
|
|
430 |
$this->db->freeResult($result);
|
|
|
431 |
|
|
|
432 |
$this->assertEquals($test_strings[$string], rtrim($value), "the value retrieved for field \"user_name\" (\"$value\") doesn't match what was stored (".$test_strings[$string].')');
|
|
|
433 |
|
|
|
434 |
}
|
|
|
435 |
}
|
|
|
436 |
|
|
|
437 |
/**
|
|
|
438 |
* Test paged queries
|
|
|
439 |
*
|
|
|
440 |
* Test the use of setSelectedRowRange to return paged queries
|
|
|
441 |
*/
|
|
|
442 |
function testRanges() {
|
|
|
443 |
if (!$this->supported('SelectRowRanges')) {
|
|
|
444 |
return;
|
|
|
445 |
}
|
|
|
446 |
|
|
|
447 |
$data = array();
|
|
|
448 |
$total_rows = 5;
|
|
|
449 |
|
|
|
450 |
$prepared_query = $this->db->prepareQuery('INSERT INTO users (user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', $this->types);
|
|
|
451 |
|
|
|
452 |
for ($row = 0; $row < $total_rows; $row++) {
|
|
|
453 |
$data[$row]['user_name'] = "user_$row";
|
|
|
454 |
$data[$row]['user_password'] = 'somepassword';
|
|
|
455 |
$data[$row]['subscribed'] = (boolean)($row % 2);
|
|
|
456 |
$data[$row]['user_id'] = $row;
|
|
|
457 |
$data[$row]['quota'] = sprintf("%.2f",strval(1+($row+1)/100));
|
|
|
458 |
$data[$row]['weight'] = sqrt($row);
|
|
|
459 |
$data[$row]['access_date'] = MDB_Date::mdbToday();
|
|
|
460 |
$data[$row]['access_time'] = MDB_Date::mdbTime();
|
|
|
461 |
$data[$row]['approved'] = MDB_Date::mdbNow();
|
|
|
462 |
|
|
|
463 |
$this->insertTestValues($prepared_query, $data[$row]);
|
|
|
464 |
|
|
|
465 |
$result = $this->db->executeQuery($prepared_query);
|
|
|
466 |
|
|
|
467 |
if (MDB::isError($result)) {
|
|
|
468 |
$this->assertTrue(false, 'Error executing prepared query'.$result->getMessage());
|
|
|
469 |
}
|
|
|
470 |
}
|
|
|
471 |
|
|
|
472 |
$this->db->freePreparedQuery($prepared_query);
|
|
|
473 |
|
|
|
474 |
for ($rows = 2, $start_row = 0; $start_row < $total_rows; $start_row += $rows) {
|
|
|
475 |
|
|
|
476 |
$this->db->setSelectedRowRange($start_row, $rows);
|
|
|
477 |
|
|
|
478 |
$result = $this->db->query('SELECT user_name,user_password,subscribed,user_id,quota,weight,access_date,access_time,approved FROM users ORDER BY user_id', $this->types);
|
|
|
479 |
|
|
|
480 |
if (MDB::isError($result)) {
|
|
|
481 |
$this->assertTrue(false, 'Error executing select query'.$result->getMessage());
|
|
|
482 |
}
|
|
|
483 |
|
|
|
484 |
for ($row = 0; $row < $rows && ($row + $start_row < $total_rows); $row++) {
|
|
|
485 |
$this->verifyFetchedValues($result, $row, $data[$row + $start_row]);
|
|
|
486 |
}
|
|
|
487 |
}
|
|
|
488 |
|
|
|
489 |
$this->assertTrue($this->db->endOfResult($result), "The query result did not seem to have reached the end of result as expected starting row $start_row after fetching upto row $row");
|
|
|
490 |
|
|
|
491 |
$this->db->freeResult($result);
|
|
|
492 |
|
|
|
493 |
for ($rows = 2, $start_row = 0; $start_row < $total_rows; $start_row += $rows) {
|
|
|
494 |
|
|
|
495 |
$this->db->setSelectedRowRange($start_row, $rows);
|
|
|
496 |
|
|
|
497 |
$result = $this->db->query('SELECT user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved FROM users ORDER BY user_id', $this->types);
|
|
|
498 |
|
|
|
499 |
if (MDB::isError($result)) {
|
|
|
500 |
$this->assertTrue(false, 'Error executing select query'.$result->getMessage());
|
|
|
501 |
}
|
|
|
502 |
|
|
|
503 |
$result_rows = $this->db->numRows($result);
|
|
|
504 |
|
|
|
505 |
$this->assertTrue(($result_rows <= $rows), "expected a result of no more than $rows but the returned number of rows is $result_rows");
|
|
|
506 |
|
|
|
507 |
for ($row = 0; $row < $result_rows; $row++) {
|
|
|
508 |
$this->assertTrue(!$this->db->endOfResult($result), "The query result seem to have reached the end of result at row $row that is before $result_rows as expected");
|
|
|
509 |
|
|
|
510 |
$this->verifyFetchedValues($result, $row, $data[$row + $start_row]);
|
|
|
511 |
|
|
|
512 |
}
|
|
|
513 |
}
|
|
|
514 |
|
|
|
515 |
$this->assertTrue($this->db->endOfResult($result), 'the query result did not seem to have reached the end of result as expected');
|
|
|
516 |
|
|
|
517 |
$this->db->freeResult($result);
|
|
|
518 |
}
|
|
|
519 |
|
|
|
520 |
/**
|
|
|
521 |
* Test the handling of sequences
|
|
|
522 |
*/
|
|
|
523 |
function testSequences() {
|
|
|
524 |
if (!$this->supported('Sequences')) {
|
|
|
525 |
return;
|
|
|
526 |
}
|
|
|
527 |
|
|
|
528 |
for ($start_value = 1; $start_value < 4; $start_value++) {
|
|
|
529 |
$sequence_name = "test_sequence_$start_value";
|
|
|
530 |
|
|
|
531 |
$result = $this->db->createSequence($sequence_name, $start_value);
|
|
|
532 |
$this->assertTrue(!MDB::isError($result), "Error creating sequence $sequence_name with start value $start_value");
|
|
|
533 |
|
|
|
534 |
for ($sequence_value = $start_value; $sequence_value < ($start_value + 4); $sequence_value++) {
|
|
|
535 |
$value = $this->db->nextId($sequence_name, false);
|
|
|
536 |
|
|
|
537 |
$this->assertEquals($sequence_value, $value, "The returned sequence value is $value and not $sequence_value as expected with sequence start value with $start_value");
|
|
|
538 |
|
|
|
539 |
}
|
|
|
540 |
|
|
|
541 |
$result = $this->db->dropSequence($sequence_name);
|
|
|
542 |
|
|
|
543 |
if (MDB::isError($result)) {
|
|
|
544 |
$this->assertTrue(false, "Error dropping sequence $sequence_name : ".$result->getMessage());
|
|
|
545 |
}
|
|
|
546 |
|
|
|
547 |
}
|
|
|
548 |
|
|
|
549 |
// Test ondemand creation of sequences
|
|
|
550 |
$sequence_name = 'test_ondemand';
|
|
|
551 |
|
|
|
552 |
for ($sequence_value = 1; $sequence_value < 4; $sequence_value++) {
|
|
|
553 |
$value = $this->db->nextId($sequence_name);
|
|
|
554 |
|
|
|
555 |
$this->assertEquals($sequence_value, $value, "Error in ondemand sequences. The returned sequence value is $value and not $sequence_value as expected");
|
|
|
556 |
|
|
|
557 |
}
|
|
|
558 |
|
|
|
559 |
$result = $this->db->dropSequence($sequence_name);
|
|
|
560 |
|
|
|
561 |
if (MDB::isError($result)) {
|
|
|
562 |
$this->assertTrue(false, "Error dropping sequence $sequence_name : ".$result->getMessage());
|
|
|
563 |
}
|
|
|
564 |
|
|
|
565 |
// Test currId()
|
|
|
566 |
$sequence_name = 'test_currid';
|
|
|
567 |
|
|
|
568 |
$next = $this->db->nextId($sequence_name);
|
|
|
569 |
$curr = $this->db->currId($sequence_name);
|
|
|
570 |
|
|
|
571 |
if (MDB::isError($curr)) {
|
|
|
572 |
$this->assertTrue(false, "Error getting the current value of sequence $sequence_name : ".$curr->getMessage());
|
|
|
573 |
} else {
|
|
|
574 |
if ($next != $curr) {
|
|
|
575 |
if ($next+1 == $curr) {
|
|
|
576 |
$this->assertTrue(false, "Warning: currId() is using nextId() instead of a native implementation");
|
|
|
577 |
} else {
|
|
|
578 |
$this->assertEquals($next, $curr, "return value if currId() does not match the previous call to nextId()");
|
|
|
579 |
}
|
|
|
580 |
}
|
|
|
581 |
}
|
|
|
582 |
$result = $this->db->dropSequence($sequence_name);
|
|
|
583 |
if (MDB::isError($result)) {
|
|
|
584 |
$this->assertTrue(false, "Error dropping sequence $sequence_name : ".$result->getMessage());
|
|
|
585 |
}
|
|
|
586 |
}
|
|
|
587 |
|
|
|
588 |
|
|
|
589 |
/**
|
|
|
590 |
* Test replace query
|
|
|
591 |
*
|
|
|
592 |
* The replace method emulates the replace query of mysql
|
|
|
593 |
*/
|
|
|
594 |
function testReplace() {
|
|
|
595 |
if (!$this->supported('Replace')) {
|
|
|
596 |
return;
|
|
|
597 |
}
|
|
|
598 |
|
|
|
599 |
$row = 1234;
|
|
|
600 |
$data = array();
|
|
|
601 |
$data['user_name'] = "user_$row";
|
|
|
602 |
$data['user_password'] = 'somepassword';
|
|
|
603 |
$data['subscribed'] = (boolean)($row % 2);
|
|
|
604 |
$data['user_id'] = $row;
|
|
|
605 |
$data['quota'] = strval($row/100);
|
|
|
606 |
$data['weight'] = sqrt($row);
|
|
|
607 |
$data['access_date'] = MDB_Date::mdbToday();
|
|
|
608 |
$data['access_time'] = MDB_Date::mdbTime();
|
|
|
609 |
$data['approved'] = MDB_Date::mdbNow();
|
|
|
610 |
|
|
|
611 |
$fields = array(
|
|
|
612 |
'user_name' => array(
|
|
|
613 |
'Value' => "user_$row",
|
|
|
614 |
'Type' => 'text'
|
|
|
615 |
),
|
|
|
616 |
'user_password' => array(
|
|
|
617 |
'Value' => $data['user_password'],
|
|
|
618 |
'Type' => 'text'
|
|
|
619 |
),
|
|
|
620 |
'subscribed' => array(
|
|
|
621 |
'Value' => $data['subscribed'],
|
|
|
622 |
'Type' => 'boolean'
|
|
|
623 |
),
|
|
|
624 |
'user_id' => array(
|
|
|
625 |
'Value' => $data['user_id'],
|
|
|
626 |
'Type' => 'integer',
|
|
|
627 |
'Key' => 1
|
|
|
628 |
),
|
|
|
629 |
'quota' => array(
|
|
|
630 |
'Value' => $data['quota'],
|
|
|
631 |
'Type' => 'decimal'
|
|
|
632 |
),
|
|
|
633 |
'weight' => array(
|
|
|
634 |
'Value' => $data['weight'],
|
|
|
635 |
'Type' => 'float'
|
|
|
636 |
),
|
|
|
637 |
'access_date' => array(
|
|
|
638 |
'Value' => $data['access_date'],
|
|
|
639 |
'Type' => 'date'
|
|
|
640 |
),
|
|
|
641 |
'access_time' => array(
|
|
|
642 |
'Value' => $data['access_time'],
|
|
|
643 |
'Type' => 'time'
|
|
|
644 |
),
|
|
|
645 |
'approved' => array(
|
|
|
646 |
'Value' => $data['approved'],
|
|
|
647 |
'Type' => 'timestamp'
|
|
|
648 |
)
|
|
|
649 |
);
|
|
|
650 |
|
|
|
651 |
$support_affected_rows = $this->db->support('AffectedRows');
|
|
|
652 |
|
|
|
653 |
$result = $this->db->replace('users', $fields);
|
|
|
654 |
|
|
|
655 |
if (MDB::isError($result)) {
|
|
|
656 |
$this->assertTrue(false, 'Replace failed');
|
|
|
657 |
}
|
|
|
658 |
|
|
|
659 |
if ($support_affected_rows) {
|
|
|
660 |
$affected_rows = $this->db->affectedRows();
|
|
|
661 |
|
|
|
662 |
$this->assertEquals(1, $affected_rows, "replacing a row in an empty table returned $affected_rows unlike 1 as expected");
|
|
|
663 |
}
|
|
|
664 |
|
|
|
665 |
$result = $this->db->query('SELECT user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved FROM users', $this->types);
|
|
|
666 |
|
|
|
667 |
if (MDB::isError($result)) {
|
|
|
668 |
$this->assertTrue(false, 'Error selecting from users'.$result->getMessage());
|
|
|
669 |
}
|
|
|
670 |
|
|
|
671 |
$this->verifyFetchedValues($result, 0, $data);
|
|
|
672 |
|
|
|
673 |
$row = 4321;
|
|
|
674 |
$fields['user_name']['Value'] = $data['user_name'] = "user_$row";
|
|
|
675 |
$fields['user_password']['Value'] = $data['user_password'] = 'somepassword';
|
|
|
676 |
$fields['subscribed']['Value'] = $data['subscribed'] = (boolean)($row % 2);
|
|
|
677 |
$fields['quota']['Value'] = $data['quota'] = strval($row/100);
|
|
|
678 |
$fields['weight']['Value'] = $data['weight'] = sqrt($row);
|
|
|
679 |
$fields['access_date']['Value'] = $data['access_date'] = MDB_Date::mdbToday();
|
|
|
680 |
$fields['access_time']['Value'] = $data['access_time'] = MDB_Date::mdbTime();
|
|
|
681 |
$fields['approved']['Value'] = $data['approved'] = MDB_Date::mdbNow();
|
|
|
682 |
|
|
|
683 |
$result = $this->db->replace('users', $fields);
|
|
|
684 |
|
|
|
685 |
if (MDB::isError($result)) {
|
|
|
686 |
$this->assertTrue(false, 'Replace failed');
|
|
|
687 |
}
|
|
|
688 |
|
|
|
689 |
if ($support_affected_rows) {
|
|
|
690 |
$affected_rows = $this->db->affectedRows();
|
|
|
691 |
|
|
|
692 |
$this->assertEquals(2, $affected_rows, "replacing a row in a non empty table returned $affected_rows unlike 2 as expected");
|
|
|
693 |
}
|
|
|
694 |
|
|
|
695 |
$result = $this->db->query('SELECT user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved FROM users', $this->types);
|
|
|
696 |
|
|
|
697 |
if (MDB::isError($result)) {
|
|
|
698 |
$this->assertTrue(false, 'Error selecting from users'.$result->getMessage());
|
|
|
699 |
}
|
|
|
700 |
|
|
|
701 |
$this->verifyFetchedValues($result, 0, $data);
|
|
|
702 |
|
|
|
703 |
$this->assertTrue($this->db->endOfResult($result), 'the query result did not seem to have reached the end of result as expected');
|
|
|
704 |
|
|
|
705 |
$this->db->freeResult($result);
|
|
|
706 |
}
|
|
|
707 |
|
|
|
708 |
/**
|
|
|
709 |
* Test affected rows methods
|
|
|
710 |
*/
|
|
|
711 |
function testAffectedRows() {
|
|
|
712 |
if (!$this->supported('AffectedRows')) {
|
|
|
713 |
return;
|
|
|
714 |
}
|
|
|
715 |
|
|
|
716 |
$data = array();
|
|
|
717 |
$total_rows = 7;
|
|
|
718 |
|
|
|
719 |
$prepared_query = $this->db->prepareQuery('INSERT INTO users (user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', $this->types);
|
|
|
720 |
|
|
|
721 |
for ($row = 0; $row < $total_rows; $row++) {
|
|
|
722 |
$data[$row]['user_name'] = "user_$row";
|
|
|
723 |
$data[$row]['user_password'] = 'somepassword';
|
|
|
724 |
$data[$row]['subscribed'] = (boolean)($row % 2);
|
|
|
725 |
$data[$row]['user_id'] = $row;
|
|
|
726 |
$data[$row]['quota'] = sprintf("%.2f",strval(1+($row+1)/100));
|
|
|
727 |
$data[$row]['weight'] = sqrt($row);
|
|
|
728 |
$data[$row]['access_date'] = MDB_Date::mdbToday();
|
|
|
729 |
$data[$row]['access_time'] = MDB_Date::mdbTime();
|
|
|
730 |
$data[$row]['approved'] = MDB_Date::mdbNow();
|
|
|
731 |
|
|
|
732 |
$this->insertTestValues($prepared_query, $data[$row]);
|
|
|
733 |
|
|
|
734 |
$result = $this->db->executeQuery($prepared_query);
|
|
|
735 |
|
|
|
736 |
$affected_rows = $this->db->affectedRows();
|
|
|
737 |
|
|
|
738 |
$this->assertEquals($affected_rows, 1, "Inserting the row $row returned $affected_rows affected row count instead of 1 as expected");
|
|
|
739 |
|
|
|
740 |
if (MDB::isError($result)) {
|
|
|
741 |
$this->assertTrue(false, 'Error executing prepared query'.$result->getMessage());
|
|
|
742 |
}
|
|
|
743 |
}
|
|
|
744 |
|
|
|
745 |
$this->db->freePreparedQuery($prepared_query);
|
|
|
746 |
|
|
|
747 |
$prepared_query = $this->db->prepareQuery('UPDATE users SET user_password=? WHERE user_id < ?', array('text', 'integer'));
|
|
|
748 |
|
|
|
749 |
for ($row = 0; $row < $total_rows; $row++) {
|
|
|
750 |
$this->db->setParamText($prepared_query, 1, "another_password_$row");
|
|
|
751 |
$this->db->setParamInteger($prepared_query, 2, $row);
|
|
|
752 |
|
|
|
753 |
$result = $this->db->executeQuery($prepared_query);
|
|
|
754 |
|
|
|
755 |
if (MDB::isError($result)) {
|
|
|
756 |
$this->assertTrue(false, 'Error executing prepared query'.$result->getMessage());
|
|
|
757 |
}
|
|
|
758 |
|
|
|
759 |
$affected_rows = $this->db->affectedRows();
|
|
|
760 |
|
|
|
761 |
$this->assertEquals($affected_rows, $row, "Updating the $row rows returned $affected_rows affected row count");
|
|
|
762 |
|
|
|
763 |
}
|
|
|
764 |
|
|
|
765 |
$this->db->freePreparedQuery($prepared_query);
|
|
|
766 |
|
|
|
767 |
$prepared_query = $this->db->prepareQuery('DELETE FROM users WHERE user_id >= ?', array('integer'));
|
|
|
768 |
|
|
|
769 |
for ($row = $total_rows; $total_rows; $total_rows = $row) {
|
|
|
770 |
$this->db->setParamInteger($prepared_query, 1, $row = intval($total_rows / 2));
|
|
|
771 |
|
|
|
772 |
$result = $this->db->executeQuery($prepared_query);
|
|
|
773 |
|
|
|
774 |
if (MDB::isError($result)) {
|
|
|
775 |
$this->assertTrue(false, 'Error executing prepared query'.$result->getMessage());
|
|
|
776 |
}
|
|
|
777 |
|
|
|
778 |
$affected_rows = $this->db->affectedRows();
|
|
|
779 |
|
|
|
780 |
$this->assertEquals($affected_rows, ($total_rows - $row), 'Deleting '.($total_rows - $row)." rows returned $affected_rows affected row count");
|
|
|
781 |
|
|
|
782 |
}
|
|
|
783 |
|
|
|
784 |
$this->db->freePreparedQuery($prepared_query);
|
|
|
785 |
}
|
|
|
786 |
|
|
|
787 |
/**
|
|
|
788 |
* Testing transaction support
|
|
|
789 |
*/
|
|
|
790 |
function testTransactions() {
|
|
|
791 |
if (!$this->supported('Transactions')) {
|
|
|
792 |
return;
|
|
|
793 |
}
|
|
|
794 |
|
|
|
795 |
$this->db->autoCommit(0);
|
|
|
796 |
|
|
|
797 |
$row = 0;
|
|
|
798 |
$data = array();
|
|
|
799 |
$data['user_name'] = "user_$row";
|
|
|
800 |
$data['user_password'] = 'somepassword';
|
|
|
801 |
$data['subscribed'] = (boolean)($row % 2);
|
|
|
802 |
$data['user_id'] = $row;
|
|
|
803 |
$data['quota'] = strval($row/100);
|
|
|
804 |
$data['weight'] = sqrt($row);
|
|
|
805 |
$data['access_date'] = MDB_Date::mdbToday();
|
|
|
806 |
$data['access_time'] = MDB_Date::mdbTime();
|
|
|
807 |
$data['approved'] = MDB_Date::mdbNow();
|
|
|
808 |
|
|
|
809 |
$prepared_query = $this->db->prepareQuery('INSERT INTO users (user_name, user_password, subscribed, user_id, quota, weight, access_date, access_time, approved) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', $this->types);
|
|
|
810 |
|
|
|
811 |
$this->insertTestValues($prepared_query, $data);
|
|
|
812 |
$result = $this->db->executeQuery($prepared_query);
|
|
|
813 |
$this->db->rollback();
|
|
|
814 |
|
|
|
815 |
$result = $this->db->query('SELECT * FROM users');
|
|
|
816 |
if (MDB::isError($result)) {
|
|
|
817 |
$this->assertTrue(false, 'Error selecting from users'.$result->getMessage());
|
|
|
818 |
}
|
|
|
819 |
|
|
|
820 |
$this->assertTrue($this->db->endOfResult($result), 'Transaction rollback did not revert the row that was inserted');
|
|
|
821 |
$this->db->freeResult($result);
|
|
|
822 |
|
|
|
823 |
$this->insertTestValues($prepared_query, $data);
|
|
|
824 |
$result = $this->db->executeQuery($prepared_query);
|
|
|
825 |
$this->db->commit();
|
|
|
826 |
|
|
|
827 |
$result = $this->db->query('SELECT * FROM users');
|
|
|
828 |
if (MDB::isError($result)) {
|
|
|
829 |
$this->assertTrue(false, 'Error selecting from users'.$result->getMessage());
|
|
|
830 |
}
|
|
|
831 |
|
|
|
832 |
$this->assertTrue(!$this->db->endOfResult($result), 'Transaction commit did not make permanent the row that was inserted');
|
|
|
833 |
$this->db->freeResult($result);
|
|
|
834 |
|
|
|
835 |
$result = $this->db->query('DELETE FROM users');
|
|
|
836 |
if (MDB::isError($result)) {
|
|
|
837 |
$this->assertTrue(false, 'Error deleting from users'.$result->getMessage());
|
|
|
838 |
$this->db->rollback();
|
|
|
839 |
}
|
|
|
840 |
|
|
|
841 |
$autocommit = $this->db->autocommit(1);
|
|
|
842 |
$this->assertTrue(!MDB::isError($autocommit), 'Error autocommiting transactions');
|
|
|
843 |
|
|
|
844 |
$this->db->freePreparedQuery($prepared_query);
|
|
|
845 |
|
|
|
846 |
$result = $this->db->query('SELECT * FROM users');
|
|
|
847 |
if (MDB::isError($result)) {
|
|
|
848 |
$this->assertTrue(false, 'Error selecting from users'.$result->getMessage());
|
|
|
849 |
}
|
|
|
850 |
|
|
|
851 |
$this->assertTrue($this->db->endOfResult($result), 'Transaction end with implicit commit when re-enabling auto-commit did not make permanent the rows that were deleted');
|
|
|
852 |
$this->db->freeResult($result);
|
|
|
853 |
}
|
|
|
854 |
|
|
|
855 |
/**
|
|
|
856 |
* Testing LOB storage
|
|
|
857 |
*/
|
|
|
858 |
|
|
|
859 |
function testLobStorage() {
|
|
|
860 |
if (!$this->supported('LOBs')) {
|
|
|
861 |
return;
|
|
|
862 |
}
|
|
|
863 |
|
|
|
864 |
//$prepared_query = $this->db->prepareQuery('INSERT INTO files (document,picture) VALUES (?,?)');
|
|
|
865 |
$prepared_query = $this->db->prepareQuery('INSERT INTO files (ID, document,picture) VALUES (1,?,?)', array('clob', 'blob'));
|
|
|
866 |
|
|
|
867 |
$character_lob = array(
|
|
|
868 |
'Database' => $this->db,
|
|
|
869 |
'Error' => '',
|
|
|
870 |
'Data' => ''
|
|
|
871 |
);
|
|
|
872 |
for ($code = 32; $code <= 127; $code++) {
|
|
|
873 |
$character_lob['Data'] .= chr($code);
|
|
|
874 |
}
|
|
|
875 |
$binary_lob = array(
|
|
|
876 |
'Database' => $this->db,
|
|
|
877 |
'Error' => '',
|
|
|
878 |
'Data' => ''
|
|
|
879 |
);
|
|
|
880 |
for ($code = 0; $code <= 255; $code++) {
|
|
|
881 |
$binary_lob['Data'] .= chr($code);
|
|
|
882 |
}
|
|
|
883 |
|
|
|
884 |
$clob = $this->db->createLob($character_lob);
|
|
|
885 |
$this->assertTrue(!MDB::isError($clob), 'Error creating character LOB: '.$character_lob['Error']);
|
|
|
886 |
|
|
|
887 |
$blob = $this->db->createLob($binary_lob);
|
|
|
888 |
$this->assertTrue(!MDB::isError($blob), 'Error creating binary LOB: '.$binary_lob['Error']);
|
|
|
889 |
|
|
|
890 |
$this->db->setParamClob($prepared_query, 1, $clob, 'document');
|
|
|
891 |
$this->db->setParamBlob($prepared_query, 2, $blob, 'picture');
|
|
|
892 |
$result = $this->db->executeQuery($prepared_query);
|
|
|
893 |
|
|
|
894 |
if($is_error = MDB::isError($result)) {
|
|
|
895 |
$msg = $result->getUserInfo();
|
|
|
896 |
} else {
|
|
|
897 |
$msg = '';
|
|
|
898 |
}
|
|
|
899 |
$this->assertTrue(!$is_error, 'Error executing prepared query: '.$msg);
|
|
|
900 |
|
|
|
901 |
$this->db->destroyLob($blob);
|
|
|
902 |
$this->db->destroyLob($clob);
|
|
|
903 |
$this->db->freePreparedQuery($prepared_query);
|
|
|
904 |
|
|
|
905 |
$result = $this->db->query('SELECT document, picture FROM files', array('clob', 'blob'));
|
|
|
906 |
//$result = $this->db->query('SELECT document, picture FROM files');
|
|
|
907 |
if (MDB::isError($result)) {
|
|
|
908 |
$this->assertTrue(false, 'Error selecting from files'.$result->getMessage());
|
|
|
909 |
}
|
|
|
910 |
$this->assertTrue(!$this->db->endOfResult($result), 'The query result seem to have reached the end of result too soon.');
|
|
|
911 |
|
|
|
912 |
$clob = $this->db->fetchClob($result, 0, 'document');
|
|
|
913 |
if (!MDB::isError($clob)) {
|
|
|
914 |
for ($value = ''; !$this->db->endOfLob($clob);) {
|
|
|
915 |
$this->assertTrue(($this->db->readLob($clob, $data, 8192) >= 0), 'Could not read CLOB');
|
|
|
916 |
$value .= $data;
|
|
|
917 |
}
|
|
|
918 |
$this->db->destroyLob($clob);
|
|
|
919 |
|
|
|
920 |
$this->assertEquals($value, $character_lob['Data'], 'Retrieved character LOB value ("' . $value . '") is different from what was stored ("' . $character_lob['Data'] . '")');
|
|
|
921 |
} else {
|
|
|
922 |
$this->assertTrue(false, 'Error retrieving CLOB result');
|
|
|
923 |
}
|
|
|
924 |
|
|
|
925 |
$blob = $this->db->fetchBlob($result, 0, 'picture');
|
|
|
926 |
|
|
|
927 |
if (!MDB::isError($blob)) {
|
|
|
928 |
for ($value = ''; !$this->db->endOfLob($clob);) {
|
|
|
929 |
$this->assertTrue(($this->db->readLob($blob, $data, 8192) >= 0), 'Could not read BLOB');
|
|
|
930 |
$value .= $data;
|
|
|
931 |
}
|
|
|
932 |
|
|
|
933 |
$this->db->destroyLob($blob);
|
|
|
934 |
|
|
|
935 |
$this->assertEquals($value, $binary_lob['Data'], 'Retrieved binary LOB value ("'.$value.'") is different from what was stored ("'.$binary_lob['Data'].'")');
|
|
|
936 |
} else {
|
|
|
937 |
$this->assertTrue(false, 'Error retrieving CLOB result');
|
|
|
938 |
}
|
|
|
939 |
$this->db->freeResult($result);
|
|
|
940 |
}
|
|
|
941 |
|
|
|
942 |
/**
|
|
|
943 |
* Test for lob storage from and to files
|
|
|
944 |
*/
|
|
|
945 |
|
|
|
946 |
function testLobFiles() {
|
|
|
947 |
if (!$this->supported('LOBs')) {
|
|
|
948 |
return;
|
|
|
949 |
}
|
|
|
950 |
|
|
|
951 |
$prepared_query = $this->db->prepareQuery('INSERT INTO files (ID, document,picture) VALUES (1,?,?)', array('clob', 'blob'));
|
|
|
952 |
//$prepared_query = $this->db->prepareQuery('INSERT INTO files (document,picture) VALUES (?,?)');
|
|
|
953 |
|
|
|
954 |
$character_data_file = 'character_data';
|
|
|
955 |
if (($file = fopen($character_data_file, 'w'))) {
|
|
|
956 |
for ($character_data = '', $code = 32; $code <= 127; $code++) {
|
|
|
957 |
$character_data .= chr($code);
|
|
|
958 |
}
|
|
|
959 |
$character_lob = array(
|
|
|
960 |
'Type' => 'inputfile',
|
|
|
961 |
'Database' => $this->db,
|
|
|
962 |
'Error' => '',
|
|
|
963 |
'FileName' => $character_data_file
|
|
|
964 |
);
|
|
|
965 |
$this->assertTrue((fwrite($file, $character_data, strlen($character_data)) == strlen($character_data)), 'Error creating clob file to read from');
|
|
|
966 |
fclose($file);
|
|
|
967 |
}
|
|
|
968 |
|
|
|
969 |
$binary_data_file = 'binary_data';
|
|
|
970 |
if (($file = fopen($binary_data_file, 'wb'))) {
|
|
|
971 |
for($binary_data = '', $code = 0; $code <= 255; $code++) {
|
|
|
972 |
$binary_data .= chr($code);
|
|
|
973 |
}
|
|
|
974 |
$binary_lob = array(
|
|
|
975 |
'Type' => 'inputfile',
|
|
|
976 |
'Database' => $this->db,
|
|
|
977 |
'Error' => '',
|
|
|
978 |
'FileName' => $binary_data_file
|
|
|
979 |
);
|
|
|
980 |
$this->assertTrue((fwrite($file, $binary_data, strlen($binary_data)) == strlen($binary_data)), 'Error creating blob file to read from');
|
|
|
981 |
fclose($file);
|
|
|
982 |
}
|
|
|
983 |
|
|
|
984 |
$clob = $this->db->createLob($character_lob);
|
|
|
985 |
$this->assertTrue(!MDB::isError($clob), 'Error creating clob');
|
|
|
986 |
|
|
|
987 |
$blob = $this->db->createLob($binary_lob);
|
|
|
988 |
$this->assertTrue(!MDB::isError($blob), 'Error creating blob');
|
|
|
989 |
|
|
|
990 |
$this->db->setParamCLOB($prepared_query, 1, $clob, 'document');
|
|
|
991 |
$this->db->setParamBLOB($prepared_query, 2, $blob, 'picture');
|
|
|
992 |
|
|
|
993 |
$result = $this->db->executeQuery($prepared_query);
|
|
|
994 |
$this->assertTrue(!MDB::isError($result), 'Error executing prepared query - inserting LOB from files');
|
|
|
995 |
|
|
|
996 |
$this->db->destroyLOB($blob);
|
|
|
997 |
$this->db->destroyLOB($clob);
|
|
|
998 |
|
|
|
999 |
$this->db->freePreparedQuery($prepared_query);
|
|
|
1000 |
|
|
|
1001 |
$result = $this->db->query('SELECT document, picture FROM files');
|
|
|
1002 |
if (MDB::isError($result)) {
|
|
|
1003 |
$this->assertTrue(false, 'Error selecting from files'.$result->getMessage());
|
|
|
1004 |
}
|
|
|
1005 |
|
|
|
1006 |
$this->assertTrue(!$this->db->endOfResult($result), 'The query result seem to have reached the end of result too soon.');
|
|
|
1007 |
|
|
|
1008 |
$character_lob = array(
|
|
|
1009 |
'Type' => 'outputfile',
|
|
|
1010 |
'Database' => $this->db,
|
|
|
1011 |
'Result' => $result,
|
|
|
1012 |
'Row' => 0,
|
|
|
1013 |
'Field' => 'document',
|
|
|
1014 |
'Binary' => 0,
|
|
|
1015 |
'Error' => '',
|
|
|
1016 |
'FileName' => $character_data_file
|
|
|
1017 |
);
|
|
|
1018 |
|
|
|
1019 |
$clob = $this->db->createLOB($character_lob);
|
|
|
1020 |
|
|
|
1021 |
if (!MDB::isError($clob)) {
|
|
|
1022 |
$this->assertTrue(($this->db->readLOB($clob, $data, 0) >= 0), 'Error reading CLOB ');
|
|
|
1023 |
$this->db->destroyLOB($clob);
|
|
|
1024 |
|
|
|
1025 |
$this->assertTrue(($file = fopen($character_data_file, 'r')), "Error opening character data file: $character_data_file");
|
|
|
1026 |
$this->assertEquals(getType($value = fread($file, filesize($character_data_file))), 'string', "Could not read from character LOB file: $character_data_file");
|
|
|
1027 |
fclose($file);
|
|
|
1028 |
|
|
|
1029 |
$this->assertEquals($value, $character_data, "retrieved character LOB value (\"".$value."\") is different from what was stored (\"".$character_data."\")");
|
|
|
1030 |
} else {
|
|
|
1031 |
$this->assertTrue(false, 'Error creating character LOB in a file');
|
|
|
1032 |
}
|
|
|
1033 |
|
|
|
1034 |
$binary_lob = array(
|
|
|
1035 |
'Type' => 'outputfile',
|
|
|
1036 |
'Database' => $this->db,
|
|
|
1037 |
'Result' => $result,
|
|
|
1038 |
'Row' => 0,
|
|
|
1039 |
'Field' => 'picture',
|
|
|
1040 |
'Binary' => 1,
|
|
|
1041 |
'Error' => '',
|
|
|
1042 |
'FileName' => $binary_data_file
|
|
|
1043 |
);
|
|
|
1044 |
|
|
|
1045 |
$blob = $this->db->createLOB($binary_lob);
|
|
|
1046 |
|
|
|
1047 |
if (!MDB::isError($blob)) {
|
|
|
1048 |
$this->assertTrue(($this->db->readLOB($blob, $data, 0) >= 0), 'Error reading BLOB ');
|
|
|
1049 |
$this->db->destroyLOB($blob);
|
|
|
1050 |
|
|
|
1051 |
$this->assertTrue(($file = fopen($binary_data_file, 'rb')), "Error opening binary data file: $binary_data_file");
|
|
|
1052 |
$this->assertEquals(getType($value = fread($file, filesize($binary_data_file))), 'string', "Could not read from binary LOB file: $binary_data_file");
|
|
|
1053 |
fclose($file);
|
|
|
1054 |
|
|
|
1055 |
$this->assertEquals($value, $binary_data, "retrieved binary LOB value (\"".$value."\") is different from what was stored (\"".$binary_data."\")");
|
|
|
1056 |
} else {
|
|
|
1057 |
$this->assertTrue(false, 'Error creating binary LOB in a file');
|
|
|
1058 |
}
|
|
|
1059 |
|
|
|
1060 |
$this->db->freeResult($result);
|
|
|
1061 |
}
|
|
|
1062 |
|
|
|
1063 |
/**
|
|
|
1064 |
* Test handling of lob nulls
|
|
|
1065 |
*/
|
|
|
1066 |
|
|
|
1067 |
function testLobNulls() {
|
|
|
1068 |
if (!$this->supported('LOBs')) {
|
|
|
1069 |
return;
|
|
|
1070 |
}
|
|
|
1071 |
|
|
|
1072 |
$prepared_query = $this->db->prepareQuery('INSERT INTO files (ID, document,picture) VALUES (1,?,?)', array('clob', 'blob'));
|
|
|
1073 |
//$prepared_query = $this->db->prepareQuery('INSERT INTO files (document,picture) VALUES (?,?)');
|
|
|
1074 |
|
|
|
1075 |
$this->db->setParamNull($prepared_query, 1, 'clob');
|
|
|
1076 |
$this->db->setParamNull($prepared_query, 2, 'blob');
|
|
|
1077 |
|
|
|
1078 |
$result = $this->db->executeQuery($prepared_query);
|
|
|
1079 |
$this->assertTrue(!MDB::isError($result), 'Error executing prepared query - inserting NULL lobs');
|
|
|
1080 |
|
|
|
1081 |
$this->db->freePreparedQuery($prepared_query);
|
|
|
1082 |
|
|
|
1083 |
$result = $this->db->query('SELECT document, picture FROM files', array('clob', 'blob'));
|
|
|
1084 |
if (MDB::isError($result)) {
|
|
|
1085 |
$this->assertTrue(false, 'Error selecting from files'.$result->getMessage());
|
|
|
1086 |
}
|
|
|
1087 |
|
|
|
1088 |
$this->assertTrue(!$this->db->endOfResult($result), 'The query result seem to have reached the end of result too soon.');
|
|
|
1089 |
|
|
|
1090 |
$this->assertTrue($this->db->resultIsNull($result, 0, 'document'), 'A query result large object column is not NULL unlike what was expected (document)');
|
|
|
1091 |
$this->assertTrue($this->db->resultIsNull($result, 0, 'picture'), 'A query result large object column is not NULL unlike what was expected (picture)');
|
|
|
1092 |
|
|
|
1093 |
$this->db->freeResult($result);
|
|
|
1094 |
}
|
|
|
1095 |
|
|
|
1096 |
/**
|
|
|
1097 |
* test tableInfo()
|
|
|
1098 |
*/
|
|
|
1099 |
function testTableInfo()
|
|
|
1100 |
{
|
|
|
1101 |
if (!$this->methodExists('tableInfo')) {
|
|
|
1102 |
return;
|
|
|
1103 |
}
|
|
|
1104 |
|
|
|
1105 |
$table_info = $this->db->tableInfo('users');
|
|
|
1106 |
if (MDB::isError($table_info)) {
|
|
|
1107 |
$this->assertTrue(false, 'Error in tableInfo(): '.$table_info->getMessage());
|
|
|
1108 |
} else {
|
|
|
1109 |
|
|
|
1110 |
$this->assertEquals(count($this->fields), count($table_info), 'The number of fields retrieved ('.count($table_info).') is different from the expected one ('.count($this->fields).')');
|
|
|
1111 |
foreach ($table_info as $field_info) {
|
|
|
1112 |
$this->assertEquals($field_info['table'], 'users', "the table name is not correct (expected: 'users'; actual: $field_info[table])");
|
|
|
1113 |
if (!in_array(strtolower($field_info['name']), $this->fields)) {
|
|
|
1114 |
$this->assertTrue(false, 'Field names do not match ('.$field_info['name'].' not recognized');
|
|
|
1115 |
}
|
|
|
1116 |
//add check on types...
|
|
|
1117 |
}
|
|
|
1118 |
}
|
|
|
1119 |
}
|
|
|
1120 |
}
|
|
|
1121 |
?>
|