1 | <?php |
---|
2 | /* |
---|
3 | V4.51 29 July 2004 (c) 2000-2004 John Lim. 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 for best viewing. |
---|
8 | |
---|
9 | Latest version is available at http://adodb.sourceforge.net |
---|
10 | |
---|
11 | Sybase driver contributed by Toni (toni.tunkkari@finebyte.com) |
---|
12 | |
---|
13 | - MSSQL date patch applied. |
---|
14 | |
---|
15 | Date patch by Toni 15 Feb 2002 |
---|
16 | */ |
---|
17 | |
---|
18 | // security - hide paths |
---|
19 | if (!defined('ADODB_DIR')) die(); |
---|
20 | |
---|
21 | class ADODB_sybase extends ADOConnection { |
---|
22 | var $databaseType = "sybase"; |
---|
23 | //var $dataProvider = 'sybase'; |
---|
24 | var $replaceQuote = "''"; // string to use to replace quotes |
---|
25 | var $fmtDate = "'Y-m-d'"; |
---|
26 | var $fmtTimeStamp = "'Y-m-d H:i:s'"; |
---|
27 | var $hasInsertID = true; |
---|
28 | var $hasAffectedRows = true; |
---|
29 | var $metaTablesSQL="select name from sysobjects where type='U' or type='V'"; |
---|
30 | // see http://sybooks.sybase.com/onlinebooks/group-aw/awg0800e/dbrfen8/@ebt-link;pt=5981;uf=0?target=0;window=new;showtoc=true;book=dbrfen8 |
---|
31 | var $metaColumnsSQL = "SELECT c.column_name, c.column_type, c.width FROM syscolumn c, systable t WHERE t.table_name='%s' AND c.table_id=t.table_id AND t.table_type='BASE'"; |
---|
32 | /* |
---|
33 | "select c.name,t.name,c.length from |
---|
34 | syscolumns c join systypes t on t.xusertype=c.xusertype join sysobjects o on o.id=c.id |
---|
35 | where o.name='%s'"; |
---|
36 | */ |
---|
37 | var $concat_operator = '+'; |
---|
38 | var $arrayClass = 'ADORecordSet_array_sybase'; |
---|
39 | var $sysDate = 'GetDate()'; |
---|
40 | var $leftOuter = '*='; |
---|
41 | var $rightOuter = '=*'; |
---|
42 | |
---|
43 | function ADODB_sybase() |
---|
44 | { |
---|
45 | } |
---|
46 | |
---|
47 | // might require begintrans -- committrans |
---|
48 | function _insertid() |
---|
49 | { |
---|
50 | return $this->GetOne('select @@identity'); |
---|
51 | } |
---|
52 | // might require begintrans -- committrans |
---|
53 | function _affectedrows() |
---|
54 | { |
---|
55 | return $this->GetOne('select @@rowcount'); |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | function BeginTrans() |
---|
60 | { |
---|
61 | |
---|
62 | if ($this->transOff) return true; |
---|
63 | $this->transCnt += 1; |
---|
64 | |
---|
65 | $this->Execute('BEGIN TRAN'); |
---|
66 | return true; |
---|
67 | } |
---|
68 | |
---|
69 | function CommitTrans($ok=true) |
---|
70 | { |
---|
71 | if ($this->transOff) return true; |
---|
72 | |
---|
73 | if (!$ok) return $this->RollbackTrans(); |
---|
74 | |
---|
75 | $this->transCnt -= 1; |
---|
76 | $this->Execute('COMMIT TRAN'); |
---|
77 | return true; |
---|
78 | } |
---|
79 | |
---|
80 | function RollbackTrans() |
---|
81 | { |
---|
82 | if ($this->transOff) return true; |
---|
83 | $this->transCnt -= 1; |
---|
84 | $this->Execute('ROLLBACK TRAN'); |
---|
85 | return true; |
---|
86 | } |
---|
87 | |
---|
88 | // http://www.isug.com/Sybase_FAQ/ASE/section6.1.html#6.1.4 |
---|
89 | function RowLock($tables,$where) |
---|
90 | { |
---|
91 | if (!$this->_hastrans) $this->BeginTrans(); |
---|
92 | $tables = str_replace(',',' HOLDLOCK,',$tables); |
---|
93 | return $this->GetOne("select top 1 null as ignore from $tables HOLDLOCK where $where"); |
---|
94 | |
---|
95 | } |
---|
96 | |
---|
97 | function SelectDB($dbName) { |
---|
98 | $this->databaseName = $dbName; |
---|
99 | if ($this->_connectionID) { |
---|
100 | return @sybase_select_db($dbName); |
---|
101 | } |
---|
102 | else return false; |
---|
103 | } |
---|
104 | |
---|
105 | /* Returns: the last error message from previous database operation |
---|
106 | Note: This function is NOT available for Microsoft SQL Server. */ |
---|
107 | |
---|
108 | function ErrorMsg() |
---|
109 | { |
---|
110 | if ($this->_logsql) return $this->_errorMsg; |
---|
111 | $this->_errorMsg = sybase_get_last_message(); |
---|
112 | return $this->_errorMsg; |
---|
113 | } |
---|
114 | |
---|
115 | // returns true or false |
---|
116 | function _connect($argHostname, $argUsername, $argPassword, $argDatabasename) |
---|
117 | { |
---|
118 | if (!function_exists('sybase_connect')) return null; |
---|
119 | |
---|
120 | $this->_connectionID = sybase_connect($argHostname,$argUsername,$argPassword); |
---|
121 | if ($this->_connectionID === false) return false; |
---|
122 | if ($argDatabasename) return $this->SelectDB($argDatabasename); |
---|
123 | return true; |
---|
124 | } |
---|
125 | // returns true or false |
---|
126 | function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename) |
---|
127 | { |
---|
128 | if (!function_exists('sybase_connect')) return null; |
---|
129 | |
---|
130 | $this->_connectionID = sybase_pconnect($argHostname,$argUsername,$argPassword); |
---|
131 | if ($this->_connectionID === false) return false; |
---|
132 | if ($argDatabasename) return $this->SelectDB($argDatabasename); |
---|
133 | return true; |
---|
134 | } |
---|
135 | |
---|
136 | // returns query ID if successful, otherwise false |
---|
137 | function _query($sql,$inputarr) |
---|
138 | { |
---|
139 | global $ADODB_COUNTRECS; |
---|
140 | |
---|
141 | if ($ADODB_COUNTRECS == false && ADODB_PHPVER >= 0x4300) |
---|
142 | return sybase_unbuffered_query($sql,$this->_connectionID); |
---|
143 | else |
---|
144 | return sybase_query($sql,$this->_connectionID); |
---|
145 | } |
---|
146 | |
---|
147 | // See http://www.isug.com/Sybase_FAQ/ASE/section6.2.html#6.2.12 |
---|
148 | function &SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0) |
---|
149 | { |
---|
150 | if ($secs2cache > 0) {// we do not cache rowcount, so we have to load entire recordset |
---|
151 | $rs =& ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache); |
---|
152 | return $rs; |
---|
153 | } |
---|
154 | $cnt = ($nrows > 0) ? $nrows : 0; |
---|
155 | if ($offset > 0 && $cnt) $cnt += $offset; |
---|
156 | |
---|
157 | $this->Execute("set rowcount $cnt"); |
---|
158 | $rs =& ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,0); |
---|
159 | $this->Execute("set rowcount 0"); |
---|
160 | |
---|
161 | return $rs; |
---|
162 | } |
---|
163 | |
---|
164 | // returns true or false |
---|
165 | function _close() |
---|
166 | { |
---|
167 | return @sybase_close($this->_connectionID); |
---|
168 | } |
---|
169 | |
---|
170 | function UnixDate($v) |
---|
171 | { |
---|
172 | return ADORecordSet_array_sybase::UnixDate($v); |
---|
173 | } |
---|
174 | |
---|
175 | function UnixTimeStamp($v) |
---|
176 | { |
---|
177 | return ADORecordSet_array_sybase::UnixTimeStamp($v); |
---|
178 | } |
---|
179 | |
---|
180 | |
---|
181 | |
---|
182 | # Added 2003-10-05 by Chris Phillipson |
---|
183 | # Used ASA SQL Reference Manual -- http://sybooks.sybase.com/onlinebooks/group-aw/awg0800e/dbrfen8/@ebt-link;pt=16756?target=%25N%15_12018_START_RESTART_N%25 |
---|
184 | # to convert similar Microsoft SQL*Server (mssql) API into Sybase compatible version |
---|
185 | // Format date column in sql string given an input format that understands Y M D |
---|
186 | function SQLDate($fmt, $col=false) |
---|
187 | { |
---|
188 | if (!$col) $col = $this->sysTimeStamp; |
---|
189 | $s = ''; |
---|
190 | |
---|
191 | $len = strlen($fmt); |
---|
192 | for ($i=0; $i < $len; $i++) { |
---|
193 | if ($s) $s .= '+'; |
---|
194 | $ch = $fmt[$i]; |
---|
195 | switch($ch) { |
---|
196 | case 'Y': |
---|
197 | case 'y': |
---|
198 | $s .= "datename(yy,$col)"; |
---|
199 | break; |
---|
200 | case 'M': |
---|
201 | $s .= "convert(char(3),$col,0)"; |
---|
202 | break; |
---|
203 | case 'm': |
---|
204 | $s .= "replace(str(month($col),2),' ','0')"; |
---|
205 | break; |
---|
206 | case 'Q': |
---|
207 | case 'q': |
---|
208 | $s .= "datename(qq,$col)"; |
---|
209 | break; |
---|
210 | case 'D': |
---|
211 | case 'd': |
---|
212 | $s .= "replace(str(datepart(dd,$col),2),' ','0')"; |
---|
213 | break; |
---|
214 | case 'h': |
---|
215 | $s .= "substring(convert(char(14),$col,0),13,2)"; |
---|
216 | break; |
---|
217 | |
---|
218 | case 'H': |
---|
219 | $s .= "replace(str(datepart(hh,$col),2),' ','0')"; |
---|
220 | break; |
---|
221 | |
---|
222 | case 'i': |
---|
223 | $s .= "replace(str(datepart(mi,$col),2),' ','0')"; |
---|
224 | break; |
---|
225 | case 's': |
---|
226 | $s .= "replace(str(datepart(ss,$col),2),' ','0')"; |
---|
227 | break; |
---|
228 | case 'a': |
---|
229 | case 'A': |
---|
230 | $s .= "substring(convert(char(19),$col,0),18,2)"; |
---|
231 | break; |
---|
232 | |
---|
233 | default: |
---|
234 | if ($ch == '\\') { |
---|
235 | $i++; |
---|
236 | $ch = substr($fmt,$i,1); |
---|
237 | } |
---|
238 | $s .= $this->qstr($ch); |
---|
239 | break; |
---|
240 | } |
---|
241 | } |
---|
242 | return $s; |
---|
243 | } |
---|
244 | |
---|
245 | # Added 2003-10-07 by Chris Phillipson |
---|
246 | # Used ASA SQL Reference Manual -- http://sybooks.sybase.com/onlinebooks/group-aw/awg0800e/dbrfen8/@ebt-link;pt=5981;uf=0?target=0;window=new;showtoc=true;book=dbrfen8 |
---|
247 | # to convert similar Microsoft SQL*Server (mssql) API into Sybase compatible version |
---|
248 | function MetaPrimaryKeys($table) |
---|
249 | { |
---|
250 | $sql = "SELECT c.column_name " . |
---|
251 | "FROM syscolumn c, systable t " . |
---|
252 | "WHERE t.table_name='$table' AND c.table_id=t.table_id " . |
---|
253 | "AND t.table_type='BASE' " . |
---|
254 | "AND c.pkey = 'Y' " . |
---|
255 | "ORDER BY c.column_id"; |
---|
256 | |
---|
257 | $a = $this->GetCol($sql); |
---|
258 | if ($a && sizeof($a)>0) return $a; |
---|
259 | return false; |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | /*-------------------------------------------------------------------------------------- |
---|
264 | Class Name: Recordset |
---|
265 | --------------------------------------------------------------------------------------*/ |
---|
266 | global $ADODB_sybase_mths; |
---|
267 | $ADODB_sybase_mths = array( |
---|
268 | 'JAN'=>1,'FEB'=>2,'MAR'=>3,'APR'=>4,'MAY'=>5,'JUN'=>6, |
---|
269 | 'JUL'=>7,'AUG'=>8,'SEP'=>9,'OCT'=>10,'NOV'=>11,'DEC'=>12); |
---|
270 | |
---|
271 | class ADORecordset_sybase extends ADORecordSet { |
---|
272 | |
---|
273 | var $databaseType = "sybase"; |
---|
274 | var $canSeek = true; |
---|
275 | // _mths works only in non-localised system |
---|
276 | var $_mths = array('JAN'=>1,'FEB'=>2,'MAR'=>3,'APR'=>4,'MAY'=>5,'JUN'=>6,'JUL'=>7,'AUG'=>8,'SEP'=>9,'OCT'=>10,'NOV'=>11,'DEC'=>12); |
---|
277 | |
---|
278 | function ADORecordset_sybase($id,$mode=false) |
---|
279 | { |
---|
280 | if ($mode === false) { |
---|
281 | global $ADODB_FETCH_MODE; |
---|
282 | $mode = $ADODB_FETCH_MODE; |
---|
283 | } |
---|
284 | if (!$mode) $this->fetchMode = ADODB_FETCH_ASSOC; |
---|
285 | else $this->fetchMode = $mode; |
---|
286 | return $this->ADORecordSet($id,$mode); |
---|
287 | } |
---|
288 | |
---|
289 | /* Returns: an object containing field information. |
---|
290 | Get column information in the Recordset object. fetchField() can be used in order to obtain information about |
---|
291 | fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by |
---|
292 | fetchField() is retrieved. */ |
---|
293 | function &FetchField($fieldOffset = -1) |
---|
294 | { |
---|
295 | if ($fieldOffset != -1) { |
---|
296 | $o = @sybase_fetch_field($this->_queryID, $fieldOffset); |
---|
297 | } |
---|
298 | else if ($fieldOffset == -1) { /* The $fieldOffset argument is not provided thus its -1 */ |
---|
299 | $o = @sybase_fetch_field($this->_queryID); |
---|
300 | } |
---|
301 | // older versions of PHP did not support type, only numeric |
---|
302 | if ($o && !isset($o->type)) $o->type = ($o->numeric) ? 'float' : 'varchar'; |
---|
303 | return $o; |
---|
304 | } |
---|
305 | |
---|
306 | function _initrs() |
---|
307 | { |
---|
308 | global $ADODB_COUNTRECS; |
---|
309 | $this->_numOfRows = ($ADODB_COUNTRECS)? @sybase_num_rows($this->_queryID):-1; |
---|
310 | $this->_numOfFields = @sybase_num_fields($this->_queryID); |
---|
311 | } |
---|
312 | |
---|
313 | function _seek($row) |
---|
314 | { |
---|
315 | return @sybase_data_seek($this->_queryID, $row); |
---|
316 | } |
---|
317 | |
---|
318 | function _fetch($ignore_fields=false) |
---|
319 | { |
---|
320 | if ($this->fetchMode == ADODB_FETCH_NUM) { |
---|
321 | $this->fields = @sybase_fetch_row($this->_queryID); |
---|
322 | } else if ($this->fetchMode == ADODB_FETCH_ASSOC) { |
---|
323 | $this->fields = @sybase_fetch_row($this->_queryID); |
---|
324 | if (is_array($this->fields)) { |
---|
325 | $this->fields = $this->GetRowAssoc(ADODB_ASSOC_CASE); |
---|
326 | return true; |
---|
327 | } |
---|
328 | return false; |
---|
329 | } else { |
---|
330 | $this->fields = @sybase_fetch_array($this->_queryID); |
---|
331 | } |
---|
332 | if ( is_array($this->fields)) { |
---|
333 | return true; |
---|
334 | } |
---|
335 | |
---|
336 | return false; |
---|
337 | } |
---|
338 | |
---|
339 | /* close() only needs to be called if you are worried about using too much memory while your script |
---|
340 | is running. All associated result memory for the specified result identifier will automatically be freed. */ |
---|
341 | function _close() { |
---|
342 | return @sybase_free_result($this->_queryID); |
---|
343 | } |
---|
344 | |
---|
345 | // sybase/mssql uses a default date like Dec 30 2000 12:00AM |
---|
346 | function UnixDate($v) |
---|
347 | { |
---|
348 | return ADORecordSet_array_sybase::UnixDate($v); |
---|
349 | } |
---|
350 | |
---|
351 | function UnixTimeStamp($v) |
---|
352 | { |
---|
353 | return ADORecordSet_array_sybase::UnixTimeStamp($v); |
---|
354 | } |
---|
355 | } |
---|
356 | |
---|
357 | class ADORecordSet_array_sybase extends ADORecordSet_array { |
---|
358 | function ADORecordSet_array_sybase($id=-1) |
---|
359 | { |
---|
360 | $this->ADORecordSet_array($id); |
---|
361 | } |
---|
362 | |
---|
363 | // sybase/mssql uses a default date like Dec 30 2000 12:00AM |
---|
364 | function UnixDate($v) |
---|
365 | { |
---|
366 | global $ADODB_sybase_mths; |
---|
367 | |
---|
368 | //Dec 30 2000 12:00AM |
---|
369 | if (!ereg( "([A-Za-z]{3})[-/\. ]+([0-9]{1,2})[-/\. ]+([0-9]{4})" |
---|
370 | ,$v, $rr)) return parent::UnixDate($v); |
---|
371 | |
---|
372 | if ($rr[3] <= TIMESTAMP_FIRST_YEAR) return 0; |
---|
373 | |
---|
374 | $themth = substr(strtoupper($rr[1]),0,3); |
---|
375 | $themth = $ADODB_sybase_mths[$themth]; |
---|
376 | if ($themth <= 0) return false; |
---|
377 | // h-m-s-MM-DD-YY |
---|
378 | return mktime(0,0,0,$themth,$rr[2],$rr[3]); |
---|
379 | } |
---|
380 | |
---|
381 | function UnixTimeStamp($v) |
---|
382 | { |
---|
383 | global $ADODB_sybase_mths; |
---|
384 | //11.02.2001 Toni Tunkkari toni.tunkkari@finebyte.com |
---|
385 | //Changed [0-9] to [0-9 ] in day conversion |
---|
386 | if (!ereg( "([A-Za-z]{3})[-/\. ]([0-9 ]{1,2})[-/\. ]([0-9]{4}) +([0-9]{1,2}):([0-9]{1,2}) *([apAP]{0,1})" |
---|
387 | ,$v, $rr)) return parent::UnixTimeStamp($v); |
---|
388 | if ($rr[3] <= TIMESTAMP_FIRST_YEAR) return 0; |
---|
389 | |
---|
390 | $themth = substr(strtoupper($rr[1]),0,3); |
---|
391 | $themth = $ADODB_sybase_mths[$themth]; |
---|
392 | if ($themth <= 0) return false; |
---|
393 | |
---|
394 | switch (strtoupper($rr[6])) { |
---|
395 | case 'P': |
---|
396 | if ($rr[4]<12) $rr[4] += 12; |
---|
397 | break; |
---|
398 | case 'A': |
---|
399 | if ($rr[4]==12) $rr[4] = 0; |
---|
400 | break; |
---|
401 | default: |
---|
402 | break; |
---|
403 | } |
---|
404 | // h-m-s-MM-DD-YY |
---|
405 | return mktime($rr[4],$rr[5],0,$themth,$rr[2],$rr[3]); |
---|
406 | } |
---|
407 | } |
---|
408 | ?> |
---|