Completed
Push — master ( 212c5f...8d6a89 )
by Aimeos
08:59
created

PDO   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 148
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 6

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A connect() 0 17 2
A create() 0 16 4
A getRawObject() 0 4 1
A inTransaction() 0 8 2
A begin() 0 9 3
A commit() 0 9 3
A rollback() 0 16 4
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2016
7
 * @package MW
8
 * @subpackage DB
9
 */
10
11
12
namespace Aimeos\MW\DB\Connection;
13
14
15
/**
16
 * Database connection class for \PDO connections.
17
 *
18
 * @package MW
19
 * @subpackage DB
20
 */
21
class PDO extends \Aimeos\MW\DB\Connection\Base implements \Aimeos\MW\DB\Connection\Iface
22
{
23
	private $connection;
24
	private $txnumber = 0;
25
	private $stmts = array();
26
27
28
	/**
29
	 * Initializes the PDO connection object.
30
	 *
31
	 * @param array $params Associative list of connection parameters
32
	 * @param string[] $stmts List of SQL statements to execute after connecting
33
	 */
34
	public function __construct( array $params, array $stmts )
35
	{
36
		parent::__construct( $params );
37
38
		$this->stmts = $stmts;
39
		$this->connect();
40
	}
41
42
43
	/**
44
	 * Connects (or reconnects) to the database server
45
	 *
46
	 * @return \Aimeos\MW\DB\Connection\Iface Connection instance for method chaining
47
	 */
48
	public function connect()
49
	{
50
		unset( $this->connection );
51
		list( $dsn, $user, $pass, $attr ) = $this->getParameters();
52
53
		$pdo = new \PDO( $dsn, $user, $pass, $attr );
54
		$pdo->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION );
55
56
		$this->connection = $pdo;
57
		$this->txnumber = 0;
58
59
		foreach( $this->stmts as $stmt ) {
60
			$this->create( $stmt )->execute()->finish();
61
		}
62
63
		return $this;
64
	}
65
66
67
	/**
68
	 * Creates a \PDO database statement.
69
	 *
70
	 * @param string $sql SQL statement, maybe with place holders
71
	 * @param integer $type Simple or prepared statement type constant from abstract class
72
	 * @return \Aimeos\MW\DB\Statement\Iface \PDO statement object
73
	 * @throws \Aimeos\MW\DB\Exception if type is invalid or the \PDO object throws an exception
74
	 */
75
	public function create( $sql, $type = \Aimeos\MW\DB\Connection\Base::TYPE_SIMPLE )
76
	{
77
		try {
78
			switch( $type )
79
			{
80
				case \Aimeos\MW\DB\Connection\Base::TYPE_SIMPLE:
81
					return new \Aimeos\MW\DB\Statement\PDO\Simple( $this, $sql );
82
				case \Aimeos\MW\DB\Connection\Base::TYPE_PREP:
83
					return new \Aimeos\MW\DB\Statement\PDO\Prepared( $this, $sql );
84
				default:
85
					throw new \Aimeos\MW\DB\Exception( sprintf( 'Invalid value "%1$d" for statement type', $type ) );
86
			}
87
		} catch( \PDOException $e ) {
88
			throw new \Aimeos\MW\DB\Exception($e->getMessage(), $e->getCode(), $e->errorInfo );
89
		}
90
	}
91
92
93
	/**
94
	 * Returns the underlying connection object
95
	 *
96
	 * @return \PDO Underlying connection object
97
	 */
98
	public function getRawObject()
99
	{
100
		return $this->connection;
101
	}
102
103
104
	/**
105
	 * Checks if a transaction is currently running
106
	 *
107
	 * @return boolean True if transaction is currently running, false if not
108
	 */
109
	public function inTransaction()
110
	{
111
		if( $this->txnumber > 0 ) {
112
			return true;
113
		}
114
115
		return false;
116
	}
117
118
119
	/**
120
	 * Starts a transaction for this connection.
121
	 * Transactions can't be nested and a new transaction can only be started
122
	 * if the previous transaction was committed or rolled back before.
123
	 */
124
	public function begin()
125
	{
126
		if( $this->txnumber++ === 0 )
127
		{
128
			if( $this->connection->beginTransaction() === false ) {
129
				throw new \Aimeos\MW\DB\Exception( 'Unable to start new transaction' );
130
			}
131
		}
132
	}
133
134
135
	/**
136
	 * Commits the changes done inside of the transaction to the storage.
137
	 */
138
	public function commit()
139
	{
140
		if( --$this->txnumber === 0 )
141
		{
142
			if( $this->connection->commit() === false ) {
143
				throw new \Aimeos\MW\DB\Exception( 'Failed to commit transaction' );
144
			}
145
		}
146
	}
147
148
149
	/**
150
	 * Discards the changes done inside of the transaction.
151
	 */
152
	public function rollback()
153
	{
154
		try
155
		{
156
			if( --$this->txnumber === 0 )
157
			{
158
				if( $this->connection->rollBack() === false ) {
159
					throw new \Aimeos\MW\DB\Exception( 'Failed to roll back transaction' );
160
				}
161
			}
162
		}
163
		catch( \PDOException $e )
164
		{
165
			throw new \Aimeos\MW\DB\Exception( $e->getMessage(), $e->getCode(), $e->errorInfo );
166
		}
167
	}
168
}
169