| File: | blib/lib/Database/Join/Catalog.pm |
| Coverage: | 91.3% |
| line | stmt | bran | cond | sub | time | code |
|---|---|---|---|---|---|---|
| 1 | package Database::Join::Catalog; | |||||
| 2 | ||||||
| 3 | 1 1 | 127962 1 | use 5.010001; | |||
| 4 | 1 1 1 | 1 1 6 | use strict; | |||
| 5 | 1 1 1 | 1 1 19 | use warnings; | |||
| 6 | ||||||
| 7 | 1 1 1 | 1 0 20 | use Carp qw(croak); | |||
| 8 | 1 1 1 | 1 0 13 | use DBI; | |||
| 9 | 1 1 1 | 1 1 12 | use File::Temp qw(tempfile); | |||
| 10 | 1 1 1 | 152 13332 25 | use Params::Get qw(get_params); | |||
| 11 | 1 1 1 | 334 6157 23 | use Params::Validate::Strict qw(validate_strict); | |||
| 12 | 1 1 1 | 2 1 12 | use Readonly; | |||
| 13 | 1 1 1 | 1 1 10 | use Scalar::Util qw(blessed); | |||
| 14 | ||||||
| 15 | 1 1 1 | 1 0 2 | use version; our $VERSION = version->declare('0.011.4'); | |||
| 16 | ||||||
| 17 | # What a caller needs to know about a database before it opens a table of it | |||||
| 18 | # through Database::Abstraction, and the one write it may need: a table of | |||||
| 19 | # rows as a SQLite file. Database::Abstraction opens a table it is told the | |||||
| 20 | # name of, and Database::Join reads; neither lists the tables a file or a | |||||
| 21 | # server holds, says what a table's columns are in their declared order or | |||||
| 22 | # what its key is, or writes a file. An application that used DBI for those | |||||
| 23 | # would need a driver of its own beside the ones this distribution requires, | |||||
| 24 | # so they are here. | |||||
| 25 | ||||||
| 26 | Readonly my $SQLITE_RE => qr/\Adbi:SQLite:/i; | |||||
| 27 | Readonly my $MYSQL_RE => qr/\Adbi:(?:MariaDB|mysql):/i; | |||||
| 28 | Readonly my $DRIVER_RE => qr/\A[A-Za-z][A-Za-z0-9_]*\z/; | |||||
| 29 | ||||||
| 30 | Readonly::Hash my %MESSAGES => ( | |||||
| 31 | error_source_required => 'Give a file (a SQLite database) or a dsn', | |||||
| 32 | error_source_both => 'Give a file or a dsn, not both', | |||||
| 33 | error_driver_unsupported => 'Listing tables is supported for SQLite, MariaDB and MySQL, not "%s"', | |||||
| 34 | error_table_required => 'A table name is required', | |||||
| 35 | error_columns_required => 'At least one column is required', | |||||
| 36 | error_row_not_hash => 'rows[%d] is not a hash of column values', | |||||
| 37 | error_file_exists => '"%s" already exists; write_sqlite makes a new file and never replaces one', | |||||
| 38 | error_temp_file => 'Cannot make a temporary file: %s', | |||||
| 39 | error_no_reason => 'The database driver failed and gave no reason', | |||||
| 40 | ); | |||||
| 41 | ||||||
| 42 | sub _msg { | |||||
| 43 | 10 | 12 | my ($key, @args) = @_; | |||
| 44 | 10 | 20 | return @args ? sprintf($MESSAGES{$key}, @args) : $MESSAGES{$key}; | |||
| 45 | } | |||||
| 46 | ||||||
| 47 | # _fail($stage, $why) -- croak a Database::Join::Catalog::Error. | |||||
| 48 | sub _fail { | |||||
| 49 | 9 | 8 | my ($stage, $why) = @_; | |||
| 50 | 9 | 6 | $why //= ''; | |||
| 51 | # RaiseError's form: "DBD::SQLite::db prepare failed: no such table: x at FILE line N." | |||||
| 52 | # and "DBI connect('dsn','user',...) failed: ...", which names the data source. | |||||
| 53 | 9 | 22 | $why =~ s/\A(?:DBD::\w+::\w+ \w+|DBI \w+\(.*?\)) failed: //s; | |||
| 54 | 9 | 18 | $why =~ s/\s+at \S+ line \d+\.?\s*\z//; | |||
| 55 | 9 | 8 | $why = _msg('error_no_reason') unless length $why; | |||
| 56 | 9 | 19 | croak(Database::Join::Catalog::Error->new(stage => $stage, message => $why)); | |||
| 57 | } | |||||
| 58 | ||||||
| 59 | # _source(\%args) -> ($dsn, $username, $password, $is_sqlite) | |||||
| 60 | sub _source { | |||||
| 61 | 25 | 18 | my ($p) = @_; | |||
| 62 | 25 | 30 | croak _msg('error_source_both') if defined $p->{file} && defined $p->{dsn}; | |||
| 63 | 24 | 20 | if (defined $p->{file}) { | |||
| 64 | 17 | 20 | return ("dbi:SQLite:dbname=$p->{file}", '', '', 1); | |||
| 65 | } | |||||
| 66 | 7 | 10 | croak _msg('error_source_required') unless defined $p->{dsn} && length $p->{dsn}; | |||
| 67 | 6 | 15 | return ($p->{dsn}, $p->{username}, $p->{password}, $p->{dsn} =~ $SQLITE_RE ? 1 : 0); | |||
| 68 | } | |||||
| 69 | ||||||
| 70 | # _connect($dsn, $username, $password, $is_sqlite) -> $dbh | |||||
| 71 | # | |||||
| 72 | # A SQLite file is opened read-only: connecting to a name that is not there | |||||
| 73 | # would otherwise make an empty database of it. | |||||
| 74 | sub _connect { | |||||
| 75 | 21 | 15 | my ($dsn, $username, $password, $is_sqlite) = @_; | |||
| 76 | 21 | 21 | my %attr = (RaiseError => 1, PrintError => 0, AutoCommit => 1); | |||
| 77 | 21 | 11 | if ($is_sqlite) { | |||
| 78 | 18 | 281 | require DBD::SQLite::Constants; | |||
| 79 | 18 | 419 | $attr{sqlite_open_flags} = DBD::SQLite::Constants::SQLITE_OPEN_READONLY(); | |||
| 80 | } | |||||
| 81 | 21 21 | 13 27 | my $dbh = eval { DBI->connect($dsn, $username, $password, \%attr) }; | |||
| 82 | 21 | 3086 | _fail('connect', $@) unless $dbh; | |||
| 83 | 18 | 12 | return $dbh; | |||
| 84 | } | |||||
| 85 | ||||||
| 86 | # _query($dbh, $code) -> whatever $code returns; disconnects either way. | |||||
| 87 | sub _query { | |||||
| 88 | 18 | 11 | my ($dbh, $code) = @_; | |||
| 89 | 18 18 | 11 9 | my $got = eval { [ $code->() ] }; | |||
| 90 | 18 | 519 | my $why = $@; | |||
| 91 | 18 18 | 10 268 | eval { $dbh->disconnect }; | |||
| 92 | 18 | 20 | _fail('query', $why) unless $got; | |||
| 93 | 15 9 | 29 40 | return wantarray ? @{$got} : $got->[0]; | |||
| 94 | } | |||||
| 95 | ||||||
| 96 | sub _params { | |||||
| 97 | 37 | 39 | my ($schema, @args) = @_; | |||
| 98 | 37 | 81 | shift @args if @args && !ref $args[0] && $args[0] eq __PACKAGE__; | |||
| 99 | return validate_strict( | |||||
| 100 | schema => { | |||||
| 101 | file => { type => 'string', optional => 1 }, | |||||
| 102 | dsn => { type => 'string', optional => 1 }, | |||||
| 103 | username => { type => 'string', optional => 1 }, | |||||
| 104 | password => { type => 'string', optional => 1 }, | |||||
| 105 | 37 37 | 60 73 | %{$schema}, | |||
| 106 | }, | |||||
| 107 | input => get_params(undef, \@args) // {}, | |||||
| 108 | ); | |||||
| 109 | } | |||||
| 110 | ||||||
| 111 | sub available_driver { | |||||
| 112 | 3 | 1270 | my ($class, @names) = @_; | |||
| 113 | 3 | 15 | local ($@, $!); | |||
| 114 | 3 | 3 | for my $name (@names) { | |||
| 115 | 5 | 13 | next unless defined $name && $name =~ $DRIVER_RE; | |||
| 116 | 4 | 19 | my $module = "DBD/$name.pm"; | |||
| 117 | 4 4 2 | 1 124 7 | return $name if eval { require $module; 1 }; | |||
| 118 | } | |||||
| 119 | 1 | 2 | return undef; | |||
| 120 | } | |||||
| 121 | ||||||
| 122 | sub schema { | |||||
| 123 | 7 | 3252 | my ($class, @args) = @_; | |||
| 124 | 7 | 27 | local ($@, $!); | |||
| 125 | 7 | 6 | my $p = _params({}, @args); | |||
| 126 | 7 | 772 | my ($dsn, $username, $password, $is_sqlite) = _source($p); | |||
| 127 | 7 | 24 | croak _msg('error_driver_unsupported', ($dsn =~ /\Adbi:([^:]*)/i)[0] // $dsn) | |||
| 128 | unless $is_sqlite || $dsn =~ $MYSQL_RE; | |||||
| 129 | ||||||
| 130 | 6 | 12 | my $dbh = _connect($dsn, $username, $password, $is_sqlite); | |||
| 131 | return _query($dbh, sub { | |||||
| 132 | 5 | 4 | my @tables; | |||
| 133 | 5 | 2 | if ($is_sqlite) { | |||
| 134 | 3 | 7 | my $names = $dbh->selectcol_arrayref(q{SELECT name FROM sqlite_master } | |||
| 135 | . q{WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name}); | |||||
| 136 | 3 3 | 156 4 | for my $name (@{ $names // [] }) { | |||
| 137 | 7 | 11 | my $info = $dbh->selectall_arrayref('PRAGMA table_info(' . $dbh->quote_identifier($name) . ')', | |||
| 138 | { Slice => {} }); | |||||
| 139 | 7 15 7 | 1062 20 6 | push @tables, { name => $name, columns => [ map { $_->{name} } @{ $info // [] } ] }; | |||
| 140 | } | |||||
| 141 | } else { | |||||
| 142 | 2 | 3 | my $rows = $dbh->selectall_arrayref(q{SELECT TABLE_NAME, COLUMN_NAME FROM information_schema.COLUMNS } | |||
| 143 | . q{WHERE TABLE_SCHEMA = DATABASE() ORDER BY TABLE_NAME, ORDINAL_POSITION}); | |||||
| 144 | 2 | 5 | my %seen; | |||
| 145 | 2 2 | 0 3 | for my $row (@{ $rows // [] }) { | |||
| 146 | 6 6 | 4 2 | my ($name, $column) = @{$row}; | |||
| 147 | 6 | 8 | push @tables, ($seen{$name} = { name => $name, columns => [] }) unless $seen{$name}; | |||
| 148 | 6 6 | 3 3 | push @{ $seen{$name}{columns} }, $column; | |||
| 149 | } | |||||
| 150 | } | |||||
| 151 | 5 | 7 | return \@tables; | |||
| 152 | 5 | 11 | }); | |||
| 153 | } | |||||
| 154 | ||||||
| 155 | sub tables { | |||||
| 156 | 12 | 81459 | my ($class, @args) = @_; | |||
| 157 | 12 | 57 | local ($@, $!); | |||
| 158 | 12 | 14 | my $p = _params({}, @args); | |||
| 159 | 12 | 1403 | my ($dsn, $username, $password, $is_sqlite) = _source($p); | |||
| 160 | 10 2 1 1 | 18 4 1 2 | return [ map { $_->{name} } @{ $class->schema(%{$p}) } ] unless $is_sqlite; | |||
| 161 | ||||||
| 162 | 9 | 8 | my $dbh = _connect($dsn, $username, $password, 1); | |||
| 163 | return _query($dbh, sub { | |||||
| 164 | 7 | 16 | return $dbh->selectcol_arrayref(q{SELECT name FROM sqlite_master } | |||
| 165 | . q{WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name}) // []; | |||||
| 166 | 7 | 15 | }); | |||
| 167 | } | |||||
| 168 | ||||||
| 169 | sub table_info { | |||||
| 170 | 7 | 1979 | my ($class, @args) = @_; | |||
| 171 | 7 | 38 | local ($@, $!); | |||
| 172 | 7 | 15 | my $p = _params({ table => { type => 'string', optional => 1 } }, @args); | |||
| 173 | 7 | 936 | croak _msg('error_table_required') unless defined $p->{table} && length $p->{table}; | |||
| 174 | 6 | 6 | my ($dsn, $username, $password, $is_sqlite) = _source($p); | |||
| 175 | ||||||
| 176 | 6 | 6 | my $dbh = _connect($dsn, $username, $password, $is_sqlite); | |||
| 177 | return _query($dbh, sub { | |||||
| 178 | 6 | 12 | my $sth = $dbh->prepare('SELECT * FROM ' . $dbh->quote_identifier($p->{table}) . ' WHERE 1=0'); | |||
| 179 | 4 | 197 | $sth->execute; | |||
| 180 | 4 4 | 4 14 | my @columns = @{ $sth->{NAME} // [] }; | |||
| 181 | 4 | 12 | $sth->finish if $sth->can('finish'); | |||
| 182 | 4 1 4 | 3 1154 9 | my @primary = grep { defined } eval { $dbh->primary_key(undef, undef, $p->{table}) }; | |||
| 183 | 4 | 734 | return { columns => \@columns, primary_key => \@primary }; | |||
| 184 | 6 | 14 | }); | |||
| 185 | } | |||||
| 186 | ||||||
| 187 | # _write($file, \%args) -- the table and its rows, into a new SQLite file. | |||||
| 188 | sub _write { | |||||
| 189 | 6 | 7 | my ($file, $p) = @_; | |||
| 190 | 6 | 2 | my $columns = $p->{columns}; | |||
| 191 | 6 | 8 | my $table = $p->{table} // 'data'; | |||
| 192 | 6 6 | 3 15 | my $dbh = eval { DBI->connect("dbi:SQLite:dbname=$file", '', '', { RaiseError => 1, PrintError => 0, AutoCommit => 1 }) }; | |||
| 193 | 6 | 821 | _fail('connect', $@) unless $dbh; | |||
| 194 | 5 | 2 | my $ok = eval { | |||
| 195 | 5 | 8 | my $t = $dbh->quote_identifier($table); | |||
| 196 | 5 8 5 | 99 26 4 | my @c = map { $dbh->quote_identifier($_) } @{$columns}; | |||
| 197 | 5 8 | 38 12 | $dbh->do("CREATE TABLE $t (" . join(', ', map { "$_ TEXT" } @c) . ')'); | |||
| 198 | 3 3 | 2815 6 | if (@{ $p->{rows} }) { | |||
| 199 | 3 | 11 | $dbh->begin_work; | |||
| 200 | 3 | 30 | my $sth = $dbh->prepare("INSERT INTO $t (" . join(', ', @c) . ') VALUES (' . join(', ', ('?') x @c) . ')'); | |||
| 201 | 3 3 | 74 4 | for my $row (@{ $p->{rows} }) { | |||
| 202 | 6 12 6 | 5 184 4 | $sth->execute(map { $row->{$_} } @{$columns}); | |||
| 203 | } | |||||
| 204 | 3 | 2371 | $dbh->commit; | |||
| 205 | } | |||||
| 206 | 3 | 7 | 1; | |||
| 207 | }; | |||||
| 208 | 5 | 8 | my $why = $@; | |||
| 209 | 5 0 | 8 0 | eval { $dbh->rollback } unless $ok || $dbh->{AutoCommit}; | |||
| 210 | 5 5 | 3 113 | eval { $dbh->disconnect }; | |||
| 211 | 5 | 5 | _fail('query', $why) unless $ok; | |||
| 212 | 3 | 36 | return 1; | |||
| 213 | } | |||||
| 214 | ||||||
| 215 | sub _write_params { | |||||
| 216 | 11 | 12 | my @args = @_; | |||
| 217 | 11 | 29 | my $p = _params({ | |||
| 218 | table => { type => 'string', optional => 1 }, | |||||
| 219 | columns => { type => 'arrayref', optional => 1 }, | |||||
| 220 | rows => { type => 'arrayref', optional => 1 }, | |||||
| 221 | }, @args); | |||||
| 222 | 11 11 | 1770 13 | croak _msg('error_columns_required') unless ref $p->{columns} eq 'ARRAY' && @{ $p->{columns} }; | |||
| 223 | 10 | 12 | croak _msg('error_table_required') if defined $p->{table} && !length $p->{table}; | |||
| 224 | 9 | 13 | $p->{rows} //= []; | |||
| 225 | 9 9 | 3 13 | for my $i (0 .. $#{ $p->{rows} }) { | |||
| 226 | 7 | 5 | croak _msg('error_row_not_hash', $i) unless ref $p->{rows}[$i] eq 'HASH'; | |||
| 227 | } | |||||
| 228 | 8 | 7 | return $p; | |||
| 229 | } | |||||
| 230 | ||||||
| 231 | sub write_sqlite { | |||||
| 232 | 5 | 3650 | my ($class, @args) = @_; | |||
| 233 | 5 | 40 | local ($@, $!); | |||
| 234 | 5 | 5 | my $p = _write_params(@args); | |||
| 235 | 5 | 14 | croak _msg('error_source_required') unless defined $p->{file} && length $p->{file}; | |||
| 236 | 4 | 36 | croak _msg('error_file_exists', $p->{file}) if -e $p->{file} || -l $p->{file}; | |||
| 237 | 3 3 | 1 4 | my $ok = eval { _write($p->{file}, $p) }; | |||
| 238 | 3 | 27 | my $err = $@; | |||
| 239 | 3 | 3 | unless ($ok) { | |||
| 240 | 2 | 18 | unlink $p->{file}; | |||
| 241 | 2 | 7 | croak $err; | |||
| 242 | } | |||||
| 243 | 1 | 5 | return 1; | |||
| 244 | } | |||||
| 245 | ||||||
| 246 | sub sqlite_bytes { | |||||
| 247 | 6 | 1289 | my ($class, @args) = @_; | |||
| 248 | 6 | 49 | local ($@, $!); | |||
| 249 | 6 | 7 | my $p = _write_params(@args); | |||
| 250 | # EXLOCK => 0: on a BSD, File::Temp's lock stops SQLite writing the file. | |||||
| 251 | 3 3 | 3 5 | my ($fh, $file) = eval { tempfile('database-join-XXXXXXXX', SUFFIX => '.db', TMPDIR => 1, UNLINK => 0, EXLOCK => 0) }; | |||
| 252 | 3 | 399 | croak _msg('error_temp_file', $@ || $!) unless $fh; | |||
| 253 | 3 | 11 | close $fh; | |||
| 254 | 3 3 | 2 2 | my $ok = eval { _write($file, $p) }; | |||
| 255 | 3 | 20 | my $err = $@; | |||
| 256 | 3 | 3 | unless ($ok) { | |||
| 257 | 1 | 16 | unlink $file; | |||
| 258 | 1 | 5 | croak $err; | |||
| 259 | } | |||||
| 260 | 2 | 2 | my $bytes = do { | |||
| 261 | 2 0 0 0 | 23 0 0 0 | open(my $in, '<:raw', $file) or do { my $e = $!; unlink $file; croak _msg('error_temp_file', $e) }; | |||
| 262 | 2 | 4 | local $/; | |||
| 263 | 2 | 16 | my $b = <$in>; | |||
| 264 | 2 | 5 | close $in; | |||
| 265 | 2 | 5 | $b; | |||
| 266 | }; | |||||
| 267 | 2 | 305 | unlink $file; | |||
| 268 | 2 | 10 | return $bytes; | |||
| 269 | } | |||||
| 270 | ||||||
| 271 | package Database::Join::Catalog::Error; | |||||
| 272 | ||||||
| 273 | 1 1 1 | 815 1 8 | use strict; | |||
| 274 | 1 1 1 | 1 1 26 | use warnings; | |||
| 275 | 1 1 1 5 0 | 1 0 50 33 0 | use overload '""' => sub { $_[0]->{message} }, bool => sub { 1 }, fallback => 1; | |||
| 276 | ||||||
| 277 | sub new { | |||||
| 278 | 9 | 11 | my ($class, %args) = @_; | |||
| 279 | 9 | 76 | return bless { stage => $args{stage}, message => $args{message} }, $class; | |||
| 280 | } | |||||
| 281 | ||||||
| 282 | 7 | 669 | sub stage { return $_[0]->{stage} } | |||
| 283 | 0 | sub message { return $_[0]->{message} } | ||||
| 284 | ||||||
| 285 | 1; | |||||
| 286 | ||||||