PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/php/extlib/adodb5/drivers/adodb-pdo_mysql.inc.php

http://github.com/movabletype/movabletype
PHP | 313 lines | 259 code | 31 blank | 23 comment | 33 complexity | 6a694bebc1380a77a0f5cb76bf3b4adc MD5 | raw file
Possible License(s): LGPL-2.1, MIT, BSD-3-Clause
  1. <?php
  2. /*
  3. @version v5.20.14 06-Jan-2019
  4. @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). 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 8.
  10. */
  11. class ADODB_pdo_mysql extends ADODB_pdo {
  12. var $metaTablesSQL = "SELECT
  13. TABLE_NAME,
  14. CASE WHEN TABLE_TYPE = 'VIEW' THEN 'V' ELSE 'T' END
  15. FROM INFORMATION_SCHEMA.TABLES
  16. WHERE TABLE_SCHEMA=";
  17. var $metaColumnsSQL = "SHOW COLUMNS FROM `%s`";
  18. var $sysDate = 'CURDATE()';
  19. var $sysTimeStamp = 'NOW()';
  20. var $hasGenID = true;
  21. var $_genIDSQL = "update %s set id=LAST_INSERT_ID(id+1);";
  22. var $_dropSeqSQL = "drop table %s";
  23. var $fmtTimeStamp = "'Y-m-d, H:i:s'";
  24. var $nameQuote = '`';
  25. function _init($parentDriver)
  26. {
  27. $parentDriver->hasTransactions = false;
  28. #$parentDriver->_bindInputArray = false;
  29. $parentDriver->hasInsertID = true;
  30. $parentDriver->_connectionID->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
  31. }
  32. // dayFraction is a day in floating point
  33. function OffsetDate($dayFraction, $date=false)
  34. {
  35. if (!$date) {
  36. $date = $this->sysDate;
  37. }
  38. $fraction = $dayFraction * 24 * 3600;
  39. return $date . ' + INTERVAL ' . $fraction . ' SECOND';
  40. // return "from_unixtime(unix_timestamp($date)+$fraction)";
  41. }
  42. function Concat()
  43. {
  44. $s = '';
  45. $arr = func_get_args();
  46. // suggestion by andrew005#mnogo.ru
  47. $s = implode(',', $arr);
  48. if (strlen($s) > 0) {
  49. return "CONCAT($s)";
  50. }
  51. return '';
  52. }
  53. function ServerInfo()
  54. {
  55. $arr['description'] = ADOConnection::GetOne('select version()');
  56. $arr['version'] = ADOConnection::_findvers($arr['description']);
  57. return $arr;
  58. }
  59. function MetaTables($ttype=false, $showSchema=false, $mask=false)
  60. {
  61. $save = $this->metaTablesSQL;
  62. if ($showSchema && is_string($showSchema)) {
  63. $this->metaTablesSQL .= $this->qstr($showSchema);
  64. } else {
  65. $this->metaTablesSQL .= 'schema()';
  66. }
  67. if ($mask) {
  68. $mask = $this->qstr($mask);
  69. $this->metaTablesSQL .= " like $mask";
  70. }
  71. $ret = ADOConnection::MetaTables($ttype, $showSchema);
  72. $this->metaTablesSQL = $save;
  73. return $ret;
  74. }
  75. /**
  76. * @param bool $auto_commit
  77. * @return void
  78. */
  79. function SetAutoCommit($auto_commit)
  80. {
  81. $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT, $auto_commit);
  82. }
  83. function SetTransactionMode($transaction_mode)
  84. {
  85. $this->_transmode = $transaction_mode;
  86. if (empty($transaction_mode)) {
  87. $this->Execute('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ');
  88. return;
  89. }
  90. if (!stristr($transaction_mode, 'isolation')) {
  91. $transaction_mode = 'ISOLATION LEVEL ' . $transaction_mode;
  92. }
  93. $this->Execute('SET SESSION TRANSACTION ' . $transaction_mode);
  94. }
  95. function MetaColumns($table, $normalize=true)
  96. {
  97. $this->_findschema($table, $schema);
  98. if ($schema) {
  99. $dbName = $this->database;
  100. $this->SelectDB($schema);
  101. }
  102. global $ADODB_FETCH_MODE;
  103. $save = $ADODB_FETCH_MODE;
  104. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  105. if ($this->fetchMode !== false) {
  106. $savem = $this->SetFetchMode(false);
  107. }
  108. $rs = $this->Execute(sprintf($this->metaColumnsSQL, $table));
  109. if ($schema) {
  110. $this->SelectDB($dbName);
  111. }
  112. if (isset($savem)) {
  113. $this->SetFetchMode($savem);
  114. }
  115. $ADODB_FETCH_MODE = $save;
  116. if (!is_object($rs)) {
  117. $false = false;
  118. return $false;
  119. }
  120. $retarr = array();
  121. while (!$rs->EOF){
  122. $fld = new ADOFieldObject();
  123. $fld->name = $rs->fields[0];
  124. $type = $rs->fields[1];
  125. // split type into type(length):
  126. $fld->scale = null;
  127. if (preg_match('/^(.+)\((\d+),(\d+)/', $type, $query_array)) {
  128. $fld->type = $query_array[1];
  129. $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
  130. $fld->scale = is_numeric($query_array[3]) ? $query_array[3] : -1;
  131. } elseif (preg_match('/^(.+)\((\d+)/', $type, $query_array)) {
  132. $fld->type = $query_array[1];
  133. $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
  134. } elseif (preg_match('/^(enum)\((.*)\)$/i', $type, $query_array)) {
  135. $fld->type = $query_array[1];
  136. $arr = explode(',', $query_array[2]);
  137. $fld->enums = $arr;
  138. $zlen = max(array_map('strlen', $arr)) - 2; // PHP >= 4.0.6
  139. $fld->max_length = ($zlen > 0) ? $zlen : 1;
  140. } else {
  141. $fld->type = $type;
  142. $fld->max_length = -1;
  143. }
  144. $fld->not_null = ($rs->fields[2] != 'YES');
  145. $fld->primary_key = ($rs->fields[3] == 'PRI');
  146. $fld->auto_increment = (strpos($rs->fields[5], 'auto_increment') !== false);
  147. $fld->binary = (strpos($type, 'blob') !== false);
  148. $fld->unsigned = (strpos($type, 'unsigned') !== false);
  149. if (!$fld->binary) {
  150. $d = $rs->fields[4];
  151. if ($d != '' && $d != 'NULL') {
  152. $fld->has_default = true;
  153. $fld->default_value = $d;
  154. } else {
  155. $fld->has_default = false;
  156. }
  157. }
  158. if ($save == ADODB_FETCH_NUM) {
  159. $retarr[] = $fld;
  160. } else {
  161. $retarr[strtoupper($fld->name)] = $fld;
  162. }
  163. $rs->MoveNext();
  164. }
  165. $rs->Close();
  166. return $retarr;
  167. }
  168. // returns true or false
  169. function SelectDB($dbName)
  170. {
  171. $this->database = $dbName;
  172. $this->databaseName = $dbName; # obsolete, retained for compat with older adodb versions
  173. $try = $this->Execute('use ' . $dbName);
  174. return ($try !== false);
  175. }
  176. // parameters use PostgreSQL convention, not MySQL
  177. function SelectLimit($sql, $nrows=-1, $offset=-1, $inputarr=false, $secs=0)
  178. {
  179. $nrows = (int) $nrows;
  180. $offset = (int) $offset;
  181. $offsetStr =($offset>=0) ? "$offset," : '';
  182. // jason judge, see http://phplens.com/lens/lensforum/msgs.php?id=9220
  183. if ($nrows < 0) {
  184. $nrows = '18446744073709551615';
  185. }
  186. if ($secs) {
  187. $rs = $this->CacheExecute($secs, $sql . " LIMIT $offsetStr$nrows", $inputarr);
  188. } else {
  189. $rs = $this->Execute($sql . " LIMIT $offsetStr$nrows", $inputarr);
  190. }
  191. return $rs;
  192. }
  193. function SQLDate($fmt, $col=false)
  194. {
  195. if (!$col) {
  196. $col = $this->sysTimeStamp;
  197. }
  198. $s = 'DATE_FORMAT(' . $col . ",'";
  199. $concat = false;
  200. $len = strlen($fmt);
  201. for ($i=0; $i < $len; $i++) {
  202. $ch = $fmt[$i];
  203. switch($ch) {
  204. default:
  205. if ($ch == '\\') {
  206. $i++;
  207. $ch = substr($fmt, $i, 1);
  208. }
  209. // FALL THROUGH
  210. case '-':
  211. case '/':
  212. $s .= $ch;
  213. break;
  214. case 'Y':
  215. case 'y':
  216. $s .= '%Y';
  217. break;
  218. case 'M':
  219. $s .= '%b';
  220. break;
  221. case 'm':
  222. $s .= '%m';
  223. break;
  224. case 'D':
  225. case 'd':
  226. $s .= '%d';
  227. break;
  228. case 'Q':
  229. case 'q':
  230. $s .= "'),Quarter($col)";
  231. if ($len > $i+1) {
  232. $s .= ",DATE_FORMAT($col,'";
  233. } else {
  234. $s .= ",('";
  235. }
  236. $concat = true;
  237. break;
  238. case 'H':
  239. $s .= '%H';
  240. break;
  241. case 'h':
  242. $s .= '%I';
  243. break;
  244. case 'i':
  245. $s .= '%i';
  246. break;
  247. case 's':
  248. $s .= '%s';
  249. break;
  250. case 'a':
  251. case 'A':
  252. $s .= '%p';
  253. break;
  254. case 'w':
  255. $s .= '%w';
  256. break;
  257. case 'W':
  258. $s .= '%U';
  259. break;
  260. case 'l':
  261. $s .= '%W';
  262. break;
  263. }
  264. }
  265. $s .= "')";
  266. if ($concat) {
  267. $s = "CONCAT($s)";
  268. }
  269. return $s;
  270. }
  271. }