Passed
Branch php-cs-fixer (b9836a)
by Fabio
15:02
created

TDbConnection::setColumnCase()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 1
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * TDbConnection class file
4
 *
5
 * @author Qiang Xue <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @copyright Copyright &copy; 2005-2016 The PRADO Group
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 * @package Prado\Data
10
 */
11
12
namespace Prado\Data;
13
14
use PDO;
15
use PDOException;
16
use Prado\Data\Common\TDbMetaData;
17
use Prado\Exceptions\TDbException;
18
use Prado\Prado;
19
use Prado\TPropertyValue;
20
21
22
/**
23
 * TDbConnection class
24
 *
25
 * TDbConnection represents a connection to a database.
26
 *
27
 * TDbConnection works together with {@link TDbCommand}, {@link TDbDataReader}
28
 * and {@link TDbTransaction} to provide data access to various DBMS
29
 * in a common set of APIs. They are a thin wrapper of the {@link http://www.php.net/manual/en/ref.pdo.php PDO}
30
 * PHP extension.
31
 *
32
 * To establish a connection, set {@link setActive Active} to true after
33
 * specifying {@link setConnectionString ConnectionString}, {@link setUsername Username}
34
 * and {@link setPassword Password}.
35
 *
36
 * Since 3.1.2, the connection charset can be set (for MySQL and PostgreSQL databases only)
37
 * using the {@link setCharset Charset} property. The value of this property is database dependant.
38
 * e.g. for mysql, you can use 'latin1' for cp1252 West European, 'utf8' for unicode, ...
39
 *
40
 * The following example shows how to create a TDbConnection instance and establish
41
 * the actual connection:
42
 * <code>
43
 * $connection=new TDbConnection($dsn,$username,$password);
44
 * $connection->Active=true;
45
 * </code>
46
 *
47
 * After the DB connection is established, one can execute an SQL statement like the following:
48
 * <code>
49
 * $command=$connection->createCommand($sqlStatement);
50
 * $command->execute();   // a non-query SQL statement execution
51
 * // or execute an SQL query and fetch the result set
52
 * $reader=$command->query();
53
 *
54
 * // each $row is an array representing a row of data
55
 * foreach($reader as $row) ...
56
 * </code>
57
 *
58
 * One can do prepared SQL execution and bind parameters to the prepared SQL:
59
 * <code>
60
 * $command=$connection->createCommand($sqlStatement);
61
 * $command->bindParameter($name1,$value1);
62
 * $command->bindParameter($name2,$value2);
63
 * $command->execute();
64
 * </code>
65
 *
66
 * To use transaction, do like the following:
67
 * <code>
68
 * $transaction=$connection->beginTransaction();
69
 * try
70
 * {
71
 *    $connection->createCommand($sql1)->execute();
72
 *    $connection->createCommand($sql2)->execute();
73
 *    //.... other SQL executions
74
 *    $transaction->commit();
75
 * }
76
 * catch(Exception $e)
77
 * {
78
 *    $transaction->rollBack();
79
 * }
80
 * </code>
81
 *
82
 * TDbConnection provides a set of methods to support setting and querying
83
 * of certain DBMS attributes, such as {@link getNullConversion NullConversion}.
84
 *
85
 * @author Qiang Xue <[email protected]>
86
 * @package Prado\Data
87
 * @since 3.0
88
 */
89
class TDbConnection extends \Prado\TComponent
90
{
91
	/**
92
	 *
93
	 * @since 3.1.7
94
	 */
95
	const DEFAULT_TRANSACTION_CLASS = 'System.Data.TDbTransaction';
96
97
	private $_dsn = '';
98
	private $_username = '';
99
	private $_password = '';
100
	private $_charset = '';
101
	private $_attributes = [];
102
	private $_active = false;
103
	private $_pdo;
104
	private $_transaction;
105
106
	/**
107
	 * @var TDbMetaData
108
	 */
109
	private $_dbMeta;
110
111
	/**
112
	 * @var string
113
	 * @since 3.1.7
114
	 */
115
	private $_transactionClass = self::DEFAULT_TRANSACTION_CLASS;
116
117
	/**
118
	 * Constructor.
119
	 * Note, the DB connection is not established when this connection
120
	 * instance is created. Set {@link setActive Active} property to true
121
	 * to establish the connection.
122
	 * Since 3.1.2, you can set the charset for MySql connection
123
	 *
124
	 * @param string The Data Source Name, or DSN, contains the information required to connect to the database.
125
	 * @param string The user name for the DSN string.
126
	 * @param string The password for the DSN string.
127
	 * @param string Charset used for DB Connection (MySql & pgsql only). If not set, will use the default charset of your database server
0 ignored issues
show
Bug introduced by
The type Prado\Data\Charset was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
128
	 * @see http://www.php.net/manual/en/function.PDO-construct.php
129
	 */
130
	public function __construct($dsn = '', $username = '', $password = '', $charset = '')
131
	{
132
		$this->_dsn = $dsn;
133
		$this->_username = $username;
134
		$this->_password = $password;
135
		$this->_charset = $charset;
136
	}
137
138
	/**
139
	 * Close the connection when serializing.
140
	 */
141
	public function __sleep()
142
	{
143
//		$this->close(); - DO NOT CLOSE the current connection as serializing doesn't neccessarily mean we don't this connection anymore in the current session
144
		return array_diff(parent::__sleep(), ["\0Prado\Data\TDbConnection\0_pdo","\0Prado\Data\TDbConnection\0_active"]);
145
	}
146
147
	/**
148
	 * @return array list of available PDO drivers
149
	 * @see http://www.php.net/manual/en/function.PDO-getAvailableDrivers.php
150
	 */
151
	public static function getAvailableDrivers()
152
	{
153
		return PDO::getAvailableDrivers();
154
	}
155
156
	/**
157
	 * @return boolean whether the DB connection is established
158
	 */
159
	public function getActive()
160
	{
161
		return $this->_active;
162
	}
163
164
	/**
165
	 * Open or close the DB connection.
166
	 * @param boolean whether to open or close DB connection
0 ignored issues
show
Bug introduced by
The type Prado\Data\whether was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
167
	 * @throws TDbException if connection fails
168
	 */
169
	public function setActive($value)
170
	{
171
		$value = TPropertyValue::ensureBoolean($value);
172
		if($value !== $this->_active)
173
		{
174
			if($value)
175
				$this->open();
176
			else
177
				$this->close();
178
		}
179
	}
180
181
	/**
182
	 * Opens DB connection if it is currently not
183
	 * @throws TDbException if connection fails
184
	 */
185
	protected function open()
186
	{
187
		if($this->_pdo === null)
188
		{
189
			try
190
			{
191
				$this->_pdo = new PDO($this->getConnectionString(),$this->getUsername(),
192
									$this->getPassword(), $this->_attributes);
193
				// This attribute is only useful for PDO::MySql driver.
194
				// Ignore the warning if a driver doesn't understand this.
195
				@$this->_pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for setAttribute(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

195
				/** @scrutinizer ignore-unhandled */ @$this->_pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
196
				$this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
197
				$this->_active = true;
198
				$this->setConnectionCharset();
199
			}
200
			catch(PDOException $e)
201
			{
202
				throw new TDbException('dbconnection_open_failed', $e->getMessage());
203
			}
204
		}
205
	}
206
207
	/**
208
	 * Closes the currently active DB connection.
209
	 * It does nothing if the connection is already closed.
210
	 */
211
	protected function close()
212
	{
213
		$this->_pdo = null;
214
		$this->_active = false;
215
	}
216
217
	/*
218
	 * Set the database connection charset.
219
	 * Only MySql databases are supported for now.
220
	 * @since 3.1.2
221
	 */
222
	protected function setConnectionCharset()
223
	{
224
		if ($this->_charset === '' || $this->_active === false)
225
			return;
226
		switch ($this->_pdo->getAttribute(PDO::ATTR_DRIVER_NAME))
227
		{
228
			case 'mysql':
229
			case 'sqlite':
230
				$stmt = $this->_pdo->prepare('SET NAMES ?');
231
			break;
232
			case 'pgsql':
233
				$stmt = $this->_pdo->prepare('SET client_encoding TO ?');
234
			break;
235
			default:
236
				throw new TDbException('dbconnection_unsupported_driver_charset', $driver);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $driver seems to be never defined.
Loading history...
237
		}
238
		$stmt->execute([$this->_charset]);
239
	}
240
241
	/**
242
	 * @return string The Data Source Name, or DSN, contains the information required to connect to the database.
243
	 */
244
	public function getConnectionString()
245
	{
246
		return $this->_dsn;
247
	}
248
249
	/**
250
	 * @param string The Data Source Name, or DSN, contains the information required to connect to the database.
0 ignored issues
show
Bug introduced by
The type Prado\Data\The was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
251
	 * @see http://www.php.net/manual/en/function.PDO-construct.php
252
	 */
253
	public function setConnectionString($value)
254
	{
255
		$this->_dsn = $value;
256
	}
257
258
	/**
259
	 * @return string the username for establishing DB connection. Defaults to empty string.
260
	 */
261
	public function getUsername()
262
	{
263
		return $this->_username;
264
	}
265
266
	/**
267
	 * @param string the username for establishing DB connection
0 ignored issues
show
Bug introduced by
The type Prado\Data\the was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
268
	 */
269
	public function setUsername($value)
270
	{
271
		$this->_username = $value;
272
	}
273
274
	/**
275
	 * @return string the password for establishing DB connection. Defaults to empty string.
276
	 */
277
	public function getPassword()
278
	{
279
		return $this->_password;
280
	}
281
282
	/**
283
	 * @param string the password for establishing DB connection
284
	 */
285
	public function setPassword($value)
286
	{
287
		$this->_password = $value;
288
	}
289
290
	/**
291
	 * @return string the charset used for database connection. Defaults to emtpy string.
292
	 */
293
	public function getCharset ()
294
	{
295
		return $this->_charset;
296
	}
297
298
	/**
299
	 * @param string the charset used for database connection
300
	 */
301
	public function setCharset ($value)
302
	{
303
		$this->_charset = $value;
304
		$this->setConnectionCharset();
305
	}
306
307
	/**
308
	 * @return PDO the PDO instance, null if the connection is not established yet
309
	 */
310
	public function getPdoInstance()
311
	{
312
		return $this->_pdo;
313
	}
314
315
	/**
316
	 * Creates a command for execution.
317
	 * @param string SQL statement associated with the new command.
318
	 * @return TDbCommand the DB command
319
	 * @throws TDbException if the connection is not active
320
	 */
321
	public function createCommand($sql)
322
	{
323
		if($this->getActive())
324
			return new TDbCommand($this, $sql);
325
		else
326
			throw new TDbException('dbconnection_connection_inactive');
327
	}
328
329
	/**
330
	 * @return TDbTransaction the currently active transaction. Null if no active transaction.
331
	 */
332
	public function getCurrentTransaction()
333
	{
334
		if($this->_transaction !== null)
335
		{
336
			if($this->_transaction->getActive())
337
				return $this->_transaction;
338
		}
339
		return null;
340
	}
341
342
	/**
343
	 * Starts a transaction.
344
	 * @return TDbTransaction the transaction initiated
345
	 * @throws TDbException if the connection is not active
346
	 */
347
	public function beginTransaction()
348
	{
349
		if($this->getActive())
350
		{
351
			$this->_pdo->beginTransaction();
352
			return $this->_transaction = Prado::createComponent($this->getTransactionClass(), $this);
353
		}
354
		else
355
			throw new TDbException('dbconnection_connection_inactive');
356
	}
357
358
	/**
359
	 * @return string Transaction class name to be created by calling {@link TDbConnection::beginTransaction}. Defaults to 'System.Data.TDbTransaction'.
360
	 * @since 3.1.7
361
	 */
362
	public function getTransactionClass()
363
	{
364
		return $this->_transactionClass;
365
	}
366
367
368
	/**
369
	 * @param string Transaction class name to be created by calling {@link TDbConnection::beginTransaction}.
0 ignored issues
show
Bug introduced by
The type Prado\Data\Transaction was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
370
	 * @since 3.1.7
371
	 */
372
	public function setTransactionClass($value)
373
	{
374
		$this->_transactionClass = (string)$value;
375
	}
376
377
	/**
378
	 * Returns the ID of the last inserted row or sequence value.
379
	 * @param string name of the sequence object (required by some DBMS)
0 ignored issues
show
Bug introduced by
The type Prado\Data\name was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
380
	 * @return string the row ID of the last row inserted, or the last value retrieved from the sequence object
381
	 * @see http://www.php.net/manual/en/function.PDO-lastInsertId.php
382
	 */
383
	public function getLastInsertID($sequenceName = '')
384
	{
385
		if($this->getActive())
386
			return $this->_pdo->lastInsertId($sequenceName);
387
		else
388
			throw new TDbException('dbconnection_connection_inactive');
389
	}
390
391
	/**
392
	 * Quotes a string for use in a query.
393
	 * @param string string to be quoted
394
	 * @return string the properly quoted string
395
	 * @see http://www.php.net/manual/en/function.PDO-quote.php
396
	 */
397
	public function quoteString($str)
398
	{
399
		if($this->getActive())
400
			return $this->_pdo->quote($str);
401
		else
402
			throw new TDbException('dbconnection_connection_inactive');
403
	}
404
405
	/**
406
	 * Quotes a table name for use in a query.
407
	 * @param string $name table name
408
	 * @return string the properly quoted table name
409
	 */
410
	public function quoteTableName($name)
411
	{
412
		return $this->getDbMetaData()->quoteTableName($name);
413
	}
414
415
	/**
416
	 * Quotes a column name for use in a query.
417
	 * @param string $name column name
418
	 * @return string the properly quoted column name
419
	 */
420
	public function quoteColumnName($name)
421
	{
422
		return $this->getDbMetaData()->quoteColumnName($name);
423
	}
424
425
	/**
426
	 * Quotes a column alias for use in a query.
427
	 * @param string $name column name
428
	 * @return string the properly quoted column alias
429
	 */
430
	public function quoteColumnAlias($name)
431
	{
432
		return $this->getDbMetaData()->quoteColumnAlias($name);
433
	}
434
435
	/**
436
	 * @return TDbMetaData
437
	 */
438
	public function getDbMetaData()
439
	{
440
		if($this->_dbMeta === null)
441
		{
442
			$this->_dbMeta = TDbMetaData::getInstance($this);
443
		}
444
		return $this->_dbMeta;
445
	}
446
447
	/**
448
	 * @return TDbColumnCaseMode the case of the column names
449
	 */
450
	public function getColumnCase()
451
	{
452
		switch($this->getAttribute(PDO::ATTR_CASE))
453
		{
454
			case PDO::CASE_NATURAL:
455
				return TDbColumnCaseMode::Preserved;
0 ignored issues
show
Bug Best Practice introduced by
The expression return Prado\Data\TDbColumnCaseMode::Preserved returns the type string which is incompatible with the documented return type Prado\Data\TDbColumnCaseMode.
Loading history...
456
			case PDO::CASE_LOWER:
457
				return TDbColumnCaseMode::LowerCase;
0 ignored issues
show
Bug Best Practice introduced by
The expression return Prado\Data\TDbColumnCaseMode::LowerCase returns the type string which is incompatible with the documented return type Prado\Data\TDbColumnCaseMode.
Loading history...
458
			case PDO::CASE_UPPER:
459
				return TDbColumnCaseMode::UpperCase;
0 ignored issues
show
Bug Best Practice introduced by
The expression return Prado\Data\TDbColumnCaseMode::UpperCase returns the type string which is incompatible with the documented return type Prado\Data\TDbColumnCaseMode.
Loading history...
460
		}
461
	}
462
463
	/**
464
	 * @param TDbColumnCaseMode the case of the column names
465
	 */
466
	public function setColumnCase($value)
467
	{
468
		switch(TPropertyValue::ensureEnum($value, 'Prado\\Data\\TDbColumnCaseMode'))
469
		{
470
			case TDbColumnCaseMode::Preserved:
471
				$value = PDO::CASE_NATURAL;
472
				break;
473
			case TDbColumnCaseMode::LowerCase:
474
				$value = PDO::CASE_LOWER;
475
				break;
476
			case TDbColumnCaseMode::UpperCase:
477
				$value = PDO::CASE_UPPER;
478
				break;
479
		}
480
		$this->setAttribute(PDO::ATTR_CASE, $value);
481
	}
482
483
	/**
484
	 * @return TDbNullConversionMode how the null and empty strings are converted
485
	 */
486
	public function getNullConversion()
487
	{
488
		switch($this->getAttribute(PDO::ATTR_ORACLE_NULLS))
489
		{
490
			case PDO::NULL_NATURAL:
491
				return TDbNullConversionMode::Preserved;
0 ignored issues
show
Bug Best Practice introduced by
The expression return Prado\Data\TDbNullConversionMode::Preserved returns the type string which is incompatible with the documented return type Prado\Data\TDbNullConversionMode.
Loading history...
492
			case PDO::NULL_EMPTY_STRING:
493
				return TDbNullConversionMode::EmptyStringToNull;
0 ignored issues
show
Bug Best Practice introduced by
The expression return Prado\Data\TDbNul...Mode::EmptyStringToNull returns the type string which is incompatible with the documented return type Prado\Data\TDbNullConversionMode.
Loading history...
494
			case PDO::NULL_TO_STRING:
495
				return TDbNullConversionMode::NullToEmptyString;
0 ignored issues
show
Bug Best Practice introduced by
The expression return Prado\Data\TDbNul...Mode::NullToEmptyString returns the type string which is incompatible with the documented return type Prado\Data\TDbNullConversionMode.
Loading history...
496
		}
497
	}
498
499
	/**
500
	 * @param TDbNullConversionMode how the null and empty strings are converted
0 ignored issues
show
Bug introduced by
The type Prado\Data\how was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
501
	 */
502
	public function setNullConversion($value)
503
	{
504
		switch(TPropertyValue::ensureEnum($value, 'Prado\\Data\\TDbNullConversionMode'))
505
		{
506
			case TDbNullConversionMode::Preserved:
507
				$value = PDO::NULL_NATURAL;
508
				break;
509
			case TDbNullConversionMode::EmptyStringToNull:
510
				$value = PDO::NULL_EMPTY_STRING;
511
				break;
512
			case TDbNullConversionMode::NullToEmptyString:
513
				$value = PDO::NULL_TO_STRING;
514
				break;
515
		}
516
		$this->setAttribute(PDO::ATTR_ORACLE_NULLS, $value);
517
	}
518
519
	/**
520
	 * @return boolean whether creating or updating a DB record will be automatically committed.
521
	 * Some DBMS (such as sqlite) may not support this feature.
522
	 */
523
	public function getAutoCommit()
524
	{
525
		return $this->getAttribute(PDO::ATTR_AUTOCOMMIT);
526
	}
527
528
	/**
529
	 * @param boolean whether creating or updating a DB record will be automatically committed.
530
	 * Some DBMS (such as sqlite) may not support this feature.
531
	 */
532
	public function setAutoCommit($value)
533
	{
534
		$this->setAttribute(PDO::ATTR_AUTOCOMMIT, TPropertyValue::ensureBoolean($value));
535
	}
536
537
	/**
538
	 * @return boolean whether the connection is persistent or not
539
	 * Some DBMS (such as sqlite) may not support this feature.
540
	 */
541
	public function getPersistent()
542
	{
543
		return $this->getAttribute(PDO::ATTR_PERSISTENT);
544
	}
545
546
	/**
547
	 * @param boolean whether the connection is persistent or not
548
	 * Some DBMS (such as sqlite) may not support this feature.
549
	 */
550
	public function setPersistent($value)
551
	{
552
		return $this->setAttribute(PDO::ATTR_PERSISTENT, TPropertyValue::ensureBoolean($value));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->setAttribute(PDO:...:ensureBoolean($value)) targeting Prado\Data\TDbConnection::setAttribute() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
553
	}
554
555
	/**
556
	 * @return string name of the DB driver
557
	 */
558
	public function getDriverName()
559
	{
560
		return $this->getAttribute(PDO::ATTR_DRIVER_NAME);
561
	}
562
563
	/**
564
	 * @return string the version information of the DB driver
565
	 */
566
	public function getClientVersion()
567
	{
568
		return $this->getAttribute(PDO::ATTR_CLIENT_VERSION);
569
	}
570
571
	/**
572
	 * @return string the status of the connection
573
	 * Some DBMS (such as sqlite) may not support this feature.
574
	 */
575
	public function getConnectionStatus()
576
	{
577
		return $this->getAttribute(PDO::ATTR_CONNECTION_STATUS);
578
	}
579
580
	/**
581
	 * @return boolean whether the connection performs data prefetching
582
	 */
583
	public function getPrefetch()
584
	{
585
		return $this->getAttribute(PDO::ATTR_PREFETCH);
586
	}
587
588
	/**
589
	 * @return string the information of DBMS server
590
	 */
591
	public function getServerInfo()
592
	{
593
		return $this->getAttribute(PDO::ATTR_SERVER_INFO);
594
	}
595
596
	/**
597
	 * @return string the version information of DBMS server
598
	 */
599
	public function getServerVersion()
600
	{
601
		return $this->getAttribute(PDO::ATTR_SERVER_VERSION);
602
	}
603
604
	/**
605
	 * @return int timeout settings for the connection
606
	 */
607
	public function getTimeout()
608
	{
609
		return $this->getAttribute(PDO::ATTR_TIMEOUT);
610
	}
611
612
	/**
613
	 * Obtains a specific DB connection attribute information.
614
	 * @param int the attribute to be queried
615
	 * @return mixed the corresponding attribute information
616
	 * @see http://www.php.net/manual/en/function.PDO-getAttribute.php
617
	 */
618
	public function getAttribute($name)
619
	{
620
		if($this->getActive())
621
			return $this->_pdo->getAttribute($name);
622
		else
623
			throw new TDbException('dbconnection_connection_inactive');
624
	}
625
626
	/**
627
	 * Sets an attribute on the database connection.
628
	 * @param int the attribute to be set
629
	 * @param mixed the attribute value
630
	 * @see http://www.php.net/manual/en/function.PDO-setAttribute.php
631
	 */
632
	public function setAttribute($name, $value)
633
	{
634
		if($this->_pdo instanceof PDO)
635
			$this->_pdo->setAttribute($name, $value);
636
		else
637
			$this->_attributes[$name] = $value;
638
	}
639
}