Completed
Push — master ( 0c0ab1...4bb828 )
by Hong
02:29
created

StatementAbstract::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 2
eloc 15
nc 2
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
        if (null === $this->prepared) {
77
            try {
78
                $res = $this->realPrepare($this->getDriver()->getLink(), $sql);
79
                if (false !== $res) {
80
                    $this->prepared = $res;
81
                    $this->getDriver()->getProfiler()->setSql($sql);
82
                    return true;
83
                }
84
            } catch (\Exception $e) {
85
                throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
86
            }
87
            throw new RuntimeException(
88
                Message::get(Message::DB_STMT_PREPARE_FAIL, $sql),
89
                Message::DB_STMT_PREPARE_FAIL
90
            );
91
        }
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97
    public function execute(array $parameters = [])/*# : bool */
98
    {
99
        $this->checkPreparation(); // must be prepared
100
        $this->close(); // close previous result if any
101
102
        // int profiler
103
        $time = microtime(true);
104
        $this->getDriver()->getProfiler()->setParameters($parameters);
105
106
        if ($this->realExecute($parameters)) {
107
            $result = clone $this->result_prototype;
108
            $this->result = $result($this->prepared);
109
            $this->getDriver()->getProfiler()->setExecutionTime(microtime(true) - $time);
110
            return true;
111
        }
112
113
        throw new RuntimeException(
114
            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
            $this->realClose($this->prepared);
142
            $this->result = null;
143
        }
144
    }
145
146
    /**
147
     * Throw exception if not prepared
148
     *
149
     * @throws RuntimeException
150
     * @access protected
151
     */
152
    protected function checkPreparation()
153
    {
154
        if (null === $this->prepared) {
155
            throw new RuntimeException(
156
                Message::get(Message::DB_STMT_NOTPREPARED),
157
                Message::DB_STMT_NOTPREPARED
158
            );
159
        }
160
    }
161
162
    /**
163
     * Driver specific prepare statement
164
     *
165
     * @param  mixed $link db link resource
166
     * @param  string $sql
167
     * @return mixed
168
     * @throws RuntimeException
169
     * @access protected
170
     */
171
    abstract protected function realPrepare($link, /*# string */ $sql);
172
173
    /**
174
     * Driver specific statement execution
175
     *
176
     * @param  array $parameters
177
     * @return bool
178
     * @throws RuntimeException
179
     * @access protected
180
     */
181
    abstract protected function realExecute(array $parameters)/*# : bool */;
182
183
    /**
184
     * Close statement's result set
185
     *
186
     * @param  mixed prepared low-level statement
187
     * @access protected
188
     */
189
    abstract protected function realClose($stmt);
190
}
191