adodb-sybase.inc.php 12 KB

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