Passed
Push — master ( 76b08e...5cd6b2 )
by Ryuichi
58:40 queued 56:20
created

Query::execute()   B

Complexity

Conditions 7
Paths 33

Size

Total Lines 33
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 9.3554

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 24
c 1
b 0
f 0
nc 33
nop 0
dl 0
loc 33
ccs 14
cts 22
cp 0.6364
crap 9.3554
rs 8.6026
1
<?php
2
3
namespace WebStream\Database;
4
5
use WebStream\DI\Injector;
0 ignored issues
show
Bug introduced by
The type WebStream\DI\Injector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use WebStream\Database\Driver\DatabaseDriver;
7
use WebStream\Exception\Extend\DatabaseException;
0 ignored issues
show
Bug introduced by
The type WebStream\Exception\Extend\DatabaseException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
/**
10
 * Query
11
 * @author Ryuichi TANAKA.
12
 * @since 2013/12/07
13
 * @version 0.7
14
 */
15
class Query
16
{
17
    use Injector;
18
19
    /**
20
     * @var DatabaseDriver データベースコネクション
21
     */
22
    private $connection;
23
24
    /**
25
     * @var string SQL
26
     */
27
    private $sql;
28
29
    /**
30
     * @var array<mixed> バインドパラメータ
31
     */
32
    private $bind;
33
34
    /**
35
     * @var \Doctrine\DBAL\Statement ステートメント
36
     */
37
    private $stmt;
38
39
    /**
40
     * Constructor
41
     * @param DatabaseDriver データベースコネクション
0 ignored issues
show
Documentation Bug introduced by
The doc comment データベースコネクション at position 0 could not be parsed: Unknown type name 'データベースコネクション' at position 0 in データベースコネクション.
Loading history...
42
     */
43 52
    public function __construct(DatabaseDriver $connection)
44
    {
45 52
        $this->connection = $connection;
46 52
    }
47
48
    /**
49
     * SQLを設定する
50
     * @param string SQL
0 ignored issues
show
Bug introduced by
The type WebStream\Database\SQL was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
51
     */
52 52
    public function setSql($sql)
53
    {
54 52
        $this->sql = $sql;
55 52
    }
56
57
    /**
58
     * バインドパラメータを設定する
59
     * @param array<string> バインドパラメータ
0 ignored issues
show
Documentation Bug introduced by
The doc comment バインドパラメータ at position 0 could not be parsed: Unknown type name 'バインドパラメータ' at position 0 in バインドパラメータ.
Loading history...
60
     */
61 52
    public function setBind(array $bind)
62
    {
63 52
        $this->bind = $bind;
64 52
    }
65
66
    /**
67
     * SELECT
68
     * @return object 取得結果
69
     */
70 44
    public function select()
71
    {
72 44
        $this->logger->debug("execute select.");
73 44
        $this->execute();
74 44
        $result = new Result($this->stmt);
0 ignored issues
show
Bug introduced by
It seems like $this->stmt can also be of type null; however, parameter $stmt of WebStream\Database\Result::__construct() does only seem to accept Doctrine\DBAL\Statement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
        $result = new Result(/** @scrutinizer ignore-type */ $this->stmt);
Loading history...
75 44
        $result->inject('logger', $this->logger);
76
77 44
        return $result;
78
    }
79
80
    /**
81
     * INSERT
82
     * @return integer 処理結果件数
83
     */
84 20
    public function insert()
85
    {
86 20
        $this->logger->debug("execute insert.");
87
88 20
        return $this->execute();
89
    }
90
91
    /**
92
     * UPDATE
93
     * @return integer 処理結果件数
94
     */
95 4
    public function update()
96
    {
97 4
        $this->logger->debug("execute update.");
98
99 4
        return $this->execute();
100
    }
101
102
    /**
103
     * DELETE
104
     * @return integer 処理結果件数
105
     */
106 20
    public function delete()
107
    {
108 20
        $this->logger->debug("execute delete.");
109
110 20
        return $this->execute();
111
    }
112
113
    /**
114
     * SQLを実行する
115
     * @return integer 結果件数
116
     */
117 52
    private function execute()
118
    {
119 52
        $this->stmt = null;
120
121
        try {
122 52
            $stmt = $this->connection->getStatement($this->sql);
123 52
            if ($stmt === false) {
0 ignored issues
show
introduced by
The condition $stmt === false is always false.
Loading history...
124
                throw new DatabaseException("Can't create statement: " . $this->sql);
125
            }
126 52
            $this->logger->info("Executed SQL: " . $this->sql);
127 52
            foreach ($this->bind as $key => $value) {
128 36
                $this->logger->info("Bind statement: $key => $value");
129 36
                if (preg_match("/^[0-9]+$/", $value) && is_int($value)) {
130 16
                    $stmt->bindValue($key, $value, \PDO::PARAM_INT);
131
                } else {
132 24
                    $stmt->bindValue($key, $value, \PDO::PARAM_STR);
133
                }
134
            }
135
136 52
            if ($stmt->execute()) {
137 52
                $this->stmt = $stmt;
138 52
                $rowCount = $stmt->rowCount();
139
140 52
                return $rowCount;
141
            } else {
142
                $messages = $stmt->errorInfo();
143
                $message = $messages[2];
144
                $sqlState = "(SQL STATE: ${messages[0]})";
145
                $errorCode = "(ERROR CODE: ${messages[1]})";
146
                throw new DatabaseException("${message} ${sqlState} ${errorCode}");
147
            }
148
        } catch (\Exception $e) {
149
            throw new DatabaseException($e);
150
        }
151
    }
152
}
153