summaryrefslogtreecommitdiff
path: root/framework/DataAccess/TAdodbProvider.php
blob: 65e336a916d75ce70e7402e8d7ea0f9232f2058a (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php

Prado::using('System.DataAccess.TDatabaseProvider');

/**
 * Adbodb data access module.
 *
 * Usage:
 * <code>
 * $provider = new TAdodbProvider;
 * $provider->setConnectionString($dsn);
 * $connection = $provider->getConnection();
 * $resultSet = $connection->execute('....');
 * </code>
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version $Revision: $  $Date: $
 * @package System.DataAccess
 * @since 3.0
 */
class TAdodbProvider extends TDatabaseProvider
{
	const FETCH_ASSOCIATIVE='associative';
	const FETCH_NUMERIC='numeric';
	const FETCH_BOTH='both';
	const FETCH_DEFAULT='default';

	private $_connection = null;
	private $_cachedir='';
	private $_fetchMode = 'associative';

	private static $_hasImported=false;

	private $_adodbLibrary='';

	public function getConnection()
	{
		if(is_null($this->_connection))
		{
			$this->importAdodbLibrary();
			$this->_connection = new TAdodbConnection($this);
		}
		return $this->_connection;
	}

	/**
	 * @return string the cache directory for adodb module
	 */
	public function getCacheDir()
	{
		return $this->_cachedir;
	}

	public function getAdodbLibrary()
	{
		if(strlen($this->_adodbLibrary) < 1)
			return dirname(__FILE__).'/adodb';
		else
			return $this->_adodbLibrary;
	}

	public function setAdodbLibrary($path)
	{
		$this->_adodbLibrary = Prado::getPathOfNamespace($path);
	}

	public function importAdodbLibrary()
	{
		if(!self::$_hasImported)
		{
			require($this->getAdodbLibrary().'/adodb-exceptions.inc.php');
			require($this->getAdodbLibrary().'/adodb.inc.php');
			self::$_hasImported = true;
		}
	}

	/**
	 * Sets the cache directory for ADODB (in adodb it is
	 * called to $ADODB_CACHE_DIR)
	 * @param string the cache directory for adodb module
	 */
	public function setCacheDir($value)
	{
		$this->_cachedir=$value;
	}

	/**
	 * @return string fetch mode of query data
	 */
	public function getFetchMode()
	{
		return $this->_fetchMode;
	}

	/**
	 * Sets the fetch mode of query data: Associative, Numeric, Both, Default (default)
	 * @param string the fetch mode of query data
	 */
	public function setFetchMode($value)
	{
		$value = strtolower($value);
		if($value===self::FETCH_ASSOCIATIVE || $value===self::FETCH_NUMERIC
				|| $value===self::FETCH_BOTH)
			$this->_fetchMode=$value;
		else
			$this->_fetchMode=self::FETCH_DEFAULT;
	}

}

/**
 * TAdodbConnection is a wrapper class of the ADODB ADOConnection class.
 * For more information about the ADODB library, see {@link http://adodb.sourceforge.net/}.
 *
 * You can call any method implemented in ADOConnection class via TAdodbConnection,
 * such as TAdodbConnection::FetchRow(), and so on. The method calls
 * will be passed to ADOConnection class.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version $Revision: $  $Date: $
 * @package System.DataAccess
 * @since 3.0
 */
class TAdodbConnection extends TDbConnection
{
	private $_connection;

	/**
	 * Constructor, initialize a new Adodb connection.
	 * @param string|TAdodbProvider DSN connection string or a TAdodbProvider
	 */
	public function __construct($provider=null)
	{
		parent::__construct($provider);
		if(is_string($provider))
			$this->initProvider($provider);
	}

	/**
	 * Create a new provider for this connection using the DSN string.
	 * @param string DSN connection string.
	 */
	protected function initProvider($connectionString)
	{
		$provider  = new TAdodbProvider();
		$provider->setConnectionString($connectionString);
		$this->setProvider($provider);
	}

	/**
	 * PHP magic function.
	 * This method will pass all method calls to ADOConnection class
	 * provided in the ADODB library.
	 * @param mixed method name
	 * @param mixed method call parameters
	 * @param mixed return value of the method call
	 */
	public function __call($method, $params)
	{
		if(is_null($this->_connection) || !$this->_connection->IsConnected())
			$this->open();
		return call_user_func_array(array($this->_connection,$method),$params);
	}

	/**
	 * Cleanup work before serializing.
	 * This is a PHP defined magic method.
	 * @return array the names of instance-variables to serialize.
	 */
	public function __sleep()
	{
		$this->close();
		return array_keys(get_object_vars($this));
	}

	/**
	 * This method will be automatically called when unserialization happens.
	 * This is a PHP defined magic method.
	 */
	public function __wakeup()
	{
	}


	public function getIsClosed()
	{
		return is_null($this->_connection) || !$this->_connection->IsConnected();
	}

	public function prepare($statement)
	{
		return $this->_connection->prepare($statement);
	}

	public function execute($sql, $parameters=array())
	{
		return $this->_connection->execute($sql, $parameters);
	}

	/**
	 * Start a transaction on this connection.
	 */
	public function beginTransaction()
	{
		return $this->_connection->StartTrans();
	}

	/**
	 * Finish and cleanup transactions.
	 */
	public function completeTranaction()
	{
		return $this->connection->CompleteTrans();
	}

	/**
	 * Fail the current transaction.
	 */
	public function failTransaction()
	{
		return $this->connection->FailTrans();
	}

	/**
	 * @return boolean true if transaction has failed.
	 */
	public function getHasTransactionFailed()
	{
		return $this->connection->HasFailedTrans();
	}

	public function commit()
	{
		return $this->connection->CommitTrans();
	}


	public function rollback()
	{
		return $this->connection->RollbackTrans();
	}

	/**
	 * Establishes a DB connection.
	 * An ADOConnection instance will be created if none.
	 */
	public function open()
	{
		if($this->getIsClosed())
		{
			$provider = $this->getProvider();
			$provider->importAdodbLibrary();
			if(strlen($provider->getConnectionString()) < 1)
			{
				if(strlen($provider->getDriver()) < 1)
					throw new TDbConnectionException('db_driver_required');

				$this->_connection=ADONewConnection($provider->getDriver());
				$this->initConnection();
			}
			else
				$this->_connection=ADONewConnection($provider->getConnectionString());
			$this->initFetchMode();
			$this->initCacheDir();
		}
		return $this->_connection->IsConnected();
	}

	/**
	 * Complete the database connection.
	 */
	protected function initConnection()
	{
		$provider = $this->getProvider();

		if($provider->getUsePersistentConnection())
		{
			$this->_connection->PConnect($provider->getHost(),
				$provider->getUsername(),$provider->getPassword(),
					$provider->getDatabase());
		}
		else
		{
			$this->_connection->Connect($provider->getHost(),
				$provider->getUsername(),$provider->getPassword(),
					$provider->getDatabase());
		}
	}

	/**
	 * Initialize the fetch mode.
	 */
	protected function initFetchMode()
	{
		global $ADODB_FETCH_MODE;
		$provider = $this->getProvider();
		if($provider->getFetchMode()===TAdodbProvider::FETCH_ASSOCIATIVE)
			$ADODB_FETCH_MODE=ADODB_FETCH_ASSOC;
		else if($provider->fetchMode===TAdodbProvider::FETCH_NUMERIC)
			$ADODB_FETCH_MODE=ADODB_FETCH_NUM;
		else if($provider->fetchMode===TAdodbProvider::FETCH_BOTH)
			$ADODB_FETCH_MODE=ADODB_FETCH_BOTH;
		else
			$ADODB_FETCH_MODE=ADODB_FETCH_DEFAULT;
	}

	/**
	 * Initialize the cache directory.
	 */
	protected function initCacheDir()
	{
		global $ADODB_CACHE_DIR;
		$provider = $this->getProvider();
		if($provider->getCacheDir()!=='')
			$ADODB_CACHE_DIR=$provider->getCacheDir();
	}

	/**
	 * Closes the DB connection.
	 * You are not required to call this method as PHP will automatically
	 * to close any DB connections when exiting a script.
	 */
	public function close()
	{
		if(!is_null($this->_connection) && $this->_connection->IsConnected())
			$this->_connection->Close();
	}

	/**
	 * @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)
	{
		return $this->_connection->qstr($string, $magic_quotes);
	}

}

?>