1
0

adodb-lib.inc.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  1. <?php
  2. // security - hide paths
  3. if (!defined('ADODB_DIR')) die();
  4. global $ADODB_INCLUDED_LIB;
  5. $ADODB_INCLUDED_LIB = 1;
  6. /*
  7. @version v5.20.3 01-Jan-2016
  8. @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
  9. @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
  10. Released under both BSD license and Lesser GPL library license.
  11. Whenever there is any discrepancy between the two licenses,
  12. the BSD license will take precedence. See License.txt.
  13. Set tabs to 4 for best viewing.
  14. Less commonly used functions are placed here to reduce size of adodb.inc.php.
  15. */
  16. function adodb_strip_order_by($sql)
  17. {
  18. $rez = preg_match('/(\sORDER\s+BY\s(?:[^)](?!LIMIT))*)/is', $sql, $arr);
  19. if ($arr)
  20. if (strpos($arr[1], '(') !== false) {
  21. $at = strpos($sql, $arr[1]);
  22. $cntin = 0;
  23. for ($i=$at, $max=strlen($sql); $i < $max; $i++) {
  24. $ch = $sql[$i];
  25. if ($ch == '(') {
  26. $cntin += 1;
  27. } elseif($ch == ')') {
  28. $cntin -= 1;
  29. if ($cntin < 0) {
  30. break;
  31. }
  32. }
  33. }
  34. $sql = substr($sql,0,$at).substr($sql,$i);
  35. } else {
  36. $sql = str_replace($arr[1], '', $sql);
  37. }
  38. return $sql;
  39. }
  40. if (false) {
  41. $sql = 'select * from (select a from b order by a(b),b(c) desc)';
  42. $sql = '(select * from abc order by 1)';
  43. die(adodb_strip_order_by($sql));
  44. }
  45. function adodb_probetypes(&$array,&$types,$probe=8)
  46. {
  47. // probe and guess the type
  48. $types = array();
  49. if ($probe > sizeof($array)) $max = sizeof($array);
  50. else $max = $probe;
  51. for ($j=0;$j < $max; $j++) {
  52. $row = $array[$j];
  53. if (!$row) break;
  54. $i = -1;
  55. foreach($row as $v) {
  56. $i += 1;
  57. if (isset($types[$i]) && $types[$i]=='C') continue;
  58. //print " ($i ".$types[$i]. "$v) ";
  59. $v = trim($v);
  60. if (!preg_match('/^[+-]{0,1}[0-9\.]+$/',$v)) {
  61. $types[$i] = 'C'; // once C, always C
  62. continue;
  63. }
  64. if ($j == 0) {
  65. // If empty string, we presume is character
  66. // test for integer for 1st row only
  67. // after that it is up to testing other rows to prove
  68. // that it is not an integer
  69. if (strlen($v) == 0) $types[$i] = 'C';
  70. if (strpos($v,'.') !== false) $types[$i] = 'N';
  71. else $types[$i] = 'I';
  72. continue;
  73. }
  74. if (strpos($v,'.') !== false) $types[$i] = 'N';
  75. }
  76. }
  77. }
  78. function adodb_transpose(&$arr, &$newarr, &$hdr, &$fobjs)
  79. {
  80. $oldX = sizeof(reset($arr));
  81. $oldY = sizeof($arr);
  82. if ($hdr) {
  83. $startx = 1;
  84. $hdr = array('Fields');
  85. for ($y = 0; $y < $oldY; $y++) {
  86. $hdr[] = $arr[$y][0];
  87. }
  88. } else
  89. $startx = 0;
  90. for ($x = $startx; $x < $oldX; $x++) {
  91. if ($fobjs) {
  92. $o = $fobjs[$x];
  93. $newarr[] = array($o->name);
  94. } else
  95. $newarr[] = array();
  96. for ($y = 0; $y < $oldY; $y++) {
  97. $newarr[$x-$startx][] = $arr[$y][$x];
  98. }
  99. }
  100. }
  101. // Force key to upper.
  102. // See also http://www.php.net/manual/en/function.array-change-key-case.php
  103. function _array_change_key_case($an_array)
  104. {
  105. if (is_array($an_array)) {
  106. $new_array = array();
  107. foreach($an_array as $key=>$value)
  108. $new_array[strtoupper($key)] = $value;
  109. return $new_array;
  110. }
  111. return $an_array;
  112. }
  113. function _adodb_replace(&$zthis, $table, $fieldArray, $keyCol, $autoQuote, $has_autoinc)
  114. {
  115. if (count($fieldArray) == 0) return 0;
  116. $first = true;
  117. $uSet = '';
  118. if (!is_array($keyCol)) {
  119. $keyCol = array($keyCol);
  120. }
  121. foreach($fieldArray as $k => $v) {
  122. if ($v === null) {
  123. $v = 'NULL';
  124. $fieldArray[$k] = $v;
  125. } else if ($autoQuote && /*!is_numeric($v) /*and strncmp($v,"'",1) !== 0 -- sql injection risk*/ strcasecmp($v,$zthis->null2null)!=0) {
  126. $v = $zthis->qstr($v);
  127. $fieldArray[$k] = $v;
  128. }
  129. if (in_array($k,$keyCol)) continue; // skip UPDATE if is key
  130. if ($first) {
  131. $first = false;
  132. $uSet = "$k=$v";
  133. } else
  134. $uSet .= ",$k=$v";
  135. }
  136. $where = false;
  137. foreach ($keyCol as $v) {
  138. if (isset($fieldArray[$v])) {
  139. if ($where) $where .= ' and '.$v.'='.$fieldArray[$v];
  140. else $where = $v.'='.$fieldArray[$v];
  141. }
  142. }
  143. if ($uSet && $where) {
  144. $update = "UPDATE $table SET $uSet WHERE $where";
  145. $rs = $zthis->Execute($update);
  146. if ($rs) {
  147. if ($zthis->poorAffectedRows) {
  148. /*
  149. The Select count(*) wipes out any errors that the update would have returned.
  150. http://phplens.com/lens/lensforum/msgs.php?id=5696
  151. */
  152. if ($zthis->ErrorNo()<>0) return 0;
  153. # affected_rows == 0 if update field values identical to old values
  154. # for mysql - which is silly.
  155. $cnt = $zthis->GetOne("select count(*) from $table where $where");
  156. if ($cnt > 0) return 1; // record already exists
  157. } else {
  158. if (($zthis->Affected_Rows()>0)) return 1;
  159. }
  160. } else
  161. return 0;
  162. }
  163. // print "<p>Error=".$this->ErrorNo().'<p>';
  164. $first = true;
  165. foreach($fieldArray as $k => $v) {
  166. if ($has_autoinc && in_array($k,$keyCol)) continue; // skip autoinc col
  167. if ($first) {
  168. $first = false;
  169. $iCols = "$k";
  170. $iVals = "$v";
  171. } else {
  172. $iCols .= ",$k";
  173. $iVals .= ",$v";
  174. }
  175. }
  176. $insert = "INSERT INTO $table ($iCols) VALUES ($iVals)";
  177. $rs = $zthis->Execute($insert);
  178. return ($rs) ? 2 : 0;
  179. }
  180. // Requires $ADODB_FETCH_MODE = ADODB_FETCH_NUM
  181. function _adodb_getmenu(&$zthis, $name,$defstr='',$blank1stItem=true,$multiple=false,
  182. $size=0, $selectAttr='',$compareFields0=true)
  183. {
  184. $hasvalue = false;
  185. if ($multiple or is_array($defstr)) {
  186. if ($size==0) $size=5;
  187. $attr = ' multiple size="'.$size.'"';
  188. if (!strpos($name,'[]')) $name .= '[]';
  189. } else if ($size) $attr = ' size="'.$size.'"';
  190. else $attr ='';
  191. $s = '<select name="'.$name.'"'.$attr.' '.$selectAttr.'>';
  192. if ($blank1stItem)
  193. if (is_string($blank1stItem)) {
  194. $barr = explode(':',$blank1stItem);
  195. if (sizeof($barr) == 1) $barr[] = '';
  196. $s .= "\n<option value=\"".$barr[0]."\">".$barr[1]."</option>";
  197. } else $s .= "\n<option></option>";
  198. if ($zthis->FieldCount() > 1) $hasvalue=true;
  199. else $compareFields0 = true;
  200. $value = '';
  201. $optgroup = null;
  202. $firstgroup = true;
  203. $fieldsize = $zthis->FieldCount();
  204. while(!$zthis->EOF) {
  205. $zval = rtrim(reset($zthis->fields));
  206. if ($blank1stItem && $zval=="") {
  207. $zthis->MoveNext();
  208. continue;
  209. }
  210. if ($fieldsize > 1) {
  211. if (isset($zthis->fields[1]))
  212. $zval2 = rtrim($zthis->fields[1]);
  213. else
  214. $zval2 = rtrim(next($zthis->fields));
  215. }
  216. $selected = ($compareFields0) ? $zval : $zval2;
  217. $group = '';
  218. if ($fieldsize > 2) {
  219. $group = rtrim($zthis->fields[2]);
  220. }
  221. /*
  222. if ($optgroup != $group) {
  223. $optgroup = $group;
  224. if ($firstgroup) {
  225. $firstgroup = false;
  226. $s .="\n<optgroup label='". htmlspecialchars($group) ."'>";
  227. } else {
  228. $s .="\n</optgroup>";
  229. $s .="\n<optgroup label='". htmlspecialchars($group) ."'>";
  230. }
  231. }
  232. */
  233. if ($hasvalue)
  234. $value = " value='".htmlspecialchars($zval2)."'";
  235. if (is_array($defstr)) {
  236. if (in_array($selected,$defstr))
  237. $s .= "\n<option selected='selected'$value>".htmlspecialchars($zval).'</option>';
  238. else
  239. $s .= "\n<option".$value.'>'.htmlspecialchars($zval).'</option>';
  240. }
  241. else {
  242. if (strcasecmp($selected,$defstr)==0)
  243. $s .= "\n<option selected='selected'$value>".htmlspecialchars($zval).'</option>';
  244. else
  245. $s .= "\n<option".$value.'>'.htmlspecialchars($zval).'</option>';
  246. }
  247. $zthis->MoveNext();
  248. } // while
  249. // closing last optgroup
  250. if($optgroup != null) {
  251. $s .= "\n</optgroup>";
  252. }
  253. return $s ."\n</select>\n";
  254. }
  255. // Requires $ADODB_FETCH_MODE = ADODB_FETCH_NUM
  256. function _adodb_getmenu_gp(&$zthis, $name,$defstr='',$blank1stItem=true,$multiple=false,
  257. $size=0, $selectAttr='',$compareFields0=true)
  258. {
  259. $hasvalue = false;
  260. if ($multiple or is_array($defstr)) {
  261. if ($size==0) $size=5;
  262. $attr = ' multiple size="'.$size.'"';
  263. if (!strpos($name,'[]')) $name .= '[]';
  264. } else if ($size) $attr = ' size="'.$size.'"';
  265. else $attr ='';
  266. $s = '<select name="'.$name.'"'.$attr.' '.$selectAttr.'>';
  267. if ($blank1stItem)
  268. if (is_string($blank1stItem)) {
  269. $barr = explode(':',$blank1stItem);
  270. if (sizeof($barr) == 1) $barr[] = '';
  271. $s .= "\n<option value=\"".$barr[0]."\">".$barr[1]."</option>";
  272. } else $s .= "\n<option></option>";
  273. if ($zthis->FieldCount() > 1) $hasvalue=true;
  274. else $compareFields0 = true;
  275. $value = '';
  276. $optgroup = null;
  277. $firstgroup = true;
  278. $fieldsize = sizeof($zthis->fields);
  279. while(!$zthis->EOF) {
  280. $zval = rtrim(reset($zthis->fields));
  281. if ($blank1stItem && $zval=="") {
  282. $zthis->MoveNext();
  283. continue;
  284. }
  285. if ($fieldsize > 1) {
  286. if (isset($zthis->fields[1]))
  287. $zval2 = rtrim($zthis->fields[1]);
  288. else
  289. $zval2 = rtrim(next($zthis->fields));
  290. }
  291. $selected = ($compareFields0) ? $zval : $zval2;
  292. $group = '';
  293. if (isset($zthis->fields[2])) {
  294. $group = rtrim($zthis->fields[2]);
  295. }
  296. if ($optgroup != $group) {
  297. $optgroup = $group;
  298. if ($firstgroup) {
  299. $firstgroup = false;
  300. $s .="\n<optgroup label='". htmlspecialchars($group) ."'>";
  301. } else {
  302. $s .="\n</optgroup>";
  303. $s .="\n<optgroup label='". htmlspecialchars($group) ."'>";
  304. }
  305. }
  306. if ($hasvalue)
  307. $value = " value='".htmlspecialchars($zval2)."'";
  308. if (is_array($defstr)) {
  309. if (in_array($selected,$defstr))
  310. $s .= "\n<option selected='selected'$value>".htmlspecialchars($zval).'</option>';
  311. else
  312. $s .= "\n<option".$value.'>'.htmlspecialchars($zval).'</option>';
  313. }
  314. else {
  315. if (strcasecmp($selected,$defstr)==0)
  316. $s .= "\n<option selected='selected'$value>".htmlspecialchars($zval).'</option>';
  317. else
  318. $s .= "\n<option".$value.'>'.htmlspecialchars($zval).'</option>';
  319. }
  320. $zthis->MoveNext();
  321. } // while
  322. // closing last optgroup
  323. if($optgroup != null) {
  324. $s .= "\n</optgroup>";
  325. }
  326. return $s ."\n</select>\n";
  327. }
  328. /*
  329. Count the number of records this sql statement will return by using
  330. query rewriting heuristics...
  331. Does not work with UNIONs, except with postgresql and oracle.
  332. Usage:
  333. $conn->Connect(...);
  334. $cnt = _adodb_getcount($conn, $sql);
  335. */
  336. function _adodb_getcount(&$zthis, $sql,$inputarr=false,$secs2cache=0)
  337. {
  338. $qryRecs = 0;
  339. if (!empty($zthis->_nestedSQL) || preg_match("/^\s*SELECT\s+DISTINCT/is", $sql) ||
  340. preg_match('/\s+GROUP\s+BY\s+/is',$sql) ||
  341. preg_match('/\s+UNION\s+/is',$sql)) {
  342. $rewritesql = adodb_strip_order_by($sql);
  343. // ok, has SELECT DISTINCT or GROUP BY so see if we can use a table alias
  344. // but this is only supported by oracle and postgresql...
  345. if ($zthis->dataProvider == 'oci8') {
  346. // Allow Oracle hints to be used for query optimization, Chris Wrye
  347. if (preg_match('#/\\*+.*?\\*\\/#', $sql, $hint)) {
  348. $rewritesql = "SELECT ".$hint[0]." COUNT(*) FROM (".$rewritesql.")";
  349. } else
  350. $rewritesql = "SELECT COUNT(*) FROM (".$rewritesql.")";
  351. } else if (strncmp($zthis->databaseType,'postgres',8) == 0 || strncmp($zthis->databaseType,'mysql',5) == 0) {
  352. $rewritesql = "SELECT COUNT(*) FROM ($rewritesql) _ADODB_ALIAS_";
  353. } else {
  354. $rewritesql = "SELECT COUNT(*) FROM ($rewritesql)";
  355. }
  356. } else {
  357. // now replace SELECT ... FROM with SELECT COUNT(*) FROM
  358. if ( strpos($sql, '_ADODB_COUNT') !== FALSE ) {
  359. $rewritesql = preg_replace('/^\s*?SELECT\s+_ADODB_COUNT(.*)_ADODB_COUNT\s/is','SELECT COUNT(*) ',$sql);
  360. } else {
  361. $rewritesql = preg_replace('/^\s*?SELECT\s.*?\s+(.*?)\s+FROM\s/is','SELECT COUNT(*) FROM ',$sql);
  362. }
  363. // fix by alexander zhukov, alex#unipack.ru, because count(*) and 'order by' fails
  364. // with mssql, access and postgresql. Also a good speedup optimization - skips sorting!
  365. // also see http://phplens.com/lens/lensforum/msgs.php?id=12752
  366. $rewritesql = adodb_strip_order_by($rewritesql);
  367. }
  368. if (isset($rewritesql) && $rewritesql != $sql) {
  369. if (preg_match('/\sLIMIT\s+[0-9]+/i',$sql,$limitarr)) $rewritesql .= $limitarr[0];
  370. if ($secs2cache) {
  371. // we only use half the time of secs2cache because the count can quickly
  372. // become inaccurate if new records are added
  373. $qryRecs = $zthis->CacheGetOne($secs2cache/2,$rewritesql,$inputarr);
  374. } else {
  375. $qryRecs = $zthis->GetOne($rewritesql,$inputarr);
  376. }
  377. if ($qryRecs !== false) return $qryRecs;
  378. }
  379. //--------------------------------------------
  380. // query rewrite failed - so try slower way...
  381. // strip off unneeded ORDER BY if no UNION
  382. if (preg_match('/\s*UNION\s*/is', $sql)) $rewritesql = $sql;
  383. else $rewritesql = $rewritesql = adodb_strip_order_by($sql);
  384. if (preg_match('/\sLIMIT\s+[0-9]+/i',$sql,$limitarr)) $rewritesql .= $limitarr[0];
  385. if ($secs2cache) {
  386. $rstest = $zthis->CacheExecute($secs2cache,$rewritesql,$inputarr);
  387. if (!$rstest) $rstest = $zthis->CacheExecute($secs2cache,$sql,$inputarr);
  388. } else {
  389. $rstest = $zthis->Execute($rewritesql,$inputarr);
  390. if (!$rstest) $rstest = $zthis->Execute($sql,$inputarr);
  391. }
  392. if ($rstest) {
  393. $qryRecs = $rstest->RecordCount();
  394. if ($qryRecs == -1) {
  395. global $ADODB_EXTENSION;
  396. // some databases will return -1 on MoveLast() - change to MoveNext()
  397. if ($ADODB_EXTENSION) {
  398. while(!$rstest->EOF) {
  399. adodb_movenext($rstest);
  400. }
  401. } else {
  402. while(!$rstest->EOF) {
  403. $rstest->MoveNext();
  404. }
  405. }
  406. $qryRecs = $rstest->_currentRow;
  407. }
  408. $rstest->Close();
  409. if ($qryRecs == -1) return 0;
  410. }
  411. return $qryRecs;
  412. }
  413. /*
  414. Code originally from "Cornel G" <conyg@fx.ro>
  415. This code might not work with SQL that has UNION in it
  416. Also if you are using CachePageExecute(), there is a strong possibility that
  417. data will get out of synch. use CachePageExecute() only with tables that
  418. rarely change.
  419. */
  420. function _adodb_pageexecute_all_rows(&$zthis, $sql, $nrows, $page,
  421. $inputarr=false, $secs2cache=0)
  422. {
  423. $atfirstpage = false;
  424. $atlastpage = false;
  425. $lastpageno=1;
  426. // If an invalid nrows is supplied,
  427. // we assume a default value of 10 rows per page
  428. if (!isset($nrows) || $nrows <= 0) $nrows = 10;
  429. $qryRecs = false; //count records for no offset
  430. $qryRecs = _adodb_getcount($zthis,$sql,$inputarr,$secs2cache);
  431. $lastpageno = (int) ceil($qryRecs / $nrows);
  432. $zthis->_maxRecordCount = $qryRecs;
  433. // ***** Here we check whether $page is the last page or
  434. // whether we are trying to retrieve
  435. // a page number greater than the last page number.
  436. if ($page >= $lastpageno) {
  437. $page = $lastpageno;
  438. $atlastpage = true;
  439. }
  440. // If page number <= 1, then we are at the first page
  441. if (empty($page) || $page <= 1) {
  442. $page = 1;
  443. $atfirstpage = true;
  444. }
  445. // We get the data we want
  446. $offset = $nrows * ($page-1);
  447. if ($secs2cache > 0)
  448. $rsreturn = $zthis->CacheSelectLimit($secs2cache, $sql, $nrows, $offset, $inputarr);
  449. else
  450. $rsreturn = $zthis->SelectLimit($sql, $nrows, $offset, $inputarr, $secs2cache);
  451. // Before returning the RecordSet, we set the pagination properties we need
  452. if ($rsreturn) {
  453. $rsreturn->_maxRecordCount = $qryRecs;
  454. $rsreturn->rowsPerPage = $nrows;
  455. $rsreturn->AbsolutePage($page);
  456. $rsreturn->AtFirstPage($atfirstpage);
  457. $rsreturn->AtLastPage($atlastpage);
  458. $rsreturn->LastPageNo($lastpageno);
  459. }
  460. return $rsreturn;
  461. }
  462. // Iván Oliva version
  463. function _adodb_pageexecute_no_last_page(&$zthis, $sql, $nrows, $page, $inputarr=false, $secs2cache=0)
  464. {
  465. $atfirstpage = false;
  466. $atlastpage = false;
  467. if (!isset($page) || $page <= 1) {
  468. // If page number <= 1, then we are at the first page
  469. $page = 1;
  470. $atfirstpage = true;
  471. }
  472. if ($nrows <= 0) {
  473. // If an invalid nrows is supplied, we assume a default value of 10 rows per page
  474. $nrows = 10;
  475. }
  476. $pagecounteroffset = ($page * $nrows) - $nrows;
  477. // To find out if there are more pages of rows, simply increase the limit or
  478. // nrows by 1 and see if that number of records was returned. If it was,
  479. // then we know there is at least one more page left, otherwise we are on
  480. // the last page. Therefore allow non-Count() paging with single queries
  481. // rather than three queries as was done before.
  482. $test_nrows = $nrows + 1;
  483. if ($secs2cache > 0) {
  484. $rsreturn = $zthis->CacheSelectLimit($secs2cache, $sql, $nrows, $pagecounteroffset, $inputarr);
  485. } else {
  486. $rsreturn = $zthis->SelectLimit($sql, $test_nrows, $pagecounteroffset, $inputarr, $secs2cache);
  487. }
  488. // Now check to see if the number of rows returned was the higher value we asked for or not.
  489. if ( $rsreturn->_numOfRows == $test_nrows ) {
  490. // Still at least 1 more row, so we are not on last page yet...
  491. // Remove the last row from the RS.
  492. $rsreturn->_numOfRows = ( $rsreturn->_numOfRows - 1 );
  493. } elseif ( $rsreturn->_numOfRows == 0 && $page > 1 ) {
  494. // Likely requested a page that doesn't exist, so need to find the last
  495. // page and return it. Revert to original method and loop through pages
  496. // until we find some data...
  497. $pagecounter = $page + 1;
  498. $pagecounteroffset = ($pagecounter * $nrows) - $nrows;
  499. $rstest = $rsreturn;
  500. if ($rstest) {
  501. while ($rstest && $rstest->EOF && $pagecounter > 0) {
  502. $atlastpage = true;
  503. $pagecounter--;
  504. $pagecounteroffset = $nrows * ($pagecounter - 1);
  505. $rstest->Close();
  506. if ($secs2cache>0) {
  507. $rstest = $zthis->CacheSelectLimit($secs2cache, $sql, $nrows, $pagecounteroffset, $inputarr);
  508. }
  509. else {
  510. $rstest = $zthis->SelectLimit($sql, $nrows, $pagecounteroffset, $inputarr, $secs2cache);
  511. }
  512. }
  513. if ($rstest) $rstest->Close();
  514. }
  515. if ($atlastpage) {
  516. // If we are at the last page or beyond it, we are going to retrieve it
  517. $page = $pagecounter;
  518. if ($page == 1) {
  519. // We have to do this again in case the last page is the same as
  520. // the first page, that is, the recordset has only 1 page.
  521. $atfirstpage = true;
  522. }
  523. }
  524. // We get the data we want
  525. $offset = $nrows * ($page-1);
  526. if ($secs2cache > 0) {
  527. $rsreturn = $zthis->CacheSelectLimit($secs2cache, $sql, $nrows, $offset, $inputarr);
  528. }
  529. else {
  530. $rsreturn = $zthis->SelectLimit($sql, $nrows, $offset, $inputarr, $secs2cache);
  531. }
  532. } elseif ( $rsreturn->_numOfRows < $test_nrows ) {
  533. // Rows is less than what we asked for, so must be at the last page.
  534. $atlastpage = true;
  535. }
  536. // Before returning the RecordSet, we set the pagination properties we need
  537. if ($rsreturn) {
  538. $rsreturn->rowsPerPage = $nrows;
  539. $rsreturn->AbsolutePage($page);
  540. $rsreturn->AtFirstPage($atfirstpage);
  541. $rsreturn->AtLastPage($atlastpage);
  542. }
  543. return $rsreturn;
  544. }
  545. function _adodb_getupdatesql(&$zthis,&$rs, $arrFields,$forceUpdate=false,$magicq=false,$force=2)
  546. {
  547. global $ADODB_QUOTE_FIELDNAMES;
  548. if (!$rs) {
  549. printf(ADODB_BAD_RS,'GetUpdateSQL');
  550. return false;
  551. }
  552. $fieldUpdatedCount = 0;
  553. $arrFields = _array_change_key_case($arrFields);
  554. $hasnumeric = isset($rs->fields[0]);
  555. $setFields = '';
  556. // Loop through all of the fields in the recordset
  557. for ($i=0, $max=$rs->FieldCount(); $i < $max; $i++) {
  558. // Get the field from the recordset
  559. $field = $rs->FetchField($i);
  560. // If the recordset field is one
  561. // of the fields passed in then process.
  562. $upperfname = strtoupper($field->name);
  563. if (adodb_key_exists($upperfname,$arrFields,$force)) {
  564. // If the existing field value in the recordset
  565. // is different from the value passed in then
  566. // go ahead and append the field name and new value to
  567. // the update query.
  568. if ($hasnumeric) $val = $rs->fields[$i];
  569. else if (isset($rs->fields[$upperfname])) $val = $rs->fields[$upperfname];
  570. else if (isset($rs->fields[$field->name])) $val = $rs->fields[$field->name];
  571. else if (isset($rs->fields[strtolower($upperfname)])) $val = $rs->fields[strtolower($upperfname)];
  572. else $val = '';
  573. if ($forceUpdate || strcmp($val, $arrFields[$upperfname])) {
  574. // Set the counter for the number of fields that will be updated.
  575. $fieldUpdatedCount++;
  576. // Based on the datatype of the field
  577. // Format the value properly for the database
  578. $type = $rs->MetaType($field->type);
  579. if ($type == 'null') {
  580. $type = 'C';
  581. }
  582. if ((strpos($upperfname,' ') !== false) || ($ADODB_QUOTE_FIELDNAMES)) {
  583. switch ($ADODB_QUOTE_FIELDNAMES) {
  584. case 'LOWER':
  585. $fnameq = $zthis->nameQuote.strtolower($field->name).$zthis->nameQuote;break;
  586. case 'NATIVE':
  587. $fnameq = $zthis->nameQuote.$field->name.$zthis->nameQuote;break;
  588. case 'UPPER':
  589. default:
  590. $fnameq = $zthis->nameQuote.$upperfname.$zthis->nameQuote;break;
  591. }
  592. } else
  593. $fnameq = $upperfname;
  594. //********************************************************//
  595. if (is_null($arrFields[$upperfname])
  596. || (empty($arrFields[$upperfname]) && strlen($arrFields[$upperfname]) == 0)
  597. || $arrFields[$upperfname] === $zthis->null2null
  598. )
  599. {
  600. switch ($force) {
  601. //case 0:
  602. // //Ignore empty values. This is allready handled in "adodb_key_exists" function.
  603. //break;
  604. case 1:
  605. //Set null
  606. $setFields .= $field->name . " = null, ";
  607. break;
  608. case 2:
  609. //Set empty
  610. $arrFields[$upperfname] = "";
  611. $setFields .= _adodb_column_sql($zthis, 'U', $type, $upperfname, $fnameq,$arrFields, $magicq);
  612. break;
  613. default:
  614. case 3:
  615. //Set the value that was given in array, so you can give both null and empty values
  616. if (is_null($arrFields[$upperfname]) || $arrFields[$upperfname] === $zthis->null2null) {
  617. $setFields .= $field->name . " = null, ";
  618. } else {
  619. $setFields .= _adodb_column_sql($zthis, 'U', $type, $upperfname, $fnameq,$arrFields, $magicq);
  620. }
  621. break;
  622. }
  623. //********************************************************//
  624. } else {
  625. //we do this so each driver can customize the sql for
  626. //DB specific column types.
  627. //Oracle needs BLOB types to be handled with a returning clause
  628. //postgres has special needs as well
  629. $setFields .= _adodb_column_sql($zthis, 'U', $type, $upperfname, $fnameq,
  630. $arrFields, $magicq);
  631. }
  632. }
  633. }
  634. }
  635. // If there were any modified fields then build the rest of the update query.
  636. if ($fieldUpdatedCount > 0 || $forceUpdate) {
  637. // Get the table name from the existing query.
  638. if (!empty($rs->tableName)) $tableName = $rs->tableName;
  639. else {
  640. preg_match("/FROM\s+".ADODB_TABLE_REGEX."/is", $rs->sql, $tableName);
  641. $tableName = $tableName[1];
  642. }
  643. // Get the full where clause excluding the word "WHERE" from
  644. // the existing query.
  645. preg_match('/\sWHERE\s(.*)/is', $rs->sql, $whereClause);
  646. $discard = false;
  647. // not a good hack, improvements?
  648. if ($whereClause) {
  649. #var_dump($whereClause);
  650. if (preg_match('/\s(ORDER\s.*)/is', $whereClause[1], $discard));
  651. else if (preg_match('/\s(LIMIT\s.*)/is', $whereClause[1], $discard));
  652. else if (preg_match('/\s(FOR UPDATE.*)/is', $whereClause[1], $discard));
  653. else preg_match('/\s.*(\) WHERE .*)/is', $whereClause[1], $discard); # see http://sourceforge.net/tracker/index.php?func=detail&aid=1379638&group_id=42718&atid=433976
  654. } else
  655. $whereClause = array(false,false);
  656. if ($discard)
  657. $whereClause[1] = substr($whereClause[1], 0, strlen($whereClause[1]) - strlen($discard[1]));
  658. $sql = 'UPDATE '.$tableName.' SET '.substr($setFields, 0, -2);
  659. if (strlen($whereClause[1]) > 0)
  660. $sql .= ' WHERE '.$whereClause[1];
  661. return $sql;
  662. } else {
  663. return false;
  664. }
  665. }
  666. function adodb_key_exists($key, &$arr,$force=2)
  667. {
  668. if ($force<=0) {
  669. // the following is the old behaviour where null or empty fields are ignored
  670. return (!empty($arr[$key])) || (isset($arr[$key]) && strlen($arr[$key])>0);
  671. }
  672. if (isset($arr[$key])) return true;
  673. ## null check below
  674. if (ADODB_PHPVER >= 0x4010) return array_key_exists($key,$arr);
  675. return false;
  676. }
  677. /**
  678. * There is a special case of this function for the oci8 driver.
  679. * The proper way to handle an insert w/ a blob in oracle requires
  680. * a returning clause with bind variables and a descriptor blob.
  681. *
  682. *
  683. */
  684. function _adodb_getinsertsql(&$zthis,&$rs,$arrFields,$magicq=false,$force=2)
  685. {
  686. static $cacheRS = false;
  687. static $cacheSig = 0;
  688. static $cacheCols;
  689. global $ADODB_QUOTE_FIELDNAMES;
  690. $tableName = '';
  691. $values = '';
  692. $fields = '';
  693. $recordSet = null;
  694. $arrFields = _array_change_key_case($arrFields);
  695. $fieldInsertedCount = 0;
  696. if (is_string($rs)) {
  697. //ok we have a table name
  698. //try and get the column info ourself.
  699. $tableName = $rs;
  700. //we need an object for the recordSet
  701. //because we have to call MetaType.
  702. //php can't do a $rsclass::MetaType()
  703. $rsclass = $zthis->rsPrefix.$zthis->databaseType;
  704. $recordSet = new $rsclass(-1,$zthis->fetchMode);
  705. $recordSet->connection = $zthis;
  706. if (is_string($cacheRS) && $cacheRS == $rs) {
  707. $columns = $cacheCols;
  708. } else {
  709. $columns = $zthis->MetaColumns( $tableName );
  710. $cacheRS = $tableName;
  711. $cacheCols = $columns;
  712. }
  713. } else if (is_subclass_of($rs, 'adorecordset')) {
  714. if (isset($rs->insertSig) && is_integer($cacheRS) && $cacheRS == $rs->insertSig) {
  715. $columns = $cacheCols;
  716. } else {
  717. for ($i=0, $max=$rs->FieldCount(); $i < $max; $i++)
  718. $columns[] = $rs->FetchField($i);
  719. $cacheRS = $cacheSig;
  720. $cacheCols = $columns;
  721. $rs->insertSig = $cacheSig++;
  722. }
  723. $recordSet = $rs;
  724. } else {
  725. printf(ADODB_BAD_RS,'GetInsertSQL');
  726. return false;
  727. }
  728. // Loop through all of the fields in the recordset
  729. foreach( $columns as $field ) {
  730. $upperfname = strtoupper($field->name);
  731. if (adodb_key_exists($upperfname,$arrFields,$force)) {
  732. $bad = false;
  733. if ((strpos($upperfname,' ') !== false) || ($ADODB_QUOTE_FIELDNAMES)) {
  734. switch ($ADODB_QUOTE_FIELDNAMES) {
  735. case 'LOWER':
  736. $fnameq = $zthis->nameQuote.strtolower($field->name).$zthis->nameQuote;break;
  737. case 'NATIVE':
  738. $fnameq = $zthis->nameQuote.$field->name.$zthis->nameQuote;break;
  739. case 'UPPER':
  740. default:
  741. $fnameq = $zthis->nameQuote.$upperfname.$zthis->nameQuote;break;
  742. }
  743. } else
  744. $fnameq = $upperfname;
  745. $type = $recordSet->MetaType($field->type);
  746. /********************************************************/
  747. if (is_null($arrFields[$upperfname])
  748. || (empty($arrFields[$upperfname]) && strlen($arrFields[$upperfname]) == 0)
  749. || $arrFields[$upperfname] === $zthis->null2null
  750. )
  751. {
  752. switch ($force) {
  753. case 0: // we must always set null if missing
  754. $bad = true;
  755. break;
  756. case 1:
  757. $values .= "null, ";
  758. break;
  759. case 2:
  760. //Set empty
  761. $arrFields[$upperfname] = "";
  762. $values .= _adodb_column_sql($zthis, 'I', $type, $upperfname, $fnameq,$arrFields, $magicq);
  763. break;
  764. default:
  765. case 3:
  766. //Set the value that was given in array, so you can give both null and empty values
  767. if (is_null($arrFields[$upperfname]) || $arrFields[$upperfname] === $zthis->null2null) {
  768. $values .= "null, ";
  769. } else {
  770. $values .= _adodb_column_sql($zthis, 'I', $type, $upperfname, $fnameq, $arrFields, $magicq);
  771. }
  772. break;
  773. } // switch
  774. /*********************************************************/
  775. } else {
  776. //we do this so each driver can customize the sql for
  777. //DB specific column types.
  778. //Oracle needs BLOB types to be handled with a returning clause
  779. //postgres has special needs as well
  780. $values .= _adodb_column_sql($zthis, 'I', $type, $upperfname, $fnameq,
  781. $arrFields, $magicq);
  782. }
  783. if ($bad) continue;
  784. // Set the counter for the number of fields that will be inserted.
  785. $fieldInsertedCount++;
  786. // Get the name of the fields to insert
  787. $fields .= $fnameq . ", ";
  788. }
  789. }
  790. // If there were any inserted fields then build the rest of the insert query.
  791. if ($fieldInsertedCount <= 0) return false;
  792. // Get the table name from the existing query.
  793. if (!$tableName) {
  794. if (!empty($rs->tableName)) $tableName = $rs->tableName;
  795. else if (preg_match("/FROM\s+".ADODB_TABLE_REGEX."/is", $rs->sql, $tableName))
  796. $tableName = $tableName[1];
  797. else
  798. return false;
  799. }
  800. // Strip off the comma and space on the end of both the fields
  801. // and their values.
  802. $fields = substr($fields, 0, -2);
  803. $values = substr($values, 0, -2);
  804. // Append the fields and their values to the insert query.
  805. return 'INSERT INTO '.$tableName.' ( '.$fields.' ) VALUES ( '.$values.' )';
  806. }
  807. /**
  808. * This private method is used to help construct
  809. * the update/sql which is generated by GetInsertSQL and GetUpdateSQL.
  810. * It handles the string construction of 1 column -> sql string based on
  811. * the column type. We want to do 'safe' handling of BLOBs
  812. *
  813. * @param string the type of sql we are trying to create
  814. * 'I' or 'U'.
  815. * @param string column data type from the db::MetaType() method
  816. * @param string the column name
  817. * @param array the column value
  818. *
  819. * @return string
  820. *
  821. */
  822. function _adodb_column_sql_oci8(&$zthis,$action, $type, $fname, $fnameq, $arrFields, $magicq)
  823. {
  824. $sql = '';
  825. // Based on the datatype of the field
  826. // Format the value properly for the database
  827. switch($type) {
  828. case 'B':
  829. //in order to handle Blobs correctly, we need
  830. //to do some magic for Oracle
  831. //we need to create a new descriptor to handle
  832. //this properly
  833. if (!empty($zthis->hasReturningInto)) {
  834. if ($action == 'I') {
  835. $sql = 'empty_blob(), ';
  836. } else {
  837. $sql = $fnameq. '=empty_blob(), ';
  838. }
  839. //add the variable to the returning clause array
  840. //so the user can build this later in
  841. //case they want to add more to it
  842. $zthis->_returningArray[$fname] = ':xx'.$fname.'xx';
  843. } else if (empty($arrFields[$fname])){
  844. if ($action == 'I') {
  845. $sql = 'empty_blob(), ';
  846. } else {
  847. $sql = $fnameq. '=empty_blob(), ';
  848. }
  849. } else {
  850. //this is to maintain compatibility
  851. //with older adodb versions.
  852. $sql = _adodb_column_sql($zthis, $action, $type, $fname, $fnameq, $arrFields, $magicq,false);
  853. }
  854. break;
  855. case "X":
  856. //we need to do some more magic here for long variables
  857. //to handle these correctly in oracle.
  858. //create a safe bind var name
  859. //to avoid conflicts w/ dupes.
  860. if (!empty($zthis->hasReturningInto)) {
  861. if ($action == 'I') {
  862. $sql = ':xx'.$fname.'xx, ';
  863. } else {
  864. $sql = $fnameq.'=:xx'.$fname.'xx, ';
  865. }
  866. //add the variable to the returning clause array
  867. //so the user can build this later in
  868. //case they want to add more to it
  869. $zthis->_returningArray[$fname] = ':xx'.$fname.'xx';
  870. } else {
  871. //this is to maintain compatibility
  872. //with older adodb versions.
  873. $sql = _adodb_column_sql($zthis, $action, $type, $fname, $fnameq, $arrFields, $magicq,false);
  874. }
  875. break;
  876. default:
  877. $sql = _adodb_column_sql($zthis, $action, $type, $fname, $fnameq, $arrFields, $magicq,false);
  878. break;
  879. }
  880. return $sql;
  881. }
  882. function _adodb_column_sql(&$zthis, $action, $type, $fname, $fnameq, $arrFields, $magicq, $recurse=true)
  883. {
  884. if ($recurse) {
  885. switch($zthis->dataProvider) {
  886. case 'postgres':
  887. if ($type == 'L') $type = 'C';
  888. break;
  889. case 'oci8':
  890. return _adodb_column_sql_oci8($zthis, $action, $type, $fname, $fnameq, $arrFields, $magicq);
  891. }
  892. }
  893. switch($type) {
  894. case "C":
  895. case "X":
  896. case 'B':
  897. $val = $zthis->qstr($arrFields[$fname],$magicq);
  898. break;
  899. case "D":
  900. $val = $zthis->DBDate($arrFields[$fname]);
  901. break;
  902. case "T":
  903. $val = $zthis->DBTimeStamp($arrFields[$fname]);
  904. break;
  905. case "N":
  906. $val = $arrFields[$fname];
  907. if (!is_numeric($val)) $val = str_replace(',', '.', (float)$val);
  908. break;
  909. case "I":
  910. case "R":
  911. $val = $arrFields[$fname];
  912. if (!is_numeric($val)) $val = (integer) $val;
  913. break;
  914. default:
  915. $val = str_replace(array("'"," ","("),"",$arrFields[$fname]); // basic sql injection defence
  916. if (empty($val)) $val = '0';
  917. break;
  918. }
  919. if ($action == 'I') return $val . ", ";
  920. return $fnameq . "=" . $val . ", ";
  921. }
  922. function _adodb_debug_execute(&$zthis, $sql, $inputarr)
  923. {
  924. $ss = '';
  925. if ($inputarr) {
  926. foreach($inputarr as $kk=>$vv) {
  927. if (is_string($vv) && strlen($vv)>64) $vv = substr($vv,0,64).'...';
  928. if (is_null($vv)) $ss .= "($kk=>null) ";
  929. else $ss .= "($kk=>'$vv') ";
  930. }
  931. $ss = "[ $ss ]";
  932. }
  933. $sqlTxt = is_array($sql) ? $sql[0] : $sql;
  934. /*str_replace(', ','##1#__^LF',is_array($sql) ? $sql[0] : $sql);
  935. $sqlTxt = str_replace(',',', ',$sqlTxt);
  936. $sqlTxt = str_replace('##1#__^LF', ', ' ,$sqlTxt);
  937. */
  938. // check if running from browser or command-line
  939. $inBrowser = isset($_SERVER['HTTP_USER_AGENT']);
  940. $dbt = $zthis->databaseType;
  941. if (isset($zthis->dsnType)) $dbt .= '-'.$zthis->dsnType;
  942. if ($inBrowser) {
  943. if ($ss) {
  944. $ss = '<code>'.htmlspecialchars($ss).'</code>';
  945. }
  946. if ($zthis->debug === -1)
  947. ADOConnection::outp( "<br>\n($dbt): ".htmlspecialchars($sqlTxt)." &nbsp; $ss\n<br>\n",false);
  948. else if ($zthis->debug !== -99)
  949. ADOConnection::outp( "<hr>\n($dbt): ".htmlspecialchars($sqlTxt)." &nbsp; $ss\n<hr>\n",false);
  950. } else {
  951. $ss = "\n ".$ss;
  952. if ($zthis->debug !== -99)
  953. ADOConnection::outp("-----<hr>\n($dbt): ".$sqlTxt." $ss\n-----<hr>\n",false);
  954. }
  955. $qID = $zthis->_query($sql,$inputarr);
  956. /*
  957. Alexios Fakios notes that ErrorMsg() must be called before ErrorNo() for mssql
  958. because ErrorNo() calls Execute('SELECT @ERROR'), causing recursion
  959. */
  960. if ($zthis->databaseType == 'mssql') {
  961. // ErrorNo is a slow function call in mssql, and not reliable in PHP 4.0.6
  962. if($emsg = $zthis->ErrorMsg()) {
  963. if ($err = $zthis->ErrorNo()) {
  964. if ($zthis->debug === -99)
  965. ADOConnection::outp( "<hr>\n($dbt): ".htmlspecialchars($sqlTxt)." &nbsp; $ss\n<hr>\n",false);
  966. ADOConnection::outp($err.': '.$emsg);
  967. }
  968. }
  969. } else if (!$qID) {
  970. if ($zthis->debug === -99)
  971. if ($inBrowser) ADOConnection::outp( "<hr>\n($dbt): ".htmlspecialchars($sqlTxt)." &nbsp; $ss\n<hr>\n",false);
  972. else ADOConnection::outp("-----<hr>\n($dbt): ".$sqlTxt."$ss\n-----<hr>\n",false);
  973. ADOConnection::outp($zthis->ErrorNo() .': '. $zthis->ErrorMsg());
  974. }
  975. if ($zthis->debug === 99) _adodb_backtrace(true,9999,2);
  976. return $qID;
  977. }
  978. # pretty print the debug_backtrace function
  979. function _adodb_backtrace($printOrArr=true,$levels=9999,$skippy=0,$ishtml=null)
  980. {
  981. if (!function_exists('debug_backtrace')) return '';
  982. if ($ishtml === null) $html = (isset($_SERVER['HTTP_USER_AGENT']));
  983. else $html = $ishtml;
  984. $fmt = ($html) ? "</font><font color=#808080 size=-1> %% line %4d, file: <a href=\"file:/%s\">%s</a></font>" : "%% line %4d, file: %s";
  985. $MAXSTRLEN = 128;
  986. $s = ($html) ? '<pre align=left>' : '';
  987. if (is_array($printOrArr)) $traceArr = $printOrArr;
  988. else $traceArr = debug_backtrace();
  989. array_shift($traceArr);
  990. array_shift($traceArr);
  991. $tabs = sizeof($traceArr)-2;
  992. foreach ($traceArr as $arr) {
  993. if ($skippy) {$skippy -= 1; continue;}
  994. $levels -= 1;
  995. if ($levels < 0) break;
  996. $args = array();
  997. for ($i=0; $i < $tabs; $i++) $s .= ($html) ? ' &nbsp; ' : "\t";
  998. $tabs -= 1;
  999. if ($html) $s .= '<font face="Courier New,Courier">';
  1000. if (isset($arr['class'])) $s .= $arr['class'].'.';
  1001. if (isset($arr['args']))
  1002. foreach($arr['args'] as $v) {
  1003. if (is_null($v)) $args[] = 'null';
  1004. else if (is_array($v)) $args[] = 'Array['.sizeof($v).']';
  1005. else if (is_object($v)) $args[] = 'Object:'.get_class($v);
  1006. else if (is_bool($v)) $args[] = $v ? 'true' : 'false';
  1007. else {
  1008. $v = (string) @$v;
  1009. $str = htmlspecialchars(str_replace(array("\r","\n"),' ',substr($v,0,$MAXSTRLEN)));
  1010. if (strlen($v) > $MAXSTRLEN) $str .= '...';
  1011. $args[] = $str;
  1012. }
  1013. }
  1014. $s .= $arr['function'].'('.implode(', ',$args).')';
  1015. $s .= @sprintf($fmt, $arr['line'],$arr['file'],basename($arr['file']));
  1016. $s .= "\n";
  1017. }
  1018. if ($html) $s .= '</pre>';
  1019. if ($printOrArr) print $s;
  1020. return $s;
  1021. }
  1022. /*
  1023. function _adodb_find_from($sql)
  1024. {
  1025. $sql = str_replace(array("\n","\r"), ' ', $sql);
  1026. $charCount = strlen($sql);
  1027. $inString = false;
  1028. $quote = '';
  1029. $parentheseCount = 0;
  1030. $prevChars = '';
  1031. $nextChars = '';
  1032. for($i = 0; $i < $charCount; $i++) {
  1033. $char = substr($sql,$i,1);
  1034. $prevChars = substr($sql,0,$i);
  1035. $nextChars = substr($sql,$i+1);
  1036. if((($char == "'" || $char == '"' || $char == '`') && substr($prevChars,-1,1) != '\\') && $inString === false) {
  1037. $quote = $char;
  1038. $inString = true;
  1039. }
  1040. elseif((($char == "'" || $char == '"' || $char == '`') && substr($prevChars,-1,1) != '\\') && $inString === true && $quote == $char) {
  1041. $quote = "";
  1042. $inString = false;
  1043. }
  1044. elseif($char == "(" && $inString === false)
  1045. $parentheseCount++;
  1046. elseif($char == ")" && $inString === false && $parentheseCount > 0)
  1047. $parentheseCount--;
  1048. elseif($parentheseCount <= 0 && $inString === false && $char == " " && strtoupper(substr($prevChars,-5,5)) == " FROM")
  1049. return $i;
  1050. }
  1051. }
  1052. */