1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace ManiaLive\Database\MySQL;
13:
14: use ManiaLive\Database\SelectionException;
15: use ManiaLive\Database\NotSupportedException;
16: use ManiaLive\Database\QueryException;
17: use ManiaLive\Database\DisconnectionException;
18: use ManiaLive\Database\NotConnectedException;
19: use ManiaLive\Database\Exception;
20: use ManiaLive\Database\ConnectionException;
21: use ManiaLive\Event\Dispatcher;
22: use ManiaLive\Features\Tick\Event as TickEvent;
23: use ManiaLive\Features\Tick\Listener as TickListener;
24:
25: class Connection extends \ManiaLive\Database\Connection implements TickListener
26: {
27: protected $connection;
28: protected $host;
29: protected $user;
30: protected $password;
31: protected $database;
32:
33: protected $tick = 0;
34:
35: function __construct($host, $username, $password, $database, $port)
36: {
37:
38: $this->host = $host.($port ? ':'.$port : '');
39: $this->user = $username;
40: $this->password = $password;
41: $this->connect($database);
42: }
43:
44: function onTick()
45: {
46: if($this->tick++ % 3600 == 0)
47: {
48: $this->execute('SELECT 1');
49: $this->tick = 0;
50: }
51: }
52:
53: protected function connect($database)
54: {
55:
56: try
57: {
58: $this->connection = mysql_connect(
59: $this->host,
60: $this->user,
61: $this->password
62: );
63: }
64: catch(\ErrorException $err)
65: {
66: throw new ConnectionException($err->getMessage(), $err->getCode());
67: }
68:
69:
70: if(!$this->connection)
71: {
72: throw new ConnectionException;
73: }
74:
75: $this->select($database);
76:
77:
78: $this->setCharset('utf8');
79: Dispatcher::register(TickEvent::getClass(), $this);
80: }
81:
82: 83: 84: 85:
86: function getHandle()
87: {
88: return $this->connection;
89: }
90:
91: function setCharset($charset)
92: {
93: if(function_exists('mysql_set_charset'))
94: {
95: if(!$this->isConnected())
96: {
97: $this->connect($this->database);
98: }
99:
100: if(!mysql_set_charset($charset, $this->connection))
101: {
102: throw new Exception;
103: }
104: }
105: else
106: {
107: $charset = $this->quote($charset);
108: $this->execute('SET NAMES '.$charset);
109: }
110: }
111:
112: function select($database)
113: {
114: $this->database = $database;
115: if(!mysql_select_db($this->database, $this->connection))
116: {
117: throw new SelectionException(mysql_error($this->connection), mysql_errno($this->connection));
118: }
119: }
120:
121: function quote($string)
122: {
123: if(!$this->isConnected())
124: {
125: $this->connect($this->database);
126: }
127: return '\''.mysql_real_escape_string($string, $this->connection).'\'';
128: }
129:
130: function execute($query)
131: {
132: if(!$this->isConnected())
133: {
134: $this->connect($this->database);
135: }
136:
137: Connection::startMeasuring($this);
138: if(func_num_args() > 1)
139: {
140: $query = call_user_func_array('sprintf', func_get_args());
141: }
142: $result = mysql_query($query, $this->connection);
143: Connection::endMeasuring($this);
144:
145: if(!$result)
146: {
147: throw new QueryException(mysql_error($this->connection), mysql_errno($this->connection));
148: }
149: return new RecordSet($result);
150: }
151:
152: function affectedRows()
153: {
154: if(!$this->isConnected())
155: {
156: $this->connect($this->database);
157: }
158: return mysql_affected_rows($this->connection);
159: }
160:
161: function insertID()
162: {
163: if(!$this->isConnected())
164: {
165: $this->connect($this->database);
166: }
167: return mysql_insert_id($this->connection);
168: }
169:
170: function isConnected()
171: {
172: return (bool)$this->connection;
173: }
174:
175: function disconnect()
176: {
177: if(!mysql_close($this->connection))
178: {
179: throw new DisconnectionException;
180: }
181: $this->connection = null;
182: Dispatcher::unregister(TickEvent::getClass(), $this);
183: }
184:
185: function getDatabase()
186: {
187: return $this->database;
188: }
189:
190: function tableExists($tableName)
191: {
192: $table = $this->execute('SHOW TABLES LIKE '.$this->quote($tableName));
193: return ($table->recordCount() > 0);
194: }
195: }
196: ?>