1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: namespace ManiaLive\Database;
14:
15: abstract class Connection
16: {
17: private static $connections = array();
18: private static $queryStartTime = array();
19: private static $queriesCount = array();
20: private static $queriesTotalTimes = array();
21: private static $queriesAverageTimes = array();
22: public $id;
23:
24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36:
37: static function getConnection($host = 'localhost', $username = 'root', $password = '', $database = 'manialive', $type = 'MySQL', $port = null)
38: {
39:
40: $id = md5($type.'|'.$host.'|'.$username.'|'.$password.'|'.$database.'|'.$port);
41:
42:
43: if(array_key_exists($id, self::$connections))
44: {
45: return self::$connections[$id];
46: }
47:
48:
49: $className = '\\ManiaLive\\Database\\'.$type.'\\Connection';
50:
51:
52: if(!class_exists($className))
53: {
54: throw new NotSupportedException('There is no connection class for the database type "'.$type.'" yet!');
55: }
56:
57:
58: if(!is_subclass_of($className, '\\ManiaLive\\Database\\Connection'))
59: {
60: throw new NotSupportedException('The database type "'.$type.'" does not support the connection interface!');
61: }
62:
63:
64: $connection = new $className($host, $username, $password, $database, $port, null);
65: $connection->id = $id;
66:
67:
68: self::$connections[$id] = $connection;
69: self::$queriesCount[$id] = 0;
70: self::$queriesTotalTimes[$id] = 0;
71: self::$queriesAverageTimes[$id] = 0;
72:
73: return $connection;
74: }
75:
76: abstract function __construct($host, $username, $password, $database, $port);
77:
78: 79: 80: 81:
82: abstract function setCharset($charset);
83:
84: 85: 86: 87:
88: abstract function select($database);
89:
90: 91: 92: 93: 94:
95: abstract function quote($string);
96:
97: 98: 99: 100: 101:
102: final function query($query)
103: {
104: return call_user_func_array(array($this, 'execute'), func_get_args());
105: }
106:
107: 108: 109: 110: 111:
112: abstract function execute($query);
113:
114: 115: 116: 117:
118: abstract function affectedRows();
119:
120: 121: 122: 123:
124: abstract function insertID();
125:
126: 127: 128: 129:
130: abstract function isConnected();
131:
132: 133: 134:
135: abstract function disconnect();
136:
137: 138: 139: 140:
141: abstract function getDatabase();
142:
143: 144: 145: 146: 147:
148: abstract function tableExists($table);
149:
150: 151: 152: 153:
154: abstract function getHandle();
155:
156: static function startMeasuring(Connection $connection)
157: {
158: self::$queryStartTime[$connection->id] = microtime(true);
159: }
160:
161: static function endMeasuring(Connection $connection)
162: {
163: ++self::$queriesCount[$connection->id];
164: self::$queriesTotalTimes[$connection->id] += microtime(true) - self::$queryStartTime[$connection->id];
165: self::$queriesAverageTimes[$connection->id] = self::$queriesTotalTimes[$connection->id] / self::$queriesCount[$connection->id];
166: }
167:
168: static function getMeasuredAverageTimes()
169: {
170: return self::$queriesAverageTimes;
171: }
172: }
173:
174: class ConnectionException extends \Exception {}
175: class DisconnectionException extends \Exception {}
176: class NotSupportedException extends \Exception {}
177: class NotConnectedException extends \Exception {}
178: class SelectionException extends \Exception {}
179: class QueryException extends \Exception {}
180:
181: ?>