summaryrefslogtreecommitdiff
path: root/trunk/reprap/web/james-bom/MDB2/MDB2-2.4.1/tests/MDB2_extended_testcase.php
blob: b0ffff79c69ba164c5df2a3ee10bfb5b895d2021 (plain)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
<?php
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5                                                 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Paul Cooper, Lukas Smith, Lorenzo Alberton   |
// | All rights reserved.                                                 |
// +----------------------------------------------------------------------+
// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB  |
// | API as well as database abstraction for PHP applications.            |
// | This LICENSE is in the BSD license style.                            |
// |                                                                      |
// | Redistribution and use in source and binary forms, with or without   |
// | modification, are permitted provided that the following conditions   |
// | are met:                                                             |
// |                                                                      |
// | Redistributions of source code must retain the above copyright       |
// | notice, this list of conditions and the following disclaimer.        |
// |                                                                      |
// | Redistributions in binary form must reproduce the above copyright    |
// | notice, this list of conditions and the following disclaimer in the  |
// | documentation and/or other materials provided with the distribution. |
// |                                                                      |
// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken,    |
// | Lukas Smith nor the names of his contributors may be used to endorse |
// | or promote products derived from this software without specific prior|
// | written permission.                                                  |
// |                                                                      |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
// |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
// | POSSIBILITY OF SUCH DAMAGE.                                          |
// +----------------------------------------------------------------------+
// | Authors: Paul Cooper <pgc@ucecom.com>                                |
// |          Lorenzo Alberton <l dot alberton at quipo dot it>           |
// +----------------------------------------------------------------------+
//
// $Id: MDB2_extended_testcase.php,v 1.13 2007/02/28 12:15:26 quipo Exp $

require_once 'MDB2_testcase.php';

class MDB2_Extended_TestCase extends MDB2_TestCase
{
    /**
     *
     */
    function testAutoExecute()
    {
        $data = $this->getSampleData();
        $select_query = 'SELECT ' . implode(', ', array_keys($this->fields)) . ' FROM users';

        $result = $this->db->loadModule('Extended');
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error loading "Extended" module: '.$result->getMessage());
        }
        $result = $this->db->extended->autoExecute('users', $data, MDB2_AUTOQUERY_INSERT, null, $this->fields);
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error auto executing insert: '.$result->getMessage());
        }

        $this->db->setFetchMode(MDB2_FETCHMODE_ASSOC);
        $result =& $this->db->query($select_query, $this->fields);
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error selecting from users: '.$result->getMessage());
        } else {
            $this->verifyFetchedValues($result, null, $data);
            $result->free();
        }

        $where = 'user_id = '.$this->db->quote($data['user_id'], 'integer');
        $result = $this->db->extended->autoExecute('users', null, MDB2_AUTOQUERY_SELECT, $where, null, true, $this->fields);
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error auto executing select: '.$result->getMessage());
        } else {
            $this->verifyFetchedValues($result, null, $data);
            $result->free();
        }

        $where = 'user_id = '.$this->db->quote($data['user_id'], 'integer');
        $result = $this->db->extended->autoExecute('users', null, MDB2_AUTOQUERY_SELECT, $where, null, true, MDB2_PREPARE_RESULT);
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error auto executing select: '.$result->getMessage());
        } else {
            $result->setResultTypes($this->fields);
            $this->verifyFetchedValues($result, null, $data);
            $result->free();
        }

        $update_data = array();
        $data['user_name'] = $update_data['user_name'] = 'foo';

        $where = 'user_id = '.$this->db->quote($data['user_id'], 'integer');
        $result = $this->db->extended->autoExecute('users', $update_data, MDB2_AUTOQUERY_UPDATE, $where, $this->fields);
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error auto executing insert: '.$result->getMessage());
        }

        $this->db->setFetchMode(MDB2_FETCHMODE_ASSOC);
        $result =& $this->db->query($select_query, $this->fields);
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error selecting from users: '.$result->getMessage());
        } else {
            $this->verifyFetchedValues($result, null, $data);
            $result->free();
        }

        $where = array($where, 'user_name = '.$this->db->quote($data['user_name'], 'text'));
        $result = $this->db->extended->autoExecute('users', null, MDB2_AUTOQUERY_DELETE, $where, null);
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error auto executing insert: '.$result->getMessage());
        }

        $this->db->setFetchMode(MDB2_FETCHMODE_ASSOC);
        $result =& $this->db->query($select_query, $this->fields);
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error selecting from users: '.$result->getMessage());
        } else {
            $this->assertEquals(0, $result->numRows(), 'No rows were expected to be returned');
            $result->free();
        }
    }

    /**
     * Test getAssoc()
     *
     * Test fetching two columns from a resultset. Return them as (key,value) pairs.
     */
    function testGetAssoc()
    {
        $result = $this->db->loadModule('Extended');
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error loading "Extended" module: '.$result->getMessage());
        }

        $data = $this->getSampleData(1234);

        $query = 'INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES ('.implode(', ', array_fill(0, count($this->fields), '?')).')';
        $stmt = $this->db->prepare($query, array_values($this->fields), MDB2_PREPARE_MANIP);
        $result = $stmt->execute(array_values($data));
        $stmt->free();

        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error executing prepared query: '.$result->getMessage());
        }

        //test getAssoc() with query parameters
        $query = 'SELECT user_id, user_name FROM users WHERE user_id=?';
        $result = $this->db->extended->getAssoc($query, array('integer', 'text'), array(1234), array('integer'));
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error executing getAssoc(): '.$result->getMessage());
        }
        $this->assertTrue(array_key_exists($data['user_id'], $result), 'Unexpected returned key');
        $this->assertEquals($data['user_name'], $result[$data['user_id']], 'Unexpected returned value');
        
        //test getAssoc() without query parameters
        $query = 'SELECT user_id, user_name FROM users WHERE user_id=1234';
        $result = $this->db->extended->getAssoc($query, array('integer', 'text'));
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error executing getAssoc(): '.$result->getMessage());
        } else {
            $this->assertTrue(array_key_exists($data['user_id'], $result), 'Unexpected returned key');
            $this->assertEquals($data['user_name'], $result[$data['user_id']], 'Unexpected returned value');
        }
        
        //add another record to the db
        $data2 = $this->getSampleData(4321);
        $query = 'INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES ('.implode(', ', array_fill(0, count($this->fields), '?')).')';
        $stmt = $this->db->prepare($query, array_values($this->fields), MDB2_PREPARE_MANIP);
        $result = $stmt->execute(array_values($data2));
        $stmt->free();

        //test getAssoc() with $force_array=true
        $query = 'SELECT user_id, user_name FROM users ORDER BY user_id';
        $values = $this->db->extended->getAssoc($query, array('integer', 'text'), null, null, MDB2_FETCHMODE_ASSOC, true);
        if (PEAR::isError($values)) {
            $this->assertTrue(false, 'Error executing getAssoc(): '.$values->getMessage());
        } else {
            $this->assertEquals(2, count($values), 'Error: incorrect number of returned rows');
            list($id, $value) = each($values);
            $this->assertEquals($data['user_id'],   $id,                 'Unexpected returned value');
            $this->assertEquals($data['user_name'], $value['user_name'], 'Unexpected returned value');
            list($id, $value) = each($values);
            $this->assertEquals($data2['user_id'],   $id,                 'Unexpected returned value');
            $this->assertEquals($data2['user_name'], $value['user_name'], 'Unexpected returned value');
        }


        //test getAssoc() with $force_array=false and $group=true
        $query = 'SELECT user_id, user_name FROM users ORDER BY user_id';
        $values = $this->db->extended->getAssoc($query, array('integer', 'text'), null, null, MDB2_FETCHMODE_ASSOC, false, true);
        if (PEAR::isError($values)) {
            $this->assertTrue(false, 'Error executing getAssoc(): '.$values->getMessage());
        } else {
            //@todo: check if MDB2_FETCHMODE_ASSOC is behaving correctly in this case
            $this->assertEquals(2, count($values), 'Error: incorrect number of returned rows');
            list($id, $value) = each($values);
            $this->assertEquals($data['user_id'],   $id,       'Unexpected returned value');
            $this->assertEquals($data['user_name'], $value[0], 'Unexpected returned value');
            list($id, $value) = each($values);
            $this->assertEquals($data2['user_id'],   $id,       'Unexpected returned value');
            $this->assertEquals($data2['user_name'], $value[0], 'Unexpected returned value');

        }
        
        //test $group=true with 3 fields
        $query = 'SELECT user_password, user_id, user_name FROM users ORDER BY user_id';
        $values = $this->db->extended->getAssoc($query, array('integer', 'text', 'text'), null, null, MDB2_FETCHMODE_ASSOC, false, true);
        if (PEAR::isError($values)) {
            $this->assertTrue(false, 'Error executing getAssoc(): '.$values->getMessage());
        } else {
            //the 2 values for user_password are equals, so they are collapsed in the same array
            $this->assertEquals(1, count($values), 'Error: incorrect number of returned rows');
            $values = $values[0];
            //there are 2 records
            $this->assertEquals(2, count($values), 'Error: incorrect number of returned rows');
            $value = array_shift($values);
            $this->assertEquals($data['user_id'],   $value['user_id'], 'Unexpected returned value');
            $this->assertEquals($data['user_name'], $value['user_name'], 'Unexpected returned value');
            $value = array_shift($values);
            $this->assertEquals($data2['user_id'],   $value['user_id'], 'Unexpected returned value');
            $this->assertEquals($data2['user_name'], $value['user_name'], 'Unexpected returned value');
        }
        
        //test $group=false with 3 fields
        $values = $this->db->extended->getAssoc($query, array('integer', 'text', 'text'), null, null, MDB2_FETCHMODE_ASSOC, false, false);
        if (PEAR::isError($values)) {
            $this->assertTrue(false, 'Error executing getAssoc(): '.$values->getMessage());
        } else {
            //the 2 values for user_password are equals, so the first record is overwritten
            $this->assertEquals(1, count($values), 'Error: incorrect number of returned rows');
            $values = $values[0];
            $this->assertEquals($data2['user_id'],   $value['user_id'], 'Unexpected returned value');
            $this->assertEquals($data2['user_name'], $value['user_name'], 'Unexpected returned value');
        }
    }

    /**
     * Test getOne()
     *
     * Test fetching a single value
     */
    function testGetOne()
    {
        $result = $this->db->loadModule('Extended');
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error loading "Extended" module: '.$result->getMessage());
        }

        $data = $this->getSampleData(1234);

        $query = 'INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES ('.implode(', ', array_fill(0, count($this->fields), '?')).')';
        $stmt = $this->db->prepare($query, array_values($this->fields), MDB2_PREPARE_MANIP);
        $result = $stmt->execute(array_values($data));
        $stmt->free();

        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error executing prepared query: '.$result->getMessage());
        }

        //test getOne() with query parameters
        $query = 'SELECT user_name FROM users WHERE user_id=?';
        $result = $this->db->extended->getOne($query, 'text', array(1234), array('integer'));
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error executing getOne(): '.$result->getMessage());
        }
        $this->assertEquals($data['user_name'], $result, 'Unexpected returned value');

        //test getOne() without query parameters
        $query = 'SELECT user_name FROM users WHERE user_id=1234';
        $result = $this->db->extended->getOne($query, 'text');
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error executing getOne(): '.$result->getMessage());
        }
        $this->assertEquals($data['user_name'], $result, 'Unexpected returned value');

        //test getOne() with column number (resultset: 0-based array)
        $query = 'SELECT user_id, user_name, approved FROM users WHERE user_id=1234';
        $result = $this->db->extended->getOne($query, 'text', null, null, 1); //get the 2nd column
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error executing getOne(): '.$result->getMessage());
        }
        $this->assertEquals($data['user_name'], $result, 'Unexpected returned value');
    }

    /**
     * Test getCol()
     *
     * Test fetching a column of result data.
     */
    function testGetCol()
    {
        $result = $this->db->loadModule('Extended');
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error loading "Extended" module: '.$result->getMessage());
        }

        $data = array(
            0 => $this->getSampleData(1234),
            1 => $this->getSampleData(4321),
        );
        $query = 'INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES ('.implode(', ', array_fill(0, count($this->fields), '?')).')';
        $stmt = $this->db->prepare($query, array_values($this->fields), MDB2_PREPARE_MANIP);
        $result = $stmt->execute(array_values($data[0]));
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error executing prepared query: '.$result->getMessage());
        }
        $result = $stmt->execute(array_values($data[1]));
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error executing prepared query: '.$result->getMessage());
        }
        $stmt->free();

        //test getCol() with query parameters
        $query = 'SELECT user_name FROM users WHERE user_id>?';
        $result = $this->db->extended->getCol($query, 'text', array(1), array('integer'));
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error executing getCol(): '.$result->getMessage());
        }
        $expected = array(
            $data[0]['user_name'],
            $data[1]['user_name'],
        );
        $this->assertEquals($expected, $result, 'Unexpected returned value');

        //test getCol() without query parameters
        $query = 'SELECT user_name FROM users';
        $result = $this->db->extended->getCol($query, 'text');
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error executing getCol(): '.$result->getMessage());
        }
        $this->assertEquals($expected, $result, 'Unexpected returned value');

        //test getCol() with column number (resultset: 0-based array)
        $query = 'SELECT user_id, user_name, approved FROM users';
        $result = $this->db->extended->getCol($query, 'text', null, null, 1); //get the 2nd column
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error executing getCol(): '.$result->getMessage());
        }
        $this->assertEquals($expected, $result, 'Unexpected returned value');
    }

    /**
     * Test getRow()
     *
     * Test fetching a row of result data.
     */
    function testGetRow()
    {
        $result = $this->db->loadModule('Extended');
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error loading "Extended" module: '.$result->getMessage());
        }

        $data = $this->getSampleData(1234);
        $query = 'INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES ('.implode(', ', array_fill(0, count($this->fields), '?')).')';
        $stmt = $this->db->prepare($query, array_values($this->fields), MDB2_PREPARE_MANIP);
        $result = $stmt->execute(array_values($data));
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error executing prepared query: '.$result->getMessage());
        }
        $stmt->free();

        //test getRow() with query parameters
        $query = 'SELECT user_id, user_name, user_password FROM users WHERE user_id=?';
        $result = $this->db->extended->getRow($query, array('integer', 'text', 'text'), array(1234), array('integer'), MDB2_FETCHMODE_ASSOC);
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error executing getRow(): '.$result->getMessage());
        }
        $this->assertEquals($data['user_id'],       $result['user_id'],       'Unexpected returned value');
        $this->assertEquals($data['user_name'],     $result['user_name'],     'Unexpected returned value');
        $this->assertEquals($data['user_password'], $result['user_password'], 'Unexpected returned value');

        //test getRow() without query parameters
        $query = 'SELECT user_id, user_name, user_password FROM users';
        $result = $this->db->extended->getRow($query, array('integer', 'text', 'text'), null, null, MDB2_FETCHMODE_ASSOC);
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error executing getRow(): '.$result->getMessage());
        }
        $this->assertEquals($data['user_id'],       $result['user_id'],       'Unexpected returned value');
        $this->assertEquals($data['user_name'],     $result['user_name'],     'Unexpected returned value');
        $this->assertEquals($data['user_password'], $result['user_password'], 'Unexpected returned value');
    }

    /**
     * Test getAll()
     *
     * Test fetching result data all at once.
     */
    function testGetAll()
    {
        $result = $this->db->loadModule('Extended');
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error loading "Extended" module: '.$result->getMessage());
        }

        $data = array();
        $total_rows = 5;

        $stmt = $this->db->prepare('INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES ('.implode(', ', array_fill(0, count($this->fields), '?')).')', array_values($this->fields), MDB2_PREPARE_MANIP);

        for ($row = 0; $row < $total_rows; $row++) {
            $data[$row] = $this->getSampleData($row);
            $result = $stmt->execute(array_values($data[$row]));
            if (PEAR::isError($result)) {
                $this->assertTrue(false, 'Error executing prepared query: '.$result->getMessage());
            }
        }
        $stmt->free();

        //test getAll() with query parameters
        $query = 'SELECT user_id, user_name, user_password FROM users WHERE user_id > ? ORDER BY user_id';
        $values = $this->db->extended->getAll($query, array('integer', 'text', 'text'), array(2), array('integer'), MDB2_FETCHMODE_ASSOC);
        if (PEAR::isError($values)) {
            $this->assertTrue(false, 'Error fetching the result set: '.$values->getMessage());
        } else {
            $this->assertEquals(2, count($values), 'Error: incorrect number of returned rows');
            for ($i=0; $i<2; $i++) {
                $this->assertEquals($data[$i+3]['user_id'],       $values[$i]['user_id'],       'Unexpected returned value');
                $this->assertEquals($data[$i+3]['user_name'],     $values[$i]['user_name'],     'Unexpected returned value');
                $this->assertEquals($data[$i+3]['user_password'], $values[$i]['user_password'], 'Unexpected returned value');
            }
        }

        //test getAll() without query parameters
        $query = 'SELECT user_id, user_name, user_password FROM users ORDER BY user_id';
        $values = $this->db->extended->getAll($query, array('integer', 'text', 'text'), null, null, MDB2_FETCHMODE_ASSOC);
        if (PEAR::isError($values)) {
            $this->assertTrue(false, 'Error fetching the result set: '.$values->getMessage());
        } else {
            $this->assertEquals($total_rows, count($values), 'Error: incorrect number of returned rows');
            for ($i=0; $i<$total_rows; $i++) {
                $this->assertEquals($data[$i]['user_id'],       $values[$i]['user_id'],       'Unexpected returned value');
                $this->assertEquals($data[$i]['user_name'],     $values[$i]['user_name'],     'Unexpected returned value');
                $this->assertEquals($data[$i]['user_password'], $values[$i]['user_password'], 'Unexpected returned value');
            }
        }
    }

    /**
     * Test limitQuery()
     *
     * Test fetching a limited resultset.
     */
    function testLimitQuery()
    {
        $result = $this->db->loadModule('Extended');
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error loading "Extended" module: '.$result->getMessage());
        }

        $data = array();
        $total_rows = 5;

        $stmt = $this->db->prepare('INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES ('.implode(', ', array_fill(0, count($this->fields), '?')).')', array_values($this->fields), MDB2_PREPARE_MANIP);

        for ($row = 0; $row < $total_rows; $row++) {
            $data[$row] = $this->getSampleData($row);
            $result = $stmt->execute(array_values($data[$row]));
            if (PEAR::isError($result)) {
                $this->assertTrue(false, 'Error executing prepared query: '.$result->getMessage());
            }
        }
        $stmt->free();
        
        $query = 'SELECT user_id, user_name, user_password FROM users ORDER BY user_id';
        $types = array('integer', 'text', 'text');

        //test limitQuery() with offset = 0
        $result = $this->db->extended->limitQuery($query, $types, 2);
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error executing limitQuery(): '.$result->getMessage());
        }
        $values = $result->fetchAll(MDB2_FETCHMODE_ASSOC);
        if (PEAR::isError($values)) {
            $this->assertTrue(false, 'Error fetching the result set');
        } else {
            $this->assertEquals(2, count($values), 'Error: incorrect number of returned rows');
            for ($i=0; $i<2; $i++) {
                $this->assertEquals($data[$i]['user_id'],       $values[$i]['user_id'],       'Unexpected returned value');
                $this->assertEquals($data[$i]['user_name'],     $values[$i]['user_name'],     'Unexpected returned value');
                $this->assertEquals($data[$i]['user_password'], $values[$i]['user_password'], 'Unexpected returned value');
            }
        }
        $result->free();

        //test limitQuery() with offset > 0
        $result = $this->db->extended->limitQuery($query, $types, 3, 2);
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error executing limitQuery(): '.$result->getMessage());
        }
        $values = $result->fetchAll(MDB2_FETCHMODE_ASSOC);
        if (PEAR::isError($values)) {
            $this->assertTrue(false, 'Error fetching the result set');
        } else {
            $this->assertEquals(3, count($values), 'Error: incorrect number of returned rows');
            for ($i=0; $i<3; $i++) {
                $this->assertEquals($data[$i+2]['user_id'],       $values[$i]['user_id'],       'Unexpected returned value');
                $this->assertEquals($data[$i+2]['user_name'],     $values[$i]['user_name'],     'Unexpected returned value');
                $this->assertEquals($data[$i+2]['user_password'], $values[$i]['user_password'], 'Unexpected returned value');
            }
        }
        $result->free();
    }

    /**
     * Test execParam()
     *
     * Test executing a query with parameters
     */
    function testExecParam()
    {
        $result = $this->db->loadModule('Extended');
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error loading "Extended" module: '.$result->getMessage());
        }
        
        $data = $this->getSampleData(1234);
        
        $query = 'INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES ('.implode(', ', array_fill(0, count($this->fields), '?')).')';
        
        $result = $this->db->extended->execParam($query, array_values($data), $this->fields);
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error executing execParam(): '.$result->getMessage());
        }
        
        $query = 'SELECT user_id, user_name, user_password FROM users WHERE user_id=?';
        $result = $this->db->extended->getRow($query, array('integer', 'text', 'text'), array(1234), array('integer'), MDB2_FETCHMODE_ASSOC);
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error executing getRow(): '.$result->getMessage());
        }
        $this->assertEquals($data['user_id'],       $result['user_id'],       'Unexpected returned value');
        $this->assertEquals($data['user_name'],     $result['user_name'],     'Unexpected returned value');
        $this->assertEquals($data['user_password'], $result['user_password'], 'Unexpected returned value');
    }

    /**
     * Test executeMultiple()
     *
     * Test executing multiple prepared queries
     */
    function testExecuteMultiple()
    {
        $result = $this->db->loadModule('Extended');
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error loading "Extended" module: '.$result->getMessage());
        }

        $data = array();
        $total_rows = 5;
        
        for ($row = 0; $row < $total_rows; $row++) {
            $data[$row] = array_values($this->getSampleData($row));
        }

        $stmt = $this->db->prepare('INSERT INTO users (' . implode(', ', array_keys($this->fields)) . ') VALUES ('.implode(', ', array_fill(0, count($this->fields), '?')).')');
        $result = $this->db->extended->executeMultiple($stmt, $data);
        if (PEAR::isError($result)) {
            $this->assertTrue(false, 'Error executing executeMultiple(): '.$result->getMessage());
        }
        $stmt->free();
        
        $query = 'SELECT ' . implode(', ', array_keys($this->fields)) . ' FROM users';
        $values = $this->db->queryAll($query, $this->fields, MDB2_FETCHMODE_ORDERED);
        
        $n_fields = count($this->fields);
        for ($i=0; $i<$total_rows; $i++) {
            for ($field=0; $field<$n_fields; $field++) {
                $this->assertEquals(strval($data[$i][$field]), strval($values[$i][$field]), 'Unexpected returned value');
            }
        }
    }
}
?>