-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.class.php
executable file
·193 lines (165 loc) · 4.2 KB
/
db.class.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
<?php
/**
* db_bug
* a helper class for debuging.
* Pleas note the circular reference of TDBBase
* keeps a list of querys made to database in development
*/
class db_debug {
public $queries = array();
/**
* db_debug::queryCount()
*
* @return integer the number of queries made
*/
function queryCount()
{
return count($this->queries);
}
/**
* db_debug::query()
* a dirty way to get sql results while debuging. never use in production
*
* @param mixed $sql
* @return mixed
*/
function query($sql)
{
return TDBBase::query($sql,true);
}
}
class DBException extends PDOException{
};
class db{
/*** An array of pdo database connections ***/
private static $instances = array();
private static $transactionCount = 0;
private static $db_debug = null;
/**
* db::get_db_debug()
* helper function to access db_debug class
* @return db_debug
*/
static function get_db_debug()
{
if (empty(self::$db_debug))
{
self::$db_debug = new db_debug();
}
return self::$db_debug;
}
/**
* db::getMicroTime()
* get the mirco time as integer
* @return integer
*/
static function getMicroTime() {
$time = microtime();
$time = explode(' ', $time);
return $time[1] + $time[0];
}
/**
* db::addQuery()
*
* adds a query to the db_debug class and figure time.
* also sends to firephp if installed
* @param mixed $sql
* @param mixed $start the microtime the sql started.
*
*/
static function addQuery($sql,$start)
{
$query = array(
'sql' => $sql,
'time' => (self::getMicroTime() - $start)*1000
);
if (function_exists('fb') )
fb($sql, 'SQL', FirePHP::LOG);
array_push(self::get_db_debug()->queries, $query);
}
/**
*
* the constructor is set to private so
* so nobody can create a new instance using new
*
*/
private function __construct() {
/*** maybe set the db name here later ***/
}
/**
*
* Return DB instance or create intitial connection
*
* @return object (PDO)
*
* @access public
*
*/
public static function getInstance($instname=null) {
if (!$instname)
$instname = 'default';
// check is exists if not create it with defualt settings
if (!self::$instances[$instname])
{
self::createInstance($instname ,DIMPLE_DB_URI, DIMPLE_DB_USERNAME, DIMPLE_DB_PASSWORD);
}
return self::$instances[$instname];
}
public static function createInstance($instname ,$hosturi, $username, $password)
{
$instance = new PDO($hosturi, $username, $password,array(
PDO::ATTR_PERSISTENT => true,PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
$instance-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$instances[$instname] = $instance;
}
/*
* Passes on any static calls to this class onto the singleton PDO instance
* @param $chrMethod, $arrArguments
* @return $mix
*/
final public static function __callStatic( $chrMethod, $arrArguments ) {
if (preg_match('/^(.*)_(.*)/',$chrMethod,$match) )
{
$instance = $match[2];
$chrMethod = $match[2];
}
else
$instance = null;
$objInstance = self::getInstance($instance);
return call_user_func_array(array($objInstance, $chrMethod), $arrArguments);
}
/**
*
* Like the constructor, we make __clone private
* so nobody can clone the instance
*
*/
private function __clone(){
}
static function startTransaction($instance = null)
{
if (self::$transactionCount == 0)
{
self::getInstance($instance)->beginTransaction();
}
self::$transactionCount++;
}
static function commit($instance = null)
{
self::$transactionCount--;
if (empty( self::$transactionCount))
self::getInstance($instance)->commit();
}
static function rollback($instance = null)
{
self::getInstance($instance)->rollback();
}
static function reset()
{
self::$instances = null;
}
static function setInstance($object, $instance="default")
{
self::$instances[$instance] = $object;
}
} /*** end of class ***/