| 1 | package DBIO::DuckDB::Storage; | ||||||||||||||||||||
| 2 | # ABSTRACT: DuckDB storage driver for DBIO | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 8 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 8 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 8 | ▶ | use base qw/DBIO::Storage::DBI/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 8 | ▶ | use mro 'c3'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 8 | ▶ | use Try::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 8 | ▶ | use namespace::clean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 8 | __PACKAGE__->register_driver('DuckDB' => __PACKAGE__); | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 0 | ▶ | sub dbio_deploy_class { 'DBIO::DuckDB::Deploy' } | ||||||||||||||||||
|
Subroutine
dbio_deploy_class: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 8 | __PACKAGE__->sql_maker_class('DBIO::DuckDB::SQLMaker'); | |||||||||||||||||||
| 18 | 8 | __PACKAGE__->sql_quote_char ('"'); | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 0 | ▶ | sub sql_maker { | ||||||||||||||||||
|
Subroutine
sql_maker: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 22 | 0 | my $self = shift; | |||||||||||||||||||
| 23 | 0 | my $sm = $self->next::method(@_); | |||||||||||||||||||
| 24 | 0 | ▶ | $sm->{quote_char} //= $self->sql_quote_char; | ||||||||||||||||||
|
Condition0%0/3
$sm->{"quote_char"} //= $self->sql_quote_char
MC/DC0%0/2
$sm->{"quote_char"} //= $self->sql_quote_char
$sm->{"quote_char"}
$self->sql_quote_char
Truth table0%0/3
A //= B
A= $sm->{"quote_char"}B= $self->sql_quote_char
| |||||||||||||||||||||
| 25 | 0 | ▶ | $sm->{name_sep} //= $self->sql_name_sep; | ||||||||||||||||||
|
Condition0%0/3
$sm->{"name_sep"} //= $self->sql_name_sep
MC/DC0%0/2
$sm->{"name_sep"} //= $self->sql_name_sep
$sm->{"name_sep"}
$self->sql_name_sep
Truth table0%0/3
A //= B
A= $sm->{"name_sep"}B= $self->sql_name_sep
| |||||||||||||||||||||
| 26 | 0 | $sm; | |||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 0 | ▶ | sub _determine_supports_insert_returning { 1 } | ||||||||||||||||||
|
Subroutine
_determine_supports_insert_returning: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | # DuckDB supports standard savepoints. | ||||||||||||||||||||
| 32 | 0 | ▶ | sub _exec_svp_begin { | ||||||||||||||||||
|
Subroutine
_exec_svp_begin: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 33 | 0 | my ($self, $name) = @_; | |||||||||||||||||||
| 34 | 0 | $self->_dbh->do("SAVEPOINT $name"); | |||||||||||||||||||
| 35 | } | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 0 | ▶ | sub _exec_svp_release { | ||||||||||||||||||
|
Subroutine
_exec_svp_release: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 38 | 0 | my ($self, $name) = @_; | |||||||||||||||||||
| 39 | 0 | $self->_dbh->do("RELEASE SAVEPOINT $name"); | |||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 0 | ▶ | sub _exec_svp_rollback { | ||||||||||||||||||
|
Subroutine
_exec_svp_rollback: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 43 | 0 | my ($self, $name) = @_; | |||||||||||||||||||
| 44 | 0 | $self->_dbh->do("ROLLBACK TO SAVEPOINT $name"); | |||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 0 | ▶ | sub _ping { | ||||||||||||||||||
|
Subroutine
_ping: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 48 | 0 | my $self = shift; | |||||||||||||||||||
| 49 | 0 | ▶ | my $dbh = $self->_dbh or return undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 50 | 0 | ▶ | return undef unless $dbh->FETCH('Active'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | 0 | ▶ | return try { $dbh->do('SELECT 1'); 1 } catch { undef }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 0 | ▶ | sub duckdb_appender { | ||||||||||||||||||
|
Subroutine
duckdb_appender: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 56 | 0 | my ($self, $table, $schema) = @_; | |||||||||||||||||||
| 57 | 0 | ▶ | $self->throw_exception('Usage: $storage->duckdb_appender($table [, $schema])') unless $table; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | 0 | return $self->_get_dbh->x_duckdb_appender($table, $schema); | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 0 | ▶ | sub duckdb_arrow_fetch { | ||||||||||||||||||
|
Subroutine
duckdb_arrow_fetch: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 63 | 0 | my ($self, $sql, $binds) = @_; | |||||||||||||||||||
| 64 | 0 | ▶ | $binds //= []; | ||||||||||||||||||
|
Condition0%0/2
$binds //= []
MC/DC0%0/1
$binds //= []
$binds
Truth table0%0/2
A
A= $binds
| |||||||||||||||||||||
| 65 | 0 | my $dbh = $self->_get_dbh; | |||||||||||||||||||
| 66 | 0 | my $sth = $dbh->prepare($sql); | |||||||||||||||||||
| 67 | 0 | $sth->execute(@$binds); | |||||||||||||||||||
| 68 | 0 | my @rows; | |||||||||||||||||||
| 69 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | 0 | push @rows, { %$row }; | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | 0 | return \@rows; | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 0 | ▶ | sub duckdb_read_csv { | ||||||||||||||||||
|
Subroutine
duckdb_read_csv: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 77 | 0 | my ($self, $path, $opts) = @_; | |||||||||||||||||||
| 78 | 0 | ▶ | $self->throw_exception('Usage: $storage->duckdb_read_csv($path [, \%opts])') unless $path; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 0 | ▶ | my $sth = $self->_get_dbh->x_duckdb_read_csv($path, $opts // {}); | ||||||||||||||||||
|
Condition0%0/2
$opts // {}
MC/DC0%0/1
$opts // {}
$opts
Truth table0%0/2
A
A= $opts
| |||||||||||||||||||||
| 80 | 0 | my @rows; | |||||||||||||||||||
| 81 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { push @rows, { %$row } } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | 0 | return \@rows; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 0 | ▶ | sub duckdb_read_parquet { | ||||||||||||||||||
|
Subroutine
duckdb_read_parquet: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 87 | 0 | my ($self, $path) = @_; | |||||||||||||||||||
| 88 | 0 | ▶ | $self->throw_exception('Usage: $storage->duckdb_read_parquet($path)') unless $path; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | 0 | return $self->_get_dbh->selectall_arrayref( | |||||||||||||||||||
| 90 | 'SELECT * FROM read_parquet(?)', | ||||||||||||||||||||
| 91 | { Slice => {} }, | ||||||||||||||||||||
| 92 | $path, | ||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 0 | ▶ | sub duckdb_read_json { | ||||||||||||||||||
|
Subroutine
duckdb_read_json: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 98 | 0 | my ($self, $path, $opts) = @_; | |||||||||||||||||||
| 99 | 0 | ▶ | $self->throw_exception('Usage: $storage->duckdb_read_json($path [, \%opts])') unless $path; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | 0 | ▶ | my $sth = $self->_get_dbh->x_duckdb_read_json($path, $opts // {}); | ||||||||||||||||||
|
Condition0%0/2
$opts // {}
MC/DC0%0/1
$opts // {}
$opts
Truth table0%0/2
A
A= $opts
| |||||||||||||||||||||
| 101 | 0 | my @rows; | |||||||||||||||||||
| 102 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { push @rows, { %$row } } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 0 | return \@rows; | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 0 | ▶ | sub duckdb_version { | ||||||||||||||||||
|
Subroutine
duckdb_version: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 108 | 0 | my $self = shift; | |||||||||||||||||||
| 109 | 0 | return $self->_get_dbh->x_duckdb_version; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 0 | ▶ | sub duckdb_install_extension { | ||||||||||||||||||
|
Subroutine
duckdb_install_extension: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 114 | 0 | my ($self, $name, %opts) = @_; | |||||||||||||||||||
| 115 | 0 | ▶ | $self->throw_exception('extension name required') unless $name; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | 0 | ▶ | $name =~ /^[A-Za-z_][A-Za-z0-9_]*$/ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | or $self->throw_exception("invalid extension name: $name"); | ||||||||||||||||||||
| 118 | 0 | my $dbh = $self->_get_dbh; | |||||||||||||||||||
| 119 | 0 | $dbh->do("INSTALL $name"); | |||||||||||||||||||
| 120 | 0 | ▶ | $dbh->do("LOAD $name") if $opts{load}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | 0 | return 1; | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 0 | ▶ | sub duckdb_checkpoint { | ||||||||||||||||||
|
Subroutine
duckdb_checkpoint: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 126 | 0 | my $self = shift; | |||||||||||||||||||
| 127 | 0 | $self->_get_dbh->do('CHECKPOINT'); | |||||||||||||||||||
| 128 | 0 | return 1; | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 0 | ▶ | sub quack_serve { | ||||||||||||||||||
|
Subroutine
quack_serve: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 133 | 0 | my ($self, $addr, %opts) = @_; | |||||||||||||||||||
| 134 | 0 | ▶ | $addr //= 'quack:localhost'; | ||||||||||||||||||
|
Condition0%0/2
$addr //= "quack:localhost"
MC/DC0%0/1
$addr //= "quack:localhost"
$addr
Truth table0%0/2
A
A= $addr
| |||||||||||||||||||||
| 135 | 0 | _validate_quack_addr($self, $addr); | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 0 | $self->duckdb_install_extension('quack', load => 1); | |||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 0 | my $dbh = $self->_get_dbh; | |||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 0 | ▶ | if (defined $opts{token}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 142 | 0 | _validate_quack_token($self, $opts{token}); | |||||||||||||||||||
| 143 | 0 | my $tok_lit = $opts{token}; | |||||||||||||||||||
| 144 | 0 | $tok_lit =~ s/'/''/g; | |||||||||||||||||||
| 145 | 0 | my $addr_lit = $addr; | |||||||||||||||||||
| 146 | 0 | $addr_lit =~ s/'/''/g; | |||||||||||||||||||
| 147 | 0 | $dbh->do("CALL quack_serve('$addr_lit', token => '$tok_lit')"); | |||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | else { | ||||||||||||||||||||
| 150 | 0 | my $addr_lit = $addr; | |||||||||||||||||||
| 151 | 0 | $addr_lit =~ s/'/''/g; | |||||||||||||||||||
| 152 | 0 | $dbh->do("CALL quack_serve('$addr_lit')"); | |||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 0 | return 1; | |||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 0 | ▶ | sub quack_attach { | ||||||||||||||||||
|
Subroutine
quack_attach: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||
| 160 | 0 | my ($self, $addr, %opts) = @_; | |||||||||||||||||||
| 161 | 0 | ▶ | $self->throw_exception('Usage: $storage->quack_attach($addr, as => $name, ...)') unless $addr; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 162 | 0 | _validate_quack_addr($self, $addr); | |||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | my $as = $opts{as} | ||||||||||||||||||||
| 165 | 0 | ▶ | or $self->throw_exception('quack_attach: "as" option (catalog alias) is required'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 166 | 0 | ▶ | $as =~ /^[A-Za-z_][A-Za-z0-9_]*$/ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 167 | or $self->throw_exception("quack_attach: invalid catalog alias '$as'"); | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | 0 | $self->duckdb_install_extension('quack', load => 1); | |||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 0 | my $dbh = $self->_get_dbh; | |||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | 0 | ▶ | if (defined $opts{token}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 174 | 0 | _validate_quack_token($self, $opts{token}); | |||||||||||||||||||
| 175 | 0 | my $tok_lit = $opts{token}; | |||||||||||||||||||
| 176 | 0 | $tok_lit =~ s/'/''/g; | |||||||||||||||||||
| 177 | 0 | $dbh->do("CREATE SECRET (TYPE quack, TOKEN '$tok_lit')"); | |||||||||||||||||||
| 178 | } | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 0 | my $addr_lit = $addr; | |||||||||||||||||||
| 181 | 0 | $addr_lit =~ s/'/''/g; | |||||||||||||||||||
| 182 | 0 | my $attach_sql = "ATTACH '$addr_lit' AS $as"; | |||||||||||||||||||
| 183 | 0 | ▶ | $attach_sql .= ' (READ_ONLY)' if $opts{read_only}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 184 | 0 | $dbh->do($attach_sql); | |||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | 0 | return 1; | |||||||||||||||||||
| 187 | } | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | 0 | ▶ | sub quack_detach { | ||||||||||||||||||
|
Subroutine
quack_detach: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 191 | 0 | my ($self, $name) = @_; | |||||||||||||||||||
| 192 | 0 | ▶ | $self->throw_exception('Usage: $storage->quack_detach($name)') unless defined $name; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 193 | 0 | ▶ | $name =~ /^[A-Za-z_][A-Za-z0-9_]*$/ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 194 | or $self->throw_exception("quack_detach: invalid catalog name '$name'"); | ||||||||||||||||||||
| 195 | 0 | $self->_get_dbh->do("DETACH $name"); | |||||||||||||||||||
| 196 | 0 | return 1; | |||||||||||||||||||
| 197 | } | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | 0 | ▶ | sub connect_call_quack_attach { | ||||||||||||||||||
|
Subroutine
connect_call_quack_attach: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 201 | 0 | my ($self, $args) = @_; | |||||||||||||||||||
| 202 | 0 | ▶ | $self->throw_exception('connect_call_quack_attach: hashref argument required') | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 203 | unless ref $args eq 'HASH'; | ||||||||||||||||||||
| 204 | my $addr = $args->{addr} | ||||||||||||||||||||
| 205 | 0 | ▶ | or $self->throw_exception('connect_call_quack_attach: "addr" required in args hashref'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 206 | 0 | return $self->quack_attach($addr, %{ $args }{ grep { $_ ne 'addr' } keys %$args }); | |||||||||||||||||||
| 207 | } | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | # ---- private helpers ---- | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | 0 | ▶ | sub _validate_quack_addr { | ||||||||||||||||||
|
Subroutine
_validate_quack_addr: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 212 | 0 | my ($self, $addr) = @_; | |||||||||||||||||||
| 213 | 0 | ▶ | $addr =~ /^quack:/ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 214 | or $self->throw_exception("quack address must start with 'quack:' (got: $addr)"); | ||||||||||||||||||||
| 215 | 0 | ▶ | $addr =~ /'/ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 216 | and $self->throw_exception("quack address must not contain single quotes"); | ||||||||||||||||||||
| 217 | 0 | ▶ | $addr =~ /\n/ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 218 | and $self->throw_exception("quack address must not contain newlines"); | ||||||||||||||||||||
| 219 | 0 | return 1; | |||||||||||||||||||
| 220 | } | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | 0 | ▶ | sub _validate_quack_token { | ||||||||||||||||||
|
Subroutine
_validate_quack_token: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 223 | 0 | my ($self, $tok) = @_; | |||||||||||||||||||
| 224 | 0 | ▶ | $tok =~ /\n/ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 225 | and $self->throw_exception("quack token must not contain newlines"); | ||||||||||||||||||||
| 226 | 0 | return 1; | |||||||||||||||||||
| 227 | } | ||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | 0 | ▶ | sub bind_attribute_by_data_type { undef } | ||||||||||||||||||
|
Subroutine
bind_attribute_by_data_type: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | 8 | 1; | |||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | __END__ | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | =pod | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | =encoding UTF-8 | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | =head1 NAME | ||||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | DBIO::DuckDB::Storage - DuckDB storage driver for DBIO | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | =head1 VERSION | ||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | version 0.900001 | ||||||||||||||||||||
| 248 | |||||||||||||||||||||
| 249 | =head1 DESCRIPTION | ||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | DuckDB storage driver for DBIO. Extends L<DBIO::Storage::DBI> with | ||||||||||||||||||||
| 252 | DuckDB-specific behavior and escape-hatch methods for DuckDB-native | ||||||||||||||||||||
| 253 | features that do not fit the row-oriented DBI model. | ||||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | Sits on top of L<DBD::DuckDB>, a pure-FFI DBI driver. No XS compile, | ||||||||||||||||||||
| 256 | C<libduckdb> must be installed at runtime. | ||||||||||||||||||||
| 257 | |||||||||||||||||||||
| 258 | Registered as the driver for C<DuckDB> and loaded automatically when | ||||||||||||||||||||
| 259 | connecting to a C<dbi:DuckDB:*> DSN. | ||||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | =head1 METHODS | ||||||||||||||||||||
| 262 | |||||||||||||||||||||
| 263 | =head2 duckdb_appender | ||||||||||||||||||||
| 264 | |||||||||||||||||||||
| 265 | my $appender = $storage->duckdb_appender($table); | ||||||||||||||||||||
| 266 | my $appender = $storage->duckdb_appender($table, $schema); | ||||||||||||||||||||
| 267 | |||||||||||||||||||||
| 268 | Returns a L<DBD::DuckDB::Appender> bound to C<$table> (optionally | ||||||||||||||||||||
| 269 | qualified with C<$schema>, default C<main>). The Appender is DuckDB's | ||||||||||||||||||||
| 270 | native bulk-insert API -- dramatically faster than row-by-row INSERT | ||||||||||||||||||||
| 271 | for loading large batches. | ||||||||||||||||||||
| 272 | |||||||||||||||||||||
| 273 | my $app = $storage->duckdb_appender('events'); | ||||||||||||||||||||
| 274 | for my $event (@events) { | ||||||||||||||||||||
| 275 | $app->append_int64($event->{id}); | ||||||||||||||||||||
| 276 | $app->append_varchar($event->{name}); | ||||||||||||||||||||
| 277 | $app->append_timestamp($event->{ts}); | ||||||||||||||||||||
| 278 | $app->end_row; | ||||||||||||||||||||
| 279 | } | ||||||||||||||||||||
| 280 | $app->flush; | ||||||||||||||||||||
| 281 | |||||||||||||||||||||
| 282 | Uses L<DBD::DuckDB/x_duckdb_appender> under the hood. | ||||||||||||||||||||
| 283 | |||||||||||||||||||||
| 284 | =head2 duckdb_arrow_fetch | ||||||||||||||||||||
| 285 | |||||||||||||||||||||
| 286 | my $result = $storage->duckdb_arrow_fetch($sql, \@binds); | ||||||||||||||||||||
| 287 | |||||||||||||||||||||
| 288 | Reserved escape hatch for columnar Arrow-format fetches. The intent is to | ||||||||||||||||||||
| 289 | stream DuckDB query results directly as Arrow IPC buffers, bypassing the | ||||||||||||||||||||
| 290 | DBI row iterator and the Perl-scalar type coercion completely. This is | ||||||||||||||||||||
| 291 | where DuckDB's columnar execution model actually pays off. | ||||||||||||||||||||
| 292 | |||||||||||||||||||||
| 293 | Current implementation is a fallback: it runs the query via DBI and | ||||||||||||||||||||
| 294 | returns C<< [ { col => val, ... }, ... ] >>. A future version will switch | ||||||||||||||||||||
| 295 | to the libduckdb Arrow API (C<duckdb_query_arrow>, | ||||||||||||||||||||
| 296 | C<duckdb_result_arrow_array>) via L<FFI::Platypus>, and optionally hand | ||||||||||||||||||||
| 297 | back a L<Data::Frame> or L<PDL::DataFrame> object when those modules are | ||||||||||||||||||||
| 298 | available. | ||||||||||||||||||||
| 299 | |||||||||||||||||||||
| 300 | This method is marked B<experimental>: the return type will change once | ||||||||||||||||||||
| 301 | the real Arrow path lands. Do not rely on the fallback shape in | ||||||||||||||||||||
| 302 | long-lived code. | ||||||||||||||||||||
| 303 | |||||||||||||||||||||
| 304 | =head2 duckdb_read_csv | ||||||||||||||||||||
| 305 | |||||||||||||||||||||
| 306 | my $rs = $storage->duckdb_read_csv('/path/to/file.csv', \%opts); | ||||||||||||||||||||
| 307 | |||||||||||||||||||||
| 308 | Wraps DuckDB's C<read_csv> table function. Returns an arrayref of row | ||||||||||||||||||||
| 309 | hashrefs. C<%opts> is passed through to | ||||||||||||||||||||
| 310 | L<DBD::DuckDB/x_duckdb_read_csv>. | ||||||||||||||||||||
| 311 | |||||||||||||||||||||
| 312 | =head2 duckdb_read_parquet | ||||||||||||||||||||
| 313 | |||||||||||||||||||||
| 314 | my $rs = $storage->duckdb_read_parquet('/path/to/file.parquet'); | ||||||||||||||||||||
| 315 | |||||||||||||||||||||
| 316 | Runs C<SELECT * FROM read_parquet(?)> against DuckDB. Returns an arrayref | ||||||||||||||||||||
| 317 | of row hashrefs. Requires DuckDB's C<parquet> extension (bundled with | ||||||||||||||||||||
| 318 | most builds). | ||||||||||||||||||||
| 319 | |||||||||||||||||||||
| 320 | =head2 duckdb_read_json | ||||||||||||||||||||
| 321 | |||||||||||||||||||||
| 322 | my $rs = $storage->duckdb_read_json('/path/to/file.json', \%opts); | ||||||||||||||||||||
| 323 | |||||||||||||||||||||
| 324 | Wraps DuckDB's C<read_json> table function via | ||||||||||||||||||||
| 325 | L<DBD::DuckDB/x_duckdb_read_json>. | ||||||||||||||||||||
| 326 | |||||||||||||||||||||
| 327 | =head2 duckdb_version | ||||||||||||||||||||
| 328 | |||||||||||||||||||||
| 329 | my $v = $storage->duckdb_version; | ||||||||||||||||||||
| 330 | |||||||||||||||||||||
| 331 | Returns the linked libduckdb version string (e.g. C<v1.0.0>). | ||||||||||||||||||||
| 332 | |||||||||||||||||||||
| 333 | =head2 duckdb_install_extension | ||||||||||||||||||||
| 334 | |||||||||||||||||||||
| 335 | $storage->duckdb_install_extension('httpfs'); | ||||||||||||||||||||
| 336 | $storage->duckdb_install_extension('httpfs', load => 1); | ||||||||||||||||||||
| 337 | |||||||||||||||||||||
| 338 | Runs C<INSTALL $name>. With C<< load => 1 >> also runs C<LOAD $name>. | ||||||||||||||||||||
| 339 | |||||||||||||||||||||
| 340 | =head2 duckdb_checkpoint | ||||||||||||||||||||
| 341 | |||||||||||||||||||||
| 342 | $storage->duckdb_checkpoint; | ||||||||||||||||||||
| 343 | |||||||||||||||||||||
| 344 | Issues a C<CHECKPOINT> to flush the WAL and compact storage. | ||||||||||||||||||||
| 345 | |||||||||||||||||||||
| 346 | =head2 quack_serve | ||||||||||||||||||||
| 347 | |||||||||||||||||||||
| 348 | $storage->quack_serve('quack:localhost:9500'); | ||||||||||||||||||||
| 349 | $storage->quack_serve('quack:localhost:9500', token => 'my-secret'); | ||||||||||||||||||||
| 350 | |||||||||||||||||||||
| 351 | Loads the quack extension (C<INSTALL quack; LOAD quack>) then starts a | ||||||||||||||||||||
| 352 | Quack server on the given C<$addr>. The server continues to serve the | ||||||||||||||||||||
| 353 | current database until the process exits or the connection is closed. | ||||||||||||||||||||
| 354 | |||||||||||||||||||||
| 355 | C<$addr> must start with C<quack:> and must not contain single quotes or | ||||||||||||||||||||
| 356 | newlines. C<token> is optional; when given it is embedded as a SQL | ||||||||||||||||||||
| 357 | string literal (single quotes escaped). | ||||||||||||||||||||
| 358 | |||||||||||||||||||||
| 359 | Returns 1 on success. Throws a L<DBIO::Exception> on validation failure or SQL error. | ||||||||||||||||||||
| 360 | |||||||||||||||||||||
| 361 | =head2 quack_attach | ||||||||||||||||||||
| 362 | |||||||||||||||||||||
| 363 | $storage->quack_attach('quack:localhost:9500', as => 'remote'); | ||||||||||||||||||||
| 364 | $storage->quack_attach('quack:localhost:9500', | ||||||||||||||||||||
| 365 | as => 'remote', | ||||||||||||||||||||
| 366 | token => 'my-secret', | ||||||||||||||||||||
| 367 | read_only => 1, | ||||||||||||||||||||
| 368 | ); | ||||||||||||||||||||
| 369 | |||||||||||||||||||||
| 370 | Loads quack, optionally creates a secret for authentication, then | ||||||||||||||||||||
| 371 | C<ATTACH>es the remote Quack server as a named catalog. | ||||||||||||||||||||
| 372 | |||||||||||||||||||||
| 373 | C<as> is required and must be a valid SQL identifier | ||||||||||||||||||||
| 374 | (C<^[A-Za-z_][A-Za-z0-9_]*$>). C<$addr> must start with C<quack:> and | ||||||||||||||||||||
| 375 | contain no single quotes or newlines. C<read_only> attaches the catalog | ||||||||||||||||||||
| 376 | in read-only mode. | ||||||||||||||||||||
| 377 | |||||||||||||||||||||
| 378 | Returns 1 on success. | ||||||||||||||||||||
| 379 | |||||||||||||||||||||
| 380 | =head2 quack_detach | ||||||||||||||||||||
| 381 | |||||||||||||||||||||
| 382 | $storage->quack_detach('remote'); | ||||||||||||||||||||
| 383 | |||||||||||||||||||||
| 384 | Detaches a previously attached Quack (or any) catalog by name. Name must | ||||||||||||||||||||
| 385 | be a valid SQL identifier. | ||||||||||||||||||||
| 386 | |||||||||||||||||||||
| 387 | Returns 1 on success. | ||||||||||||||||||||
| 388 | |||||||||||||||||||||
| 389 | =head2 connect_call_quack_attach | ||||||||||||||||||||
| 390 | |||||||||||||||||||||
| 391 | on_connect_call => [[ quack_attach => { | ||||||||||||||||||||
| 392 | addr => 'quack:localhost:9500', | ||||||||||||||||||||
| 393 | as => 'remote', | ||||||||||||||||||||
| 394 | token => 'my-secret', | ||||||||||||||||||||
| 395 | read_only => 0, | ||||||||||||||||||||
| 396 | }]] | ||||||||||||||||||||
| 397 | |||||||||||||||||||||
| 398 | On-connect hook that calls L</quack_attach> with the supplied hashref. | ||||||||||||||||||||
| 399 | Use via C<on_connect_call> in the connection options to auto-attach a | ||||||||||||||||||||
| 400 | Quack server whenever the storage connects. | ||||||||||||||||||||
| 401 | |||||||||||||||||||||
| 402 | C<addr> and C<as> are required in the hashref. C<token> and C<read_only> | ||||||||||||||||||||
| 403 | are optional. See L</quack_attach> for validation rules. | ||||||||||||||||||||
| 404 | |||||||||||||||||||||
| 405 | =head2 bind_attribute_by_data_type | ||||||||||||||||||||
| 406 | |||||||||||||||||||||
| 407 | Returns undef for all types -- DuckDB's prepared-statement binding is | ||||||||||||||||||||
| 408 | already strongly typed via libduckdb, and DBD::DuckDB maps perl scalars | ||||||||||||||||||||
| 409 | onto the correct logical type based on the prepared parameter. No DBI | ||||||||||||||||||||
| 410 | bind-attr overrides needed. | ||||||||||||||||||||
| 411 | |||||||||||||||||||||
| 412 | =head1 ESCAPE HATCHES | ||||||||||||||||||||
| 413 | |||||||||||||||||||||
| 414 | DBI is row-oriented. DuckDB is columnar. These methods bypass the DBI | ||||||||||||||||||||
| 415 | statement-handle path where it hurts: | ||||||||||||||||||||
| 416 | |||||||||||||||||||||
| 417 | =head1 QUACK (CLIENT-SERVER RPC) | ||||||||||||||||||||
| 418 | |||||||||||||||||||||
| 419 | Quack is a DuckDB extension (v1.5+) that exposes an embedded DuckDB | ||||||||||||||||||||
| 420 | instance over HTTP-based RPC, turning it into a lightweight server. The | ||||||||||||||||||||
| 421 | client remains a normal in-process DuckDB using DBD::DuckDB -- no new | ||||||||||||||||||||
| 422 | transport layer. | ||||||||||||||||||||
| 423 | |||||||||||||||||||||
| 424 | B<Requires libduckdb E<gt>= 1.5.> The Alien::DuckDB 0.03 package ships | ||||||||||||||||||||
| 425 | 1.3.0, which does not include quack. To use these methods, supply a | ||||||||||||||||||||
| 426 | newer libduckdb via C<DUCKDB_NO_ALIEN=1 LD_LIBRARY_PATH=/path/to/1.5.x>. | ||||||||||||||||||||
| 427 | |||||||||||||||||||||
| 428 | =seealso | ||||||||||||||||||||
| 429 | |||||||||||||||||||||
| 430 | =over | ||||||||||||||||||||
| 431 | |||||||||||||||||||||
| 432 | =item * L<DBIO::DuckDB> - Schema component that activates this storage | ||||||||||||||||||||
| 433 | |||||||||||||||||||||
| 434 | =item * L<DBIO::DuckDB::SQLMaker> - SQL generation for DuckDB | ||||||||||||||||||||
| 435 | |||||||||||||||||||||
| 436 | =item * L<DBIO::DuckDB::Deploy> - test-deploy-and-compare schema deploy | ||||||||||||||||||||
| 437 | |||||||||||||||||||||
| 438 | =item * L<DBD::DuckDB> - underlying DBI driver | ||||||||||||||||||||
| 439 | |||||||||||||||||||||
| 440 | =item * L<DBIO::Storage::DBI> - Base DBI storage class | ||||||||||||||||||||
| 441 | |||||||||||||||||||||
| 442 | =back | ||||||||||||||||||||
| 443 | |||||||||||||||||||||
| 444 | =head1 AUTHOR | ||||||||||||||||||||
| 445 | |||||||||||||||||||||
| 446 | DBIO Authors | ||||||||||||||||||||
| 447 | |||||||||||||||||||||
| 448 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 449 | |||||||||||||||||||||
| 450 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||
| 451 | |||||||||||||||||||||
| 452 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 453 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 454 | |||||||||||||||||||||
| 455 | =cut | ||||||||||||||||||||