source: trunk/phpgwapi/inc/adodb/contrib/toxmlrpc.inc.php @ 2

Revision 2, 5.7 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
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 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    */
13
14    /**
15    * Include the main libraries
16    */   
17    require_once('xmlrpc.inc');
18    require_once('adodb.inc.php');
19           
20    /**
21    * Builds an xmlrpc struct value out of an AdoDB recordset
22    */
23    function rs2xmlrpcval(&$adodbrs) {
24
25        $numfields = $adodbrs->FieldCount();
26        $numrecords = $adodbrs->RecordCount();
27
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");
46        }
47        $fieldcount = new xmlrpcval ($numfields, "int");
48        $recordcount = new xmlrpcval ($numrecords, "int");
49        $sql = new xmlrpcval ($adodbrs->sql);
50        $fieldinfo = new xmlrpcval ($fieldstruct, "array");
51
52        $header = new xmlrpcval ( array(
53                "fieldcount" => $fieldcount,
54                "recordcount" => $recordcount,
55                "sql" => $sql,
56                "fieldinfo" => $fieldinfo
57                ), "struct");
58
59        // build structure containing recordset data
60        $rows = array();
61        while (!$adodbrs->EOF) {
62            $columns = array();
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 ('');
68                    else
69                        $columns[$i] = xmlrpc_encode ($adodbrs->fields[$i]);
70            else
71                foreach ($adodbrs->fields as $val)
72                    if ($val === null)
73                        $columns[$i] = new xmlrpcval ('');
74                    else
75                        $columns[] = xmlrpc_encode ($val);
76
77            $rows[] = new xmlrpcval ($columns, "array");
78
79            $adodbrs->MoveNext();
80        }
81        $body = new xmlrpcval ($rows, "array");
82
83        // put it all together and build final xmlrpc struct
84        $xmlrpcrs = new xmlrpcval ( array(
85                "header" => $header,
86                "body" => $body,
87                ), "struct");
88
89        return $xmlrpcrs;
90
91    }
92
93    /**
94    * Returns an xmlrpc struct value as string out of an AdoDB recordset
95    */   
96    function rs2xmlrpcstring (&$adodbrs) {
97        $xmlrpc = rs2xmlrpcval ($adodbrs);
98        if ($xmlrpc)
99          return $xmlrpc->serialize();
100        else
101          return null;
102    }
103
104    /**
105    * Given a well-formed xmlrpc struct object returns an AdoDB object
106    *
107    * @todo add some error checking on the input value
108    */
109    function xmlrpcval2rs (&$xmlrpcval) {
110
111        $fields_array = array();
112        $data_array = array();
113 
114        // rebuild column information 
115        $header =& $xmlrpcval->structmem('header');
116       
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();
123       
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();
135            } // while
136            $fields_array[] = $fld;
137        } // for
138
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();
147            } // for j
148        } // for i
149
150        // finally build in-memory recordset object and return it
151        $rs = new ADORecordSet_array();
152        $rs->InitArrayFields($data_array,$fields_array);
153        return $rs;
154
155    }
156
157?>
Note: See TracBrowser for help on using the repository browser.