/
Connection.php
181 lines (152 loc) · 4.64 KB
/
Connection.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
<?php
/**
* ManiaLive - TrackMania dedicated server manager in PHP
*
* @copyright Copyright (c) 2009-2011 NADEO (http://www.nadeo.com)
* @license http://www.gnu.org/licenses/lgpl.html LGPL License 3
* @version $Revision$:
* @author $Author$:
* @date $Date$:
*/
namespace ManiaLive\Database;
abstract class Connection
{
private static $connections = array();
private static $queryStartTime = array();
private static $queriesCount = array();
private static $queriesTotalTimes = array();
private static $queriesAverageTimes = array();
public $id;
/**
* Factory method that will create and
* manage database connections.
*
* @param string $host
* @param string $username
* @param string $password
* @param string $database
* @param string $type
* @param integer $port
* @return \ManiaLive\Database\Connection
* @throws NotSupportedException
*/
static function getConnection($host = 'localhost', $username = 'root', $password = '', $database = 'manialive', $type = 'MySQL', $port = null)
{
// create connection handle ...
$id = md5($type.'|'.$host.'|'.$username.'|'.$password.'|'.$database.'|'.$port);
// return connection already available ...
if(array_key_exists($id, self::$connections))
{
return self::$connections[$id];
}
// create class name/path depending on the database type to create ...
$className = '\\ManiaLive\\Database\\'.$type.'\\Connection';
// check whether database class exists ...
if(!class_exists($className))
{
throw new NotSupportedException('There is no connection class for the database type "'.$type.'" yet!');
}
// check whether the class is an extension of the abstract connection class ...
if(!is_subclass_of($className, '\\ManiaLive\\Database\\Connection'))
{
throw new NotSupportedException('The database type "'.$type.'" does not support the connection interface!');
}
// create connection ...
$connection = new $className($host, $username, $password, $database, $port, null);
$connection->id = $id;
// and insert into the storage ...
self::$connections[$id] = $connection;
self::$queriesCount[$id] = 0;
self::$queriesTotalTimes[$id] = 0;
self::$queriesAverageTimes[$id] = 0;
return $connection;
}
abstract function __construct($host, $username, $password, $database, $port);
/**
* Set the charset used to communicate with the database
* @param string $charset
*/
abstract function setCharset($charset);
/**
* select the given database
* @param string $database
*/
abstract function select($database);
/**
* Escape and add quotes around the given string
* @param string $string
* @return string
*/
abstract function quote($string);
/**
* Execute a query and return the result
* @deprecated use execute instead
* @return \ManiaLive\Database\RecordSet
*/
final function query($query)
{
return call_user_func_array(array($this, 'execute'), func_get_args());
}
/**
* Execute a query and return the result
* @param string $query
* @return \ManiaLive\Database\RecordSet
*/
abstract function execute($query);
/**
* Return the number of rows affected by the previous query
* @return int
*/
abstract function affectedRows();
/**
* Return the id of the last insert query
* @return int
*/
abstract function insertID();
/**
* Return true if the connection to the database is open, false in the other cases
* @return bool
*/
abstract function isConnected();
/**
* Close the current connection to the database
*/
abstract function disconnect();
/**
* Get the selected database
* @return string
*/
abstract function getDatabase();
/**
* Check if the given table exists
* @param string $table
* @return bool
*/
abstract function tableExists($table);
/**
* Return the current Connection Link
* @return resource
*/
abstract function getHandle();
static function startMeasuring(Connection $connection)
{
self::$queryStartTime[$connection->id] = microtime(true);
}
static function endMeasuring(Connection $connection)
{
++self::$queriesCount[$connection->id];
self::$queriesTotalTimes[$connection->id] += microtime(true) - self::$queryStartTime[$connection->id];
self::$queriesAverageTimes[$connection->id] = self::$queriesTotalTimes[$connection->id] / self::$queriesCount[$connection->id];
}
static function getMeasuredAverageTimes()
{
return self::$queriesAverageTimes;
}
}
class ConnectionException extends \Exception {}
class DisconnectionException extends \Exception {}
class NotSupportedException extends \Exception {}
class NotConnectedException extends \Exception {}
class SelectionException extends \Exception {}
class QueryException extends \Exception {}
?>