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

DBAL::inTransaction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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