-
Notifications
You must be signed in to change notification settings - Fork 0
/
Driver.php
230 lines (198 loc) · 4.85 KB
/
Driver.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
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
229
230
<?php
/**
* Database Driver Class File
*
* PHP versions 5
*
* @author Satoshi Nishimura <nishim314@gmail.com>
* @copyright 2005-2011 Satoshi Nishimura
*/
/**
* Database Driver Class
*
* @author Satoshi Nishimura <nishim314@gmail.com>
*/
abstract class YokazeDb_Driver
{
/** @var PDO */
private $conn;
/**
* Return tables information.
*
* @return array
* @access public
*/
abstract public function getMetaTables();
/**
* Return Columns information.
*
* @return array
* @access public
*/
abstract public function getMetaColumns($tableName);
/**
* Return primary key information.
*
* @return array
* @access public
*/
abstract public function getMetaPrimaryKeys($tableName);
/**
* Setting DSN.
*
* @param string $dsn
* @param string $mode error mode for PDO
* @access public
*/
public function __construct($dsn, $errMode = null){
try{
$this->conn = new PDO($dsn);
if ($errMode == 'exception')
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch (PDOException $e){
trigger_error($e->getMessage(), E_USER_ERROR);
}
}
/**
* Close connection.
*
* @access public
*/
public function __destruct(){
$this->conn = null;
}
/**
* Get a row by primary key.
*
* @param string $sql
* @param int $key
* @return string[]
* @access public
*/
public function getRowByKey($sql, $key){
return $this->getRow($sql, $key, true);
}
/**
* Get a row.
*
* @param string $sql
* @param string|array $where
* @return string[]
* @access public
*/
public function getRow($sql, $where = null, $onlyone = false){
if ($onlyone && $where === null){
trigger_error('Not set second argument.', E_USER_WARNING);
return null;
}
$stmt = $this->query($sql, $where);
$count = $stmt->rowCount();
if ($stmt && $count){
if ($onlyone && $count !== 1)
trigger_error("Two or more data exists [$sql].", E_USER_WARNING);
return $stmt->fetchRow();
}
}
/**
* Query.
*
* @param string $sql
* @param mixed $p place holder
* @return PDOStatement
* @access public
*/
public function query($sql, $p = null){
if ($p !== null){
static $oldSql;
static $stmt;
if ($oldSql !== $sql || !$stmt){
// for speedup
$oldSql = $sql;
$stmt = $this->conn->prepare($sql);
if (!$stmt)
return false;
}
if (!is_array($p)){
$p = array($p);
}
$stmt->execute($p);
}else{
$stmt = $this->conn->query($sql);
if (!$stmt)
return false;
}
if ($stmt->errorCode() != '00000'){
$errInfo = $stmt->errorInfo();
trigger_error($sql . '['.$errInfo[0].':'.$errInfo[1].']'.$errInfo[2], E_USER_WARNING);
}
return $stmt;
}
/**
* PDO Execute.
*
* @param string $sql
* @param mixed $p
* @return int
*/
public function execute($sql, $p = null){
if ($p === null)
return $this->conn->exec($sql);
$stmt = $this->query($sql, $p);
if (!$stmt)
return 0;
$code = $stmt->errorCode();
if ($code !== '00000'){ // Error Code defined by ANSI SQL
$error = $stmt->errorInfo();
if (isset($error[2]))
$msg = $error[2];
else
$msg = '';
trigger_error("SQL error [$code]:$msg : arguments : " . var_export($p, true),
E_USER_WARNING);
return 0;
}
return $stmt->rowCount();
}
/**
* Begin transaction.
*
* @access public
*/
public function begin(){
$this->conn->beginTransaction();
}
/**
* Finish transaction.
*
* @access public
*/
public function commit(){
$this->conn->commit();
}
/**
* Abort transaction.
*
* @access public
*/
public function abort(){
$this->conn->rollBack();
}
/**
* Lock table.
*
* @param string $table
* @access public
*/
abstract function lock($table);
public function quote($str, $type = null){
return $this->conn->quote($str, $type);
}
/**
* Setting PDO error mode.
*
* @param int $mode
*/
public function setErrorMode($mode){
$this->conn->setAttribute(PDO::ATTR_ERRMODE, $mode);
}
}