1
|
|
|
<?php
|
2
|
|
|
namespace Gnf\db;
|
3
|
|
|
|
4
|
|
|
class PDO extends base
|
5
|
|
|
{
|
6
|
|
|
/**
|
7
|
|
|
* @var \PDO
|
8
|
|
|
*/
|
9
|
|
|
protected $db;
|
10
|
|
|
private $select_db;
|
11
|
|
|
|
12
|
|
|
/**
|
13
|
|
|
* @param \PDO $pdo
|
14
|
|
|
*/
|
15
|
|
|
public function __construct($pdo)
|
16
|
|
|
{
|
17
|
|
|
parent::__construct();
|
18
|
|
|
$this->db = $pdo;
|
19
|
|
|
}
|
20
|
|
|
|
21
|
|
|
public function select_db($db)
|
22
|
|
|
{
|
23
|
|
|
$this->select_db = $db;
|
24
|
|
|
}
|
25
|
|
|
|
26
|
|
|
/*
|
27
|
|
|
* addslashes is not safe in multibyte
|
28
|
|
|
* str_replace is safe in multibyte but only utf-8
|
29
|
|
|
*/
|
30
|
|
|
protected function escapeLiteral($value)
|
31
|
|
|
{
|
32
|
|
|
if (!is_string($value)) {
|
33
|
|
|
$value = strval($value);
|
34
|
|
|
}
|
35
|
|
|
|
36
|
|
|
return str_replace(
|
37
|
|
|
['\\', "\0", "\n", "\r", "'", '"', "\x1a"],
|
38
|
|
|
['\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'],
|
39
|
|
|
$value
|
40
|
|
|
);
|
41
|
|
|
}
|
42
|
|
|
|
43
|
|
|
protected function query($sql)
|
44
|
|
|
{
|
45
|
|
|
return $this->db->query($sql);
|
46
|
|
|
}
|
47
|
|
|
|
48
|
|
|
/**
|
49
|
|
|
* @param \PDOStatement $handle
|
50
|
|
|
*
|
51
|
|
|
* @return null|\stdClass
|
52
|
|
|
*/
|
53
|
|
|
protected function getError($handle)
|
54
|
|
|
{
|
55
|
|
|
$info = $handle->errorInfo();
|
56
|
|
|
if ($info[1] != 0) {
|
57
|
|
|
$ret = new \stdClass();
|
58
|
|
|
$ret->errno = $info[1];
|
59
|
|
|
$ret->message = $info[2];
|
60
|
|
|
return $ret;
|
61
|
|
|
} else {
|
62
|
|
|
return null;
|
63
|
|
|
}
|
64
|
|
|
}
|
65
|
|
|
|
66
|
|
|
/**
|
67
|
|
|
* @param \PDOStatement $handle
|
68
|
|
|
*
|
69
|
|
|
* @return mixed
|
70
|
|
|
*/
|
71
|
|
|
protected function fetchRow($handle)
|
72
|
|
|
{
|
73
|
|
|
return $handle->fetch(\PDO::FETCH_NUM);
|
74
|
|
|
}
|
75
|
|
|
|
76
|
|
|
/**
|
77
|
|
|
* @param \PDOStatement $handle
|
78
|
|
|
*
|
79
|
|
|
* @return mixed
|
80
|
|
|
*/
|
81
|
|
|
protected function fetchAssoc($handle)
|
82
|
|
|
{
|
83
|
|
|
return $handle->fetch(\PDO::FETCH_ASSOC);
|
84
|
|
|
}
|
85
|
|
|
|
86
|
|
|
/**
|
87
|
|
|
* @param \PDOStatement $handle
|
88
|
|
|
*
|
89
|
|
|
* @return mixed
|
90
|
|
|
*/
|
91
|
|
|
protected function fetchObject($handle)
|
92
|
|
|
{
|
93
|
|
|
return $handle->fetch(\PDO::FETCH_OBJ);
|
94
|
|
|
}
|
95
|
|
|
|
96
|
|
|
/**
|
97
|
|
|
* @param \PDOStatement $handle
|
98
|
|
|
*
|
99
|
|
|
* @return mixed
|
100
|
|
|
*/
|
101
|
|
|
protected function fetchBoth($handle)
|
102
|
|
|
{
|
103
|
|
|
return $handle->fetch(\PDO::FETCH_BOTH);
|
104
|
|
|
}
|
105
|
|
|
|
106
|
|
|
public function insert_id()
|
107
|
|
|
{
|
108
|
|
|
return $this->db->lastInsertId();
|
109
|
|
|
}
|
110
|
|
|
|
111
|
|
|
/**
|
112
|
|
|
* @param \PDOStatement $handle
|
113
|
|
|
*
|
114
|
|
|
* @return mixed
|
115
|
|
|
*/
|
116
|
|
|
protected function getAffectedRows($handle)
|
117
|
|
|
{
|
118
|
|
|
return $handle->rowCount();
|
119
|
|
|
}
|
120
|
|
|
|
121
|
|
|
protected function hasConnected()
|
122
|
|
|
{
|
123
|
|
|
return is_resource($this->db);
|
124
|
|
|
}
|
125
|
|
|
|
126
|
|
|
protected function doConnect()
|
127
|
|
|
{
|
128
|
|
|
$this->afterConnect();
|
129
|
|
|
$this->db->query('USE ' . $this->escapeLiteral($this->select_db));
|
130
|
|
|
}
|
131
|
|
|
|
132
|
|
|
protected function transactionBegin()
|
133
|
|
|
{
|
134
|
|
|
$this->db->beginTransaction();
|
135
|
|
|
}
|
136
|
|
|
|
137
|
|
|
protected function transactionCommit()
|
138
|
|
|
{
|
139
|
|
|
$this->db->commit();
|
140
|
|
|
}
|
141
|
|
|
|
142
|
|
|
protected function transactionRollback()
|
143
|
|
|
{
|
144
|
|
|
$this->db->rollBack();
|
145
|
|
|
}
|
146
|
|
|
|
147
|
|
|
/**
|
148
|
|
|
* @return bool
|
149
|
|
|
*/
|
150
|
|
|
protected function configIsSupportNestedTransaction()
|
151
|
|
|
{
|
152
|
|
|
return false;
|
153
|
|
|
}
|
154
|
|
|
}
|
155
|
|
|
|