new approach to logging database access and upgraded adodb
[openemr.git] / library / adodb / drivers / adodb-pdo_mysql.inc.php
blob8809072201aeca5e0f6d63885f729e2370734018
1 <?php
4 /*
5 V5.14 8 Sept 2011 (c) 2000-2011 John Lim (jlim#natsoft.com). 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";
21 var $fmtTimeStamp = "'Y-m-d, H:i: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 Concat()
46 $s = "";
47 $arr = func_get_args();
49 // suggestion by andrew005#mnogo.ru
50 $s = implode(',',$arr);
51 if (strlen($s) > 0) return "CONCAT($s)"; return '';
54 function ServerInfo()
56 $arr['description'] = ADOConnection::GetOne("select version()");
57 $arr['version'] = ADOConnection::_findvers($arr['description']);
58 return $arr;
61 function MetaTables($ttype=false,$showSchema=false,$mask=false)
63 $save = $this->metaTablesSQL;
64 if ($showSchema && is_string($showSchema)) {
65 $this->metaTablesSQL .= " from $showSchema";
68 if ($mask) {
69 $mask = $this->qstr($mask);
70 $this->metaTablesSQL .= " like $mask";
72 $ret = ADOConnection::MetaTables($ttype,$showSchema);
74 $this->metaTablesSQL = $save;
75 return $ret;
78 function SetTransactionMode( $transaction_mode )
80 $this->_transmode = $transaction_mode;
81 if (empty($transaction_mode)) {
82 $this->Execute('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ');
83 return;
85 if (!stristr($transaction_mode,'isolation')) $transaction_mode = 'ISOLATION LEVEL '.$transaction_mode;
86 $this->Execute("SET SESSION TRANSACTION ".$transaction_mode);
89 function MetaColumns($table,$normalize=true)
91 $this->_findschema($table,$schema);
92 if ($schema) {
93 $dbName = $this->database;
94 $this->SelectDB($schema);
96 global $ADODB_FETCH_MODE;
97 $save = $ADODB_FETCH_MODE;
98 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
100 if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false);
101 $rs = $this->Execute(sprintf($this->metaColumnsSQL,$table));
103 if ($schema) {
104 $this->SelectDB($dbName);
107 if (isset($savem)) $this->SetFetchMode($savem);
108 $ADODB_FETCH_MODE = $save;
109 if (!is_object($rs)) {
110 $false = false;
111 return $false;
114 $retarr = array();
115 while (!$rs->EOF){
116 $fld = new ADOFieldObject();
117 $fld->name = $rs->fields[0];
118 $type = $rs->fields[1];
120 // split type into type(length):
121 $fld->scale = null;
122 if (preg_match("/^(.+)\((\d+),(\d+)/", $type, $query_array)) {
123 $fld->type = $query_array[1];
124 $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
125 $fld->scale = is_numeric($query_array[3]) ? $query_array[3] : -1;
126 } elseif (preg_match("/^(.+)\((\d+)/", $type, $query_array)) {
127 $fld->type = $query_array[1];
128 $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
129 } elseif (preg_match("/^(enum)\((.*)\)$/i", $type, $query_array)) {
130 $fld->type = $query_array[1];
131 $arr = explode(",",$query_array[2]);
132 $fld->enums = $arr;
133 $zlen = max(array_map("strlen",$arr)) - 2; // PHP >= 4.0.6
134 $fld->max_length = ($zlen > 0) ? $zlen : 1;
135 } else {
136 $fld->type = $type;
137 $fld->max_length = -1;
139 $fld->not_null = ($rs->fields[2] != 'YES');
140 $fld->primary_key = ($rs->fields[3] == 'PRI');
141 $fld->auto_increment = (strpos($rs->fields[5], 'auto_increment') !== false);
142 $fld->binary = (strpos($type,'blob') !== false);
143 $fld->unsigned = (strpos($type,'unsigned') !== false);
145 if (!$fld->binary) {
146 $d = $rs->fields[4];
147 if ($d != '' && $d != 'NULL') {
148 $fld->has_default = true;
149 $fld->default_value = $d;
150 } else {
151 $fld->has_default = false;
155 if ($save == ADODB_FETCH_NUM) {
156 $retarr[] = $fld;
157 } else {
158 $retarr[strtoupper($fld->name)] = $fld;
160 $rs->MoveNext();
163 $rs->Close();
164 return $retarr;
168 // parameters use PostgreSQL convention, not MySQL
169 function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs=0)
171 $offsetStr =($offset>=0) ? "$offset," : '';
172 // jason judge, see http://phplens.com/lens/lensforum/msgs.php?id=9220
173 if ($nrows < 0) $nrows = '18446744073709551615';
175 if ($secs)
176 $rs = $this->CacheExecute($secs,$sql." LIMIT $offsetStr$nrows",$inputarr);
177 else
178 $rs = $this->Execute($sql." LIMIT $offsetStr$nrows",$inputarr);
179 return $rs;