1 |
<? |
2 |
|
3 |
/////////////////////////////////////////////////////////////////////////// |
4 |
// |
5 |
// WddxRecordset |
6 |
// |
7 |
/////////////////////////////////////////////////////////////////////////// |
8 |
|
9 |
class WddxRecordset { |
10 |
|
11 |
var $matrix = array(); |
12 |
var $preserveFieldCase = '1'; |
13 |
|
14 |
|
15 |
/* |
16 |
/////////////////////////////////////////////////////////////////////////// |
17 |
// isColumn(name) returns true/false based on whether this is a column name |
18 |
function wddxRecordset_isColumn(name) |
19 |
{ |
20 |
// Columns must be objects |
21 |
// WddxRecordset extensions might use properties prefixed with |
22 |
// _private_ and these will not be treated as columns |
23 |
return (typeof(this[name]) == "object" && |
24 |
name.indexOf("_private_") == -1); |
25 |
} |
26 |
*/ |
27 |
|
28 |
|
29 |
/////////////////////////////////////////////////////////////////////////// |
30 |
// getRowCount() returns the number of rows in the recordset |
31 |
|
32 |
function getRowCount() |
33 |
{ |
34 |
|
35 |
$nRowCount = 0; |
36 |
reset ($this->matrix); |
37 |
while (list($key, $val) = each ($this->matrix)) { |
38 |
|
39 |
$nRowCount = count($this->matrix[$key]); |
40 |
return $nRowCount; |
41 |
|
42 |
} |
43 |
|
44 |
} |
45 |
|
46 |
|
47 |
/////////////////////////////////////////////////////////////////////////// |
48 |
// addColumn(name) adds a column with that name and length == getRowCount() |
49 |
function addColumn($name) |
50 |
{ |
51 |
|
52 |
$this->matrix["$name"]["0"] = ''; |
53 |
$nLen = $this->getRowCount(); |
54 |
|
55 |
for ($i = 0; $i < $nLen; $i++) |
56 |
{ |
57 |
$this->matrix["$name"]["$i"] = ''; |
58 |
} |
59 |
// $this[$this->preserveFieldCase ? $name : lc($name)] = $colValue; |
60 |
// $this->matrix[$name][0] = $colValue; |
61 |
} |
62 |
|
63 |
|
64 |
/////////////////////////////////////////////////////////////////////////// |
65 |
// addRows() adds n rows to all columns of the recordset |
66 |
function addRows($n) |
67 |
{ |
68 |
|
69 |
reset ($this->matrix); |
70 |
while (list($key, $val) = each ($this->matrix)) { |
71 |
|
72 |
$nRowCount = count($this->matrix[$key]); |
73 |
for ($i = $nRowCount; $i < $nRowCount + $n; ++$i) |
74 |
{ |
75 |
$this->matrix["$key"]["$i"] = ''; |
76 |
} |
77 |
|
78 |
} |
79 |
|
80 |
/* |
81 |
for (var col in this) |
82 |
{ |
83 |
if (this.isColumn(col)) |
84 |
{ |
85 |
var nLen = this[col].length; |
86 |
for (var i = nLen; i < nLen + n; ++i) |
87 |
{ |
88 |
this[col][i] = ''; |
89 |
} |
90 |
} |
91 |
} |
92 |
*/ |
93 |
} |
94 |
|
95 |
|
96 |
/////////////////////////////////////////////////////////////////////////// |
97 |
// getField() returns the element in a given (row, col) position |
98 |
function getField($row, $col) |
99 |
{ |
100 |
// return this[this.preserveFieldCase ? col : col.toLowerCase()][row]; |
101 |
return $this->matrix[$col][$row]; |
102 |
} |
103 |
|
104 |
|
105 |
/////////////////////////////////////////////////////////////////////////// |
106 |
// setField() sets the element in a given (row, col) position to value |
107 |
function setField($row, $col, $value) |
108 |
{ |
109 |
// this[this.preserveFieldCase ? col : col.toLowerCase()][row] = value; |
110 |
$this->matrix[$col][$row] = $value; |
111 |
} |
112 |
|
113 |
|
114 |
|
115 |
/////////////////////////////////////////////////////////////////////////// |
116 |
// wddxSerialize() serializes a recordset |
117 |
// returns true/false |
118 |
function wddxSerialize(&$serializer) |
119 |
{ |
120 |
// Create an array and a list of column names |
121 |
$colNamesList = ""; |
122 |
$colNames = array(); |
123 |
$i = 0; |
124 |
|
125 |
reset ($this->matrix); |
126 |
$priv_bool_isFirst = 1; |
127 |
while (list($key, $val) = each ($this->matrix)) { |
128 |
|
129 |
$colNames[$i] = $key; |
130 |
$i++; |
131 |
|
132 |
if (!$priv_bool_isFirst) |
133 |
{ |
134 |
$colNamesList .= ","; |
135 |
} |
136 |
$colNamesList .= $key; |
137 |
|
138 |
$priv_bool_isFirst = 0; |
139 |
} |
140 |
|
141 |
|
142 |
/* |
143 |
for (var col in this) |
144 |
{ |
145 |
if (this.isColumn(col)) |
146 |
{ |
147 |
colNames[i++] = col; |
148 |
|
149 |
if (colNamesList.length > 0) |
150 |
{ |
151 |
colNamesList += ","; |
152 |
} |
153 |
colNamesList += col; |
154 |
} |
155 |
} |
156 |
*/ |
157 |
|
158 |
$nRows = $this->getRowCount(); |
159 |
|
160 |
$serializer->write("<recordset rowCount='" . $nRows . "' fieldNames='" . $colNamesList . "'>"); |
161 |
|
162 |
$j = 0; |
163 |
$bSuccess = true; |
164 |
|
165 |
for ($j = 0; $bSuccess && $j < count($colNames); $j++) |
166 |
{ |
167 |
$name = $colNames[$j]; |
168 |
$serializer->write("<field name='" . $name . "'>"); |
169 |
|
170 |
for ($row = 0; $bSuccess && $row < $nRows; $row++) |
171 |
{ |
172 |
$bSuccess = $serializer->serializeValue($this->matrix[$name][$row]); |
173 |
} |
174 |
|
175 |
$serializer->write("</field>"); |
176 |
} |
177 |
|
178 |
$serializer->write("</recordset>"); |
179 |
|
180 |
return $bSuccess; |
181 |
} |
182 |
|
183 |
|
184 |
/* |
185 |
/////////////////////////////////////////////////////////////////////////// |
186 |
// dump(escapeStrings) returns an HTML table with the recordset data |
187 |
// It is a convenient routine for debugging and testing recordsets |
188 |
// The boolean parameter escapeStrings determines whether the <>& |
189 |
// characters in string values are escaped as <>& |
190 |
function wddxRecordset_dump(escapeStrings) |
191 |
{ |
192 |
// Get row count |
193 |
var nRows = this.getRowCount(); |
194 |
|
195 |
// Determine column names |
196 |
var colNames = new Array(); |
197 |
var i = 0; |
198 |
for (var col in this) |
199 |
{ |
200 |
if (typeof(this[col]) == "object") |
201 |
{ |
202 |
colNames[i++] = col; |
203 |
} |
204 |
} |
205 |
|
206 |
// Build table headers |
207 |
var o = "<table border=1><tr><td><b>RowNumber</b></td>"; |
208 |
for (i = 0; i < colNames.length; ++i) |
209 |
{ |
210 |
o += "<td><b>" + colNames[i] + "</b></td>"; |
211 |
} |
212 |
o += "</tr>"; |
213 |
|
214 |
// Build data cells |
215 |
for (var row = 0; row < nRows; ++row) |
216 |
{ |
217 |
o += "<tr><td>" + row + "</td>"; |
218 |
for (i = 0; i < colNames.length; ++i) |
219 |
{ |
220 |
var elem = this.getField(row, colNames[i]); |
221 |
if (escapeStrings && typeof(elem) == "string") |
222 |
{ |
223 |
var str = ""; |
224 |
for (var j = 0; j < elem.length; ++j) |
225 |
{ |
226 |
var ch = elem.charAt(j); |
227 |
if (ch == '<') |
228 |
{ |
229 |
str += "<"; |
230 |
} |
231 |
else if (ch == '>') |
232 |
{ |
233 |
str += ">"; |
234 |
} |
235 |
else if (ch == '&') |
236 |
{ |
237 |
str += "&"; |
238 |
} |
239 |
else |
240 |
{ |
241 |
str += ch; |
242 |
} |
243 |
} |
244 |
o += ("<td>" + str + "</td>"); |
245 |
} |
246 |
else |
247 |
{ |
248 |
o += ("<td>" + elem + "</td>"); |
249 |
} |
250 |
} |
251 |
o += "</tr>"; |
252 |
} |
253 |
|
254 |
// Close table |
255 |
o += "</table>"; |
256 |
|
257 |
// Return HTML recordset dump |
258 |
return o; |
259 |
} |
260 |
|
261 |
|
262 |
/////////////////////////////////////////////////////////////////////////// |
263 |
// WddxRecordset([flagPreserveFieldCase]) creates an empty recordset. |
264 |
// WddxRecordset(columns [, flagPreserveFieldCase]) creates a recordset |
265 |
// with a given set of columns provided as an array of strings. |
266 |
// WddxRecordset(columns, rows [, flagPreserveFieldCase]) creates a |
267 |
// recordset with these columns and some number of rows. |
268 |
// In all cases, flagPreserveFieldCase determines whether the exact case |
269 |
// of field names is preserved. If omitted, the default value is false |
270 |
// which means that all field names will be lowercased. |
271 |
function WddxRecordset() |
272 |
{ |
273 |
// Add default properties |
274 |
this.preserveFieldCase = false; |
275 |
|
276 |
// Add extensions |
277 |
if (typeof(wddxRecordsetExtensions) == "object") |
278 |
{ |
279 |
for (var prop in wddxRecordsetExtensions) |
280 |
{ |
281 |
// Hook-up method to WddxRecordset object |
282 |
this[prop] = wddxRecordsetExtensions[prop] |
283 |
} |
284 |
} |
285 |
|
286 |
// Add built-in methods |
287 |
this.getRowCount = wddxRecordset_getRowCount; |
288 |
this.addColumn = wddxRecordset_addColumn; |
289 |
this.addRows = wddxRecordset_addRows; |
290 |
this.isColumn = wddxRecordset_isColumn; |
291 |
this.getField = wddxRecordset_getField; |
292 |
this.setField = wddxRecordset_setField; |
293 |
this.wddxSerialize = wddxRecordset_wddxSerialize; |
294 |
this.dump = wddxRecordset_dump; |
295 |
|
296 |
// Perfom any needed initialization |
297 |
if (WddxRecordset.arguments.length > 0) |
298 |
{ |
299 |
if (typeof(val = WddxRecordset.arguments[0].valueOf()) == "boolean") |
300 |
{ |
301 |
// Case preservation flag is provided as 1st argument |
302 |
this.preserveFieldCase = WddxRecordset.arguments[0]; |
303 |
} |
304 |
else |
305 |
{ |
306 |
// First argument is the array of column names |
307 |
var cols = WddxRecordset.arguments[0]; |
308 |
|
309 |
// Second argument could be the length or the preserve case flag |
310 |
var nLen = 0; |
311 |
if (WddxRecordset.arguments.length > 1) |
312 |
{ |
313 |
if (typeof(val = WddxRecordset.arguments[1].valueOf()) == "boolean") |
314 |
{ |
315 |
// Case preservation flag is provided as 2nd argument |
316 |
this.preserveFieldCase = WddxRecordset.arguments[1]; |
317 |
} |
318 |
else |
319 |
{ |
320 |
// Explicitly specified recordset length |
321 |
nLen = WddxRecordset.arguments[1]; |
322 |
|
323 |
if (WddxRecordset.arguments.length > 2) |
324 |
{ |
325 |
// Case preservation flag is provided as 3rd argument |
326 |
this.preserveFieldCase = WddxRecordset.arguments[2]; |
327 |
} |
328 |
} |
329 |
} |
330 |
|
331 |
for (var i = 0; i < cols.length; ++i) |
332 |
{ |
333 |
var colValue = new Array(nLen); |
334 |
for (var j = 0; j < nLen; ++j) |
335 |
{ |
336 |
colValue[j] = null; |
337 |
} |
338 |
|
339 |
this[this.preserveFieldCase ? cols[i] : cols[i].toLowerCase()] = colValue; |
340 |
} |
341 |
} |
342 |
} |
343 |
} |
344 |
*/ |
345 |
|
346 |
} |
347 |
|
348 |
?> |