3 * Helper functions to convert between ADODB recordset objects and XMLRPC values.
4 * Uses John Lim's AdoDB and Edd Dumbill's phpxmlrpc libs
6 * @author Daniele Baroncelli
7 * @author Gaetano Giunta
8 * @copyright (c) 2003 Giunta/Baroncelli. All rights reserved.
10 * @todo some more error checking here and there
11 * @todo document the xmlrpc-struct used to encode recordset info
15 * Include the main libraries
17 require_once('xmlrpc.inc');
18 require_once('adodb.inc.php');
21 * Builds an xmlrpc struct value out of an AdoDB recordset
23 function rs2xmlrpcval(&$adodbrs) {
25 $numfields = $adodbrs->FieldCount();
26 $numrecords = $adodbrs->RecordCount();
28 // build structure holding recordset information
29 $fieldstruct = array();
30 for ($i = 0; $i < $numfields; $i++
) {
31 $fld = $adodbrs->FetchField($i);
32 $fieldarray = array();
33 if (isset($fld->name
))
34 $fieldarray["name"] = new xmlrpcval ($fld->name
);
35 if (isset($fld->type
))
36 $fieldarray["type"] = new xmlrpcval ($fld->type
);
37 if (isset($fld->max_length
))
38 $fieldarray["max_length"] = new xmlrpcval ($fld->max_length
, "int");
39 if (isset($fld->not_null
))
40 $fieldarray["not_null"] = new xmlrpcval ($fld->not_null
, "boolean");
41 if (isset($fld->has_default
))
42 $fieldarray["has_default"] = new xmlrpcval ($fld->has_default
, "boolean");
43 if (isset($fld->default_value
))
44 $fieldarray["default_value"] = new xmlrpcval ($fld->default_value
);
45 $fieldstruct[$i] = new xmlrpcval ($fieldarray, "struct");
47 $fieldcount = new xmlrpcval ($numfields, "int");
48 $recordcount = new xmlrpcval ($numrecords, "int");
49 $sql = new xmlrpcval ($adodbrs->sql
);
50 $fieldinfo = new xmlrpcval ($fieldstruct, "array");
52 $header = new xmlrpcval ( array(
53 "fieldcount" => $fieldcount,
54 "recordcount" => $recordcount,
56 "fieldinfo" => $fieldinfo
59 // build structure containing recordset data
61 while (!$adodbrs->EOF
) {
63 // This should work on all cases of fetch mode: assoc, num, both or default
64 if ($adodbrs->fetchMode
== 'ADODB_FETCH_BOTH' ||
count($adodbrs->fields
) == 2 * $adodbrs->FieldCount())
65 for ($i = 0; $i < $numfields; $i++
)
66 if ($columns[$i] === null)
67 $columns[$i] = new xmlrpcval ('');
69 $columns[$i] = xmlrpc_encode ($adodbrs->fields
[$i]);
71 foreach ($adodbrs->fields
as $val)
73 $columns[$i] = new xmlrpcval ('');
75 $columns[] = xmlrpc_encode ($val);
77 $rows[] = new xmlrpcval ($columns, "array");
81 $body = new xmlrpcval ($rows, "array");
83 // put it all together and build final xmlrpc struct
84 $xmlrpcrs = new xmlrpcval ( array(
94 * Returns an xmlrpc struct value as string out of an AdoDB recordset
96 function rs2xmlrpcstring (&$adodbrs) {
97 $xmlrpc = rs2xmlrpcval ($adodbrs);
99 return $xmlrpc->serialize();
105 * Given a well-formed xmlrpc struct object returns an AdoDB object
107 * @todo add some error checking on the input value
109 function xmlrpcval2rs (&$xmlrpcval) {
111 $fields_array = array();
112 $data_array = array();
114 // rebuild column information
115 $header =& $xmlrpcval->structmem('header');
117 $numfields = $header->structmem('fieldcount');
118 $numfields = $numfields->scalarval();
119 $numrecords = $header->structmem('recordcount');
120 $numrecords = $numrecords->scalarval();
121 $sqlstring = $header->structmem('sql');
122 $sqlstring = $sqlstring->scalarval();
124 $fieldinfo =& $header->structmem('fieldinfo');
125 for ($i = 0; $i < $numfields; $i++
) {
126 $temp =& $fieldinfo->arraymem($i);
127 $fld = new ADOFieldObject();
128 while (list($key,$value) = $temp->structeach()) {
129 if ($key == "name") $fld->name
= $value->scalarval();
130 if ($key == "type") $fld->type
= $value->scalarval();
131 if ($key == "max_length") $fld->max_length
= $value->scalarval();
132 if ($key == "not_null") $fld->not_null
= $value->scalarval();
133 if ($key == "has_default") $fld->has_default
= $value->scalarval();
134 if ($key == "default_value") $fld->default_value
= $value->scalarval();
136 $fields_array[] = $fld;
139 // fetch recordset information into php array
140 $body =& $xmlrpcval->structmem('body');
141 for ($i = 0; $i < $numrecords; $i++
) {
142 $data_array[$i]= array();
143 $xmlrpcrs_row =& $body->arraymem($i);
144 for ($j = 0; $j < $numfields; $j++
) {
145 $temp =& $xmlrpcrs_row->arraymem($j);
146 $data_array[$i][$j] = $temp->scalarval();
150 // finally build in-memory recordset object and return it
151 $rs = new ADORecordSet_array();
152 $rs->InitArrayFields($data_array,$fields_array);