| 1 | package Mail::DMARC::Report::Store::SQL::Grammars::SQLite; | ||||||||||||
| 2 | 5 | our $VERSION = '2.20260827'; | |||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 5 | ▶ | use feature 'signatures'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 25 | ▶ | sub new($class) { | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 8 | 25 | my $self = {}; | |||||||||||
| 9 | 25 | bless $self, $class; | |||||||||||
| 10 | 25 | return $self; | |||||||||||
| 11 | } | ||||||||||||
| 12 | |||||||||||||
| 13 | 91 | ▶ | sub language { | ||||||||||
|
Subroutine
language: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 14 | 91 | return 'sqlite'; | |||||||||||
| 15 | } | ||||||||||||
| 16 | |||||||||||||
| 17 | 1830 | ▶ | sub dsn { | ||||||||||
|
Subroutine
dsn: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 18 | 1830 | return 'SQLite'; | |||||||||||
| 19 | } | ||||||||||||
| 20 | |||||||||||||
| 21 | 152 | ▶ | sub and_arg( $self, $column, $operator = undef ) { | ||||||||||
|
Subroutine
and_arg: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 22 | 152 | ▶ | $operator //= '='; | ||||||||||
|
Condition100%2/2
$operator //= "="
MC/DC100%1/1
$operator //= "="
$operator
Truth table100%2/2
A
A= $operator
| |||||||||||||
| 23 | 152 | return " AND $column $operator ?"; | |||||||||||
| 24 | } | ||||||||||||
| 25 | |||||||||||||
| 26 | 19 | ▶ | sub report_record_id { | ||||||||||
|
Subroutine
report_record_id: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 27 | 19 | return 'SELECT id FROM report_record WHERE report_id=?'; | |||||||||||
| 28 | } | ||||||||||||
| 29 | |||||||||||||
| 30 | 45 | ▶ | sub delete_from_where_record_in( $self, $table ) { | ||||||||||
|
Subroutine
delete_from_where_record_in: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 31 | 45 | return "DELETE FROM $table WHERE report_record_id IN (??)"; | |||||||||||
| 32 | } | ||||||||||||
| 33 | |||||||||||||
| 34 | 57 | ▶ | sub delete_from_where_report( $self, $table ) { | ||||||||||
|
Subroutine
delete_from_where_report: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 35 | 57 | return "DELETE FROM $table WHERE report_id=?"; | |||||||||||
| 36 | } | ||||||||||||
| 37 | |||||||||||||
| 38 | 19 | ▶ | sub delete_report { | ||||||||||
|
Subroutine
delete_report: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 39 | 19 | return "DELETE FROM report WHERE id=?"; | |||||||||||
| 40 | } | ||||||||||||
| 41 | |||||||||||||
| 42 | 183 | ▶ | sub select_domain_id { | ||||||||||
|
Subroutine
select_domain_id: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 43 | 183 | return 'SELECT id FROM domain WHERE domain=?'; | |||||||||||
| 44 | } | ||||||||||||
| 45 | |||||||||||||
| 46 | 39 | ▶ | sub insert_domain { | ||||||||||
|
Subroutine
insert_domain: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 47 | 39 | return 'INSERT INTO domain (domain) VALUES (?)'; | |||||||||||
| 48 | } | ||||||||||||
| 49 | |||||||||||||
| 50 | 22 | ▶ | sub select_author_id { | ||||||||||
|
Subroutine
select_author_id: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 51 | 22 | return 'SELECT id FROM author WHERE org_name=?'; | |||||||||||
| 52 | } | ||||||||||||
| 53 | |||||||||||||
| 54 | 19 | ▶ | sub insert_author { | ||||||||||
|
Subroutine
insert_author: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 55 | 19 | return 'INSERT INTO author (org_name,email,extra_contact) VALUES (?,?,?)'; | |||||||||||
| 56 | } | ||||||||||||
| 57 | |||||||||||||
| 58 | 0 | ▶ | sub select_report_id { | ||||||||||
|
Subroutine
select_report_id: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 59 | 0 | return 'SELECT id FROM report WHERE uuid=? AND author_id=?'; | |||||||||||
| 60 | } | ||||||||||||
| 61 | |||||||||||||
| 62 | 19 | ▶ | sub select_id_with_end { | ||||||||||
|
Subroutine
select_id_with_end: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 63 | return | ||||||||||||
| 64 | 19 | 'SELECT id FROM report WHERE from_domain_id=? AND end > ? AND author_id=?'; | |||||||||||
| 65 | } | ||||||||||||
| 66 | |||||||||||||
| 67 | 19 | ▶ | sub insert_report { | ||||||||||
|
Subroutine
insert_report: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 68 | return | ||||||||||||
| 69 | 19 | 'INSERT INTO report (from_domain_id, begin, end, author_id, uuid) VALUES (?,?,?,?,?)'; | |||||||||||
| 70 | } | ||||||||||||
| 71 | |||||||||||||
| 72 | 13 | ▶ | sub order_by( $self, $arg, $order ) { | ||||||||||
|
Subroutine
order_by: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 73 | 13 | return " ORDER BY $arg $order"; | |||||||||||
| 74 | } | ||||||||||||
| 75 | |||||||||||||
| 76 | 14 | ▶ | sub count_reports { | ||||||||||
|
Subroutine
count_reports: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 77 | 14 | return 'SELECT COUNT(*) FROM report'; | |||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | 4 | ▶ | sub limit( $self, $number_of_entries = undef ) { | ||||||||||
|
Subroutine
limit: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 81 | 4 | ▶ | $number_of_entries //= 1; | ||||||||||
|
Condition100%2/2
$number_of_entries //= 1
MC/DC100%1/1
$number_of_entries //= 1
$number_of_entries
Truth table100%2/2
A
A= $number_of_entries
| |||||||||||||
| 82 | 4 | return " LIMIT $number_of_entries"; | |||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | 1 | ▶ | sub limit_args( $self, $number_of_entries = undef ) { | ||||||||||
|
Subroutine
limit_args: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||
| 86 | 1 | ▶ | $number_of_entries //= 1; | ||||||||||
|
Condition50%1/2
$number_of_entries //= 1
MC/DC0%0/1
$number_of_entries //= 1
$number_of_entries
Truth table50%1/2
A
A= $number_of_entries
| |||||||||||||
| 87 | 1 | my $return = ' LIMIT '; | |||||||||||
| 88 | 1 | ▶ | for ( my $i = 1; $i <= $number_of_entries; $i++ ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 89 | 2 | $return .= '?'; | |||||||||||
| 90 | 2 | ▶ | $return .= ',' if $i < $number_of_entries; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 91 | } | ||||||||||||
| 92 | 1 | return $return; | |||||||||||
| 93 | } | ||||||||||||
| 94 | |||||||||||||
| 95 | 13 | ▶ | sub select_report_policy_published { | ||||||||||
|
Subroutine
select_report_policy_published: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 96 | 13 | return 'SELECT * from report_policy_published WHERE report_id=?'; | |||||||||||
| 97 | } | ||||||||||||
| 98 | |||||||||||||
| 99 | 0 | ▶ | sub select_report_reason { | ||||||||||
|
Subroutine
select_report_reason: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 100 | 0 | return 'SELECT type,comment FROM report_record_reason WHERE report_record_id=?'; | |||||||||||
| 101 | } | ||||||||||||
| 102 | |||||||||||||
| 103 | 13 | ▶ | sub select_report_error { | ||||||||||
|
Subroutine
select_report_error: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 104 | 13 | return 'SELECT error FROM report_error WHERE report_id=?'; | |||||||||||
| 105 | } | ||||||||||||
| 106 | |||||||||||||
| 107 | 0 | ▶ | sub select_report_record { | ||||||||||
|
Subroutine
select_report_record: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 108 | return | ||||||||||||
| 109 | 0 | 'SELECT id FROM report_record WHERE report_id=? AND source_ip=? AND count=?'; | |||||||||||
| 110 | } | ||||||||||||
| 111 | |||||||||||||
| 112 | 13 | ▶ | sub select_todo_query { | ||||||||||
|
Subroutine
select_todo_query: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 113 | return <<'EO_TODO_QUERY' | ||||||||||||
| 114 | SELECT r.id AS rid, | ||||||||||||
| 115 | r.begin AS begin, | ||||||||||||
| 116 | r.end AS end, | ||||||||||||
| 117 | a.org_name AS author, | ||||||||||||
| 118 | fd.domain AS from_domain | ||||||||||||
| 119 | FROM report r | ||||||||||||
| 120 | LEFT JOIN report_record rr ON r.id=rr.report_id | ||||||||||||
| 121 | LEFT JOIN author a ON r.author_id=a.id | ||||||||||||
| 122 | LEFT JOIN domain fd ON r.from_domain_id=fd.id | ||||||||||||
| 123 | WHERE rr.count IS NULL | ||||||||||||
| 124 | AND rr.report_id IS NOT NULL | ||||||||||||
| 125 | AND r.end < ? | ||||||||||||
| 126 | GROUP BY r.id | ||||||||||||
| 127 | ORDER BY r.id ASC | ||||||||||||
| 128 | EO_TODO_QUERY | ||||||||||||
| 129 | 13 | ; | |||||||||||
| 130 | } | ||||||||||||
| 131 | |||||||||||||
| 132 | 14 | ▶ | sub select_row_spf { | ||||||||||
|
Subroutine
select_row_spf: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 133 | return <<"EO_SPF_ROW" | ||||||||||||
| 134 | SELECT d.domain AS domain, | ||||||||||||
| 135 | s.result AS result, | ||||||||||||
| 136 | s.scope AS scope | ||||||||||||
| 137 | FROM report_record_spf s | ||||||||||||
| 138 | LEFT JOIN domain d ON s.domain_id=d.id | ||||||||||||
| 139 | WHERE s.report_record_id=? | ||||||||||||
| 140 | EO_SPF_ROW | ||||||||||||
| 141 | 14 | ; | |||||||||||
| 142 | } | ||||||||||||
| 143 | |||||||||||||
| 144 | 14 | ▶ | sub select_row_dkim { | ||||||||||
|
Subroutine
select_row_dkim: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 145 | return <<"EO_DKIM_ROW" | ||||||||||||
| 146 | SELECT d.domain AS domain, | ||||||||||||
| 147 | k.selector AS selector, | ||||||||||||
| 148 | k.result AS result, | ||||||||||||
| 149 | k.human_result AS human_result | ||||||||||||
| 150 | FROM report_record_dkim k | ||||||||||||
| 151 | LEFT JOIN domain d ON k.domain_id=d.id | ||||||||||||
| 152 | WHERE report_record_id=? | ||||||||||||
| 153 | EO_DKIM_ROW | ||||||||||||
| 154 | 14 | ; | |||||||||||
| 155 | } | ||||||||||||
| 156 | |||||||||||||
| 157 | 14 | ▶ | sub select_row_reason { | ||||||||||
|
Subroutine
select_row_reason: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 158 | return <<"EO_ROW_QUERY" | ||||||||||||
| 159 | SELECT type,comment | ||||||||||||
| 160 | FROM report_record_reason | ||||||||||||
| 161 | WHERE report_record_id=? | ||||||||||||
| 162 | EO_ROW_QUERY | ||||||||||||
| 163 | 14 | ; | |||||||||||
| 164 | } | ||||||||||||
| 165 | |||||||||||||
| 166 | 13 | ▶ | sub select_rr_query { | ||||||||||
|
Subroutine
select_rr_query: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 167 | return <<'EO_ROW_QUERY' | ||||||||||||
| 168 | SELECT rr.*, | ||||||||||||
| 169 | etd.domain AS envelope_to, | ||||||||||||
| 170 | efd.domain AS envelope_from, | ||||||||||||
| 171 | hfd.domain AS header_from | ||||||||||||
| 172 | FROM report_record rr | ||||||||||||
| 173 | LEFT JOIN domain etd ON etd.id=rr.envelope_to_did | ||||||||||||
| 174 | LEFT JOIN domain efd ON efd.id=rr.envelope_from_did | ||||||||||||
| 175 | LEFT JOIN domain hfd ON hfd.id=rr.header_from_did | ||||||||||||
| 176 | WHERE report_id = ? | ||||||||||||
| 177 | ORDER BY id ASC | ||||||||||||
| 178 | EO_ROW_QUERY | ||||||||||||
| 179 | 13 | ; | |||||||||||
| 180 | } | ||||||||||||
| 181 | |||||||||||||
| 182 | 27 | ▶ | sub select_report_query { | ||||||||||
|
Subroutine
select_report_query: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 183 | return <<'EO_REPORTS' | ||||||||||||
| 184 | SELECT r.id AS rid, | ||||||||||||
| 185 | r.uuid, | ||||||||||||
| 186 | r.begin AS begin, | ||||||||||||
| 187 | r.end AS end, | ||||||||||||
| 188 | a.org_name AS author, | ||||||||||||
| 189 | fd.domain AS from_domain | ||||||||||||
| 190 | FROM report r | ||||||||||||
| 191 | LEFT JOIN author a ON r.author_id=a.id | ||||||||||||
| 192 | LEFT JOIN domain fd ON r.from_domain_id=fd.id | ||||||||||||
| 193 | WHERE 1=1 | ||||||||||||
| 194 | EO_REPORTS | ||||||||||||
| 195 | 27 | ; | |||||||||||
| 196 | } | ||||||||||||
| 197 | |||||||||||||
| 198 | 7 | ▶ | sub count_filtered_report_query { | ||||||||||
|
Subroutine
count_filtered_report_query: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 199 | return <<'EO_SQL' | ||||||||||||
| 200 | SELECT COUNT(*) | ||||||||||||
| 201 | FROM report r | ||||||||||||
| 202 | LEFT JOIN author a ON r.author_id=a.id | ||||||||||||
| 203 | LEFT JOIN domain fd ON r.from_domain_id=fd.id | ||||||||||||
| 204 | WHERE 1=1 | ||||||||||||
| 205 | EO_SQL | ||||||||||||
| 206 | 7 | ; | |||||||||||
| 207 | } | ||||||||||||
| 208 | |||||||||||||
| 209 | 8 | ▶ | sub insert_error( $self, $which ) { | ||||||||||
|
Subroutine
insert_error: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 210 | 8 | ▶ | if ( $which == 0 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 211 | 4 | return 'UPDATE report SET end=? WHERE id=?'; | |||||||||||
| 212 | } | ||||||||||||
| 213 | else { | ||||||||||||
| 214 | 4 | return 'INSERT INTO report_error (report_id, error) VALUES (?,?)'; | |||||||||||
| 215 | } | ||||||||||||
| 216 | } | ||||||||||||
| 217 | |||||||||||||
| 218 | 18 | ▶ | sub insert_rr_reason { | ||||||||||
|
Subroutine
insert_rr_reason: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 219 | return | ||||||||||||
| 220 | 18 | 'INSERT INTO report_record_reason (report_record_id, type, comment) VALUES (?,?,?)'; | |||||||||||
| 221 | } | ||||||||||||
| 222 | |||||||||||||
| 223 | 27 | ▶ | sub insert_rr_dkim( $self, $fields ) { | ||||||||||
|
Subroutine
insert_rr_dkim: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 224 | 27 | my $fields_str = join ', ', @$fields; | |||||||||||
| 225 | return <<"EO_DKIM" | ||||||||||||
| 226 | INSERT INTO report_record_dkim | ||||||||||||
| 227 | (report_record_id, $fields_str) | ||||||||||||
| 228 | VALUES (??) | ||||||||||||
| 229 | EO_DKIM | ||||||||||||
| 230 | 27 | ; | |||||||||||
| 231 | } | ||||||||||||
| 232 | |||||||||||||
| 233 | 32 | ▶ | sub insert_rr_spf( $self, $fields ) { | ||||||||||
|
Subroutine
insert_rr_spf: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 234 | 32 | my $fields_str = join ', ', @$fields; | |||||||||||
| 235 | return | ||||||||||||
| 236 | 32 | "INSERT INTO report_record_spf (report_record_id, $fields_str) VALUES(??)"; | |||||||||||
| 237 | } | ||||||||||||
| 238 | |||||||||||||
| 239 | 35 | ▶ | sub insert_rr { | ||||||||||
|
Subroutine
insert_rr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 240 | return <<'EO_ROW_INSERT' | ||||||||||||
| 241 | INSERT INTO report_record | ||||||||||||
| 242 | (report_id, source_ip, count, header_from_did, envelope_to_did, envelope_from_did, | ||||||||||||
| 243 | disposition, dkim, spf) | ||||||||||||
| 244 | VALUES (??) | ||||||||||||
| 245 | EO_ROW_INSERT | ||||||||||||
| 246 | 35 | ; | |||||||||||
| 247 | } | ||||||||||||
| 248 | |||||||||||||
| 249 | 20 | ▶ | sub insert_policy_published { | ||||||||||
|
Subroutine
insert_policy_published: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 250 | return <<"EO_RPP" | ||||||||||||
| 251 | INSERT INTO report_policy_published | ||||||||||||
| 252 | (report_id, adkim, aspf, p, sp, pct, rua) | ||||||||||||
| 253 | VALUES (??) | ||||||||||||
| 254 | EO_RPP | ||||||||||||
| 255 | 20 | ; | |||||||||||
| 256 | } | ||||||||||||
| 257 | |||||||||||||
| 258 | 9 | ▶ | sub select_from( $self, $columns, $table ) { | ||||||||||
|
Subroutine
select_from: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 259 | 9 | my $colStr = join( ', ', @$columns ); | |||||||||||
| 260 | 9 | return "SELECT $colStr FROM $table WHERE 1=1"; | |||||||||||
| 261 | } | ||||||||||||
| 262 | |||||||||||||
| 263 | 8 | ▶ | sub insert_into( $self, $table, $cols ) { | ||||||||||
|
Subroutine
insert_into: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 264 | 8 | my $columns = join ', ', @$cols; | |||||||||||
| 265 | 8 | return "INSERT INTO $table ($columns) VALUES (??)"; | |||||||||||
| 266 | } | ||||||||||||
| 267 | |||||||||||||
| 268 | 0 | ▶ | sub update( $self, $table, $cols ) { | ||||||||||
|
Subroutine
update: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 269 | 0 | my $columns = join( ' = ?, ', @$cols ) . ' = ?'; | |||||||||||
| 270 | 0 | return "UPDATE $table SET $columns WHERE 1=1"; | |||||||||||
| 271 | } | ||||||||||||
| 272 | |||||||||||||
| 273 | 1 | ▶ | sub replace_into( $self, $table, $cols ) { | ||||||||||
|
Subroutine
replace_into: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 274 | 1 | my $columns = join ', ', @$cols; | |||||||||||
| 275 | 1 | return "REPLACE INTO $table ($columns) VALUES (??)"; | |||||||||||
| 276 | } | ||||||||||||
| 277 | |||||||||||||
| 278 | 5 | ▶ | sub delete_from( $self, $table ) { | ||||||||||
|
Subroutine
delete_from: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 279 | 5 | return "DELETE FROM $table WHERE 1=1"; | |||||||||||
| 280 | } | ||||||||||||
| 281 | |||||||||||||
| 282 | # Aggregate queries for the report views. Every one of these is scoped by a | ||||||||||||
| 283 | # WHERE fragment the store builds from placeholders, so $where never carries | ||||||||||||
| 284 | # caller-supplied text. | ||||||||||||
| 285 | |||||||||||||
| 286 | # The DMARC-evaluated (aligned) results live in report_record.dkim/spf, which | ||||||||||||
| 287 | # are nullable; COALESCE keeps a NULL out of the comparison so that no message | ||||||||||||
| 288 | # escapes all four alignment buckets. | ||||||||||||
| 289 | 18 | ▶ | sub outcome_buckets { | ||||||||||
|
Subroutine
outcome_buckets: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 290 | return <<'EO_BUCKETS' | ||||||||||||
| 291 | SUM(CASE WHEN COALESCE(rr.dkim,'')='pass' AND COALESCE(rr.spf,'')='pass' | ||||||||||||
| 292 | THEN COALESCE(rr.count,1) ELSE 0 END) AS aligned_both, | ||||||||||||
| 293 | SUM(CASE WHEN COALESCE(rr.dkim,'')='pass' AND COALESCE(rr.spf,'')<>'pass' | ||||||||||||
| 294 | THEN COALESCE(rr.count,1) ELSE 0 END) AS aligned_dkim, | ||||||||||||
| 295 | SUM(CASE WHEN COALESCE(rr.dkim,'')<>'pass' AND COALESCE(rr.spf,'')='pass' | ||||||||||||
| 296 | THEN COALESCE(rr.count,1) ELSE 0 END) AS aligned_spf, | ||||||||||||
| 297 | SUM(CASE WHEN COALESCE(rr.dkim,'')<>'pass' AND COALESCE(rr.spf,'')<>'pass' | ||||||||||||
| 298 | THEN COALESCE(rr.count,1) ELSE 0 END) AS aligned_none, | ||||||||||||
| 299 | SUM(CASE WHEN rr.disposition='none' THEN COALESCE(rr.count,1) ELSE 0 END) AS disp_none, | ||||||||||||
| 300 | SUM(CASE WHEN rr.disposition='quarantine' THEN COALESCE(rr.count,1) ELSE 0 END) AS disp_quarantine, | ||||||||||||
| 301 | SUM(CASE WHEN rr.disposition='reject' THEN COALESCE(rr.count,1) ELSE 0 END) AS disp_reject | ||||||||||||
| 302 | EO_BUCKETS | ||||||||||||
| 303 | 18 | ; | |||||||||||
| 304 | } | ||||||||||||
| 305 | |||||||||||||
| 306 | 43 | ▶ | sub agg_from_clause { | ||||||||||
|
Subroutine
agg_from_clause: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 307 | return <<'EO_FROM' | ||||||||||||
| 308 | FROM report r | ||||||||||||
| 309 | JOIN report_record rr ON r.id=rr.report_id | ||||||||||||
| 310 | LEFT JOIN author a ON r.author_id=a.id | ||||||||||||
| 311 | LEFT JOIN domain fd ON r.from_domain_id=fd.id | ||||||||||||
| 312 | EO_FROM | ||||||||||||
| 313 | 43 | ; | |||||||||||
| 314 | } | ||||||||||||
| 315 | |||||||||||||
| 316 | # Reporting windows are epoch seconds, so a UTC day needs no dialect date | ||||||||||||
| 317 | # functions. A window spanning a day boundary is attributed to its begin day; | ||||||||||||
| 318 | # reporters almost always send daily windows, so the skew is negligible. | ||||||||||||
| 319 | 10 | ▶ | sub select_timeseries_query( $self, $where ) { | ||||||||||
|
Subroutine
select_timeseries_query: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 320 | 10 | my $buckets = $self->outcome_buckets; | |||||||||||
| 321 | 10 | my $from = $self->agg_from_clause; | |||||||||||
| 322 | return <<"EO_TIMESERIES" | ||||||||||||
| 323 | SELECT (r.begin - (r.begin % 86400)) AS day, | ||||||||||||
| 324 | SUM(COALESCE(rr.count,1)) AS messages, | ||||||||||||
| 325 | $buckets | ||||||||||||
| 326 | $from WHERE 1=1$where | ||||||||||||
| 327 | GROUP BY day | ||||||||||||
| 328 | ORDER BY day ASC | ||||||||||||
| 329 | EO_TIMESERIES | ||||||||||||
| 330 | 10 | ; | |||||||||||
| 331 | } | ||||||||||||
| 332 | |||||||||||||
| 333 | 7 | ▶ | sub select_sources_query( $self, $where ) { | ||||||||||
|
Subroutine
select_sources_query: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 334 | 7 | my $buckets = $self->outcome_buckets; | |||||||||||
| 335 | 7 | my $from = $self->agg_from_clause; | |||||||||||
| 336 | return <<"EO_SOURCES" | ||||||||||||
| 337 | SELECT rr.source_ip AS source_ip, | ||||||||||||
| 338 | SUM(COALESCE(rr.count,1)) AS messages, | ||||||||||||
| 339 | $buckets, | ||||||||||||
| 340 | COUNT(DISTINCT r.author_id) AS reporters, | ||||||||||||
| 341 | MIN(r.begin) AS first_seen, | ||||||||||||
| 342 | MAX(r.end) AS last_seen | ||||||||||||
| 343 | $from WHERE 1=1$where | ||||||||||||
| 344 | GROUP BY rr.source_ip | ||||||||||||
| 345 | ORDER BY messages DESC | ||||||||||||
| 346 | EO_SOURCES | ||||||||||||
| 347 | 7 | ; | |||||||||||
| 348 | } | ||||||||||||
| 349 | |||||||||||||
| 350 | # Kept out of the sources query on purpose: a record with two reasons would | ||||||||||||
| 351 | # join twice and double every SUM in that row. | ||||||||||||
| 352 | 7 | ▶ | sub select_source_reasons_query( $self, $where ) { | ||||||||||
|
Subroutine
select_source_reasons_query: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 353 | 7 | my $from = $self->agg_from_clause; | |||||||||||
| 354 | return <<"EO_REASONS" | ||||||||||||
| 355 | SELECT rr.source_ip AS source_ip, | ||||||||||||
| 356 | rrr.type AS type, | ||||||||||||
| 357 | rrr.comment AS comment, | ||||||||||||
| 358 | SUM(COALESCE(rr.count,1)) AS messages | ||||||||||||
| 359 | $from JOIN report_record_reason rrr ON rrr.report_record_id=rr.id | ||||||||||||
| 360 | WHERE 1=1$where | ||||||||||||
| 361 | GROUP BY rr.source_ip, rrr.type, rrr.comment | ||||||||||||
| 362 | EO_REASONS | ||||||||||||
| 363 | 7 | ; | |||||||||||
| 364 | } | ||||||||||||
| 365 | |||||||||||||
| 366 | 4 | ▶ | sub select_source_detail_query( $self, $where ) { | ||||||||||
|
Subroutine
select_source_detail_query: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 367 | 4 | my $from = $self->agg_from_clause; | |||||||||||
| 368 | return <<"EO_DETAIL" | ||||||||||||
| 369 | SELECT hfd.domain AS header_from, | ||||||||||||
| 370 | rr.disposition AS disposition, | ||||||||||||
| 371 | rr.dkim AS dkim, | ||||||||||||
| 372 | rr.spf AS spf, | ||||||||||||
| 373 | SUM(COALESCE(rr.count,1)) AS messages | ||||||||||||
| 374 | $from LEFT JOIN domain hfd ON hfd.id=rr.header_from_did | ||||||||||||
| 375 | WHERE 1=1$where | ||||||||||||
| 376 | GROUP BY hfd.domain, rr.disposition, rr.dkim, rr.spf | ||||||||||||
| 377 | ORDER BY messages DESC | ||||||||||||
| 378 | EO_DETAIL | ||||||||||||
| 379 | 4 | ; | |||||||||||
| 380 | } | ||||||||||||
| 381 | |||||||||||||
| 382 | # The d= domain and selector a source presented are what distinguish a | ||||||||||||
| 383 | # misconfigured ESP from an unauthenticated spoof. | ||||||||||||
| 384 | 8 | ▶ | sub select_source_dkim_query( $self, $where ) { | ||||||||||
|
Subroutine
select_source_dkim_query: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 385 | 8 | my $from = $self->agg_from_clause; | |||||||||||
| 386 | return <<"EO_SRC_DKIM" | ||||||||||||
| 387 | SELECT d.domain AS domain, | ||||||||||||
| 388 | k.selector AS selector, | ||||||||||||
| 389 | k.result AS result, | ||||||||||||
| 390 | SUM(COALESCE(rr.count,1)) AS messages | ||||||||||||
| 391 | $from JOIN report_record_dkim k ON k.report_record_id=rr.id | ||||||||||||
| 392 | LEFT JOIN domain d ON d.id=k.domain_id | ||||||||||||
| 393 | WHERE 1=1$where | ||||||||||||
| 394 | GROUP BY d.domain, k.selector, k.result | ||||||||||||
| 395 | ORDER BY messages DESC | ||||||||||||
| 396 | EO_SRC_DKIM | ||||||||||||
| 397 | 8 | ; | |||||||||||
| 398 | } | ||||||||||||
| 399 | |||||||||||||
| 400 | 7 | ▶ | sub select_source_spf_query( $self, $where ) { | ||||||||||
|
Subroutine
select_source_spf_query: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 401 | 7 | my $from = $self->agg_from_clause; | |||||||||||
| 402 | return <<"EO_SRC_SPF" | ||||||||||||
| 403 | SELECT d.domain AS domain, | ||||||||||||
| 404 | s.scope AS scope, | ||||||||||||
| 405 | s.result AS result, | ||||||||||||
| 406 | SUM(COALESCE(rr.count,1)) AS messages | ||||||||||||
| 407 | $from JOIN report_record_spf s ON s.report_record_id=rr.id | ||||||||||||
| 408 | LEFT JOIN domain d ON d.id=s.domain_id | ||||||||||||
| 409 | WHERE 1=1$where | ||||||||||||
| 410 | GROUP BY d.domain, s.scope, s.result | ||||||||||||
| 411 | ORDER BY messages DESC | ||||||||||||
| 412 | EO_SRC_SPF | ||||||||||||
| 413 | 7 | ; | |||||||||||
| 414 | } | ||||||||||||
| 415 | |||||||||||||
| 416 | # Per report totals for the report list, so a row can show what it contains | ||||||||||||
| 417 | # without being expanded. Returned one row per (report, From, To) rather than | ||||||||||||
| 418 | # aggregated into a string, because GROUP_CONCAT, group_concat and string_agg | ||||||||||||
| 419 | # are spelled differently in all three engines; the caller assembles the sets. | ||||||||||||
| 420 | 11 | ▶ | sub and_failing { | ||||||||||
|
Subroutine
and_failing: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 421 | 11 | return " AND COALESCE(rr.dkim,'')<>'pass' AND COALESCE(rr.spf,'')<>'pass'"; | |||||||||||
| 422 | } | ||||||||||||
| 423 | |||||||||||||
| 424 | 1 | ▶ | sub select_report_summary_query { | ||||||||||
|
Subroutine
select_report_summary_query: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 425 | 1 | my ($self) = @_; | |||||||||||
| 426 | 1 | my $buckets = $self->outcome_buckets; | |||||||||||
| 427 | return <<"EO_SUMMARY" | ||||||||||||
| 428 | SELECT rr.report_id AS rid, | ||||||||||||
| 429 | hfd.domain AS header_from, | ||||||||||||
| 430 | etd.domain AS envelope_to, | ||||||||||||
| 431 | SUM(COALESCE(rr.count,1)) AS messages, | ||||||||||||
| 432 | $buckets | ||||||||||||
| 433 | FROM report_record rr | ||||||||||||
| 434 | LEFT JOIN domain hfd ON hfd.id=rr.header_from_did | ||||||||||||
| 435 | LEFT JOIN domain etd ON etd.id=rr.envelope_to_did | ||||||||||||
| 436 | WHERE rr.report_id IN (??) | ||||||||||||
| 437 | GROUP BY rr.report_id, hfd.domain, etd.domain | ||||||||||||
| 438 | EO_SUMMARY | ||||||||||||
| 439 | 1 | ; | |||||||||||
| 440 | } | ||||||||||||
| 441 | |||||||||||||
| 442 | 3 | ▶ | sub select_report_domains_query( $self, $where ) { | ||||||||||
|
Subroutine
select_report_domains_query: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 443 | return <<"EO_DOMAINS" | ||||||||||||
| 444 | SELECT fd.domain AS domain, | ||||||||||||
| 445 | COUNT(*) AS reports, | ||||||||||||
| 446 | MIN(r.begin) AS first_seen, | ||||||||||||
| 447 | MAX(r.end) AS last_seen | ||||||||||||
| 448 | FROM report r | ||||||||||||
| 449 | JOIN domain fd ON fd.id=r.from_domain_id | ||||||||||||
| 450 | LEFT JOIN author a ON a.id=r.author_id | ||||||||||||
| 451 | WHERE 1=1$where | ||||||||||||
| 452 | GROUP BY fd.domain | ||||||||||||
| 453 | ORDER BY fd.domain ASC | ||||||||||||
| 454 | EO_DOMAINS | ||||||||||||
| 455 | 3 | ; | |||||||||||
| 456 | } | ||||||||||||
| 457 | |||||||||||||
| 458 | 5 | 1; | |||||||||||
| 459 | |||||||||||||
| 460 | __END__ | ||||||||||||
| 461 | |||||||||||||
| 462 | =pod | ||||||||||||
| 463 | |||||||||||||
| 464 | =head1 NAME | ||||||||||||
| 465 | |||||||||||||
| 466 | Mail::DMARC::Report::Store::SQL::Grammars::SQLite - Grammar for working with sqlite databases. | ||||||||||||
| 467 | |||||||||||||
| 468 | =head1 VERSION | ||||||||||||
| 469 | |||||||||||||
| 470 | version 2.20260827 | ||||||||||||
| 471 | |||||||||||||
| 472 | =head1 SYPNOSIS | ||||||||||||
| 473 | |||||||||||||
| 474 | Allow DMARC to be able to speak to SQLite databases. | ||||||||||||
| 475 | |||||||||||||
| 476 | =head1 DESCRIPTION | ||||||||||||
| 477 | |||||||||||||
| 478 | Uses ANSI SQL syntax, keeping the SQL as portable as possible. | ||||||||||||
| 479 | |||||||||||||
| 480 | DB engine specific features are to be avoided. | ||||||||||||
| 481 | |||||||||||||
| 482 | =head1 AUTHORS | ||||||||||||
| 483 | |||||||||||||
| 484 | =over 4 | ||||||||||||
| 485 | |||||||||||||
| 486 | =item * | ||||||||||||
| 487 | |||||||||||||
| 488 | Matt Simerson <msimerson@cpan.org> | ||||||||||||
| 489 | |||||||||||||
| 490 | =item * | ||||||||||||
| 491 | |||||||||||||
| 492 | Davide Migliavacca <shari@cpan.org> | ||||||||||||
| 493 | |||||||||||||
| 494 | =item * | ||||||||||||
| 495 | |||||||||||||
| 496 | Marc Bradshaw <marc@marcbradshaw.net> | ||||||||||||
| 497 | |||||||||||||
| 498 | =back | ||||||||||||
| 499 | |||||||||||||
| 500 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 501 | |||||||||||||
| 502 | This software is copyright (c) 2026 by Matt Simerson. | ||||||||||||
| 503 | |||||||||||||
| 504 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 505 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 506 | |||||||||||||
| 507 | =cut | ||||||||||||