]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/adodb/drivers/adodb-csv.inc.php
Reformat code
[SourceForge/phpwiki.git] / lib / WikiDB / adodb / drivers / adodb-csv.inc.php
1 <?php
2 /*
3 V4.22 15 Apr 2004  (c) 2000-2004 John Lim (jlim@natsoft.com.my). All rights reserved.
4   Released under both BSD license and Lesser GPL library license.
5   Whenever there is any discrepancy between the two licenses,
6   the BSD license will take precedence.
7   Set tabs to 4.
8
9   Currently unsupported: MetaDatabases, MetaTables and MetaColumns, and also inputarr in Execute.
10   Native types have been converted to MetaTypes.
11   Transactions not supported yet.
12 */
13
14 if (!defined("_ADODB_CSV_LAYER")) {
15     define("_ADODB_CSV_LAYER", 1);
16
17     include_once(ADODB_DIR . '/adodb-csvlib.inc.php');
18
19     class ADODB_csv extends ADOConnection
20     {
21         var $databaseType = 'csv';
22         var $databaseProvider = 'csv';
23         var $hasInsertID = true;
24         var $hasAffectedRows = true;
25         var $fmtTimeStamp = "'Y-m-d H:i:s'";
26         var $_affectedrows = 0;
27         var $_insertid = 0;
28         var $_url;
29         var $replaceQuote = "''"; // string to use to replace quotes
30         var $hasTransactions = false;
31         var $_errorNo = false;
32
33         function ADODB_csv()
34         {
35         }
36
37         function _insertid()
38         {
39             return $this->_insertid;
40         }
41
42         function _affectedrows()
43         {
44             return $this->_affectedrows;
45         }
46
47         function &MetaDatabases()
48         {
49             return false;
50         }
51
52         // returns true or false
53         function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
54         {
55             if (strtolower(substr($argHostname, 0, 7)) !== 'http://') return false;
56             $this->_url = $argHostname;
57             return true;
58         }
59
60         // returns true or false
61         function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
62         {
63             if (strtolower(substr($argHostname, 0, 7)) !== 'http://') return false;
64             $this->_url = $argHostname;
65             return true;
66         }
67
68         function &MetaColumns($table)
69         {
70             return false;
71         }
72
73         // parameters use PostgreSQL convention, not MySQL
74         function &SelectLimit($sql, $nrows = -1, $offset = -1)
75         {
76             global $ADODB_FETCH_MODE;
77
78             $url = $this->_url . '?sql=' . urlencode($sql) . "&nrows=$nrows&fetch=" .
79                 (($this->fetchMode !== false) ? $this->fetchMode : $ADODB_FETCH_MODE) .
80                 "&offset=$offset";
81             $err = false;
82             $rs = csv2rs($url, $err, false);
83
84             if ($this->debug) print "$url<br><i>$err</i><br>";
85
86             $at = strpos($err, '::::');
87             if ($at === false) {
88                 $this->_errorMsg = $err;
89                 $this->_errorNo = (integer)$err;
90             } else {
91                 $this->_errorMsg = substr($err, $at + 4, 1024);
92                 $this->_errorNo = -9999;
93             }
94             if ($this->_errorNo)
95                 if ($fn = $this->raiseErrorFn) {
96                     $fn($this->databaseType, 'EXECUTE', $this->ErrorNo(), $this->ErrorMsg(), $sql, '');
97                 }
98
99             if (is_object($rs)) {
100
101                 $rs->databaseType = 'csv';
102                 $rs->fetchMode = ($this->fetchMode !== false) ? $this->fetchMode : $ADODB_FETCH_MODE;
103                 $rs->connection = &$this;
104             }
105             return $rs;
106         }
107
108         // returns queryID or false
109         function &_Execute($sql, $inputarr = false)
110         {
111             global $ADODB_FETCH_MODE;
112
113             if (!$this->_bindInputArray && $inputarr) {
114                 $sqlarr = explode('?', $sql);
115                 $sql = '';
116                 $i = 0;
117                 foreach ($inputarr as $v) {
118
119                     $sql .= $sqlarr[$i];
120                     if (gettype($v) == 'string')
121                         $sql .= $this->qstr($v);
122                     else if ($v === null)
123                         $sql .= 'NULL';
124                     else
125                         $sql .= $v;
126                     $i += 1;
127
128                 }
129                 $sql .= $sqlarr[$i];
130                 if ($i + 1 != sizeof($sqlarr))
131                     print "Input Array does not match ?: " . htmlspecialchars($sql);
132                 $inputarr = false;
133             }
134
135             $url = $this->_url . '?sql=' . urlencode($sql) . "&fetch=" .
136                 (($this->fetchMode !== false) ? $this->fetchMode : $ADODB_FETCH_MODE);
137             $err = false;
138
139             $rs = csv2rs($url, $err, false);
140             if ($this->debug) print urldecode($url) . "<br><i>$err</i><br>";
141             $at = strpos($err, '::::');
142             if ($at === false) {
143                 $this->_errorMsg = $err;
144                 $this->_errorNo = (integer)$err;
145             } else {
146                 $this->_errorMsg = substr($err, $at + 4, 1024);
147                 $this->_errorNo = -9999;
148             }
149
150             if ($this->_errorNo)
151                 if ($fn = $this->raiseErrorFn) {
152                     $fn($this->databaseType, 'EXECUTE', $this->ErrorNo(), $this->ErrorMsg(), $sql, $inputarr);
153                 }
154             if (is_object($rs)) {
155                 $rs->fetchMode = ($this->fetchMode !== false) ? $this->fetchMode : $ADODB_FETCH_MODE;
156
157                 $this->_affectedrows = $rs->affectedrows;
158                 $this->_insertid = $rs->insertid;
159                 $rs->databaseType = 'csv';
160                 $rs->connection = &$this;
161             }
162             return $rs;
163         }
164
165         /*      Returns: the last error message from previous database operation        */
166         function ErrorMsg()
167         {
168             return $this->_errorMsg;
169         }
170
171         /*      Returns: the last error number from previous database operation */
172         function ErrorNo()
173         {
174             return $this->_errorNo;
175         }
176
177         // returns true or false
178         function _close()
179         {
180             return true;
181         }
182     } // class
183
184     class ADORecordset_csv extends ADORecordset
185     {
186         function ADORecordset_csv($id, $mode = false)
187         {
188             $this->ADORecordset($id, $mode);
189         }
190
191         function _close()
192         {
193             return true;
194         }
195     }
196
197 } // define