-
Notifications
You must be signed in to change notification settings - Fork 375
/
Copy pathTC44_FetchArray.phpt
184 lines (164 loc) · 5.14 KB
/
TC44_FetchArray.phpt
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
--TEST--
Fetch Array Test
--DESCRIPTION--
Verifies data retrieval via "sqlsrv_fetch_array",
by checking all fetch type modes.
--ENV--
PHPT_EXEC=true
--SKIPIF--
<?
require_once('MsCommon.inc');
// locale must be set before 1st connection
setUSAnsiLocale();
require('skipif_versions_old.inc');
?>
--FILE--
<?php
require_once('MsCommon.inc');
function fetchRow($minFetchMode, $maxFetchMode)
{
if (!isMarsSupported()) {
return;
}
setup();
$tableName = 'TC44test';
if (useUTF8Data()) {
$conn1 = AE\connect(array( 'CharacterSet'=>'UTF-8' ));
} else {
$conn1 = AE\connect();
}
$tableName = 'TC44Test';
AE\createTestTable($conn1, $tableName);
$noRows = 10;
$numFields = 0;
AE\insertTestRows($conn1, $tableName, $noRows);
for ($k = $minFetchMode; $k <= $maxFetchMode; $k++) {
$stmt1 = AE\selectFromTable($conn1, $tableName);
$stmt2 = AE\selectFromTable($conn1, $tableName);
if ($numFields == 0) {
$numFields = sqlsrv_num_fields($stmt1);
} else {
$count = sqlsrv_num_fields($stmt1);
if ($count != $numFields) {
die("Unexpected number of fields: $count");
}
}
switch ($k) {
case 1: // fetch array - numeric mode
fetchArray($stmt1, $stmt2, SQLSRV_FETCH_NUMERIC, $noRows, $numFields);
break;
case 2: // fetch array - associative mode
fetchArray($stmt1, $stmt2, SQLSRV_FETCH_ASSOC, $noRows, $numFields);
break;
case 3: // fetch array - both numeric & associative
fetchArray($stmt1, $stmt2, SQLSRV_FETCH_BOTH, $noRows, $numFields);
break;
default: // default
fetchArray($stmt1, $stmt2, null, $noRows, $numFields);
break;
}
sqlsrv_free_stmt($stmt1);
sqlsrv_free_stmt($stmt2);
}
dropTable($conn1, $tableName);
sqlsrv_close($conn1);
}
function fetchArray($stmt, $stmtRef, $mode, $rows, $fields)
{
$size = $fields;
$fetchMode = $mode;
if ($fetchMode == SQLSRV_FETCH_NUMERIC) {
trace("\tRetrieving $rows arrays of size $size (Fetch Mode = NUMERIC) ...\n");
} elseif ($fetchMode == SQLSRV_FETCH_ASSOC) {
trace("\tRetrieving $rows arrays of size $size (Fetch Mode = ASSOCIATIVE) ...\n");
} elseif ($fetchMode == SQLSRV_FETCH_BOTH) {
$size = $fields * 2;
trace("\tRetrieving $rows arrays of size $size (Fetch Mode = BOTH) ...\n");
} else {
$fetchMode = null;
$size = $fields * 2;
trace("\tRetrieving $rows arrays of size $size (Fetch Mode = DEFAULT) ...\n");
}
for ($i = 0; $i < $rows; $i++) {
if ($fetchMode == null) {
$row = sqlsrv_fetch_array($stmt);
} else {
$row = sqlsrv_fetch_array($stmt, $fetchMode);
}
if ($row === false) {
fatalError("Row $i is missing");
}
$rowSize = count($row);
if ($rowSize != $size) {
die("Row array has an incorrect size: ".$rowSize);
}
$rowRref = sqlsrv_fetch($stmtRef);
for ($j = 0; $j < $fields; $j++) {
if (!checkData($row, $stmtRef, $j, $fetchMode)) {
die("Data corruption on row ".($i + 1)." column ".($j + 1));
}
}
}
}
function checkData($row, $stmt, $index, $mode)
{
$success = true;
$col = $index + 1;
$actual = (($mode == SQLSRV_FETCH_ASSOC) ? $row[getColName($col)] : $row[$index]);
$expected = null;
if (!isUpdatable($col)) {
// do not check the timestamp
} elseif (isNumeric($col) || isDateTime($col)) {
$expected = sqlsrv_get_field($stmt, $index);
if ($expected != $actual) {
$success = false;
}
} elseif (isBinary($col)) {
$expected = sqlsrv_get_field($stmt, $index, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
$actual = bin2hex($actual);
if (strcasecmp($actual, $expected) != 0) {
$success = false;
}
} else { // if (isChar($col))
if (useUTF8Data()) {
$expected = sqlsrv_get_field($stmt, $index, SQLSRV_PHPTYPE_STRING('UTF-8'));
} else {
$expected = sqlsrv_get_field($stmt, $index, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
}
if (strcmp($actual, $expected) != 0) {
$success = false;
}
}
if (!$success) {
trace("\nData error\nExpected:\n$expected\nActual:\n$actual\n");
}
return ($success);
}
// locale must be set before 1st connection
setUSAnsiLocale();
$testName = "Fetch - Array";
// test ansi only if windows or non-UTF8 locales are supported (ODBC 17 and above)
startTest($testName);
if (isLocaleSupported()) {
try {
setUTF8Data(false);
fetchRow(1, 4);
} catch (Exception $e) {
echo $e->getMessage();
}
}
endTest($testName);
// test utf8
startTest($testName);
try {
setUTF8Data(true);
resetLocaleToDefault();
fetchRow(1, 4);
} catch (Exception $e) {
echo $e->getMessage();
}
endTest($testName);
?>
--EXPECT--
Test "Fetch - Array" completed successfully.
Test "Fetch - Array" completed successfully.