/
connect.php
207 lines (185 loc) · 4.55 KB
/
connect.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
<?php
namespace fl\db;
use fl\base\object;
abstract class connect extends object implements Iconnect
{
/**
* 数据库链接
*
* @var \PDO
*/
static $connections = array();
/**
* 数据库组KEY
*
* @var string
*/
protected $_dbhash = '';
/**
* 数据库前缀
*
* @var string
*/
public $prefix = '';
/**
* 数据库类型
*
* @var string
*/
public $type = null;
/**
* 数据库编码
*
* @var string
*/
protected $_charset = 'utf8';
/**
* 是否禁用主库
* 0未初始化,1已禁用,-1未禁用
*
* @var int
*/
protected $disableMaster = 0;
/**
* 是否禁用从库
* 0未初始化,1已禁用,-1未禁用
*
* @var int
*/
protected $disableSlave = 0;
/**
* PDO从连接
*
* @var \fl\cfg\cfg
*/
protected $cfg;
public function __construct($dbcfg)
{
if (! is_string($dbcfg)) {
return false;
}
$this->cfg = \fl\cfg\cfg::getcfgobj('db/' . $dbcfg, 'ini');
$this->_dbhash = $dbcfg;
$this->type = $this->cfg->get('main', 'type');
$this->prefix = $this->cfg->get('main', 'prefix');
if ($this->cfg->get('main', 'charset')) {
$this->_charset = $this->cfg->get('main', 'charset');
}
}
public function __destruct()
{
unset($this->cfg);
}
/**
* 数据库是否存在
*
* @return boolean
*/
public function dbexist()
{
if (count($this->cfg->get('main')) == 0) {
return false;
} else {
return true;
}
}
/**
* 取得DBhash
*
* @return boolean
*/
public function intransaction()
{
return transaction::inTransation($this->_dbhash);
}
public function disconnect()
{
unset(self::$connections[$this->_dbhash]);
}
public function beginTransaction()
{
$pdo = $this->getmasterpdo();
$pdo->beginTransaction();
}
public function rollback()
{
$pdo = $this->getmasterpdo();
$pdo->rollBack();
}
public function commint()
{
$pdo = $this->getmasterpdo();
$pdo->commit();
}
public function query($sql, $bindparams = array(), $ismaster = false)
{
$sth = $this->runsql($sql, $bindparams, $ismaster);
if (! $sth) {
return false;
}
return $sth;
}
public function exec($sql, $bindparams = array(), $ismaster = false)
{
$sth = $this->runsql($sql, $bindparams, $ismaster);
if (! $sth) {
return false;
}
return $sth->rowCount() ? $sth->rowCount() : true;
}
/**
* 执行一个SQL
*
* @param unknown $sql
* SQL
* @param array $bindparams
* 绑定参数
* @param string $ismaster
* 是否主库执行
* @return boolean|\PDOStatement
*/
private function runsql($sql, $bindparams = array(), $ismaster = false)
{
if (FL_DEBUG) {
$this->lastsql = $sql . ' BIND Value :' . var_export($bindparams, true);
}
if ($ismaster) {
$pdo = $this->getmasterpdo();
} else {
$pdo = $this->getslavepdo();
}
if (! $pdo) {
return false;
}
$sth = $pdo->prepare($sql);
if (is_array($bindparams)) {
foreach ($bindparams as $k => $v) {
$sth->bindValue($k + 1, $v);
}
}
$sth->setFetchMode(\PDO::FETCH_ASSOC);
if (! $sth->execute()) {
$errInfo = $sth->errorInfo();
throw new \PDOException("SQL ERROR." . $sql . ' BIND params :' . var_export($bindparams, true) . $errInfo[2], - 1);
}
return $sth;
}
final public static function getQueryerBuilder($dbcfg)
{
static $querybuilder = array();
if (isset($querybuilder[$dbcfg])) {
return $querybuilder[$dbcfg];
}
$s_cfg = \fl\cfg\cfg::getcfgobj('db/' . $dbcfg, 'ini');
switch (strtolower($s_cfg->get('main', 'type'))) {
case 'mysql':
$querybuilder[$dbcfg] = new mysql\QueryBuilder(new mysql\connect($dbcfg));
break;
case 'sqlite':
$querybuilder[$dbcfg] = new sqlite\QueryBuilder(new sqlite\connect($dbcfg));
break;
default:
}
return $querybuilder[$dbcfg];
}
}