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

Prepared::exec()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 12
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\Statement\DBAL;
12
13
14
/**
15
 * Database statement class for prepared DBAL statements
16
 *
17
 * @package MW
18
 * @subpackage DB
19
 */
20
class Prepared extends \Aimeos\MW\DB\Statement\Base implements \Aimeos\MW\DB\Statement\Iface
21
{
22
	private $binds = [];
23
	private $sql;
24
25
26
	/**
27
	 * Initializes the statement object
28
	 *
29
	 * @param \Aimeos\MW\DB\Connection\DBAL $conn Database connection object
30
	 * @param string $sql SQL statement
31
	 */
32
	public function __construct( \Aimeos\MW\DB\Connection\DBAL $conn, $sql )
33
	{
34
		parent::__construct( $conn );
35
		$this->sql = $sql;
36
	}
37
38
39
	/**
40
	 * Binds a value to a parameter in the statement
41
	 *
42
	 * @param integer $position Position index of the placeholder
43
	 * @param mixed $value Value which should be bound to the placeholder
44
	 * @param integer $type Type of given value defined in \Aimeos\MW\DB\Statement\Base as constant
45
	 * @throws \Aimeos\MW\DB\Exception If an error occured in the unterlying driver
46
	 */
47
	public function bind( $position, $value, $type = \Aimeos\MW\DB\Statement\Base::PARAM_STR )
48
	{
49
		$this->binds[$position] = [$value, $type];
50
	}
51
52
53
	/**
54
	 * Executes the statement
55
	 *
56
	 * @return \Aimeos\MW\DB\Result\Iface Result object
57
	 * @throws \Aimeos\MW\DB\Exception If an error occured in the unterlying driver
58
	 */
59
	public function execute()
60
	{
61
		try
62
		{
63
			$stmt = $this->exec();
64
		}
65
		catch( \Doctrine\DBAL\DBALException $e )
66
		{
67
			try {
68
				$stmt = $this->reconnect( $e )->exec();
69
			} catch( \Doctrine\DBAL\DBALException $e ) {
70
				throw new \Aimeos\MW\DB\Exception( $e->getMessage(), $e->getCode() );
71
			}
72
		}
73
74
		return new \Aimeos\MW\DB\Result\DBAL( $stmt );
75
	}
76
77
78
	/**
79
	 * Binds the parameters and executes the SQL statment
80
	 *
81
	 * @return \Doctrine\DBAL\Driver\Statement Executed DBAL statement
82
	 */
83
	protected function exec()
84
	{
85
		$stmt = $this->getConnection()->getRawObject()->prepare( $this->sql );
86
87
		foreach( $this->binds as $position => $list ) {
88
			$stmt->bindValue( $position, $list[0], $this->getPdoType( $list[1], $list[0] ) );
89
		}
90
91
		$stmt->execute();
92
93
		return $stmt;
94
	}
95
}
96