Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
CrashReportsEntityDao
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 5
30
0.00% covered (danger)
0.00%
0 / 1
 selectExistsCrashReportsBySequence
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 selectCrashReportsPageTotalCount
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 selectCrashReportsPageItems
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 insertCrashReports
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
2
 deleteCrashReportsBySequence
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\App\Models\Dao\Entities;
6
7use PeServer\App\Models\Data\Dto\CrashReportListItemDto;
8use PeServer\Core\Binary;
9use PeServer\Core\Database\DaoBase;
10use PeServer\Core\Database\DaoTrait;
11use PeServer\Core\Database\IDatabaseContext;
12use PeServer\Core\Serialization\Mapper;
13
14class CrashReportsEntityDao extends DaoBase
15{
16    use DaoTrait;
17
18    #region function
19
20
21    /**
22     * クラッシュレポートを主キー検索で有無確認。
23     *
24     * @param int $sequence
25     * @return bool
26     */
27    public function selectExistsCrashReportsBySequence(int $sequence): bool
28    {
29        return 1 === $this->context->selectSingleCount(
30            <<<SQL
31
32            select
33                count(*)
34            from
35                crash_reports
36            where
37                crash_reports.sequence = :sequence
38
39            SQL,
40            [
41                'sequence' => $sequence,
42            ]
43        );
44    }
45
46    /**
47     * クラッシュレポート ページ 全件数取得。
48     *
49     * @return int
50     * @phpstan-return non-negative-int
51     */
52    public function selectCrashReportsPageTotalCount(): int
53    {
54        return $this->context->selectSingleCount(
55            <<<SQL
56
57            select
58                count(*)
59            from
60                crash_reports
61
62            SQL
63        );
64    }
65
66    /**
67     * クラッシュレポート ページ 表示データ取得。
68     *
69     * @param int $index
70     * @phpstan-param non-negative-int $index
71     * @param int $count
72     * @phpstan-param non-negative-int $count
73     * @return CrashReportListItemDto[]
74     */
75    public function selectCrashReportsPageItems(int $index, int $count): array
76    {
77        $result = $this->context->selectOrdered(
78            <<<SQL
79
80            select
81                crash_reports.sequence,
82                crash_reports.timestamp,
83                crash_reports.version,
84                REPLACE(REPLACE(crash_reports.exception, CHAR(13, 10), CHAR(10)), CHAR(13), CHAR(10)) as exception_lf,
85                case INSTR(REPLACE(REPLACE(crash_reports.exception, CHAR(13, 10), CHAR(10)), CHAR(13), CHAR(10)), CHAR(10))
86                    when 0 then
87                        REPLACE(REPLACE(crash_reports.exception, CHAR(13, 10), CHAR(10)), CHAR(13), CHAR(10))
88                    else
89                        SUBSTR(REPLACE(REPLACE(crash_reports.exception, CHAR(13, 10), CHAR(10)), CHAR(13), CHAR(10)), 0, INSTR(REPLACE(REPLACE(crash_reports.exception, CHAR(13, 10), CHAR(10)), CHAR(13), CHAR(10)), CHAR(10)))
90                end as exception_subject
91            from
92                crash_reports
93            order by
94                crash_reports.timestamp desc,
95                crash_reports.sequence desc
96            limit
97                :count
98            offset
99                :index
100
101            SQL,
102            [
103                'index' => $index,
104                'count' => $count,
105            ]
106        );
107
108        return $result->mapping(CrashReportListItemDto::class);
109    }
110
111    public function insertCrashReports(
112        string $ipAddress,
113        string $version,
114        string $revision,
115        string $build,
116        string $userId,
117        string $exception,
118        string $email,
119        string $comment,
120        Binary $report
121    ): void {
122        $this->context->insertSingle(
123            <<<SQL
124
125            insert into
126                crash_reports
127                (
128                    [timestamp],
129                    [ip_address],
130
131                    [version],
132                    [revision],
133                    [build],
134                    [user_id],
135
136                    [exception],
137
138                    [email],
139                    [comment],
140
141                    [report]
142                )
143                values
144                (
145                    CURRENT_TIMESTAMP,
146                    :ip_address,
147
148                    :version,
149                    :revision,
150                    :build,
151                    :user_id,
152
153                    :exception,
154
155                    :email,
156                    :comment,
157
158                    :report
159                )
160
161            SQL,
162            [
163                'ip_address' => $ipAddress,
164
165                'version' => $version,
166                'revision' => $revision,
167                'build' => $build,
168                'user_id' => $userId,
169
170                'exception' => $exception,
171
172                'email' => $email,
173                'comment' => $comment,
174
175                'report' => $report->raw
176
177            ]
178        );
179    }
180
181    public function deleteCrashReportsBySequence(int $sequence): void
182    {
183        $this->context->deleteByKey(
184            <<<SQL
185
186            delete
187            from
188                crash_reports
189            where
190                sequence = :sequence
191
192            SQL,
193            [
194                'sequence' => $sequence,
195            ]
196        );
197    }
198
199    #endregion
200}