Passed
Push — master ( 78ff77...606203 )
by Aimeos
04:53
created

Simple   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 24
dl 0
loc 93
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A exec() 0 26 5
A execute() 0 9 2
A bind() 0 3 1
A __construct() 0 4 1
A __toString() 0 3 1
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2022
7
 * @package MW
8
 * @subpackage DB
9
 */
10
11
12
namespace Aimeos\Base\DB\Statement\PDO;
13
14
15
/**
16
 * Database statement class for simple PDO statements
17
 *
18
 * @package MW
19
 * @subpackage DB
20
 */
21
class Simple extends \Aimeos\Base\DB\Statement\Base implements \Aimeos\Base\DB\Statement\Iface
22
{
23
	private $sql;
24
25
26
	/**
27
	 * Initializes the statement object
28
	 *
29
	 * @param \Aimeos\Base\DB\Connection\PDO $conn Database connection object
30
	 * @param string $sql SQL statement
31
	 */
32
	public function __construct( \Aimeos\Base\DB\Connection\PDO $conn, string $sql )
33
	{
34
		parent::__construct( $conn );
35
		$this->sql = $sql;
36
	}
37
38
39
	/**
40
	 * Returns the SQL string as sent to the database (magic PHP method)
41
	 *
42
	 * @return string SQL statement
43
	 */
44
	public function __toString()
45
	{
46
		return $this->sql;
47
	}
48
49
50
	/**
51
	 * Binds a value to a parameter in the statement.
52
	 *
53
	 * @param int $position Position index of the placeholder
54
	 * @param mixed $value Value which should be bound to the placeholder
55
	 * @param int $type Type of given value defined in \Aimeos\Base\DB\Statement\Base as constant
56
	 * @return \Aimeos\Base\DB\Statement\Iface Statement instance for method chaining
57
	 * @throws \Aimeos\Base\DB\Exception If the parameter type is invalid
58
	 */
59
	public function bind( int $position, $value, int $type = \Aimeos\Base\DB\Statement\Base::PARAM_STR ) : \Aimeos\Base\DB\Statement\Iface
60
	{
61
		throw new \Aimeos\Base\DB\Exception( 'Binding parameters is not available for simple statements: ' . $this->sql );
62
	}
63
64
65
	/**
66
	 * Executes the statement.
67
	 *
68
	 * @return \Aimeos\Base\DB\Result\Iface Result object
69
	 * @throws \Aimeos\Base\DB\Exception If an error occured in the unterlying driver or if the number of binds doesn't match
70
	 */
71
	public function execute() : \Aimeos\Base\DB\Result\Iface
72
	{
73
		try {
74
			$result = $this->exec();
75
		} catch( \PDOException $e ) {
76
			throw new \Aimeos\Base\DB\Exception( $e->getMessage() . ': ' . $this->sql, $e->getCode() );
77
		}
78
79
		return new \Aimeos\Base\DB\Result\PDO( $result );
80
	}
81
82
83
	/**
84
	 * Binds the parameters and executes the SQL statment
85
	 *
86
	 * @return \PDOStatement Executed PDO statement
87
	 */
88
	protected function exec() : \PDOStatement
89
	{
90
		$level = error_reporting(); // Workaround for PDO warnings
91
		$conn = $this->getConnection();
92
93
		try
94
		{
95
			error_reporting( $level & ~E_WARNING );
96
			$result = $conn->getRawObject()->query( $this->sql );
97
		}
98
		catch( \PDOException $e )
99
		{
100
			error_reporting( $level );
101
102
			// recover from lost connection (MySQL)
103
			if( !isset( $e->errorInfo[1] ) || $e->errorInfo[1] != 2006 || $conn->inTransaction() === true ) {
104
				throw $e;
105
			}
106
107
			$conn->connect();
108
			return $this->exec();
109
		}
110
111
		error_reporting( $level );
112
113
		return $result;
114
	}
115
}
116