PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/adodb5/drivers/adodb-sybase.inc.php

https://gitlab.com/mrktinh/bookonline
PHP | 443 lines | 359 code | 44 blank | 40 comment | 33 complexity | 64be3bff5ccbd81e6520541339d2cdb0 MD5 | raw file
  1. <?php
  2. /*
  3. @version v5.20.4 30-Mar-2016
  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.sourceforge.net
  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 = isset($php_errormsg) ? $php_errormsg : 'SYBASE error messages not supported on this platform';
  99. return $this->_errorMsg;
  100. }
  101. // returns true or false
  102. function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
  103. {
  104. if (!function_exists('sybase_connect')) return null;
  105. // Sybase connection on custom port
  106. if ($this->port) {
  107. $argHostname .= ':' . $this->port;
  108. }
  109. if ($this->charSet) {
  110. $this->_connectionID = sybase_connect($argHostname,$argUsername,$argPassword, $this->charSet);
  111. } else {
  112. $this->_connectionID = sybase_connect($argHostname,$argUsername,$argPassword);
  113. }
  114. if ($this->_connectionID === false) return false;
  115. if ($argDatabasename) return $this->SelectDB($argDatabasename);
  116. return true;
  117. }
  118. // returns true or false
  119. function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
  120. {
  121. if (!function_exists('sybase_connect')) return null;
  122. // Sybase connection on custom port
  123. if ($this->port) {
  124. $argHostname .= ':' . $this->port;
  125. }
  126. if ($this->charSet) {
  127. $this->_connectionID = sybase_pconnect($argHostname,$argUsername,$argPassword, $this->charSet);
  128. } else {
  129. $this->_connectionID = sybase_pconnect($argHostname,$argUsername,$argPassword);
  130. }
  131. if ($this->_connectionID === false) return false;
  132. if ($argDatabasename) return $this->SelectDB($argDatabasename);
  133. return true;
  134. }
  135. // returns query ID if successful, otherwise false
  136. function _query($sql,$inputarr=false)
  137. {
  138. global $ADODB_COUNTRECS;
  139. if ($ADODB_COUNTRECS == false && ADODB_PHPVER >= 0x4300)
  140. return sybase_unbuffered_query($sql,$this->_connectionID);
  141. else
  142. return sybase_query($sql,$this->_connectionID);
  143. }
  144. // See http://www.isug.com/Sybase_FAQ/ASE/section6.2.html#6.2.12
  145. function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
  146. {
  147. if ($secs2cache > 0) {// we do not cache rowcount, so we have to load entire recordset
  148. $rs = ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
  149. return $rs;
  150. }
  151. $nrows = (integer) $nrows;
  152. $offset = (integer) $offset;
  153. $cnt = ($nrows >= 0) ? $nrows : 999999999;
  154. if ($offset > 0 && $cnt) $cnt += $offset;
  155. $this->Execute("set rowcount $cnt");
  156. $rs = ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,0);
  157. $this->Execute("set rowcount 0");
  158. return $rs;
  159. }
  160. // returns true or false
  161. function _close()
  162. {
  163. return @sybase_close($this->_connectionID);
  164. }
  165. static function UnixDate($v)
  166. {
  167. return ADORecordSet_array_sybase::UnixDate($v);
  168. }
  169. static function UnixTimeStamp($v)
  170. {
  171. return ADORecordSet_array_sybase::UnixTimeStamp($v);
  172. }
  173. # Added 2003-10-05 by Chris Phillipson
  174. # 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
  175. # to convert similar Microsoft SQL*Server (mssql) API into Sybase compatible version
  176. // Format date column in sql string given an input format that understands Y M D
  177. function SQLDate($fmt, $col=false)
  178. {
  179. if (!$col) $col = $this->sysTimeStamp;
  180. $s = '';
  181. $len = strlen($fmt);
  182. for ($i=0; $i < $len; $i++) {
  183. if ($s) $s .= '+';
  184. $ch = $fmt[$i];
  185. switch($ch) {
  186. case 'Y':
  187. case 'y':
  188. $s .= "datename(yy,$col)";
  189. break;
  190. case 'M':
  191. $s .= "convert(char(3),$col,0)";
  192. break;
  193. case 'm':
  194. $s .= "str_replace(str(month($col),2),' ','0')";
  195. break;
  196. case 'Q':
  197. case 'q':
  198. $s .= "datename(qq,$col)";
  199. break;
  200. case 'D':
  201. case 'd':
  202. $s .= "str_replace(str(datepart(dd,$col),2),' ','0')";
  203. break;
  204. case 'h':
  205. $s .= "substring(convert(char(14),$col,0),13,2)";
  206. break;
  207. case 'H':
  208. $s .= "str_replace(str(datepart(hh,$col),2),' ','0')";
  209. break;
  210. case 'i':
  211. $s .= "str_replace(str(datepart(mi,$col),2),' ','0')";
  212. break;
  213. case 's':
  214. $s .= "str_replace(str(datepart(ss,$col),2),' ','0')";
  215. break;
  216. case 'a':
  217. case 'A':
  218. $s .= "substring(convert(char(19),$col,0),18,2)";
  219. break;
  220. default:
  221. if ($ch == '\\') {
  222. $i++;
  223. $ch = substr($fmt,$i,1);
  224. }
  225. $s .= $this->qstr($ch);
  226. break;
  227. }
  228. }
  229. return $s;
  230. }
  231. # Added 2003-10-07 by Chris Phillipson
  232. # 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
  233. # to convert similar Microsoft SQL*Server (mssql) API into Sybase compatible version
  234. function MetaPrimaryKeys($table, $owner = false)
  235. {
  236. $sql = "SELECT c.column_name " .
  237. "FROM syscolumn c, systable t " .
  238. "WHERE t.table_name='$table' AND c.table_id=t.table_id " .
  239. "AND t.table_type='BASE' " .
  240. "AND c.pkey = 'Y' " .
  241. "ORDER BY c.column_id";
  242. $a = $this->GetCol($sql);
  243. if ($a && sizeof($a)>0) return $a;
  244. return false;
  245. }
  246. }
  247. /*--------------------------------------------------------------------------------------
  248. Class Name: Recordset
  249. --------------------------------------------------------------------------------------*/
  250. global $ADODB_sybase_mths;
  251. $ADODB_sybase_mths = array(
  252. 'JAN'=>1,'FEB'=>2,'MAR'=>3,'APR'=>4,'MAY'=>5,'JUN'=>6,
  253. 'JUL'=>7,'AUG'=>8,'SEP'=>9,'OCT'=>10,'NOV'=>11,'DEC'=>12);
  254. class ADORecordset_sybase extends ADORecordSet {
  255. var $databaseType = "sybase";
  256. var $canSeek = true;
  257. // _mths works only in non-localised system
  258. 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);
  259. function __construct($id,$mode=false)
  260. {
  261. if ($mode === false) {
  262. global $ADODB_FETCH_MODE;
  263. $mode = $ADODB_FETCH_MODE;
  264. }
  265. if (!$mode) $this->fetchMode = ADODB_FETCH_ASSOC;
  266. else $this->fetchMode = $mode;
  267. parent::__construct($id,$mode);
  268. }
  269. /* Returns: an object containing field information.
  270. Get column information in the Recordset object. fetchField() can be used in order to obtain information about
  271. fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by
  272. fetchField() is retrieved. */
  273. function FetchField($fieldOffset = -1)
  274. {
  275. if ($fieldOffset != -1) {
  276. $o = @sybase_fetch_field($this->_queryID, $fieldOffset);
  277. }
  278. else if ($fieldOffset == -1) { /* The $fieldOffset argument is not provided thus its -1 */
  279. $o = @sybase_fetch_field($this->_queryID);
  280. }
  281. // older versions of PHP did not support type, only numeric
  282. if ($o && !isset($o->type)) $o->type = ($o->numeric) ? 'float' : 'varchar';
  283. return $o;
  284. }
  285. function _initrs()
  286. {
  287. global $ADODB_COUNTRECS;
  288. $this->_numOfRows = ($ADODB_COUNTRECS)? @sybase_num_rows($this->_queryID):-1;
  289. $this->_numOfFields = @sybase_num_fields($this->_queryID);
  290. }
  291. function _seek($row)
  292. {
  293. return @sybase_data_seek($this->_queryID, $row);
  294. }
  295. function _fetch($ignore_fields=false)
  296. {
  297. if ($this->fetchMode == ADODB_FETCH_NUM) {
  298. $this->fields = @sybase_fetch_row($this->_queryID);
  299. } else if ($this->fetchMode == ADODB_FETCH_ASSOC) {
  300. $this->fields = @sybase_fetch_assoc($this->_queryID);
  301. if (is_array($this->fields)) {
  302. $this->fields = $this->GetRowAssoc();
  303. return true;
  304. }
  305. return false;
  306. } else {
  307. $this->fields = @sybase_fetch_array($this->_queryID);
  308. }
  309. if ( is_array($this->fields)) {
  310. return true;
  311. }
  312. return false;
  313. }
  314. /* close() only needs to be called if you are worried about using too much memory while your script
  315. is running. All associated result memory for the specified result identifier will automatically be freed. */
  316. function _close() {
  317. return @sybase_free_result($this->_queryID);
  318. }
  319. // sybase/mssql uses a default date like Dec 30 2000 12:00AM
  320. static function UnixDate($v)
  321. {
  322. return ADORecordSet_array_sybase::UnixDate($v);
  323. }
  324. static function UnixTimeStamp($v)
  325. {
  326. return ADORecordSet_array_sybase::UnixTimeStamp($v);
  327. }
  328. }
  329. class ADORecordSet_array_sybase extends ADORecordSet_array {
  330. function __construct($id=-1)
  331. {
  332. parent::__construct($id);
  333. }
  334. // sybase/mssql uses a default date like Dec 30 2000 12:00AM
  335. static function UnixDate($v)
  336. {
  337. global $ADODB_sybase_mths;
  338. //Dec 30 2000 12:00AM
  339. if (!preg_match( "/([A-Za-z]{3})[-/\. ]+([0-9]{1,2})[-/\. ]+([0-9]{4})/"
  340. ,$v, $rr)) return parent::UnixDate($v);
  341. if ($rr[3] <= TIMESTAMP_FIRST_YEAR) return 0;
  342. $themth = substr(strtoupper($rr[1]),0,3);
  343. $themth = $ADODB_sybase_mths[$themth];
  344. if ($themth <= 0) return false;
  345. // h-m-s-MM-DD-YY
  346. return adodb_mktime(0,0,0,$themth,$rr[2],$rr[3]);
  347. }
  348. static function UnixTimeStamp($v)
  349. {
  350. global $ADODB_sybase_mths;
  351. //11.02.2001 Toni Tunkkari toni.tunkkari@finebyte.com
  352. //Changed [0-9] to [0-9 ] in day conversion
  353. 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})/"
  354. ,$v, $rr)) return parent::UnixTimeStamp($v);
  355. if ($rr[3] <= TIMESTAMP_FIRST_YEAR) return 0;
  356. $themth = substr(strtoupper($rr[1]),0,3);
  357. $themth = $ADODB_sybase_mths[$themth];
  358. if ($themth <= 0) return false;
  359. switch (strtoupper($rr[6])) {
  360. case 'P':
  361. if ($rr[4]<12) $rr[4] += 12;
  362. break;
  363. case 'A':
  364. if ($rr[4]==12) $rr[4] = 0;
  365. break;
  366. default:
  367. break;
  368. }
  369. // h-m-s-MM-DD-YY
  370. return adodb_mktime($rr[4],$rr[5],0,$themth,$rr[2],$rr[3]);
  371. }
  372. }