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

https://bitbucket.org/yousef_fadila/vtiger · PHP · 157 lines · 124 code · 22 blank · 11 comment · 26 complexity · a2c7f6c87cd58e40559c24be98a05fe3 MD5 · raw file

  1. <?php
  2. /*
  3. V4.90 8 June 2006 (c) 2000-2006 John Lim (jlim#natsoft.com.my). 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 8.
  8. */
  9. class ADODB_pdo_mysql extends ADODB_pdo {
  10. var $metaTablesSQL = "SHOW TABLES";
  11. var $metaColumnsSQL = "SHOW COLUMNS FROM %s";
  12. var $_bindInputArray = false;
  13. var $sysDate = 'CURDATE()';
  14. var $sysTimeStamp = 'NOW()';
  15. function _init($parentDriver)
  16. {
  17. $parentDriver->hasTransactions = false;
  18. $parentDriver->_bindInputArray = true;
  19. $parentDriver->hasInsertID = true;
  20. $parentDriver->_connectionID->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
  21. }
  22. function ServerInfo()
  23. {
  24. $arr['description'] = ADOConnection::GetOne("select version()");
  25. $arr['version'] = ADOConnection::_findvers($arr['description']);
  26. return $arr;
  27. }
  28. function &MetaTables($ttype=false,$showSchema=false,$mask=false)
  29. {
  30. $save = $this->metaTablesSQL;
  31. if ($showSchema && is_string($showSchema)) {
  32. $this->metaTablesSQL .= " from $showSchema";
  33. }
  34. if ($mask) {
  35. $mask = $this->qstr($mask);
  36. $this->metaTablesSQL .= " like $mask";
  37. }
  38. $ret =& ADOConnection::MetaTables($ttype,$showSchema);
  39. $this->metaTablesSQL = $save;
  40. return $ret;
  41. }
  42. function SetTransactionMode( $transaction_mode )
  43. {
  44. $this->_transmode = $transaction_mode;
  45. if (empty($transaction_mode)) {
  46. $this->Execute('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ');
  47. return;
  48. }
  49. if (!stristr($transaction_mode,'isolation')) $transaction_mode = 'ISOLATION LEVEL '.$transaction_mode;
  50. $this->Execute("SET SESSION TRANSACTION ".$transaction_mode);
  51. }
  52. function &MetaColumns($table)
  53. {
  54. $this->_findschema($table,$schema);
  55. if ($schema) {
  56. $dbName = $this->database;
  57. $this->SelectDB($schema);
  58. }
  59. global $ADODB_FETCH_MODE;
  60. $save = $ADODB_FETCH_MODE;
  61. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  62. if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false);
  63. $rs = $this->Execute(sprintf($this->metaColumnsSQL,$table));
  64. if ($schema) {
  65. $this->SelectDB($dbName);
  66. }
  67. if (isset($savem)) $this->SetFetchMode($savem);
  68. $ADODB_FETCH_MODE = $save;
  69. if (!is_object($rs)) {
  70. $false = false;
  71. return $false;
  72. }
  73. $retarr = array();
  74. while (!$rs->EOF){
  75. $fld = new ADOFieldObject();
  76. $fld->name = $rs->fields[0];
  77. $type = $rs->fields[1];
  78. // split type into type(length):
  79. $fld->scale = null;
  80. if (preg_match("/^(.+)\((\d+),(\d+)/", $type, $query_array)) {
  81. $fld->type = $query_array[1];
  82. $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
  83. $fld->scale = is_numeric($query_array[3]) ? $query_array[3] : -1;
  84. } elseif (preg_match("/^(.+)\((\d+)/", $type, $query_array)) {
  85. $fld->type = $query_array[1];
  86. $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
  87. } elseif (preg_match("/^(enum)\((.*)\)$/i", $type, $query_array)) {
  88. $fld->type = $query_array[1];
  89. $arr = explode(",",$query_array[2]);
  90. $fld->enums = $arr;
  91. $zlen = max(array_map("strlen",$arr)) - 2; // PHP >= 4.0.6
  92. $fld->max_length = ($zlen > 0) ? $zlen : 1;
  93. } else {
  94. $fld->type = $type;
  95. $fld->max_length = -1;
  96. }
  97. $fld->not_null = ($rs->fields[2] != 'YES');
  98. $fld->primary_key = ($rs->fields[3] == 'PRI');
  99. $fld->auto_increment = (strpos($rs->fields[5], 'auto_increment') !== false);
  100. $fld->binary = (strpos($type,'blob') !== false);
  101. $fld->unsigned = (strpos($type,'unsigned') !== false);
  102. if (!$fld->binary) {
  103. $d = $rs->fields[4];
  104. if ($d != '' && $d != 'NULL') {
  105. $fld->has_default = true;
  106. $fld->default_value = $d;
  107. } else {
  108. $fld->has_default = false;
  109. }
  110. }
  111. if ($save == ADODB_FETCH_NUM) {
  112. $retarr[] = $fld;
  113. } else {
  114. $retarr[strtoupper($fld->name)] = $fld;
  115. }
  116. $rs->MoveNext();
  117. }
  118. $rs->Close();
  119. return $retarr;
  120. }
  121. // parameters use PostgreSQL convention, not MySQL
  122. function &SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs=0)
  123. {
  124. $offsetStr =($offset>=0) ? "$offset," : '';
  125. // jason judge, see http://phplens.com/lens/lensforum/msgs.php?id=9220
  126. if ($nrows < 0) $nrows = '18446744073709551615';
  127. if ($secs)
  128. $rs =& $this->CacheExecute($secs,$sql." LIMIT $offsetStr$nrows",$inputarr);
  129. else
  130. $rs =& $this->Execute($sql." LIMIT $offsetStr$nrows",$inputarr);
  131. return $rs;
  132. }
  133. }
  134. ?>