| 1 | package DBIx::QuickORM::Dialect::DuckDB; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | our $VERSION = '0.000032'; | |||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use parent 'DBIx::QuickORM::Dialect'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | DBIx::QuickORM::Dialect::DuckDB - DuckDB dialect. | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | DuckDB through L<DBD::DuckDB>. It keeps the base defaults of | ||||||||||||||||||||||||||||||||||||||||||
| 20 | L<DBIx::QuickORM::Dialect>, but has no savepoints (so transactions cannot | ||||||||||||||||||||||||||||||||||||||||||
| 21 | nest), allows no second connection, and classifies errors by message. The | ||||||||||||||||||||||||||||||||||||||||||
| 22 | namespace is C<current_schema()>. | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | DBD::DuckDB exchanges bytes, so this dialect decodes fetched text and | ||||||||||||||||||||||||||||||||||||||||||
| 25 | upgrades text it sends. | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | my $dialect = DBIx::QuickORM::Dialect->for_dbh($dbh); # this class for DBD::DuckDB | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | =head1 PUBLIC METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | =item $bool = $dialect->supports_savepoints | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | False. | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | =item $bool = $dialect->supports_second_connection($dbh) | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | False: DuckDB holds an exclusive lock on its database file, and an in-memory | ||||||||||||||||||||||||||||||||||||||||||
| 42 | database belongs to its one connection. | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | =item $n = $dialect->bind_limit | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 7000. | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | DuckDB sets no limit, but bulk inserts measured about 30% slower at 65535 | ||||||||||||||||||||||||||||||||||||||||||
| 49 | binds a statement than at 3,000 to 7,000. | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | ▶ | sub supports_savepoints { 0 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
supports_savepoints: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | ▶ | sub supports_second_connection { 0 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
supports_second_connection: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | ▶ | sub bind_limit { 7000 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bind_limit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | =item $key = $dialect->scan_cache_key($dbh, $namespace) | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | The database file's path and the namespace, or undef for an in-memory | ||||||||||||||||||||||||||||||||||||||||||
| 66 | database, which is never cached. | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub scan_cache_key { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
scan_cache_key: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my ($dbh, $namespace) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | my ($path) = $dbh->selectrow_array('SELECT path FROM duckdb_databases() WHERE database_name = current_database()'); | |||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | return undef unless defined($path) && length($path); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $path and length $path
MC/DC0%0/2
defined $path and length $path
defined $path
length $path
Truth table0%0/3
A and B
A= defined $pathB= length $path
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | return join "\0" => 'DuckDB', $path, $namespace // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$namespace // ""
MC/DC0%0/1
$namespace // ""
$namespace
Truth table0%0/2
A
A= $namespace
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | =item $dialect->init_session($dbh, $time_zone) | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | C<SET TimeZone>. DuckDB's sessions are UTF-8 already. The text conversion is | ||||||||||||||||||||||||||||||||||||||||||
| 87 | done by C<text_from_db> and C<text_to_db>. | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | ▶ | sub init_session { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init_session: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | my ($dbh, $time_zone) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | return $dbh->do('SET TimeZone = ' . $dbh->quote($time_zone)); | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | =item $dialect->text_from_db(@values) | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | Decodes each value holding non-ASCII bytes from UTF-8, in place. DBD::DuckDB | ||||||||||||||||||||||||||||||||||||||||||
| 106 | returns text as bytes. | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 3 | ▶ | sub text_from_db { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
text_from_db: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 3 | ▶ | for (@_) { utf8::decode($_) if defined($_) && !ref($_) && m/[^\x00-\x7F]/ } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $_ and not ref $_ and /[^\x00-\x7F]/
defined $_ and not ref $_
MC/DC100%3/3
defined $_ and not ref $_ and /[^\x00-\x7F]/
defined $_
not ref $_
/[^\x00-\x7F]/
Truth table100%4/4
A and not(B) and C
A= defined $_B= not ref $_C= /[^\x00-\x7F]/
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | =item $dialect->text_to_db(@values) | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | Upgrades each value holding non-ASCII characters in place, so its bytes are | ||||||||||||||||||||||||||||||||||||||||||
| 125 | UTF-8. | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | DBD::DuckDB sends a string's internal bytes as they are, which for a Latin-1 | ||||||||||||||||||||||||||||||||||||||||||
| 128 | string that was never upgraded are not UTF-8. | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 1 | ▶ | sub text_to_db { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
text_to_db: called CC=5 SCAR=19.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 1 | ▶ | for (@_) { utf8::upgrade($_) if defined($_) && !ref($_) && m/[^\x00-\x7F]/ } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $_ and not ref $_ and /[^\x00-\x7F]/
defined $_ and not ref $_
MC/DC0%0/3
defined $_ and not ref $_ and /[^\x00-\x7F]/
defined $_
not ref $_
/[^\x00-\x7F]/
Truth table25%1/4
A and not(B) and C
A= defined $_B= not ref $_C= /[^\x00-\x7F]/
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | =item $kind = $dialect->error_kind($native, $state, $errstr) | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | The kind from the error text, since DBD::DuckDB reports the same code and | ||||||||||||||||||||||||||||||||||||||||||
| 147 | SQLSTATE for every error. | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | C<Constraint Error> messages give the constraint kinds, and a | ||||||||||||||||||||||||||||||||||||||||||
| 150 | C<TransactionContext Error> naming a conflict gives | ||||||||||||||||||||||||||||||||||||||||||
| 151 | C<serialization_failure>. Anything else is undef. | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | 2 | my @MESSAGE_KIND = ( | |||||||||||||||||||||||||||||||||||||||||
| 158 | [qr/Constraint Error: Duplicate key .* violates (?:primary key|unique) constraint/ => 'unique_violation'], | ||||||||||||||||||||||||||||||||||||||||||
| 159 | [qr/Constraint Error: PRIMARY KEY or UNIQUE constraint violation/ => 'unique_violation'], | ||||||||||||||||||||||||||||||||||||||||||
| 160 | [qr/Constraint Error: Violates foreign key constraint/ => 'foreign_key_violation'], | ||||||||||||||||||||||||||||||||||||||||||
| 161 | [qr/Constraint Error: NOT NULL constraint failed/ => 'not_null_violation'], | ||||||||||||||||||||||||||||||||||||||||||
| 162 | [qr/Constraint Error: CHECK constraint failed/ => 'check_violation'], | ||||||||||||||||||||||||||||||||||||||||||
| 163 | [qr/TransactionContext Error: .*\bconflict\b/i => 'serialization_failure'], | ||||||||||||||||||||||||||||||||||||||||||
| 164 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 10 | ▶ | sub error_kind { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error_kind: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 10 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 10 | my (undef, undef, $errstr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 169 | 10 | ▶ | return undef unless defined $errstr; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 9 | for my $pair (@MESSAGE_KIND) { | |||||||||||||||||||||||||||||||||||||||||
| 172 | 34 | ▶ | return $pair->[1] if $errstr =~ $pair->[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | 1 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | =item $sql = $dialect->tables_sql | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | =item $sql = $dialect->fields_sql | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | =item $sql = $dialect->pk_sql | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | =item $sql = $dialect->unique_sql | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | =item $sql = $dialect->fk_sql | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | The scan queries (see L<C<scan>|DBIx::QuickORM::Dialect/"PUBLIC METHODS">), | ||||||||||||||||||||||||||||||||||||||||||
| 193 | over C<duckdb_tables()>, C<duckdb_columns()>, and C<duckdb_constraints()> in | ||||||||||||||||||||||||||||||||||||||||||
| 194 | C<current_database()> and C<current_schema()>. | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | A column the database fills has a default, such as C<nextval('seq')>. | ||||||||||||||||||||||||||||||||||||||||||
| 197 | No column is marked as refusing values: the catalog does not mark generated | ||||||||||||||||||||||||||||||||||||||||||
| 198 | columns, and DuckDB allows no key on one. | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | Unique keys come from C<UNIQUE> constraints and from unique indexes (see | ||||||||||||||||||||||||||||||||||||||||||
| 201 | C<_unique_rows>). | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | 2 | my $IN_SCHEMA = <<'EOT'; | |||||||||||||||||||||||||||||||||||||||||
| 208 | WHERE database_name = current_database() | ||||||||||||||||||||||||||||||||||||||||||
| 209 | AND schema_name = current_schema() | ||||||||||||||||||||||||||||||||||||||||||
| 210 | EOT | ||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | sub tables_sql { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tables_sql: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | return <<" EOT"; | |||||||||||||||||||||||||||||||||||||||||
| 214 | SELECT table_name | ||||||||||||||||||||||||||||||||||||||||||
| 215 | FROM duckdb_tables() | ||||||||||||||||||||||||||||||||||||||||||
| 216 | $IN_SCHEMA | ||||||||||||||||||||||||||||||||||||||||||
| 217 | AND NOT internal | ||||||||||||||||||||||||||||||||||||||||||
| 218 | AND NOT temporary | ||||||||||||||||||||||||||||||||||||||||||
| 219 | EOT | ||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | ▶ | sub fields_sql { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fields_sql: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | return <<" EOT"; | |||||||||||||||||||||||||||||||||||||||||
| 224 | SELECT table_name, column_name, data_type, NULL, | ||||||||||||||||||||||||||||||||||||||||||
| 225 | CASE WHEN upper(column_default) <> 'NULL' THEN 1 END, | ||||||||||||||||||||||||||||||||||||||||||
| 226 | NULL | ||||||||||||||||||||||||||||||||||||||||||
| 227 | FROM duckdb_columns() | ||||||||||||||||||||||||||||||||||||||||||
| 228 | $IN_SCHEMA | ||||||||||||||||||||||||||||||||||||||||||
| 229 | EOT | ||||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | ▶ | sub pk_sql { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pk_sql: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | return <<" EOT"; | |||||||||||||||||||||||||||||||||||||||||
| 234 | SELECT table_name, unnest(constraint_column_names) | ||||||||||||||||||||||||||||||||||||||||||
| 235 | FROM duckdb_constraints() | ||||||||||||||||||||||||||||||||||||||||||
| 236 | $IN_SCHEMA | ||||||||||||||||||||||||||||||||||||||||||
| 237 | AND constraint_type = 'PRIMARY KEY' | ||||||||||||||||||||||||||||||||||||||||||
| 238 | EOT | ||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | sub unique_sql { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unique_sql: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | return <<" EOT"; | |||||||||||||||||||||||||||||||||||||||||
| 243 | SELECT table_name, constraint_index, unnest(constraint_column_names) | ||||||||||||||||||||||||||||||||||||||||||
| 244 | FROM duckdb_constraints() | ||||||||||||||||||||||||||||||||||||||||||
| 245 | $IN_SCHEMA | ||||||||||||||||||||||||||||||||||||||||||
| 246 | AND constraint_type = 'UNIQUE' | ||||||||||||||||||||||||||||||||||||||||||
| 247 | EOT | ||||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||
| 252 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | =item $sql = $dialect->unique_index_sql | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | The C<(table, index, expressions)> rows of the unique indexes, from | ||||||||||||||||||||||||||||||||||||||||||
| 257 | C<duckdb_indexes()>. | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | ▶ | sub unique_index_sql { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unique_index_sql: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | return <<" EOT"; | |||||||||||||||||||||||||||||||||||||||||
| 265 | SELECT table_name, index_name, expressions | ||||||||||||||||||||||||||||||||||||||||||
| 266 | FROM duckdb_indexes() | ||||||||||||||||||||||||||||||||||||||||||
| 267 | $IN_SCHEMA | ||||||||||||||||||||||||||||||||||||||||||
| 268 | AND is_unique | ||||||||||||||||||||||||||||||||||||||||||
| 269 | EOT | ||||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | ▶ | sub fk_sql { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fk_sql: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | return <<" EOT"; | |||||||||||||||||||||||||||||||||||||||||
| 274 | SELECT table_name, constraint_index, unnest(constraint_column_names), referenced_table, unnest(referenced_column_names) | ||||||||||||||||||||||||||||||||||||||||||
| 275 | FROM duckdb_constraints() | ||||||||||||||||||||||||||||||||||||||||||
| 276 | $IN_SCHEMA | ||||||||||||||||||||||||||||||||||||||||||
| 277 | AND constraint_type = 'FOREIGN KEY' | ||||||||||||||||||||||||||||||||||||||||||
| 278 | EOT | ||||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | =head1 PRIVATE METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | =item \@rows = $dialect->_unique_rows($dbh) | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | The unique constraints' rows, then a C<(table, "index $name", field)> row for | ||||||||||||||||||||||||||||||||||||||||||
| 288 | each field of each unique index. | ||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | DuckDB reports an index's fields as a string such as C<[b, a]>, quoting a | ||||||||||||||||||||||||||||||||||||||||||
| 291 | name that needs it (C<['"we ird"']>). Only a plain list of such names is | ||||||||||||||||||||||||||||||||||||||||||
| 292 | read. Anything else, such as an expression (C<['(lower(c))']>), gives one | ||||||||||||||||||||||||||||||||||||||||||
| 293 | undef field, so the scan leaves the index out. | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||
| 297 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | # A bracketed list of bare names, or names in '"..."' holding no quote or comma. | ||||||||||||||||||||||||||||||||||||||||||
| 300 | 2 | my $INDEX_FIELDS = qr/^\[((?:\w+|'"[^"',]+"')(?:, (?:\w+|'"[^"',]+"'))*)\]\z/; | |||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | ▶ | sub _unique_rows { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unique_rows: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 304 | 0 | my ($dbh) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | 0 | my @rows = @{$self->SUPER::_unique_rows($dbh)}; | |||||||||||||||||||||||||||||||||||||||||
| 307 | 0 | for my $index (@{$self->_rows($dbh, $self->unique_index_sql)}) { | |||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | my ($table, $name, $expressions) = @$index; | |||||||||||||||||||||||||||||||||||||||||
| 309 | 0 | ▶ | my @fields = $expressions =~ $INDEX_FIELDS ? map { s/^'"(.*)"'\z/$1/r } split /, / => $1 : (undef); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | push @rows => map { [$table, "index $name", $_] } @fields; | |||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | return \@rows; | |||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | __END__ | ||||||||||||||||||||||||||||||||||||||||||