Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
--TEST--
2
DB_driver::get
3
--INI--
4
error_reporting = 2047
5
--SKIPIF--
6
<?php
7
 
8
/**
9
 * Calls the get*() methods in various ways against any DBMS.
10
 *
11
 * @see DB_Common::getAll(), DB_Common::getAssoc(), DB_Common::getCol()
12
 *      DB_Common::getListOf(), DB_Common::getOne(), DB_Common::getRow()
13
 *
14
 * @package  DB
15
 * @version  $Id: 18get.phpt,v 1.9 2005/02/14 23:47:54 danielc Exp $
16
 * @category Database
17
 * @author   Daniel Convissor <danielc@analysisandsolutions.com>
18
 * @internal
19
 */
20
 
21
chdir(dirname(__FILE__));
22
require_once './skipif.inc';
23
 
24
?>
25
--FILE--
26
<?php
27
 
28
// $Id: 18get.phpt,v 1.9 2005/02/14 23:47:54 danielc Exp $
29
 
30
/**
31
 * Connect to the database and make the <kbd>phptest</kbd> table.
32
 */
33
require_once './mktable.inc';
34
 
35
 
36
/**
37
 * Local error callback handler.
38
 *
39
 * Drops the phptest table, prints out an error message and kills the
40
 * process.
41
 *
42
 * @param object  $o  PEAR error object automatically passed to this method
43
 * @return void
44
 * @see PEAR::setErrorHandling()
45
 */
46
function pe($o){
47
    global $dbh;
48
 
49
    $dbh->setErrorHandling(PEAR_ERROR_RETURN);
50
    drop_table($dbh, 'phptest');
51
 
52
    die($o->toString());
53
}
54
 
55
$dbh->setErrorHandling(PEAR_ERROR_CALLBACK, 'pe');
56
 
57
 
58
$dbh->query("INSERT INTO phptest VALUES (2, 'two', 'Two', '2002-02-22')");
59
$dbh->query("INSERT INTO phptest VALUES (42, 'three', 'Three', '2003-03-23')");
60
 
61
 
62
print "===================================================\n";
63
print 'testing getOne: ';
64
$ret =& $dbh->getOne("SELECT * FROM phptest WHERE c = 'Two'");
65
print_r($ret);
66
print "\n";
67
 
68
print 'testing getOne with string params: ';
69
$ret =& $dbh->getOne('SELECT * FROM phptest WHERE c = ?', 'Three');
70
print_r($ret);
71
print "\n";
72
 
73
print 'testing getOne with array params: ';
74
$ret =& $dbh->getOne('SELECT * FROM phptest WHERE c = ?', array('Two'));
75
print_r($ret);
76
print "\n";
77
 
78
print "\n===================================================\n";
79
print "testing getRow:\n";
80
$ret =& $dbh->getRow("SELECT * FROM phptest WHERE c = 'Two'");
81
print_r($ret);
82
 
83
print "testing getRow with null params, DB_FETCHMODE_ORDERED:\n";
84
$ret =& $dbh->getRow("SELECT * FROM phptest WHERE c = 'Two'",
85
        null, DB_FETCHMODE_ORDERED);
86
print_r($ret);
87
 
88
// THIS DOESN'T WORK DUE TO BACKWARDS COMPATIBILITY CRAP
89
// print "testing getRow with string params, DB_FETCHMODE_ORDERED:\n";
90
// $ret =& $dbh->getRow('SELECT * FROM phptest WHERE c = ?',
91
//         'Two', DB_FETCHMODE_ORDERED);
92
// print_r($ret);
93
//
94
// testing getRow with string params, DB_FETCHMODE_ORDERED:
95
// Array
96
// (
97
//     [0] => 2
98
//     [1] => two
99
//     [2] => Two
100
//     [3] => 2002-02-22
101
// )
102
 
103
   print "testing getRow with REVERSED args: DB_FETCHMODE_ASSOC, array params:\n";
104
   $ret =& $dbh->getRow('SELECT * FROM phptest WHERE c = ?',
105
           DB_FETCHMODE_ASSOC, array('Two'));
106
   print_r($ret);
107
 
108
   print "testing getRow with REVERSED args: DB_FETCHMODE_ASSOC:\n";
109
   $ret =& $dbh->getRow("SELECT * FROM phptest WHERE c = 'Two'",
110
           DB_FETCHMODE_ASSOC);
111
   print_r($ret);
112
 
113
print "testing getRow with array params, DB_FETCHMODE_ASSOC:\n";
114
$ret =& $dbh->getRow('SELECT * FROM phptest WHERE c = ?',
115
        array('Two'), DB_FETCHMODE_ASSOC);
116
print_r($ret);
117
 
118
print "testing getRow with array params, DB_FETCHMODE_OBJECT:\n";
119
$ret =& $dbh->getRow('SELECT * FROM phptest WHERE c = ?',
120
        array('Two'), DB_FETCHMODE_OBJECT);
121
print_r($ret);
122
 
123
 
124
print "\n===================================================\n";
125
print "testing getCol:\n";
126
$ret =& $dbh->getCol("SELECT * FROM phptest ORDER BY b");
127
print_r($ret);
128
 
129
print "testing getCol on query with no records:\n";
130
$ret =& $dbh->getCol('SELECT * FROM phptest WHERE a > 200');
131
print_r($ret);
132
 
133
print "testing getCol with invalid column id:\n";
134
$dbh->setErrorHandling(PEAR_ERROR_RETURN);
135
$ret =& $dbh->getCol('SELECT b FROM phptest ORDER BY b', 1);
136
if (DB::isError($ret)) {
137
    echo $ret->getMessage() . "\n";
138
} else {
139
    print ">> Should have produced 'no such field' error\n";
140
}
141
$dbh->setErrorHandling(PEAR_ERROR_CALLBACK, 'pe');
142
 
143
print "testing getCol with 1 col:\n";
144
$ret =& $dbh->getCol("SELECT * FROM phptest ORDER BY b", 1);
145
print_r($ret);
146
 
147
print "testing getCol with b col:\n";
148
$ret =& $dbh->getCol("SELECT * FROM phptest ORDER BY b", 'b');
149
print_r($ret);
150
 
151
print "testing getCol with b col, scalar params:\n";
152
$ret =& $dbh->getCol("SELECT * FROM phptest WHERE a < ? ORDER BY b",
153
        'b', 100);
154
print_r($ret);
155
 
156
print "testing getCol with b col, array params:\n";
157
$ret =& $dbh->getCol("SELECT * FROM phptest WHERE a < ? ORDER BY b",
158
        'b', array(100));
159
print_r($ret);
160
 
161
 
162
print "\n===================================================\n";
163
print "testing getAssoc:\n";
164
$ret =& $dbh->getAssoc('SELECT a, b, c FROM phptest WHERE a < 100 ORDER BY b');
165
print_r($ret);
166
 
167
print "testing getAssoc with false force, null params, DB_FETCHMODE_ORDERED:\n";
168
$ret =& $dbh->getAssoc("SELECT a, b, c FROM phptest WHERE a < 100 ORDER BY b",
169
                        false, null, DB_FETCHMODE_ORDERED);
170
print_r($ret);
171
 
172
print "testing getAssoc with false force, scalar params, DB_FETCHMODE_ASSOC:\n";
173
$ret =& $dbh->getAssoc('SELECT a, b, c FROM phptest WHERE a < ? ORDER BY b',
174
                        false, 100, DB_FETCHMODE_ASSOC);
175
print_r($ret);
176
 
177
print "testing getAssoc with two cols, false force, scalar params, DB_FETCHMODE_ASSOC:\n";
178
$ret =& $dbh->getAssoc('SELECT a, b FROM phptest WHERE a < ? ORDER BY b',
179
                        false, 100, DB_FETCHMODE_ASSOC);
180
print_r($ret);
181
 
182
print "testing getAssoc with two cols, true force, scalar params, DB_FETCHMODE_ASSOC:\n";
183
$ret =& $dbh->getAssoc('SELECT a, b FROM phptest WHERE a < ? ORDER BY b',
184
                        true, 100, DB_FETCHMODE_ASSOC);
185
print_r($ret);
186
 
187
print "testing getAssoc with false force, scalar params, DB_FETCHMODE_ASSOC, true group:\n";
188
$ret =& $dbh->getAssoc('SELECT a, b, c FROM phptest WHERE a < ? ORDER BY b',
189
                        false, 100, DB_FETCHMODE_ASSOC, true);
190
print_r($ret);
191
 
192
print "testing getAssoc with false force, array params, DB_FETCHMODE_OBJECT:\n";
193
$ret =& $dbh->getAssoc('SELECT a, b, c FROM phptest WHERE a < ? ORDER BY b',
194
                        false, array(100), DB_FETCHMODE_OBJECT);
195
print_r($ret);
196
 
197
print "testing getAssoc with true force, array params, DB_FETCHMODE_OBJECT, true group:\n";
198
$ret =& $dbh->getAssoc('SELECT a, b, c FROM phptest WHERE a < ? ORDER BY b',
199
                        false, array(100), DB_FETCHMODE_OBJECT, true);
200
print_r($ret);
201
 
202
 
203
print "\n===================================================\n";
204
print "testing getAll:\n";
205
$ret =& $dbh->getAll("SELECT * FROM phptest WHERE c = 'Two' OR c = 'Three'");
206
print_r($ret);
207
 
208
print "testing getAll with null params, DB_FETCHMODE_ORDERED:\n";
209
$ret =& $dbh->getAll("SELECT * FROM phptest WHERE c = 'Two' OR c = 'Three'",
210
        null, DB_FETCHMODE_ORDERED);
211
print_r($ret);
212
 
213
// THIS DOESN'T WORK DUE TO BACKWARDS COMPATIBILITY CRAP
214
// print "testing getAll with string params, DB_FETCHMODE_ORDERED:\n";
215
// $ret =& $dbh->getAll('SELECT * FROM phptest WHERE c = ?',
216
//         'Two', DB_FETCHMODE_ORDERED);
217
// print_r($ret);
218
//
219
// testing getAll with string params, DB_FETCHMODE_ORDERED:
220
// Array
221
// (
222
//     [0] => 2
223
//     [1] => two
224
//     [2] => Two
225
//     [3] => 2002-02-22
226
// )
227
 
228
   print "testing getAll with REVERSED args: DB_FETCHMODE_ASSOC, array params:\n";
229
   $ret =& $dbh->getAll('SELECT * FROM phptest WHERE c = ? OR c = ? ORDER BY c',
230
           DB_FETCHMODE_ASSOC, array('Two', 'Three'));
231
   print_r($ret);
232
 
233
   print "testing getAll with REVERSED args: DB_FETCHMODE_ASSOC:\n";
234
   $ret =& $dbh->getAll("SELECT * FROM phptest WHERE c = 'Two' OR c = 'Three'",
235
           DB_FETCHMODE_ASSOC);
236
   print_r($ret);
237
 
238
print "testing getAll with array params, DB_FETCHMODE_ASSOC:\n";
239
$ret =& $dbh->getAll('SELECT * FROM phptest WHERE c = ? OR c = ? ORDER BY c',
240
        array('Two', 'Three'), DB_FETCHMODE_ASSOC);
241
print_r($ret);
242
 
243
print "testing getAll with array params, DB_FETCHMODE_OBJECT:\n";
244
$ret =& $dbh->getAll('SELECT * FROM phptest WHERE c = ? OR c = ? ORDER BY c',
245
        array('Two', 'Three'), DB_FETCHMODE_OBJECT);
246
print_r($ret);
247
 
248
 
249
print "\n===================================================\n";
250
print 'testing getOne with null value in column: ';
251
$dbh->query("INSERT INTO phptest VALUES (9, 'nine', '', NULL)");
252
$ret =& $dbh->getOne('SELECT d FROM phptest WHERE a = 9');
253
if ($ret === '') {
254
    print "matches expected result\n";
255
} else {
256
    if ($dbh->phptype == 'msql') {
257
        if (gettype($ret) == 'NULL') {
258
            // msql doesn't even return the column.  Joy! :)
259
            // http://bugs.php.net/?id=31960
260
            print "matches expected result\n";
261
        } else {
262
            print "WOW, mSQL now returns columns that have NULLS in them\n";
263
        }
264
    } else {
265
        print 'type=' . gettype($ret) . ", value=$ret\n";
266
    }
267
}
268
 
269
print 'testing getOne with empty string in column: ';
270
$ret =& $dbh->getOne('SELECT c FROM phptest WHERE a = 9');
271
if ($ret === '') {
272
    print "empty string\n";
273
} else {
274
    print 'type=' . gettype($ret) . ", value=$ret\n";
275
}
276
 
277
 
278
print "\n===================================================\n";
279
 
280
 
281
drop_table($dbh, 'phptest');
282
 
283
 
284
?>
285
--EXPECT--
286
===================================================
287
testing getOne: 2
288
testing getOne with string params: 42
289
testing getOne with array params: 2
290
 
291
===================================================
292
testing getRow:
293
Array
294
(
295
    [0] => 2
296
    [1] => two
297
    [2] => Two
298
    [3] => 2002-02-22
299
)
300
testing getRow with null params, DB_FETCHMODE_ORDERED:
301
Array
302
(
303
    [0] => 2
304
    [1] => two
305
    [2] => Two
306
    [3] => 2002-02-22
307
)
308
testing getRow with REVERSED args: DB_FETCHMODE_ASSOC, array params:
309
Array
310
(
311
    [a] => 2
312
    [b] => two
313
    [c] => Two
314
    [d] => 2002-02-22
315
)
316
testing getRow with REVERSED args: DB_FETCHMODE_ASSOC:
317
Array
318
(
319
    [a] => 2
320
    [b] => two
321
    [c] => Two
322
    [d] => 2002-02-22
323
)
324
testing getRow with array params, DB_FETCHMODE_ASSOC:
325
Array
326
(
327
    [a] => 2
328
    [b] => two
329
    [c] => Two
330
    [d] => 2002-02-22
331
)
332
testing getRow with array params, DB_FETCHMODE_OBJECT:
333
stdClass Object
334
(
335
    [a] => 2
336
    [b] => two
337
    [c] => Two
338
    [d] => 2002-02-22
339
)
340
 
341
===================================================
342
testing getCol:
343
Array
344
(
345
    [0] => 42
346
    [1] => 42
347
    [2] => 2
348
)
349
testing getCol on query with no records:
350
Array
351
(
352
)
353
testing getCol with invalid column id:
354
DB Error: no such field
355
testing getCol with 1 col:
356
Array
357
(
358
    [0] => bing
359
    [1] => three
360
    [2] => two
361
)
362
testing getCol with b col:
363
Array
364
(
365
    [0] => bing
366
    [1] => three
367
    [2] => two
368
)
369
testing getCol with b col, scalar params:
370
Array
371
(
372
    [0] => bing
373
    [1] => three
374
    [2] => two
375
)
376
testing getCol with b col, array params:
377
Array
378
(
379
    [0] => bing
380
    [1] => three
381
    [2] => two
382
)
383
 
384
===================================================
385
testing getAssoc:
386
Array
387
(
388
    [42] => Array
389
        (
390
            [0] => three
391
            [1] => Three
392
        )
393
 
394
    [2] => Array
395
        (
396
            [0] => two
397
            [1] => Two
398
        )
399
 
400
)
401
testing getAssoc with false force, null params, DB_FETCHMODE_ORDERED:
402
Array
403
(
404
    [42] => Array
405
        (
406
            [0] => three
407
            [1] => Three
408
        )
409
 
410
    [2] => Array
411
        (
412
            [0] => two
413
            [1] => Two
414
        )
415
 
416
)
417
testing getAssoc with false force, scalar params, DB_FETCHMODE_ASSOC:
418
Array
419
(
420
    [42] => Array
421
        (
422
            [b] => three
423
            [c] => Three
424
        )
425
 
426
    [2] => Array
427
        (
428
            [b] => two
429
            [c] => Two
430
        )
431
 
432
)
433
testing getAssoc with two cols, false force, scalar params, DB_FETCHMODE_ASSOC:
434
Array
435
(
436
    [42] => three
437
    [2] => two
438
)
439
testing getAssoc with two cols, true force, scalar params, DB_FETCHMODE_ASSOC:
440
Array
441
(
442
    [42] => Array
443
        (
444
            [b] => three
445
        )
446
 
447
    [2] => Array
448
        (
449
            [b] => two
450
        )
451
 
452
)
453
testing getAssoc with false force, scalar params, DB_FETCHMODE_ASSOC, true group:
454
Array
455
(
456
    [42] => Array
457
        (
458
            [0] => Array
459
                (
460
                    [b] => bing
461
                    [c] => This is a test
462
                )
463
 
464
            [1] => Array
465
                (
466
                    [b] => three
467
                    [c] => Three
468
                )
469
 
470
        )
471
 
472
    [2] => Array
473
        (
474
            [0] => Array
475
                (
476
                    [b] => two
477
                    [c] => Two
478
                )
479
 
480
        )
481
 
482
)
483
testing getAssoc with false force, array params, DB_FETCHMODE_OBJECT:
484
Array
485
(
486
    [42] => stdClass Object
487
        (
488
            [a] => 42
489
            [b] => three
490
            [c] => Three
491
        )
492
 
493
    [2] => stdClass Object
494
        (
495
            [a] => 2
496
            [b] => two
497
            [c] => Two
498
        )
499
 
500
)
501
testing getAssoc with true force, array params, DB_FETCHMODE_OBJECT, true group:
502
Array
503
(
504
    [42] => Array
505
        (
506
            [0] => stdClass Object
507
                (
508
                    [a] => 42
509
                    [b] => bing
510
                    [c] => This is a test
511
                )
512
 
513
            [1] => stdClass Object
514
                (
515
                    [a] => 42
516
                    [b] => three
517
                    [c] => Three
518
                )
519
 
520
        )
521
 
522
    [2] => Array
523
        (
524
            [0] => stdClass Object
525
                (
526
                    [a] => 2
527
                    [b] => two
528
                    [c] => Two
529
                )
530
 
531
        )
532
 
533
)
534
 
535
===================================================
536
testing getAll:
537
Array
538
(
539
    [0] => Array
540
        (
541
            [0] => 2
542
            [1] => two
543
            [2] => Two
544
            [3] => 2002-02-22
545
        )
546
 
547
    [1] => Array
548
        (
549
            [0] => 42
550
            [1] => three
551
            [2] => Three
552
            [3] => 2003-03-23
553
        )
554
 
555
)
556
testing getAll with null params, DB_FETCHMODE_ORDERED:
557
Array
558
(
559
    [0] => Array
560
        (
561
            [0] => 2
562
            [1] => two
563
            [2] => Two
564
            [3] => 2002-02-22
565
        )
566
 
567
    [1] => Array
568
        (
569
            [0] => 42
570
            [1] => three
571
            [2] => Three
572
            [3] => 2003-03-23
573
        )
574
 
575
)
576
testing getAll with REVERSED args: DB_FETCHMODE_ASSOC, array params:
577
Array
578
(
579
    [0] => Array
580
        (
581
            [a] => 42
582
            [b] => three
583
            [c] => Three
584
            [d] => 2003-03-23
585
        )
586
 
587
    [1] => Array
588
        (
589
            [a] => 2
590
            [b] => two
591
            [c] => Two
592
            [d] => 2002-02-22
593
        )
594
 
595
)
596
testing getAll with REVERSED args: DB_FETCHMODE_ASSOC:
597
Array
598
(
599
    [0] => Array
600
        (
601
            [a] => 2
602
            [b] => two
603
            [c] => Two
604
            [d] => 2002-02-22
605
        )
606
 
607
    [1] => Array
608
        (
609
            [a] => 42
610
            [b] => three
611
            [c] => Three
612
            [d] => 2003-03-23
613
        )
614
 
615
)
616
testing getAll with array params, DB_FETCHMODE_ASSOC:
617
Array
618
(
619
    [0] => Array
620
        (
621
            [a] => 42
622
            [b] => three
623
            [c] => Three
624
            [d] => 2003-03-23
625
        )
626
 
627
    [1] => Array
628
        (
629
            [a] => 2
630
            [b] => two
631
            [c] => Two
632
            [d] => 2002-02-22
633
        )
634
 
635
)
636
testing getAll with array params, DB_FETCHMODE_OBJECT:
637
Array
638
(
639
    [0] => stdClass Object
640
        (
641
            [a] => 42
642
            [b] => three
643
            [c] => Three
644
            [d] => 2003-03-23
645
        )
646
 
647
    [1] => stdClass Object
648
        (
649
            [a] => 2
650
            [b] => two
651
            [c] => Two
652
            [d] => 2002-02-22
653
        )
654
 
655
)
656
 
657
===================================================
658
testing getOne with null value in column: matches expected result
659
testing getOne with empty string in column: empty string
660
 
661
===================================================