Passed
Push — master ( 999eec...31351a )
by Aimeos
05:04
created

Simple::exec()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 5
nop 0
dl 0
loc 26
rs 9.5222
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016-2021
6
 * @package MW
7
 * @subpackage DB
8
 */
9
10
11
namespace Aimeos\MW\DB\Statement\DBAL;
12
13
14
/**
15
 * Database statement class for simple DBAL statements
16
 *
17
 * @package MW
18
 * @subpackage DB
19
 */
20
class Simple extends \Aimeos\MW\DB\Statement\Base implements \Aimeos\MW\DB\Statement\Iface
21
{
22
	private $sql;
23
24
25
	/**
26
	 * Initializes the statement object
27
	 *
28
	 * @param \Aimeos\MW\DB\Connection\DBAL $conn Database connection object
29
	 * @param string $sql SQL statement
30
	 */
31
	public function __construct( \Aimeos\MW\DB\Connection\DBAL $conn, string $sql )
32
	{
33
		parent::__construct( $conn );
34
		$this->sql = $sql;
35
	}
36
37
38
	/**
39
	 * Returns the SQL string as sent to the database (magic PHP method)
40
	 *
41
	 * @return string SQL statement
42
	 */
43
	public function __toString()
44
	{
45
		return $this->sql;
46
	}
47
48
49
	/**
50
	 * Binds a value to a parameter in the statement.
51
	 *
52
	 * @param int $position Position index of the placeholder
53
	 * @param mixed $value Value which should be bound to the placeholder
54
	 * @param int $type Type of given value defined in \Aimeos\MW\DB\Statement\Base as constant
55
	 * @return \Aimeos\MW\DB\Statement\Iface Statement instance for method chaining
56
	 * @throws \Aimeos\MW\DB\Exception If the parameter type is invalid
57
	 */
58
	public function bind( int $position, $value, int $type = \Aimeos\MW\DB\Statement\Base::PARAM_STR ) : \Aimeos\MW\DB\Statement\Iface
59
	{
60
		throw new \Aimeos\MW\DB\Exception( 'Binding parameters is not available for simple statements: ' . $this->sql );
61
	}
62
63
64
	/**
65
	 * Executes the statement.
66
	 *
67
	 * @return \Aimeos\MW\DB\Result\Iface Result object
68
	 * @throws \Aimeos\MW\DB\Exception If an error occured in the unterlying driver or if the number of binds doesn't match
69
	 */
70
	public function execute() : \Aimeos\MW\DB\Result\Iface
71
	{
72
		try {
73
			$result = $this->getConnection()->getRawObject()->getWrappedConnection()->query( $this->sql );
74
		} catch( \Doctrine\DBAL\Driver\Exception $e ) {
75
			throw new \Aimeos\MW\DB\Exception( $e->getMessage() . ': ' . $this->sql, $e->getCode() );
76
		}
77
78
		return new \Aimeos\MW\DB\Result\DBAL( $result );
79
	}
80
}
81