2 global $ADODB_INCLUDED_CSV;
3 $ADODB_INCLUDED_CSV = 1;
6 V4.22 15 Apr 2004 (c) 2000-2004 John Lim (jlim@natsoft.com.my). All rights reserved.
7 Released under both BSD license and Lesser GPL library license.
8 Whenever there is any discrepancy between the two licenses,
9 the BSD license will take precedence. See License.txt.
10 Set tabs to 4 for best viewing.
12 Latest version is available at http://php.weblogs.com/
14 Library for CSV serialization. This is used by the csv/proxy driver and is the
15 CacheExecute() serialization format.
18 Format documented at http://php.weblogs.com/ADODB_CSV
23 * convert a recordset into special format
25 * @param rs the recordset
27 * @return the CSV formated data
29 function _rs2serialize(&$rs,$conn=false,$sql='')
31 $max = ($rs) ? $rs->FieldCount() : 0;
33 if ($sql) $sql = urlencode($sql);
36 if ($max <= 0 || $rs->dataProvider == 'empty') { // is insert/update/delete
37 if (is_object($conn)) {
38 $sql .= ','.$conn->Affected_Rows();
39 $sql .= ','.$conn->Insert_ID();
43 $text = "====-1,0,$sql\n";
46 $tt = ($rs->timeCreated) ? $rs->timeCreated : time();
48 ## changed format from ====0 to ====1
49 $line = "====1,$tt,$sql\n";
51 if ($rs->databaseType == 'array') {
56 $rows[] = $rs->fields;
61 for($i=0; $i < $max; $i++) {
62 $o =& $rs->FetchField($i);
66 $rs =& new ADORecordSet_array();
67 $rs->InitArrayFields($rows,$flds);
68 return $line.serialize($rs);
73 * Open CSV file and convert it into Data.
75 * @param url file/ftp/http url
76 * @param err returns the error message
77 * @param timeout dispose if recordset has been alive for $timeout secs
79 * @return recordset, or false if error occured. If no
80 * error occurred in sql INSERT/UPDATE/DELETE,
81 * empty recordset is returned
83 function &csv2rs($url,&$err,$timeout=0)
86 $fp = @fopen($url,'r');
88 $err = $url.' file/URL not found';
95 if ($meta = fgetcsv($fp, 32000, ",")) {
96 // check if error message
97 if (strncmp($meta[0],'****',4) === 0) {
98 $err = trim(substr($meta[0],4,1024));
102 // check for meta data
103 // $meta[0] is -1 means return an empty recordset
104 // $meta[1] contains a time
106 if (strncmp($meta[0], '====',4) === 0) {
108 if ($meta[0] == "====-1") {
109 if (sizeof($meta) < 5) {
110 $err = "Corrupt first line for format -1";
117 $err = " Illegal Timeout $timeout ";
120 $rs->fields = array();
121 $rs->timeCreated = $meta[1];
122 $rs =& new ADORecordSet($val=true);
125 $rs->sql = urldecode($meta[2]);
126 $rs->affectedrows = (integer)$meta[3];
127 $rs->insertid = $meta[4];
130 # Under high volume loads, we want only 1 thread/process to _write_file
131 # so that we don't have 50 processes queueing to write the same data.
132 # We use probabilistic timeout, ahead of time.
134 # -4 sec before timeout, give processes 1/32 chance of timing out
135 # -2 sec before timeout, give processes 1/16 chance of timing out
136 # -1 sec after timeout give processes 1/4 chance of timing out
137 # +0 sec after timeout, give processes 100% chance of timing out
138 if (sizeof($meta) > 1) {
140 $tdiff = (integer)( $meta[1]+$timeout - time());
145 if ((rand() & 31) == 0) {
152 if ((rand() & 15) == 0) {
159 if ((rand() & 3) == 0) {
171 } // if check flush cache
175 //================================================
176 // new cache format - use serialize extensively...
177 if ($meta[0] === '====1') {
181 $text = fread($fp,$MAXSIZE);
182 if (strlen($text) === $MAXSIZE) {
183 while ($txt = fread($fp,$MAXSIZE)) {
188 @$rs = unserialize($text);
189 if (is_object($rs)) $rs->timeCreated = $ttl;
194 $meta = fgetcsv($fp, 32000, ",");
197 $err = "Unexpected EOF 1";
202 // Get Column definitions
204 foreach($meta as $o) {
205 $o2 = explode(':',$o);
206 if (sizeof($o2)!=3) {
211 $fld =& new ADOFieldObject();
212 $fld->name = urldecode($o2[0]);
214 $fld->max_length = $o2[2];
219 $err = "Recordset had unexpected EOF 2";
227 while ($txt = fread($fp,$MAXSIZE)) {
232 @$arr = unserialize($text);
234 if (!is_array($arr)) {
235 $err = "Recordset had unexpected EOF (in serialized recordset)";
236 if (get_magic_quotes_runtime()) $err .= ". Magic Quotes Runtime should be disabled!";
239 $rs =& new ADORecordSet_array();
240 $rs->timeCreated = $ttl;
241 $rs->InitArrayFields($arr,$flds);