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

Simple::bind()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 7
nop 3
dl 0
loc 24
rs 6.7272
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 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 $binds = [];
23
	private $parts;
24
	private $sql;
25
26
27
	/**
28
	 * Initializes the statement object
29
	 *
30
	 * @param \Aimeos\MW\DB\Connection\DBAL $conn Database connection object
31
	 * @param string $sql SQL statement
32
	 */
33
	public function __construct( \Aimeos\MW\DB\Connection\DBAL $conn, $sql )
34
	{
35
		parent::__construct( $conn );
36
37
		$this->parts = $this->getSqlParts( $sql );
38
	}
39
40
41
	/**
42
	 * Binds a value to a parameter in the statement.
43
	 *
44
	 * @param integer $position Position index of the placeholder
45
	 * @param mixed $value Value which should be bound to the placeholder
46
	 * @param integer $type Type of given value defined in \Aimeos\MW\DB\Statement\Base as constant
47
	 */
48
	public function bind( $position, $value, $type = \Aimeos\MW\DB\Statement\Base::PARAM_STR )
49
	{
50
		if( is_null( $value ) ) {
51
			$this->binds[$position] = 'NULL'; return;
52
		}
53
54
		switch( $type )
55
		{
56
			case \Aimeos\MW\DB\Statement\Base::PARAM_NULL:
57
				$this->binds[$position] = 'NULL'; break;
58
			case \Aimeos\MW\DB\Statement\Base::PARAM_BOOL:
59
				$this->binds[$position] = (int) (bool) $value; break;
60
			case \Aimeos\MW\DB\Statement\Base::PARAM_INT:
61
				$this->binds[$position] = (int) $value; break;
62
			case \Aimeos\MW\DB\Statement\Base::PARAM_FLOAT:
63
				$this->binds[$position] = (float) $value; break;
64
			case \Aimeos\MW\DB\Statement\Base::PARAM_STR:
65
				$this->binds[$position] = $this->getConnection()->getRawObject()->quote( $value ); break;
66
			default:
67
				throw new \Aimeos\MW\DB\Exception( sprintf( 'Invalid parameter type "%1$s"', $type ) );
68
		}
69
70
		$this->sql = null;
71
	}
72
73
74
	/**
75
	 * Executes the statement.
76
	 *
77
	 * @return \Aimeos\MW\DB\Result\Iface Result object
78
	 * @throws \Aimeos\MW\DB\Exception If an error occured in the unterlying driver or if the number of binds doesn't match
79
	 */
80
	public function execute()
81
	{
82
		if( count( $this->binds ) !== count( $this->parts ) - 1 )
83
		{
84
			$msg = 'Number of binds (%1$d) doesn\'t match the number of markers in "%2$s"';
85
			throw new \Aimeos\MW\DB\Exception( sprintf( $msg, count( $this->binds ), implode( '?', $this->parts ) ) );
86
		}
87
88
		try
89
		{
90
			$result = $this->exec();
91
		}
92
		catch( \Doctrine\DBAL\DBALException $e )
93
		{
94
			try {
95
				$result = $this->reconnect( $e )->exec();
96
			} catch( \Doctrine\DBAL\DBALException $e ) {
97
				throw new \Aimeos\MW\DB\Exception( $e->getMessage(), $e->getCode() );
98
			}
99
		}
100
101
		return new \Aimeos\MW\DB\Result\DBAL( $result );
102
	}
103
104
105
	/**
106
	 * Returns the SQL string as sent to the database (magic PHP method)
107
	 *
108
	 * @return string SQL statement
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

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...
109
	 */
110
	public function __toString()
111
	{
112
		if( $this->sql === null ) {
113
			$this->sql = $this->buildSQL( $this->parts, $this->binds );
114
		}
115
116
		return $this->sql;
117
	}
118
119
120
	/**
121
	 * Binds the parameters and executes the SQL statment
122
	 *
123
	 * @return \Doctrine\DBAL\Driver\Statement Executed DBAL statement
124
	 */
125
	protected function exec()
126
	{
127
		if( $this->sql === null ) {
128
			$this->sql = $this->buildSQL( $this->parts, $this->binds );
129
		}
130
131
		return $this->getConnection()->getRawObject()->query( $this->sql );
132
	}
133
}
134