1<?php
2
3
4/*
5v4.992 10 Nov 2009  (c) 2000-2009 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.
10
11*/
12
13class 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
22	var $nameQuote = '`';
23
24	function _init($parentDriver)
25	{
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
33		// dayFraction is a day in floating point
34	function OffsetDate($dayFraction,$date=false)
35	{
36		if (!$date) $date = $this->sysDate;
37
38		$fraction = $dayFraction * 24 * 3600;
39		return $date . ' + INTERVAL ' .	 $fraction.' SECOND';
40
41//		return "from_unixtime(unix_timestamp($date)+$fraction)";
42	}
43
44	function ServerInfo()
45	{
46		$arr['description'] = ADOConnection::GetOne("select version()");
47		$arr['version'] = ADOConnection::_findvers($arr['description']);
48		return $arr;
49	}
50
51	function &MetaTables($ttype=false,$showSchema=false,$mask=false)
52	{
53		$save = $this->metaTablesSQL;
54		if ($showSchema && is_string($showSchema)) {
55			$this->metaTablesSQL .= " from $showSchema";
56		}
57
58		if ($mask) {
59			$mask = $this->qstr($mask);
60			$this->metaTablesSQL .= " like $mask";
61		}
62		$ret =& ADOConnection::MetaTables($ttype,$showSchema);
63
64		$this->metaTablesSQL = $save;
65		return $ret;
66	}
67
68	function SetTransactionMode( $transaction_mode )
69	{
70		$this->_transmode  = $transaction_mode;
71		if (empty($transaction_mode)) {
72			$this->Execute('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ');
73			return;
74		}
75		if (!stristr($transaction_mode,'isolation')) $transaction_mode = 'ISOLATION LEVEL '.$transaction_mode;
76		$this->Execute("SET SESSION TRANSACTION ".$transaction_mode);
77	}
78
79 	function &MetaColumns($table)
80	{
81		$this->_findschema($table,$schema);
82		if ($schema) {
83			$dbName = $this->database;
84			$this->SelectDB($schema);
85		}
86		global $ADODB_FETCH_MODE;
87		$save = $ADODB_FETCH_MODE;
88		$ADODB_FETCH_MODE = ADODB_FETCH_NUM;
89
90		if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false);
91		$rs = $this->Execute(sprintf($this->metaColumnsSQL,$table));
92
93		if ($schema) {
94			$this->SelectDB($dbName);
95		}
96
97		if (isset($savem)) $this->SetFetchMode($savem);
98		$ADODB_FETCH_MODE = $save;
99		if (!is_object($rs)) {
100			$false = false;
101			return $false;
102		}
103
104		$retarr = array();
105		while (!$rs->EOF){
106			$fld = new ADOFieldObject();
107			$fld->name = $rs->fields[0];
108			$type = $rs->fields[1];
109
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;
128			}
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);
134
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;
142				}
143			}
144
145			if ($save == ADODB_FETCH_NUM) {
146				$retarr[] = $fld;
147			} else {
148				$retarr[strtoupper($fld->name)] = $fld;
149			}
150				$rs->MoveNext();
151			}
152
153			$rs->Close();
154			return $retarr;
155	}
156
157
158	// parameters use PostgreSQL convention, not MySQL
159	function &SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs=0)
160	{
161		$offsetStr =($offset>=0) ? "$offset," : '';
162		// jason judge, see http://phplens.com/lens/lensforum/msgs.php?id=9220
163		if ($nrows < 0) $nrows = '18446744073709551615';
164
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;
170	}
171}
172?>