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 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\Statement\PDO;
13
14
15
/**
16
 * Database statement class for prepared PDO statements.
17
 *
18
 * @package MW
19
 * @subpackage DB
20
 */
21
class Prepared extends \Aimeos\MW\DB\Statement\Base implements \Aimeos\MW\DB\Statement\Iface
22
{
23
	private $binds = [];
24
	private $sql;
25
26
27
	/**
28
	 * Initializes the statement object
29
	 *
30
	 * @param \Aimeos\MW\DB\Connection\PDO $conn Database connection object
31
	 * @param string $sql SQL statement
32
	 */
33
	public function __construct( \Aimeos\MW\DB\Connection\PDO $conn, $sql )
34
	{
35
		parent::__construct( $conn );
36
		$this->sql = $sql;
37
	}
38
39
40
	/**
41
	 * Binds a value to a parameter in the statement
42
	 *
43
	 * @param integer $position Position index of the placeholder
44
	 * @param mixed $value Value which should be bound to the placeholder
45
	 * @param integer $type Type of given value defined in \Aimeos\MW\DB\Statement\Base as constant
46
	 * @throws \Aimeos\MW\DB\Exception If an error occured in the unterlying driver
47
	 */
48
	public function bind( $position, $value, $type = \Aimeos\MW\DB\Statement\Base::PARAM_STR )
49
	{
50
		$this->binds[$position] = [$value, $type];
51
	}
52
53
54
	/**
55
	 * Executes the statement
56
	 *
57
	 * @return \Aimeos\MW\DB\Result\Iface Result object
58
	 * @throws \Aimeos\MW\DB\Exception If an error occured in the unterlying driver
59
	 */
60
	public function execute()
61
	{
62
		try
63
		{
64
			$stmt = $this->exec();
65
		}
66
		catch( \PDOException $e )
67
		{
68
			try {
69
				$stmt = $this->reconnect( $e )->exec();
70
			} catch( \PDOException $e ) {
71
				throw new \Aimeos\MW\DB\Exception( $e->getMessage(), $e->getCode(), $e->errorInfo );
72
			}
73
		}
74
75
		return new \Aimeos\MW\DB\Result\PDO( $stmt );
0 ignored issues
show
Documentation introduced by
$stmt is of type object<Doctrine\DBAL\Statement>, but the function expects a object<PDOStatement>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
	}
77
78
79
	/**
80
	 * Binds the parameters and executes the SQL statment
81
	 *
82
	 * @return \Doctrine\DBAL\Driver\Statement Executed DBAL statement
83
	 */
84
	protected function exec()
85
	{
86
		$stmt = $this->getConnection()->getRawObject()->prepare( $this->sql );
87
88
		foreach( $this->binds as $position => $list ) {
89
			$stmt->bindValue( $position, $list[0], $this->getPdoType( $list[1], $list[0] ) );
90
		}
91
92
		$stmt->execute();
93
94
		return $stmt;
95
	}
96
}
97