Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / lib / adodb / drivers / adodb-pdo_mysql.inc.php
blobc24c010519d13f7b9ca7c2907369f7180a759f74
1 <?php
4 /*
5 V4.98 13 Feb 2008 (c) 2000-2008 John Lim (jlim#natsoft.com.my). All rights reserved.
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.
11 */
13 class ADODB_pdo_mysql extends ADODB_pdo {
14 var $metaTablesSQL = "SHOW TABLES";
15 var $metaColumnsSQL = "SHOW COLUMNS FROM `%s`";
16 var $sysDate = 'CURDATE()';
17 var $sysTimeStamp = 'NOW()';
18 var $hasGenID = true;
19 var $_genIDSQL = "update %s set id=LAST_INSERT_ID(id+1);";
20 var $_dropSeqSQL = "drop table %s";
22 var $nameQuote = '`';
24 function _init($parentDriver)
27 $parentDriver->hasTransactions = false;
28 $parentDriver->_bindInputArray = false;
29 $parentDriver->hasInsertID = true;
30 $parentDriver->_connectionID->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
33 // dayFraction is a day in floating point
34 function OffsetDate($dayFraction,$date=false)
36 if (!$date) $date = $this->sysDate;
38 $fraction = $dayFraction * 24 * 3600;
39 return $date . ' + INTERVAL ' . $fraction.' SECOND';
41 // return "from_unixtime(unix_timestamp($date)+$fraction)";
44 function ServerInfo()
46 $arr['description'] = ADOConnection::GetOne("select version()");
47 $arr['version'] = ADOConnection::_findvers($arr['description']);
48 return $arr;
51 function &MetaTables($ttype=false,$showSchema=false,$mask=false)
53 $save = $this->metaTablesSQL;
54 if ($showSchema && is_string($showSchema)) {
55 $this->metaTablesSQL .= " from $showSchema";
58 if ($mask) {
59 $mask = $this->qstr($mask);
60 $this->metaTablesSQL .= " like $mask";
62 $ret =& ADOConnection::MetaTables($ttype,$showSchema);
64 $this->metaTablesSQL = $save;
65 return $ret;
68 function SetTransactionMode( $transaction_mode )
70 $this->_transmode = $transaction_mode;
71 if (empty($transaction_mode)) {
72 $this->Execute('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ');
73 return;
75 if (!stristr($transaction_mode,'isolation')) $transaction_mode = 'ISOLATION LEVEL '.$transaction_mode;
76 $this->Execute("SET SESSION TRANSACTION ".$transaction_mode);
79 function &MetaColumns($table)
81 $this->_findschema($table,$schema);
82 if ($schema) {
83 $dbName = $this->database;
84 $this->SelectDB($schema);
86 global $ADODB_FETCH_MODE;
87 $save = $ADODB_FETCH_MODE;
88 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
90 if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false);
91 $rs = $this->Execute(sprintf($this->metaColumnsSQL,$table));
93 if ($schema) {
94 $this->SelectDB($dbName);
97 if (isset($savem)) $this->SetFetchMode($savem);
98 $ADODB_FETCH_MODE = $save;
99 if (!is_object($rs)) {
100 $false = false;
101 return $false;
104 $retarr = array();
105 while (!$rs->EOF){
106 $fld = new ADOFieldObject();
107 $fld->name = $rs->fields[0];
108 $type = $rs->fields[1];
110 // split type into type(length):
111 $fld->scale = null;
112 if (preg_match("/^(.+)\((\d+),(\d+)/", $type, $query_array)) {
113 $fld->type = $query_array[1];
114 $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
115 $fld->scale = is_numeric($query_array[3]) ? $query_array[3] : -1;
116 } elseif (preg_match("/^(.+)\((\d+)/", $type, $query_array)) {
117 $fld->type = $query_array[1];
118 $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
119 } elseif (preg_match("/^(enum)\((.*)\)$/i", $type, $query_array)) {
120 $fld->type = $query_array[1];
121 $arr = explode(",",$query_array[2]);
122 $fld->enums = $arr;
123 $zlen = max(array_map("strlen",$arr)) - 2; // PHP >= 4.0.6
124 $fld->max_length = ($zlen > 0) ? $zlen : 1;
125 } else {
126 $fld->type = $type;
127 $fld->max_length = -1;
129 $fld->not_null = ($rs->fields[2] != 'YES');
130 $fld->primary_key = ($rs->fields[3] == 'PRI');
131 $fld->auto_increment = (strpos($rs->fields[5], 'auto_increment') !== false);
132 $fld->binary = (strpos($type,'blob') !== false);
133 $fld->unsigned = (strpos($type,'unsigned') !== false);
135 if (!$fld->binary) {
136 $d = $rs->fields[4];
137 if ($d != '' && $d != 'NULL') {
138 $fld->has_default = true;
139 $fld->default_value = $d;
140 } else {
141 $fld->has_default = false;
145 if ($save == ADODB_FETCH_NUM) {
146 $retarr[] = $fld;
147 } else {
148 $retarr[strtoupper($fld->name)] = $fld;
150 $rs->MoveNext();
153 $rs->Close();
154 return $retarr;
158 // parameters use PostgreSQL convention, not MySQL
159 function &SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs=0)
161 $offsetStr =($offset>=0) ? "$offset," : '';
162 // jason judge, see http://phplens.com/lens/lensforum/msgs.php?id=9220
163 if ($nrows < 0) $nrows = '18446744073709551615';
165 if ($secs)
166 $rs =& $this->CacheExecute($secs,$sql." LIMIT $offsetStr$nrows",$inputarr);
167 else
168 $rs =& $this->Execute($sql." LIMIT $offsetStr$nrows",$inputarr);
169 return $rs;