3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Db\Adapter\Driver\IbmDb2
;
12 use Zend\Db\Adapter\Driver\StatementInterface
;
13 use Zend\Db\Adapter\Exception
;
14 use Zend\Db\Adapter\ParameterContainer
;
15 use Zend\Db\Adapter\Profiler
;
17 class Statement
implements StatementInterface
, Profiler\ProfilerAwareInterface
22 protected $db2 = null;
27 protected $driver = null;
30 * @var Profiler\ProfilerInterface
32 protected $profiler = null;
40 * @var ParameterContainer
42 protected $parameterContainer = null;
47 protected $isPrepared = false;
52 protected $resource = null;
58 public function initialize($resource)
60 $this->db2
= $resource;
65 * @param IbmDb2 $driver
68 public function setDriver(IbmDb2
$driver)
70 $this->driver
= $driver;
75 * @param Profiler\ProfilerInterface $profiler
78 public function setProfiler(Profiler\ProfilerInterface
$profiler)
80 $this->profiler
= $profiler;
85 * @return null|Profiler\ProfilerInterface
87 public function getProfiler()
89 return $this->profiler
;
98 public function setSql($sql)
109 public function getSql()
115 * Set parameter container
117 * @param ParameterContainer $parameterContainer
120 public function setParameterContainer(ParameterContainer
$parameterContainer)
122 $this->parameterContainer
= $parameterContainer;
127 * Get parameter container
131 public function getParameterContainer()
133 return $this->parameterContainer
;
138 * @throws \Zend\Db\Adapter\Exception\InvalidArgumentException
140 public function setResource($resource)
142 if (get_resource_type($resource) !== 'DB2 Statement') {
143 throw new Exception\
InvalidArgumentException('Resource must be of type DB2 Statement');
145 $this->resource = $resource;
153 public function getResource()
155 return $this->resource;
161 * @param string|null $sql
164 public function prepare($sql = null)
166 if ($this->isPrepared
) {
167 throw new Exception\
RuntimeException('This statement has been prepared already');
174 $this->resource = db2_prepare($this->db2
, $sql);
176 if ($this->resource === false) {
177 throw new Exception\
RuntimeException(db2_stmt_errormsg(), db2_stmt_error());
180 $this->isPrepared
= true;
185 * Check if is prepared
189 public function isPrepared()
191 return $this->isPrepared
;
197 * @param null $parameters
200 public function execute($parameters = null)
202 if (!$this->isPrepared
) {
206 /** START Standard ParameterContainer Merging Block */
207 if (!$this->parameterContainer
instanceof ParameterContainer
) {
208 if ($parameters instanceof ParameterContainer
) {
209 $this->parameterContainer
= $parameters;
212 $this->parameterContainer
= new ParameterContainer();
216 if (is_array($parameters)) {
217 $this->parameterContainer
->setFromArray($parameters);
219 /** END Standard ParameterContainer Merging Block */
221 if ($this->profiler
) {
222 $this->profiler
->profilerStart($this);
225 set_error_handler(function () {}, E_WARNING
); // suppress warnings
226 $response = db2_execute($this->resource, $this->parameterContainer
->getPositionalArray());
227 restore_error_handler();
229 if ($this->profiler
) {
230 $this->profiler
->profilerFinish();
233 if ($response === false) {
234 throw new Exception\
RuntimeException(db2_stmt_errormsg($this->resource));
237 $result = $this->driver
->createResult($this->resource);