adodb-sybase.inc.php 12 KB

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