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::quote
3
--INI--
4
error_reporting = 2047
5
--SKIPIF--
6
<?php chdir(dirname(__FILE__)); require_once './skipif.inc'; ?>
7
--FILE--
8
<?php
9
require_once './connect.inc';
10
require_once './droptable.inc';
11
 
12
 
13
/**
14
 * Local error callback handler.
15
 *
16
 * Drops the phptest table, prints out an error message and kills the
17
 * process.
18
 *
19
 * @param object  $o  PEAR error object automatically passed to this method
20
 * @return void
21
 * @see PEAR::setErrorHandling()
22
 */
23
function pe($o) {
24
    global $dbh;
25
 
26
    $dbh->setErrorHandling(PEAR_ERROR_RETURN);
27
    drop_table($dbh, 'pearquote');
28
 
29
    die($o->toString());
30
}
31
 
32
// DBMS boolean column type simulation...
33
$boolean_col_type = array(
34
    'dbase'  => 'Logical',
35
    'fbsql'  => 'BOOLEAN',
36
    'ibase'  => 'SMALLINT',
37
    'ifx'    => 'SMALLINT',
38
    'msql'   => 'INTEGER',
39
    'mssql'  => 'BIT',
40
    'mysql'  => 'TINYINT(1)',
41
    'mysqli' => 'TINYINT(1)',
42
    'oci8'   => 'NUMBER(1)',
43
    'odbc'   => 'SMALLINT',
44
    'pgsql'  => 'BOOLEAN',
45
    'sqlite' => 'INTEGER',
46
    'sybase' => 'TINYINT',
47
);
48
 
49
// adjust things for specific DBMS's
50
 
51
if ($dbh->phptype == 'odbc') {
52
    if ($dbh->dbsyntax == 'odbc') {
53
        $type = $dbh->phptype;
54
    } else {
55
        $type = $dbh->dbsyntax;
56
    }
57
} else {
58
    $type = $dbh->phptype;
59
}
60
 
61
switch ($type) {
62
    case 'access':
63
        $decimal = 'SINGLE';
64
        $null = '';
65
        $chr  = 'VARCHAR(8)';
66
        $identifier = 'q\ut "dnt';
67
        break;
68
    case 'db2':
69
    case 'ibase':
70
        $decimal = 'DECIMAL(3,1)';
71
        $null = '';
72
        $chr  = 'VARCHAR(8)';
73
        $identifier = 'q\ut] "dn[t';
74
        break;
75
    case 'ifx':
76
        // doing this for ifx to keep certain versions happy
77
        $decimal = 'DECIMAL(3,1)';
78
        $null = '';
79
        $chr  = 'CHAR(8)';
80
        $identifier = '';
81
        break;
82
    case 'msql':
83
        $decimal = 'REAL';
84
        $null = '';
85
        $chr  = 'CHAR(8)';
86
        $identifier = '';
87
        break;
88
    case 'fbsql':
89
    case 'oci8':
90
        $decimal = 'DECIMAL(3,1)';
91
        $null = '';
92
        $chr  = 'VARCHAR(8)';
93
        $identifier = 'q\ut] dn[t';
94
        break;
95
    default:
96
        $decimal = 'DECIMAL(3,1)';
97
        $null = 'NULL';
98
        $chr  = 'VARCHAR(8)';
99
        $identifier = 'q\ut] "dn[t';
100
}
101
 
102
$dbh->setErrorHandling(PEAR_ERROR_RETURN);
103
drop_table($dbh, 'pearquote');
104
 
105
 
106
if ($identifier) {
107
    switch ($dbh->phptype) {
108
        case 'sybase':
109
            $res = $dbh->query('set quoted_identifier on');
110
            if (DB::isError($res) ) {
111
                pe($res);
112
            }
113
            break;
114
        default:
115
    }
116
    $create = $dbh->query("
117
        CREATE TABLE pearquote (
118
          n $decimal $null,
119
          s $chr $null,
120
          " . $dbh->quoteIdentifier($identifier) . " $decimal $null,
121
          b {$boolean_col_type[$dbh->phptype]} $null
122
        )
123
    ");
124
 
125
    if (DB::isError($create) ) {
126
        pe($create);
127
    }
128
 
129
    $info = $dbh->tableInfo('pearquote');
130
    if (DB::isError($info) ) {
131
        if ($info->code == DB_ERROR_NOT_CAPABLE) {
132
            print "Got outcome expected from delimited identifier.\n";
133
        } else {
134
            print "tableInfo() failed.\n";
135
        }
136
    } else {
137
        if ($identifier == $info[2]['name']) {
138
            print "Got outcome expected from delimited identifier.\n";
139
            // print "COLUMN NAME IS: {$info[2]['name']}\n";
140
        } else {
141
            print "Expected column name: '$identifier' ... ";
142
            print "Actual column name: '{$info[2]['name']}'\n";
143
        }
144
    }
145
 
146
} else {
147
    $dbh->query("
148
        CREATE TABLE pearquote (
149
          n $decimal $null,
150
          s $chr $null,
151
          plainidentifier $decimal $null,
152
          b {$boolean_col_type[$dbh->phptype]} $null
153
        )
154
    ");
155
    print "Got outcome expected from delimited identifier.\n";
156
}
157
 
158
 
159
$dbh->setErrorHandling(PEAR_ERROR_CALLBACK, 'pe');
160
 
161
 
162
$strings = array(
163
    "'",
164
    "\"",
165
    "\\",
166
    "%",
167
    "_",
168
    "''",
169
    "\"\"",
170
    "\\\\",
171
    "\\'\\'",
172
    "\\\"\\\""
173
);
174
 
175
$nums = array(
176
    12.3,
177
    15,
178
);
179
 
180
$bools = array(
181
    TRUE,
182
    FALSE,
183
);
184
 
185
 
186
echo "String escape test: ";
187
foreach ($strings as $s) {
188
    $quoted = $dbh->quoteSmart($s);
189
    $dbh->query("INSERT INTO pearquote (s) VALUES ($quoted)");
190
}
191
$diff = array_diff($strings, $res = $dbh->getCol("SELECT s FROM pearquote"));
192
if (count($diff) > 0) {
193
    echo "FAIL";
194
    print_r($strings);
195
    print_r($res);
196
} else {
197
    echo "OK";
198
}
199
 
200
$dbh->query("DELETE FROM pearquote");
201
 
202
 
203
echo "\nNumber escape test: ";
204
foreach ($nums as $n) {
205
    $quoted = $dbh->quoteSmart($n);
206
    $dbh->query("INSERT INTO pearquote (n) VALUES ($quoted)");
207
}
208
 
209
$diff = array();
210
$res =& $dbh->getCol('SELECT n FROM pearquote ORDER BY n');
211
foreach ($nums as $key => $val) {
212
    if ($val != $res[$key]) {
213
        $diff[] = "$val != {$res[$key]}";
214
    }
215
}
216
 
217
if (count($diff) > 0) {
218
    echo "FAIL\n";
219
    print_r($diff);
220
} else {
221
    echo 'OK';
222
}
223
 
224
$dbh->query('DELETE FROM pearquote');
225
 
226
 
227
echo "\nBoolean escape test: ";
228
$i = 1;
229
foreach ($bools as $b) {
230
    $quoted = $dbh->quoteSmart($b);
231
    $dbh->query("INSERT INTO pearquote (n, b) VALUES ($i, $quoted)");
232
    $i++;
233
}
234
 
235
$diff = array();
236
$res =& $dbh->getCol('SELECT b, n FROM pearquote ORDER BY n');
237
foreach ($bools as $key => $val) {
238
    if ($val === true) {
239
        if ($res[$key] == 1 || $res[$key] == true ||
240
            substr(strtolower($res[$key]), 0, 1) == 't')
241
        {
242
            // good
243
        } else {
244
            $diff[] = "in:true != out:{$res[$key]}";
245
        }
246
    } else {
247
        if ($res[$key] == 0 || $res[$key] == false ||
248
            substr(strtolower($res[$key]), 0, 1) == 'f')
249
        {
250
            // good
251
        } else {
252
            $diff[] = "in:false != out:{$res[$key]}";
253
        }
254
    }
255
}
256
 
257
if (count($diff) > 0) {
258
    echo "FAIL\n";
259
    print_r($diff);
260
} else {
261
    echo "OK\n";
262
}
263
 
264
 
265
$dbh->setErrorHandling(PEAR_ERROR_RETURN);
266
drop_table($dbh, 'pearquote');
267
 
268
?>
269
--EXPECT--
270
Got outcome expected from delimited identifier.
271
String escape test: OK
272
Number escape test: OK
273
Boolean escape test: OK