Completed
Push — master ( bedd0e...7ec89a )
by Aimeos
08:44
created

lib/mwlib/src/MW/DB/Statement/PDO/Prepared.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 );
76
	}
77
78
79
	/**
80
	 * Binds the parameters and executes the SQL statment
81
	 *
82
	 * @return \PDOStatement Executed PDO statement
0 ignored issues
show
Should the return type not be \Doctrine\DBAL\Statement?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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