summaryrefslogtreecommitdiff
path: root/system/libraries/Database_Mysql.php
blob: a577503711881324d239f714dc830bc54fab020e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php defined('SYSPATH') or die('No direct script access.');
/**
 * MySQL database connection.
 *
 * $Id: Database_Mysql.php 4712 2009-12-10 21:47:09Z cbandy $
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2008-2009 Kohana Team
 * @license    http://kohanaphp.com/license
 */
class Database_Mysql_Core extends Database {

	// Quote character to use for identifiers (tables/columns/aliases)
	protected $quote = '`';

	// Use SET NAMES to set the character set
	protected static $set_names;

	public function connect()
	{
		if ($this->connection)
			return;

		if (Database_Mysql::$set_names === NULL)
		{
			// Determine if we can use mysql_set_charset(), which is only
			// available on PHP 5.2.3+ when compiled against MySQL 5.0+
			Database_Mysql::$set_names = ! function_exists('mysql_set_charset');
		}

		extract($this->config['connection']);

		$host = isset($host) ? $host : $socket;
		$port = isset($port) ? ':'.$port : '';

		try
		{
			// Connect to the database
			$this->connection = ($this->config['persistent'] === TRUE)
				? mysql_pconnect($host.$port, $user, $pass, $params)
				: mysql_connect($host.$port, $user, $pass, TRUE, $params);
		}
		catch (Kohana_PHP_Exception $e)
		{
			// No connection exists
			$this->connection = NULL;

			// Unable to connect to the database
			throw new Database_Exception('#:errno: :error',
				array(':error' => mysql_error(),
				':errno' => mysql_errno()));
		}

		if ( ! mysql_select_db($database, $this->connection))
		{
			// Unable to select database
			throw new Database_Exception('#:errno: :error',
				array(':error' => mysql_error($this->connection),
				':errno' => mysql_errno($this->connection)));
		}

		if (isset($this->config['character_set']))
		{
			// Set the character set
			$this->set_charset($this->config['character_set']);
		}
	}

	public function disconnect()
	{
		try
		{
			// Database is assumed disconnected
			$status = TRUE;

			if (is_resource($this->connection))
			{
				$status = mysql_close($this->connection);
			}
		}
		catch (Exception $e)
		{
			// Database is probably not disconnected
			$status = is_resource($this->connection);
		}

		return $status;
	}

	public function set_charset($charset)
	{
		// Make sure the database is connected
		$this->connection or $this->connect();

		if (Database_Mysql::$set_names === TRUE)
		{
			// PHP is compiled against MySQL 4.x
			$status = (bool) mysql_query('SET NAMES '.$this->quote($charset), $this->connection);
		}
		else
		{
			// PHP is compiled against MySQL 5.x
			$status = mysql_set_charset($charset, $this->connection);
		}

		if ($status === FALSE)
		{
			// Unable to set charset
			throw new Database_Exception('#:errno: :error',
				array(':error' => mysql_error($this->connection),
				':errno' => mysql_errno($this->connection)));
		}
	}

	public function query_execute($sql)
	{
		// Make sure the database is connected
		$this->connection or $this->connect();

		$result = mysql_query($sql, $this->connection);

		// Set the last query
		$this->last_query = $sql;

		return new Database_Mysql_Result($result, $sql, $this->connection, $this->config['object']);
	}

	public function escape($value)
	{
		// Make sure the database is connected
		$this->connection or $this->connect();

		if (($value = mysql_real_escape_string($value, $this->connection)) === FALSE)
		{
			throw new Database_Exception('#:errno: :error',
				array(':error' => mysql_error($this->connection),
				':errno' => mysql_errno($this->connection)));
		}

		return $value;
	}

	public function list_constraints($table)
	{
		$prefix = strlen($this->table_prefix());
		$result = array();

		$constraints = $this->query('
			SELECT c.constraint_name, c.constraint_type, k.column_name, k.referenced_table_name, k.referenced_column_name
			FROM information_schema.table_constraints c
			JOIN information_schema.key_column_usage k ON (k.table_schema = c.table_schema AND k.table_name = c.table_name AND k.constraint_name = c.constraint_name)
			WHERE c.table_schema = '.$this->quote($this->config['connection']['database']).'
				AND c.table_name = '.$this->quote($this->table_prefix().$table).'
				AND (k.referenced_table_schema IS NULL OR k.referenced_table_schema ='.$this->quote($this->config['connection']['database']).')
			ORDER BY k.ordinal_position
		');

		foreach ($constraints->as_array() as $row)
		{
			switch ($row['constraint_type'])
			{
				case 'FOREIGN KEY':
					if (isset($result[$row['constraint_name']]))
					{
						$result[$row['constraint_name']][1][] = $row['column_name'];
						$result[$row['constraint_name']][3][] = $row['referenced_column_name'];
					}
					else
					{
						$result[$row['constraint_name']] = array($row['constraint_type'], array($row['column_name']), substr($row['referenced_table_name'], $prefix), array($row['referenced_column_name']));
					}
				break;
				case 'PRIMARY KEY':
				case 'UNIQUE':
					if (isset($result[$row['constraint_name']]))
					{
						$result[$row['constraint_name']][1][] = $row['column_name'];
					}
					else
					{
						$result[$row['constraint_name']] = array($row['constraint_type'], array($row['column_name']));
					}
				break;
			}
		}

		return $result;
	}

	public function list_fields($table)
	{
		$result = array();

		foreach ($this->query('SHOW COLUMNS FROM '.$this->quote_table($table))->as_array() as $row)
		{
			$column = $this->sql_type($row['Type']);

			$column['default'] = $row['Default'];
			$column['nullable'] = $row['Null'] === 'YES';
			$column['sequenced'] = $row['Extra'] === 'auto_increment';

			if (isset($column['length']) AND $column['type'] === 'float')
			{
				list($column['precision'], $column['scale']) = explode(',', $column['length']);
			}

			$result[$row['Field']] = $column;
		}

		return $result;
	}

	public function list_tables()
	{
		$prefix = strlen($this->table_prefix());
		$tables = array();

		foreach ($this->query('SHOW TABLES FROM '.$this->escape($this->config['connection']['database']).' LIKE '.$this->quote($this->table_prefix().'%'))->as_array() as $row)
		{
			// The value is the table name
			$tables[] = substr(current($row), $prefix);
		}

		return $tables;
	}

} // End Database_MySQL