/
pdo.php
170 lines (142 loc) · 3.8 KB
/
pdo.php
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
<?php defined('SYSPATH') or die('No direct script access.');
/**
* PDO database connection.
*
* @package Kohana/Database
* @category Drivers
* @author Kohana Team
* @copyright (c) 2008-2009 Kohana Team
* @license http://kohanaphp.com/license
*/
class Kohana_Database_PDO extends Database {
// PDO uses no quoting for identifiers
protected $_identifier = '';
protected function __construct($name, array $config)
{
parent::__construct($name, $config);
if (isset($this->_config['identifier']))
{
// Allow the identifier to be overloaded per-connection
$this->_identifier = (string) $this->_config['identifier'];
}
}
public function connect()
{
if ($this->_connection)
return;
// Extract the connection parameters, adding required variabels
extract($this->_config['connection'] + array(
'dsn' => '',
'username' => NULL,
'password' => NULL,
'persistent' => FALSE,
));
// Clear the connection parameters for security
unset($this->_config['connection']);
// Force PDO to use exceptions for all errors
$attrs = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
if ( ! empty($persistent))
{
// Make the connection persistent
$attrs[PDO::ATTR_PERSISTENT] = TRUE;
}
// Create a new PDO connection
$this->_connection = new PDO($dsn, $username, $password, $attrs);
if ( ! empty($this->_config['charset']))
{
// Set the character set
$this->set_charset($this->_config['charset']);
}
}
public function disconnect()
{
// Destroy the PDO object
$this->_connection = NULL;
return TRUE;
}
public function set_charset($charset)
{
// Make sure the database is connected
$this->_connection or $this->connect();
// Execute a raw SET NAMES query
$this->_connection->exec('SET NAMES '.$this->quote($charset));
}
public function query($type, $sql, $as_object)
{
// Make sure the database is connected
$this->_connection or $this->connect();
if ( ! empty($this->_config['profiling']))
{
// Benchmark this query for the current instance
$benchmark = Profiler::start("Database ({$this->_instance})", $sql);
}
try
{
$result = $this->_connection->query($sql);
}
catch (Exception $e)
{
if (isset($benchmark))
{
// This benchmark is worthless
Profiler::delete($benchmark);
}
// Rethrow the exception
throw $e;
}
if (isset($benchmark))
{
Profiler::stop($benchmark);
}
// Set the last query
$this->last_query = $sql;
if ($type === Database::SELECT)
{
// Convert the result into an array, as PDOStatement::rowCount is not reliable
if ($as_object === FALSE)
{
$result->setFetchMode(PDO::FETCH_ASSOC);
}
elseif (is_string($as_object))
{
$result->setFetchMode(PDO::FETCH_CLASS, $as_object);
}
else
{
$result->setFetchMode(PDO::FETCH_CLASS, 'stdClass');
}
$result = $result->fetchAll();
// Return an iterator of results
return new Database_Result_Cached($result, $sql, $as_object);
}
elseif ($type === Database::INSERT)
{
// Return a list of insert id and rows created
return array(
$this->_connection->lastInsertId(),
$result->rowCount(),
);
}
else
{
// Return the number of rows affected
return $result->rowCount();
}
}
public function list_tables($like = NULL)
{
throw new Kohana_Exception('Database method :method is not supported by :class',
array(':method' => __FUNCTION__, ':class' => __CLASS__));
}
public function list_columns($table, $like = NULL)
{
throw new Kohana_Exception('Database method :method is not supported by :class',
array(':method' => __FUNCTION__, ':class' => __CLASS__));
}
public function escape($value)
{
// Make sure the database is connected
$this->_connection or $this->connect();
return $this->_connection->quote($value);
}
} // End Database_PDO