Completed
Push — master ( 6e5d3c...d6ba15 )
by Hong
02:17
created

StatementAbstract::realClose()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
nc 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(Message::get(
115
            Message::DB_STMT_EXECUTE_FAIL,
116
            $this->getDriver()->getProfiler()->getSql()),
117
            Message::DB_STMT_EXECUTE_FAIL
118
        );
119
    }
120
121
    /**
122
     * {@inheritDoc}
123
     */
124
    public function getResult()/*# : ResultInterface */
125
    {
126
        if (null === $this->result) {
127
            throw new NotFoundException(
128
                Message::get(Message::DB_STMT_NO_RESULT),
129
                Message::DB_STMT_NO_RESULT
130
            );
131
        }
132
        return $this->result;
133
    }
134
135
    /**
136
     * {@inheritDoc}
137
     */
138
    public function close()
139
    {
140
        if ($this->prepared) {
141
            // close result
142
            $this->closeResult();
143
144
            // close statement
145
            $this->realClose($this->prepared);
146
            $this->prepared = null;
147
        }
148
    }
149
150
    /**
151
     * Throw exception if not prepared
152
     *
153
     * @throws RuntimeException
154
     * @access protected
155
     */
156
    protected function checkPreparation()
157
    {
158
        // must be prepared
159
        if (null === $this->prepared) {
160
            throw new RuntimeException(
161
                Message::get(Message::DB_STMT_NOTPREPARED),
162
                Message::DB_STMT_NOTPREPARED
163
            );
164
        }
165
166
        // close any previous resultset
167
        $this->closeResult();
168
    }
169
170
    /**
171
     * Close resultset if any
172
     *
173
     * @access protected
174
     */
175
    protected function closeResult()
176
    {
177
        if ($this->result) {
178
            $this->result->close();
179
            $this->result = null;
180
        }
181
    }
182
183
    /**
184
     * Driver specific prepare statement
185
     *
186
     * @param  mixed $link db link resource
187
     * @param  string $sql
188
     * @return mixed
189
     * @throws RuntimeException
190
     * @access protected
191
     */
192
    abstract protected function realPrepare($link, /*# string */ $sql);
193
194
    /**
195
     * Driver specific statement execution
196
     *
197
     * @param  array $parameters
198
     * @return bool
199
     * @throws RuntimeException
200
     * @access protected
201
     */
202
    abstract protected function realExecute(array $parameters)/*# : bool */;
203
204
    /**
205
     * Close statement
206
     *
207
     * @param  mixed prepared low-level statement
208
     * @access protected
209
     */
210
    abstract protected function realClose($stmt);
211
}
212