[2] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * Helper functions to convert between ADODB recordset objects and XMLRPC values. |
---|
| 4 | * Uses John Lim's AdoDB and Edd Dumbill's phpxmlrpc libs |
---|
| 5 | * |
---|
| 6 | * @author Daniele Baroncelli |
---|
| 7 | * @author Gaetano Giunta |
---|
[34] | 8 | * @copyright (c) 2003-2004 Giunta/Baroncelli. All rights reserved. |
---|
[2] | 9 | * |
---|
| 10 | * @todo some more error checking here and there |
---|
| 11 | * @todo document the xmlrpc-struct used to encode recordset info |
---|
[34] | 12 | * @todo verify if using xmlrpc_encode($rs->GetArray()) would work with: |
---|
| 13 | * - ADODB_FETCH_BOTH |
---|
| 14 | * - null values |
---|
[2] | 15 | */ |
---|
| 16 | |
---|
| 17 | /** |
---|
| 18 | * Include the main libraries |
---|
| 19 | */ |
---|
| 20 | require_once('xmlrpc.inc'); |
---|
[34] | 21 | if (!defined('ADODB_DIR')) require_once('adodb.inc.php'); |
---|
[2] | 22 | |
---|
| 23 | /** |
---|
| 24 | * Builds an xmlrpc struct value out of an AdoDB recordset |
---|
| 25 | */ |
---|
| 26 | function rs2xmlrpcval(&$adodbrs) { |
---|
| 27 | |
---|
[34] | 28 | $header =& rs2xmlrpcval_header($adodbrs); |
---|
| 29 | $body =& rs2xmlrpcval_body($adodbrs); |
---|
| 30 | |
---|
| 31 | // put it all together and build final xmlrpc struct |
---|
| 32 | $xmlrpcrs =& new xmlrpcval ( array( |
---|
| 33 | "header" => $header, |
---|
| 34 | "body" => $body, |
---|
| 35 | ), "struct"); |
---|
| 36 | |
---|
| 37 | return $xmlrpcrs; |
---|
| 38 | |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * Builds an xmlrpc struct value describing an AdoDB recordset |
---|
| 43 | */ |
---|
| 44 | function rs2xmlrpcval_header($adodbrs) |
---|
| 45 | { |
---|
[2] | 46 | $numfields = $adodbrs->FieldCount(); |
---|
| 47 | $numrecords = $adodbrs->RecordCount(); |
---|
| 48 | |
---|
| 49 | // build structure holding recordset information |
---|
| 50 | $fieldstruct = array(); |
---|
| 51 | for ($i = 0; $i < $numfields; $i++) { |
---|
| 52 | $fld = $adodbrs->FetchField($i); |
---|
| 53 | $fieldarray = array(); |
---|
| 54 | if (isset($fld->name)) |
---|
[34] | 55 | $fieldarray["name"] =& new xmlrpcval ($fld->name); |
---|
[2] | 56 | if (isset($fld->type)) |
---|
[34] | 57 | $fieldarray["type"] =& new xmlrpcval ($fld->type); |
---|
[2] | 58 | if (isset($fld->max_length)) |
---|
[34] | 59 | $fieldarray["max_length"] =& new xmlrpcval ($fld->max_length, "int"); |
---|
[2] | 60 | if (isset($fld->not_null)) |
---|
[34] | 61 | $fieldarray["not_null"] =& new xmlrpcval ($fld->not_null, "boolean"); |
---|
[2] | 62 | if (isset($fld->has_default)) |
---|
[34] | 63 | $fieldarray["has_default"] =& new xmlrpcval ($fld->has_default, "boolean"); |
---|
[2] | 64 | if (isset($fld->default_value)) |
---|
[34] | 65 | $fieldarray["default_value"] =& new xmlrpcval ($fld->default_value); |
---|
| 66 | $fieldstruct[$i] =& new xmlrpcval ($fieldarray, "struct"); |
---|
[2] | 67 | } |
---|
[34] | 68 | $fieldcount =& new xmlrpcval ($numfields, "int"); |
---|
| 69 | $recordcount =& new xmlrpcval ($numrecords, "int"); |
---|
| 70 | $sql =& new xmlrpcval ($adodbrs->sql); |
---|
| 71 | $fieldinfo =& new xmlrpcval ($fieldstruct, "array"); |
---|
[2] | 72 | |
---|
[34] | 73 | $header =& new xmlrpcval ( array( |
---|
[2] | 74 | "fieldcount" => $fieldcount, |
---|
| 75 | "recordcount" => $recordcount, |
---|
| 76 | "sql" => $sql, |
---|
| 77 | "fieldinfo" => $fieldinfo |
---|
| 78 | ), "struct"); |
---|
| 79 | |
---|
[34] | 80 | return $header; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | * Builds an xmlrpc struct value out of an AdoDB recordset |
---|
| 85 | * (data values only, no data definition) |
---|
| 86 | */ |
---|
| 87 | function rs2xmlrpcval_body($adodbrs) |
---|
| 88 | { |
---|
| 89 | $numfields = $adodbrs->FieldCount(); |
---|
| 90 | |
---|
[2] | 91 | // build structure containing recordset data |
---|
[34] | 92 | $adodbrs->MoveFirst(); |
---|
[2] | 93 | $rows = array(); |
---|
| 94 | while (!$adodbrs->EOF) { |
---|
| 95 | $columns = array(); |
---|
| 96 | // This should work on all cases of fetch mode: assoc, num, both or default |
---|
| 97 | if ($adodbrs->fetchMode == 'ADODB_FETCH_BOTH' || count($adodbrs->fields) == 2 * $adodbrs->FieldCount()) |
---|
| 98 | for ($i = 0; $i < $numfields; $i++) |
---|
[34] | 99 | if ($adodbrs->fields[$i] === null) |
---|
| 100 | $columns[$i] =& new xmlrpcval (''); |
---|
[2] | 101 | else |
---|
[34] | 102 | $columns[$i] =& xmlrpc_encode ($adodbrs->fields[$i]); |
---|
[2] | 103 | else |
---|
| 104 | foreach ($adodbrs->fields as $val) |
---|
| 105 | if ($val === null) |
---|
[34] | 106 | $columns[] =& new xmlrpcval (''); |
---|
[2] | 107 | else |
---|
[34] | 108 | $columns[] =& xmlrpc_encode ($val); |
---|
[2] | 109 | |
---|
[34] | 110 | $rows[] =& new xmlrpcval ($columns, "array"); |
---|
[2] | 111 | |
---|
| 112 | $adodbrs->MoveNext(); |
---|
| 113 | } |
---|
[34] | 114 | $body =& new xmlrpcval ($rows, "array"); |
---|
[2] | 115 | |
---|
[34] | 116 | return $body; |
---|
[2] | 117 | } |
---|
[34] | 118 | |
---|
[2] | 119 | /** |
---|
| 120 | * Returns an xmlrpc struct value as string out of an AdoDB recordset |
---|
| 121 | */ |
---|
| 122 | function rs2xmlrpcstring (&$adodbrs) { |
---|
| 123 | $xmlrpc = rs2xmlrpcval ($adodbrs); |
---|
| 124 | if ($xmlrpc) |
---|
| 125 | return $xmlrpc->serialize(); |
---|
| 126 | else |
---|
| 127 | return null; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /** |
---|
| 131 | * Given a well-formed xmlrpc struct object returns an AdoDB object |
---|
| 132 | * |
---|
| 133 | * @todo add some error checking on the input value |
---|
| 134 | */ |
---|
| 135 | function xmlrpcval2rs (&$xmlrpcval) { |
---|
| 136 | |
---|
| 137 | $fields_array = array(); |
---|
| 138 | $data_array = array(); |
---|
| 139 | |
---|
| 140 | // rebuild column information |
---|
| 141 | $header =& $xmlrpcval->structmem('header'); |
---|
| 142 | |
---|
| 143 | $numfields = $header->structmem('fieldcount'); |
---|
| 144 | $numfields = $numfields->scalarval(); |
---|
| 145 | $numrecords = $header->structmem('recordcount'); |
---|
| 146 | $numrecords = $numrecords->scalarval(); |
---|
| 147 | $sqlstring = $header->structmem('sql'); |
---|
| 148 | $sqlstring = $sqlstring->scalarval(); |
---|
| 149 | |
---|
| 150 | $fieldinfo =& $header->structmem('fieldinfo'); |
---|
| 151 | for ($i = 0; $i < $numfields; $i++) { |
---|
| 152 | $temp =& $fieldinfo->arraymem($i); |
---|
[34] | 153 | $fld =& new ADOFieldObject(); |
---|
[2] | 154 | while (list($key,$value) = $temp->structeach()) { |
---|
| 155 | if ($key == "name") $fld->name = $value->scalarval(); |
---|
| 156 | if ($key == "type") $fld->type = $value->scalarval(); |
---|
| 157 | if ($key == "max_length") $fld->max_length = $value->scalarval(); |
---|
| 158 | if ($key == "not_null") $fld->not_null = $value->scalarval(); |
---|
| 159 | if ($key == "has_default") $fld->has_default = $value->scalarval(); |
---|
| 160 | if ($key == "default_value") $fld->default_value = $value->scalarval(); |
---|
| 161 | } // while |
---|
| 162 | $fields_array[] = $fld; |
---|
| 163 | } // for |
---|
| 164 | |
---|
| 165 | // fetch recordset information into php array |
---|
| 166 | $body =& $xmlrpcval->structmem('body'); |
---|
| 167 | for ($i = 0; $i < $numrecords; $i++) { |
---|
| 168 | $data_array[$i]= array(); |
---|
| 169 | $xmlrpcrs_row =& $body->arraymem($i); |
---|
| 170 | for ($j = 0; $j < $numfields; $j++) { |
---|
| 171 | $temp =& $xmlrpcrs_row->arraymem($j); |
---|
| 172 | $data_array[$i][$j] = $temp->scalarval(); |
---|
| 173 | } // for j |
---|
| 174 | } // for i |
---|
| 175 | |
---|
| 176 | // finally build in-memory recordset object and return it |
---|
[34] | 177 | $rs =& new ADORecordSet_array(); |
---|
[2] | 178 | $rs->InitArrayFields($data_array,$fields_array); |
---|
| 179 | return $rs; |
---|
| 180 | |
---|
| 181 | } |
---|
| 182 | |
---|
[34] | 183 | ?> |
---|