-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLBSDbConnection.php
136 lines (114 loc) · 3.43 KB
/
LBSDbConnection.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
<?php
/**
* Copyright © 2013 NEILSEN·CHAN. All rights reserved.
* Date: 2/27/13
* Description: LBSDbConnection.php
* 备注:适合当前php中常用的PDO访问持久层方式
*/
class LBSDbConnection
{
private $_pdoClass;
private $_connectionString;
private $_username;
private $_password;
private $_attributes = array();
private $_pdo;
private $_active;
private $_emulatePrepare;
private $_charset;
public function __construct($dsn='',$username='',$password='')
{
$this->_connectionString=$dsn;
$this->_username=$username;
$this->_password=$password;
}
public function getServerVersion()
{
return $this->getAttribute(PDO::ATTR_SERVER_VERSION);
}
public function getAttribute($name)
{
$this->setActive(true);
return $this->_pdo->getAttribute($name);
}
public function setAttribute($name,$value)
{
if($this->_pdo instanceof PDO)
$this->_pdo->setAttribute($name,$value);
else
$this->_attributes[$name]=$value;
}
public function getAttributes()
{
return $this->_attributes;
}
public function getActive()
{
return $this->_active;
}
public function setActive($value)
{
if($value!=$this->_active)
{
if($value)
$this->open();
else
$this->close();
}
}
public function open()
{
if($this->_pdo === null){
$this->getPDOInstance();
}
}
public function close()
{
$this->_pdo=null;
$this->_active=false;
}
protected function getPDOInstance()
{
if(empty($this->_connectionString)){
throw new PDOException('connectionString cannot be empty.');
}else{
if($this->_pdo instanceof PDO){
return $this->_pdo;
}else{
$this->_pdo = $this->_createPDOInstance();
$this->_initConnection();
$this->_active=true;
return $this->_pdo;
}
}
}
protected function _createPDOInstance()
{
$pos = strpos($this->_connectionString, ':');
if($pos > 0){
$driverStr = substr($this->_connectionString, 0, $pos);
if($driverStr == 'mysql' || $driverStr == 'dblib' || $driverStr == 'sqlsrv')
$this->_pdoClass = 'PDO';
else
throw new PDOException('Incorrect driver exists.');
}
return new $this->_pdoClass($this->_connectionString, $this->_username, $this->_password, $this->_attributes);
}
protected function _initConnection()
{
if(!$this->_pdo instanceof PDO){
$this->_pdo = $this->_createPDOInstance();
$this->_initConnection();
}else{
$this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if($this->_emulatePrepare!==null && constant('PDO::ATTR_EMULATE_PREPARES'))
$this->_pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,$this->_emulatePrepare);
if($this->_charset!==null)
{
$driver=strtolower($this->_pdo->getAttribute(PDO::ATTR_DRIVER_NAME));
if(in_array($driver,array('pgsql','mysql','mysqli')))
$this->_pdo->exec('SET NAMES '.$this->_pdo->quote($this->_charset));
}
}
}
}