Repository navigation
Expand file tree
/
Copy pathDriver.php
More file actions
214 lines (185 loc) · 4.59 KB
/
Copy pathDriver.php
File metadata and controls
214 lines (185 loc) · 4.59 KB
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
<?php
/**
* Database Driver Class File
*
* PHP versions 5
*
* @package Tsukiyo
* @author Satoshi Nishimura <nishim314@gmail.com>
* @copyright 2012 Satoshi Nishimura
*/
/**
* Database Driver Class
*
* @package Tsukiyo
* @author Satoshi Nishimura <nishim314@gmail.com>
*/
abstract class Tsukiyo_Driver
{
/** @var PDO */
private $conn;
/**
* Return tables information.
*
* @return array array('table_name1', 'table_name2', ...)
* @access public
*/
abstract public function getMetaTables();
/**
* Return Columns information.
*
* @return array array('col1' => 'type1', 'col2' => 'type2', ...)
* @access public
*/
abstract public function getMetaColumns($tableName);
/**
* Return primary key information.
*
* @return array array('col1' => 'seq1', 'col2' => 'seq2', ...)
* @access public
*/
abstract public function getMetaPrimaryKeys($tableName);
/**
* Return forign key information.
*
* @return array
* @access public
*/
abstract public function getMetaForignKeys();
/**
* Return the ID of the last inserted row or sequence value.
*/
abstract public function lastInsertId($name = null);
/**
* 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);
$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;
}
/**
* 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.
*/
public function begin(){
return $this->conn->beginTransaction();
}
/**
* Commit transaction.
*/
public function commit(){
return $this->conn->commit();
}
/**
* Abort transaction.
*/
public function abort(){
return $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);
}
/**
* PDO::lastInsertId
*/
public function lastInsertIdRaw($name = null){
return $this->conn->lastInsertId($name);
}
/**
* @param String $sql
* @return PDOStatement
*/
public function prepareRaw($sql){
return $this->conn->prepare($sql);
}
}