Issues (41)

Query.php (3 issues)

1
<?php
2
3
namespace WebStream\Database;
4
5
use Doctrine\DBAL\Statement;
6
use WebStream\Database\Driver\DatabaseDriver;
7
use WebStream\DI\Injector;
0 ignored issues
show
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...
8
use WebStream\Exception\Extend\DatabaseException;
0 ignored issues
show
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...
9
10
/**
11
 * Query
12
 * @author Ryuichi TANAKA.
13
 * @since 2013/12/07
14
 * @version 0.7
15
 */
16
class Query
17
{
18
    use Injector;
19
20
    /**
21
     * @var DatabaseDriver データベースコネクション
22
     */
23
    private DatabaseDriver $connection;
24
25
    /**
26
     * @var string SQL
27
     */
28
    private string $sql;
29
30
    /**
31
     * @var array<mixed> バインドパラメータ
32
     */
33
    private array $bind;
34
35
    /**
36
     * @var Statement ステートメント
37
     */
38
    private Statement $stmt;
39
40
    /**
41
     * Constructor
42
     * @param DatabaseDriver $connection データベースコネクション
43
     */
44 52
    public function __construct(DatabaseDriver $connection)
45
    {
46 52
        $this->connection = $connection;
47 52
    }
48
49
    /**
50
     * SQLを設定する
51
     * @param string SQL
0 ignored issues
show
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...
52
     */
53 52
    public function setSql($sql)
54
    {
55 52
        $this->sql = $sql;
56 52
    }
57
58
    /**
59
     * バインドパラメータを設定する
60
     * @param array $bind バインドパラメータ
61
     */
62 52
    public function setBind(array $bind)
63
    {
64 52
        $this->bind = $bind;
65 52
    }
66
67
    /**
68
     * SELECT
69
     * @return object 取得結果
70
     */
71 44
    public function select()
72
    {
73 44
        $this->logger->debug("execute select.");
74 44
        $this->execute();
75 44
        $result = new Result($this->stmt);
76 44
        $result->inject('logger', $this->logger);
77
78 44
        return $result;
79
    }
80
81
    /**
82
     * INSERT
83
     * @return integer 処理結果件数
84
     */
85 20
    public function insert()
86
    {
87 20
        $this->logger->debug("execute insert.");
88
89 20
        return $this->execute();
90
    }
91
92
    /**
93
     * UPDATE
94
     * @return integer 処理結果件数
95
     */
96 4
    public function update()
97
    {
98 4
        $this->logger->debug("execute update.");
99
100 4
        return $this->execute();
101
    }
102
103
    /**
104
     * DELETE
105
     * @return integer 処理結果件数
106
     */
107 20
    public function delete()
108
    {
109 20
        $this->logger->debug("execute delete.");
110
111 20
        return $this->execute();
112
    }
113
114
    /**
115
     * SQLを実行する
116
     * @return integer 結果件数
117
     */
118 52
    private function execute()
119
    {
120 52
        unset($this->stmt);
121
122
        try {
123 52
            $stmt = $this->connection->getStatement($this->sql);
124 52
            if ($stmt === false) {
125
                throw new DatabaseException("Can't create statement: " . $this->sql);
126
            }
127 52
            $this->logger->info("Executed SQL: " . $this->sql);
128 52
            foreach ($this->bind as $key => $value) {
129 36
                $this->logger->info("Bind statement: $key => $value");
130 36
                if (preg_match("/^[0-9]+$/", $value) && is_int($value)) {
131 16
                    $stmt->bindValue($key, $value, \PDO::PARAM_INT);
132
                } else {
133 24
                    $stmt->bindValue($key, $value, \PDO::PARAM_STR);
134
                }
135
            }
136
137 52
            if ($stmt->execute()) {
138 52
                $this->stmt = $stmt;
139 52
                return $stmt->rowCount();
140
            } else {
141
                $messages = $stmt->errorInfo();
142
                $message = $messages[2];
143
                $sqlState = "(SQL STATE: ${messages[0]})";
144
                $errorCode = "(ERROR CODE: ${messages[1]})";
145
                throw new DatabaseException("${message} ${sqlState} ${errorCode}");
146
            }
147
        } catch (\Exception $e) {
148
            throw new DatabaseException($e);
149
        }
150
    }
151
}
152