source: companies/celepar/phpgwapi/inc/adodb/contrib/toxmlrpc.inc.php @ 763

Revision 763, 6.5 KB checked in by niltonneto, 15 years ago (diff)

Importação inicial do Expresso da Celepar

Line 
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
8    * @copyright (c) 2003-2004 Giunta/Baroncelli. All rights reserved.
9    *
10    * @todo some more error checking here and there
11    * @todo document the xmlrpc-struct used to encode recordset info
12    * @todo verify if using xmlrpc_encode($rs->GetArray()) would work with:
13    *       - ADODB_FETCH_BOTH
14    *       - null values
15    */
16
17    /**
18    * Include the main libraries
19    */   
20    require_once('xmlrpc.inc');
21    if (!defined('ADODB_DIR')) require_once('adodb.inc.php');
22           
23    /**
24    * Builds an xmlrpc struct value out of an AdoDB recordset
25    */
26    function rs2xmlrpcval(&$adodbrs) {
27
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    {
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))
55                $fieldarray["name"] =& new xmlrpcval ($fld->name);
56            if (isset($fld->type))
57                $fieldarray["type"] =& new xmlrpcval ($fld->type);
58            if (isset($fld->max_length))
59                $fieldarray["max_length"] =& new xmlrpcval ($fld->max_length, "int");
60            if (isset($fld->not_null))
61                $fieldarray["not_null"] =& new xmlrpcval ($fld->not_null, "boolean");
62            if (isset($fld->has_default))
63                $fieldarray["has_default"] =& new xmlrpcval ($fld->has_default, "boolean");
64            if (isset($fld->default_value))
65                $fieldarray["default_value"] =& new xmlrpcval ($fld->default_value);
66            $fieldstruct[$i] =& new xmlrpcval ($fieldarray, "struct");
67        }
68        $fieldcount =& new xmlrpcval ($numfields, "int");
69        $recordcount =& new xmlrpcval ($numrecords, "int");
70        $sql =& new xmlrpcval ($adodbrs->sql);
71        $fieldinfo =& new xmlrpcval ($fieldstruct, "array");
72
73        $header =& new xmlrpcval ( array(
74                "fieldcount" => $fieldcount,
75                "recordcount" => $recordcount,
76                "sql" => $sql,
77                "fieldinfo" => $fieldinfo
78                ), "struct");
79
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
91        // build structure containing recordset data
92        $adodbrs->MoveFirst();
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++)
99                    if ($adodbrs->fields[$i] === null)
100                        $columns[$i] =& new xmlrpcval ('');
101                    else
102                        $columns[$i] =& xmlrpc_encode ($adodbrs->fields[$i]);
103            else
104                foreach ($adodbrs->fields as $val)
105                    if ($val === null)
106                        $columns[] =& new xmlrpcval ('');
107                    else
108                        $columns[] =& xmlrpc_encode ($val);
109
110            $rows[] =& new xmlrpcval ($columns, "array");
111
112            $adodbrs->MoveNext();
113        }
114        $body =& new xmlrpcval ($rows, "array");
115
116        return $body;   
117    }
118   
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);
153            $fld =& new ADOFieldObject();
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
177        $rs =& new ADORecordSet_array();
178        $rs->InitArrayFields($data_array,$fields_array);
179        return $rs;
180
181    }
182
183?>
Note: See TracBrowser for help on using the repository browser.