summaryrefslogtreecommitdiff
path: root/framework/DataAccess/TDatabaseProvider.php
blob: 2a62c03ac1b4ea4ea1bc44f4c7620e021109202c (plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php

/**
 * Database access module.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version $Revision: $  $Date: $
 * @package System.DataAccess
 * @since 3.0
 */
abstract class TDatabaseProvider extends TModule
{
	private $_connectionString = '';
	private $_database='';
	private $_driver='';
	private $_host='';
	private $_username='';
	private $_password='';
	private $_persistent=true;

	/**
	 * @param string used to open the connection
	 */
	public function	setConnectionString($value)
	{
		$this->_connectionString = $value;
	}

	/**
	 * @return string used to open the connection
	 */
	public function getConnectionString()
	{
		return $this->_connectionString;
	}

	/**
	 * @return string the DB driver (mysql, sqlite, etc.)
	 */
	public function getDriver()
	{
		return $this->_driver;
	}

	/**
	 * Sets the DB driver (mysql, sqlite, etc.)
	 * @param string the DB driver
	 */
	public function setDriver($value)
	{
		$this->_driver=$value;
	}

	/**
	 * If the driver is <tt>sqlite</tt>, the host must be dot path to the sqlite
	 * file.
	 * @return string the DB host name/IP (and port number) in the format "host[:port]"
	 */
	public function getHost()
	{
		if(strtolower($this->getDriver()) == "sqlite")
			return Prado::getPathOfNamespace($this->_host);
		else
			return $this->_host;
	}

	/**
	 * Sets the DB host name/IP (and port number) in the format "host[:port]"
	 * @param string the DB host
	 */
	public function setHost($value)
	{
		$this->_host=$value;
	}

	/**
	 * @return string the DB username
	 */
	public function getUsername()
	{
		return $this->_username;
	}

	/**
	 * Sets the DB username
	 * @param string the DB username
	 */
	public function setUsername($value)
	{
		$this->_username=$value;
	}

	/**
	 * @return string the DB password
	 */
	public function getPassword()
	{
		return $this->_password;
	}

	/**
	 * Sets the DB password
	 * @param string the DB password
	 */
	public function setPassword($value)
	{
		$this->_password=$value;
	}

	/**
	 * @return string the database name
	 */
	public function getDatabase()
	{
		return $this->_database;
	}

	/**
	 * Sets the database name
	 * @param string the database name
	 */
	public function setDatabase($value)
	{
		$this->_database=$value;
	}

	/**
	 * @return boolean whether the DB connection is persistent
	 */
	public function getUsePersistentConnection()
	{
		return $this->_persistent;
	}

	/**
	 * Sets whether the DB connection should be persistent
	 * @param boolean whether the DB connection should be persistent
	 */
	public function setUsePersistentConnection($value)
	{
		$this->_persistent=$value;
	}

	protected function buildDsn()
	{
		$driver = $this->getDriver().'://';
		$user = $this->getUsername();
		$pass = rawurlencode($this->getPassword());
		$pass = strlen($pass) > 0 ? ':'.$pass : $pass;
		$host = $this->getHost().'/';
		if(strtolower($this->getDriver()) == 'sqlite')
			$host = rawurlencode($host);
		else
			$host = '@'.$host;
		$db = $this->getDatabase();
		$db = strlen($db) > 0 ? '/'.$db : $db;
		$persist = $this->getUsePersistentConnection() ? '': '?persit';
		
		return $driver.$user.$pass.$host.$persist;
	}

	/**
	 * @return TDbConnection a database connection
	 */
	public abstract function getConnection();
}

/**
 * A connection (session) with a specific database. SQL statements are executed
 * and results are returned within the context of a connection.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version $Revision: $  $Date: $
 * @package System.DataAccess
 * @since 3.0
 */
interface IDbConnection
{
	/**
	 * Closes the connection to the database.
	 */
	public function close();

	/**
	 * @return boolean retrieves whether this connection has been closed.
	 */
	public function getIsClosed();

	/**
	 * Opens a database connection with settings provided in the ConnectionString.
	 */
	public function open();

	/**
	 * @return string creates a prepared statement for sending parameterized
	 * SQL statements to the database.
	 */
	public function prepare($statement);

	/**
	 * Executes the SQL statement which may be any kind of SQL statement,
	 * including prepared statements.
	 * @param string sql query statement
	 * @param array subsititution parameters
	 * @return mixed result set
	 */
	public function execute($sql, $parameters=array());

	/**
	 * Start a transaction on this connection.
	 */
	public function beginTransaction();

	/**
	 * Finish and cleanup transactions.
	 */
	public function completeTransaction();

	/**
	 * Makes all changes made since the previous commit/rollback permanent and
	 * releases any database locks.
	 */
	public function commit();

	/**
	 * Undoes all changes made in the current transaction and releases any
	 * database locks
	 */
	public function rollback();

	/**
	 * @param string quote a string to be sent to the database.
	 * @param boolean if true it ensure that the variable is not quoted twice,
	 * once by quote and once by the magic_quotes_gpc.
	 * @return string database specified quoted string
	 */
	public function quote($string, $magic_quotes=false);

}

/**
 * Performs the connection to the database using a TDatabaseProvider,
 * executes SQL statements.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version $Revision: $  $Date: $
 * @package System.DataAccess
 * @since 3.0
 */
abstract class TDbConnection extends TComponent implements IDbConnection
{
	private $_provider;

	public function __construct($provider)
	{
		if($provider instanceof TDatabaseProvider)
			$this->setProvider($provider);
	}

	public function setProvider($provider)
	{
		$this->_provider = $provider;
	}

	public function getProvider()
	{
		return $this->_provider;
	}

	public function __destruct()
	{
		$this->close();
	}
}

?>