1: <?php
2: /**
3: * ManiaLive - TrackMania dedicated server manager in PHP
4: *
5: * @copyright Copyright (c) 2009-2011 NADEO (http://www.nadeo.com)
6: * @license http://www.gnu.org/licenses/lgpl.html LGPL License 3
7: * @version $Revision: 440 $:
8: * @author $Author: martin.gwendal $:
9: * @date $Date: 2012-08-17 12:03:09 +0200 (ven., 17 août 2012) $:
10: */
11:
12: namespace ManiaLive\Database\SQLite;
13:
14: use ManiaLive\Database\NotSupportedException;
15: use ManiaLive\Database\QueryException;
16: use ManiaLive\Database\DisconnectionException;
17: use ManiaLive\Database\NotConnectedException;
18: use ManiaLive\Database\Exception;
19: use ManiaLive\Database\ConnectionException;
20:
21: /**
22: * SQLite Database Connection implementation.
23: */
24: class Connection extends \ManiaLive\Database\Connection
25: {
26: /** @var \SQLite3 */
27: protected $connection;
28: protected $filename;
29:
30: function __construct($host, $username, $password, $database, $port)
31: {
32: $datapath = APP_ROOT.'data';
33: if(!file_exists($datapath))
34: mkdir($datapath);
35:
36: $this->filename = $datapath.'/'.$host.'.db';
37: try
38: {
39: $this->connection = new \SQLite3($this->filename);
40: }
41: catch(\Exception $e)
42: {
43: $this->connection = null;
44: throw new ConnectionException($e->getMessage());
45: }
46: }
47:
48: function getHandle()
49: {
50: return $this->connection;
51: }
52:
53: function setCharset($charset)
54: {
55: throw new NotSupportedException();
56: }
57:
58: function isConnected()
59: {
60: return $this->connection != null;
61: }
62:
63: function getDatabase()
64: {
65: return $this->filename;
66: }
67:
68: function affectedRows()
69: {
70: return $this->connection->changes();
71: }
72:
73: function insertID()
74: {
75: return $this->connection->lastInsertRowID();
76: }
77:
78: function execute($query)
79: {
80: if(!$this->isConnected())
81: {
82: throw new NotConnectedException;
83: }
84:
85: Connection::startMeasuring($this);
86: if(func_num_args() > 1)
87: {
88: $query = call_user_func_array('sprintf', func_get_args());
89: }
90: $result = $this->connection->query($query);
91: Connection::endMeasuring($this);
92:
93: if(!$result)
94: {
95: throw new QueryException($this->connection->lastErrorMsg(), $this->connection->lastErrorCode());
96: }
97: if($result instanceof \SQLite3Result)
98: {
99: return new RecordSet($result);
100: }
101: return $result;
102: }
103:
104: function disconnect()
105: {
106: if (!$this->connection->close())
107: {
108: throw new DisconnectionException;
109: }
110: }
111:
112: function quote($string)
113: {
114: return '\''.\SQLite3::escapeString($string).'\'';
115: }
116:
117: function select($database)
118: {
119: throw new NotSupportedException();
120: }
121:
122: function tableExists($tableName)
123: {
124: $table = $this->execute(
125: 'SELECT name FROM sqlite_master WHERE name=%s AND type=%s',
126: $this->quote($tableName),
127: $this->quote('table')
128: );
129: return $table->recordAvailable();
130: }
131: }
132:
133: ?>
134: