StatementAbstract::execute()   A
last analyzed

Complexity

Conditions 3
Paths 6

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 16
nc 6
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Db
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Db\Driver;
16
17
use Phossa2\Db\Message\Message;
18
use Phossa2\Db\Traits\DriverAwareTrait;
19
use Phossa2\Shared\Base\ObjectAbstract;
20
use Phossa2\Db\Interfaces\ResultInterface;
21
use Phossa2\Db\Exception\RuntimeException;
22
use Phossa2\Db\Exception\NotFoundException;
23
use Phossa2\Db\Interfaces\StatementInterface;
24
25
/**
26
 * StatementAbstract
27
 *
28
 * @package Phossa2\Db
29
 * @author  Hong Zhang <[email protected]>
30
 * @see     ObjectAbstract
31
 * @see     StatementInterface
32
 * @version 2.0.0
33
 * @since   2.0.0 added
34
 */
35
abstract class StatementAbstract extends ObjectAbstract implements StatementInterface
36
{
37
    use DriverAwareTrait;
38
39
    /**
40
     * prepared statement
41
     *
42
     * @var    mixed
43
     * @access protected
44
     */
45
    protected $prepared;
46
47
    /**
48
     * Result prototype
49
     *
50
     * @var    ResultInterface
51
     * @access protected
52
     */
53
    protected $result_prototype;
54
55
    /**
56
     * @var    ResultInterface
57
     * @access protected
58
     */
59
    protected $result;
60
61
    /**
62
     * Desctructor
63
     *
64
     * @access public
65
     */
66
    public function __destruct()
67
    {
68
        $this->close();
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74
    public function prepare(/*# string */ $sql)/*# : bool */
75
    {
76
        // close previous prepared sql if any
77
        $this->close();
78
79
        // prepare sql
80
        try {
81
            $res = $this->realPrepare($this->getDriver()->getLink(), $sql);
82
            if (false !== $res) {
83
                $this->prepared = $res;
84
                $this->getDriver()->getProfiler()->setSql($sql);
85
                return true;
86
            }
87
        } catch (\Exception $e) {
88
            throw new RuntimeException($e->getMessage(), (int) $e->getCode(), $e);
89
        }
90
        throw new RuntimeException(
91
            Message::get(Message::DB_STMT_PREPARE_FAIL, $sql),
92
            Message::DB_STMT_PREPARE_FAIL
93
        );
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99
    public function execute(array $parameters = [])/*# : bool */
100
    {
101
        $this->checkPreparation();
102
        $this->getDriver()->getProfiler()->setParameters($parameters)->startWatch();
103
104
        try {
105
            if ($this->realExecute($parameters)) {
106
                $result = clone $this->result_prototype;
107
                $this->result = $result($this->prepared);
108
                $this->getDriver()->getProfiler()->stopWatch();
109
                return true;
110
            }
111
        } catch (\Exception $e) {
112
            throw new RuntimeException($e->getMessage(), (int) $e->getCode(), $e);
113
        }
114
        throw new RuntimeException(
115
            Message::get(
116
                Message::DB_STMT_EXECUTE_FAIL,
117
                $this->getDriver()->getProfiler()->getSql()
118
            ),
119
            Message::DB_STMT_EXECUTE_FAIL
120
        );
121
    }
122
123
    /**
124
     * {@inheritDoc}
125
     */
126
    public function getResult()/*# : ResultInterface */
127
    {
128
        if (null === $this->result) {
129
            throw new NotFoundException(
130
                Message::get(Message::DB_STMT_NO_RESULT),
131
                Message::DB_STMT_NO_RESULT
132
            );
133
        }
134
        return $this->result;
135
    }
136
137
    /**
138
     * {@inheritDoc}
139
     */
140
    public function close()
141
    {
142
        if ($this->prepared) {
143
            // close result
144
            $this->closeResult();
145
146
            // close statement
147
            $this->realClose($this->prepared);
148
            $this->prepared = null;
149
        }
150
    }
151
152
    /**
153
     * Throw exception if not prepared
154
     *
155
     * @throws RuntimeException
156
     * @access protected
157
     */
158
    protected function checkPreparation()
159
    {
160
        // must be prepared
161
        if (null === $this->prepared) {
162
            throw new RuntimeException(
163
                Message::get(Message::DB_STMT_NOTPREPARED),
164
                Message::DB_STMT_NOTPREPARED
165
            );
166
        }
167
168
        // close any previous resultset
169
        $this->closeResult();
170
    }
171
172
    /**
173
     * Close resultset if any
174
     *
175
     * @access protected
176
     */
177
    protected function closeResult()
178
    {
179
        if ($this->result) {
180
            $this->result->close();
181
            $this->result = null;
182
        }
183
    }
184
185
    /**
186
     * Driver specific prepare statement
187
     *
188
     * @param  mixed $link db link resource
189
     * @param  string $sql
190
     * @return mixed
191
     * @throws RuntimeException
192
     * @access protected
193
     */
194
    abstract protected function realPrepare($link, /*# string */ $sql);
195
196
    /**
197
     * Driver specific statement execution
198
     *
199
     * @param  array $parameters
200
     * @return bool
201
     * @throws RuntimeException
202
     * @access protected
203
     */
204
    abstract protected function realExecute(array $parameters)/*# : bool */;
205
206
    /**
207
     * Close statement
208
     *
209
     * @param  mixed prepared low-level statement
210
     * @access protected
211
     */
212
    abstract protected function realClose($stmt);
213
}
214