| 1 | package Mojo::SQLite::Database; | ||||||||||||||||||||||||||||||||||||
| 2 | 6 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use Carp qw(croak shortmess); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use DBI 'SQL_VARCHAR'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Mojo::JSON 'to_json'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Mojo::Promise; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Mojo::SQLite::Results; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Mojo::SQLite::Transaction; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Mojo::Util 'monkey_patch'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||
| 12 | 6 | our $VERSION = 'v4.0.0'; | |||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | 6 | our @CARP_NOT = qw(Mojo::SQLite::Migrations); | |||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||
| 16 | 6 | has [qw(dbh sqlite)]; | |||||||||||||||||||||||||||||||||||
| 17 | 6 | has results_class => 'Mojo::SQLite::Results'; | |||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||
| 19 | 6 | for my $name (qw(delete insert select update)) { | |||||||||||||||||||||||||||||||||||
| 20 | 19 | ▶ | monkey_patch __PACKAGE__, $name, sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
update: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||||||||||
| 21 | 19 | ▶ | my ($self, @cb) = (shift, ref $_[-1] eq 'CODE' ? pop : ()); | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 22 | 19 | return $self->query($self->sqlite->abstract->$name(@_), @cb); | |||||||||||||||||||||||||||||||||||
| 23 | 24 | }; | |||||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | monkey_patch __PACKAGE__, "${name}_p", sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Subroutine
delete_p: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||
| 25 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 26 | 0 | return $self->query_p($self->sqlite->abstract->$name(@_)); | |||||||||||||||||||||||||||||||||||
| 27 | 24 | }; | |||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||
| 30 | 123 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=5 SCAR=19.4
| |||||||||||||||||||||||||||||||||||||
| 31 | 123 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||
| 33 | # Supported on Perl 5.14+ | ||||||||||||||||||||||||||||||||||||
| 34 | 123 | ▶ | return() if defined ${^GLOBAL_PHASE} && ${^GLOBAL_PHASE} eq 'DESTRUCT'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined ${^GLOBAL_PHASE} and ${^GLOBAL_PHASE} eq "DESTRUCT"
MC/DC0%0/2
defined ${^GLOBAL_PHASE} and ${^GLOBAL_PHASE} eq "DESTRUCT"
defined ${^GLOBAL_PHASE}
${^GLOBAL_PHASE} eq "DESTRUCT"
Truth table33%1/3
A and B
A= defined ${^GLOBAL_PHASE}B= ${^GLOBAL_PHASE} eq "DESTRUCT"
| |||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||
| 36 | 123 | ▶ | return() unless (my $sql = $self->sqlite) && (my $dbh = $self->dbh); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
my $sql = $self->sqlite and my $dbh = $self->dbh
MC/DC0%0/2
my $sql = $self->sqlite and my $dbh = $self->dbh
my $sql = $self->sqlite
my $dbh = $self->dbh
Truth table33%1/3
A and B
A= my $sql = $self->sqliteB= my $dbh = $self->dbh
| |||||||||||||||||||||||||||||||||||||
| 37 | 123 | $sql->_enqueue($dbh); | |||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||
| 40 | 27 | ▶ | sub begin { | ||||||||||||||||||||||||||||||||||
|
Subroutine
begin: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 41 | 27 | my ($self, $behavior) = @_; | |||||||||||||||||||||||||||||||||||
| 42 | 27 | return Mojo::SQLite::Transaction->new(db => $self, behavior => $behavior); | |||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||
| 45 | 2 | ▶ | sub disconnect { | ||||||||||||||||||||||||||||||||||
|
Subroutine
disconnect: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 46 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 47 | 2 | $self->dbh->disconnect; | |||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||
| 50 | 1 | ▶ | sub ping { shift->dbh->ping } | ||||||||||||||||||||||||||||||||||
|
Subroutine
ping: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||
| 52 | 215 | ▶ | sub query { | ||||||||||||||||||||||||||||||||||
|
Subroutine
query: called CC=11 SCAR=24.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 53 | 215 | my ($self, $query) = (shift, shift); | |||||||||||||||||||||||||||||||||||
| 54 | 215 | ▶ | my $cb = ref $_[-1] eq 'CODE' ? pop : undef; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||
| 56 | 215 | my $dbh = $self->dbh; | |||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||
| 58 | 215 | my $prev_h = $dbh->{HandleError}; | |||||||||||||||||||||||||||||||||||
| 59 | # Better context for error messages | ||||||||||||||||||||||||||||||||||||
| 60 | 215 | ▶ | local $dbh->{HandleError} = sub { $_[0] = shortmess $_[0]; ($prev_h and $prev_h->(@_)) ? 1 : 0 }; | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.4
Branch: 1 branch
Condition66%2/3
$prev_h and &$prev_h(@_)
MC/DC0%0/2
$prev_h and &$prev_h(@_)
$prev_h
&$prev_h(@_)
Truth table66%2/3
A and B
A= $prev_hB= &$prev_h(@_)
| |||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||
| 62 | 215 | my ($sth, $errored, $error); | |||||||||||||||||||||||||||||||||||
| 63 | { | ||||||||||||||||||||||||||||||||||||
| 64 | 215 | local $@; | |||||||||||||||||||||||||||||||||||
| 65 | 215 | ▶ | unless (eval { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 66 | # If RaiseError has been disabled, we might not get a handle | ||||||||||||||||||||||||||||||||||||
| 67 | 215 | ▶ | if (defined($sth = $dbh->prepare_cached($query, undef, 3))) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 68 | 204 | _bind_params($sth, @_); | |||||||||||||||||||||||||||||||||||
| 69 | 204 | $sth->execute; | |||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||
| 71 | 209 | 1; | |||||||||||||||||||||||||||||||||||
| 72 | 6 | }) { $errored = 1; $error = $@ } | |||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||
| 75 | 215 | ▶ | die $error if $errored and !$cb; # bail out for errored "blocking" queries | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$errored and not $cb
MC/DC100%2/2
$errored and not $cb
$errored
not $cb
Truth table100%3/3
A and not(B)
A= $erroredB= not $cb
| |||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||
| 77 | # We won't have a statement handle if prepare failed in a "non-blocking" | ||||||||||||||||||||||||||||||||||||
| 78 | # query or with RaiseError disabled | ||||||||||||||||||||||||||||||||||||
| 79 | 210 | my $results; | |||||||||||||||||||||||||||||||||||
| 80 | 210 | ▶ | if (defined $sth) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 81 | 204 | $results = $self->results_class->new(db => $self, sth => $sth); | |||||||||||||||||||||||||||||||||||
| 82 | 204 | $results->{last_insert_id} = $dbh->{private_mojo_last_insert_id}; | |||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||
| 85 | 210 | ▶ | return $results unless $cb; # blocking | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||
| 87 | # Still blocking, but call the callback on the next tick | ||||||||||||||||||||||||||||||||||||
| 88 | 13 | ▶ | $error = $dbh->err ? $dbh->errstr : $errored ? ($error || 'Error running SQLite query') : undef; | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/2
$error or "Error running SQLite query"
MC/DC0%0/1
$error or "Error running SQLite query"
$error
Truth table0%0/2
A
A= $error
| |||||||||||||||||||||||||||||||||||||
| 89 | 13 | require Mojo::IOLoop; | |||||||||||||||||||||||||||||||||||
| 90 | 13 | ▶ | Mojo::IOLoop->next_tick(sub { $self->$cb($error, $results) }); | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 91 | 13 | return $self; | |||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||
| 94 | 11 | ▶ | sub query_p { | ||||||||||||||||||||||||||||||||||
|
Subroutine
query_p: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 95 | 11 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 96 | 11 | my $promise = Mojo::Promise->new; | |||||||||||||||||||||||||||||||||||
| 97 | 11 | ▶ | $self->query(@_ => sub { $_[1] ? $promise->reject($_[1]) : $promise->resolve($_[2]) }); | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 98 | 11 | return $promise; | |||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||
| 101 | 8 | ▶ | sub tables { | ||||||||||||||||||||||||||||||||||
|
Subroutine
tables: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 102 | 8 | my @tables = shift->dbh->tables(undef, undef, undef, 'TABLE,VIEW,LOCAL TEMPORARY'); | |||||||||||||||||||||||||||||||||||
| 103 | 8 | my %names; # Deduplicate returned temporary table indexes | |||||||||||||||||||||||||||||||||||
| 104 | 8 | return [grep { !$names{$_}++ } @tables]; | |||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||
| 107 | 204 | ▶ | sub _bind_params { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_bind_params: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||||||||||
| 108 | 204 | my $sth = shift; | |||||||||||||||||||||||||||||||||||
| 109 | 204 | ▶ | return $sth unless @_; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 110 | 127 | foreach my $i (0..$#_) { | |||||||||||||||||||||||||||||||||||
| 111 | 158 | my $param = $_[$i]; | |||||||||||||||||||||||||||||||||||
| 112 | 158 | ▶ | if (ref $param eq 'HASH') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 113 | 23 | ▶ | if (exists $param->{type} && exists $param->{value}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
exists $param->{"type"} and exists $param->{"value"}
MC/DC50%1/2
exists $param->{"type"} and exists $param->{"value"}
exists $param->{"type"}
exists $param->{"value"}
Truth table66%2/3
A and B
A= exists $param->{"type"}B= exists $param->{"value"}
| |||||||||||||||||||||||||||||||||||||
| 114 | 5 | $sth->bind_param($i+1, $param->{value}, $param->{type}); | |||||||||||||||||||||||||||||||||||
| 115 | } elsif (exists $param->{json}) { | ||||||||||||||||||||||||||||||||||||
| 116 | 17 | $sth->bind_param($i+1, to_json($param->{json}), SQL_VARCHAR); | |||||||||||||||||||||||||||||||||||
| 117 | } elsif (exists $param->{-json}) { | ||||||||||||||||||||||||||||||||||||
| 118 | 1 | $sth->bind_param($i+1, to_json($param->{-json}), SQL_VARCHAR); | |||||||||||||||||||||||||||||||||||
| 119 | } else { | ||||||||||||||||||||||||||||||||||||
| 120 | 0 | croak qq{Unknown parameter hashref (no "type"/"value", "json" or "-json")}; | |||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||
| 122 | } else { | ||||||||||||||||||||||||||||||||||||
| 123 | 135 | $sth->bind_param($i+1, $param); | |||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||
| 126 | 127 | return $sth; | |||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||
| 129 | 6 | 1; | |||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||
| 131 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||
| 133 | =head1 NAME | ||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||
| 135 | Mojo::SQLite::Database - Database | ||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||
| 137 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||
| 139 | use Mojo::SQLite::Database; | ||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||
| 141 | my $db = Mojo::SQLite::Database->new(sqlite => $sql, dbh => $dbh); | ||||||||||||||||||||||||||||||||||||
| 142 | $db->query('select * from foo') | ||||||||||||||||||||||||||||||||||||
| 143 | ->hashes->map(sub { $_->{bar} })->join("\n")->say; | ||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||
| 145 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||
| 147 | L<Mojo::SQLite::Database> is a container for L<DBD::SQLite> database handles | ||||||||||||||||||||||||||||||||||||
| 148 | used by L<Mojo::SQLite>. | ||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||
| 150 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||
| 152 | L<Mojo::SQLite::Database> implements the following attributes. | ||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||
| 154 | =head2 dbh | ||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||
| 156 | my $dbh = $db->dbh; | ||||||||||||||||||||||||||||||||||||
| 157 | $db = $db->dbh($dbh); | ||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||
| 159 | L<DBD::SQLite> database handle used for all queries. | ||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||
| 161 | # Use DBI utility methods | ||||||||||||||||||||||||||||||||||||
| 162 | my $quoted = $db->dbh->quote_identifier('foo.bar'); | ||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||
| 164 | =head2 results_class | ||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||
| 166 | my $class = $db->results_class; | ||||||||||||||||||||||||||||||||||||
| 167 | $db = $db->results_class('MyApp::Results'); | ||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||
| 169 | Class to be used by L</"query">, defaults to L<Mojo::SQLite::Results>. Note | ||||||||||||||||||||||||||||||||||||
| 170 | that this class needs to have already been loaded before L</"query"> is called. | ||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||
| 172 | =head2 sqlite | ||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||
| 174 | my $sql = $db->sqlite; | ||||||||||||||||||||||||||||||||||||
| 175 | $db = $db->sqlite(Mojo::SQLite->new); | ||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||
| 177 | L<Mojo::SQLite> object this database belongs to. | ||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||
| 179 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||
| 181 | L<Mojo::SQLite::Database> inherits all methods from L<Mojo::Base> and | ||||||||||||||||||||||||||||||||||||
| 182 | implements the following new ones. | ||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||
| 184 | =head2 begin | ||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||
| 186 | my $tx = $db->begin; | ||||||||||||||||||||||||||||||||||||
| 187 | my $tx = $db->begin('exclusive'); | ||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||
| 189 | Begin transaction and return L<Mojo::SQLite::Transaction> object, which will | ||||||||||||||||||||||||||||||||||||
| 190 | automatically roll back the transaction unless | ||||||||||||||||||||||||||||||||||||
| 191 | L<Mojo::SQLite::Transaction/"commit"> has been called before it is destroyed. | ||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||
| 193 | # Insert rows in a transaction | ||||||||||||||||||||||||||||||||||||
| 194 | eval { | ||||||||||||||||||||||||||||||||||||
| 195 | my $tx = $db->begin; | ||||||||||||||||||||||||||||||||||||
| 196 | $db->insert('frameworks', {name => 'Catalyst'}); | ||||||||||||||||||||||||||||||||||||
| 197 | $db->insert('frameworks', {name => 'Mojolicious'}); | ||||||||||||||||||||||||||||||||||||
| 198 | $tx->commit; | ||||||||||||||||||||||||||||||||||||
| 199 | }; | ||||||||||||||||||||||||||||||||||||
| 200 | say $@ if $@; | ||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||
| 202 | A transaction locking behavior of C<deferred>, C<immediate>, or C<exclusive> | ||||||||||||||||||||||||||||||||||||
| 203 | may optionally be passed; the default in L<DBD::SQLite> is currently | ||||||||||||||||||||||||||||||||||||
| 204 | C<immediate>. See L<DBD::SQLite/"Transaction and Database Locking"> and | ||||||||||||||||||||||||||||||||||||
| 205 | L<https://sqlite.org/lang_transaction.html> for more details. | ||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||
| 207 | =head2 delete | ||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||
| 209 | my $results = $db->delete($table, \%where); | ||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||
| 211 | Generate a C<DELETE> statement with L<Mojo::SQLite/"abstract"> (usually an | ||||||||||||||||||||||||||||||||||||
| 212 | L<SQL::Abstract::Pg> object) and execute it with L</"query">. You can also | ||||||||||||||||||||||||||||||||||||
| 213 | append a callback for API compatibility with L<Mojo::Pg>; the query is still | ||||||||||||||||||||||||||||||||||||
| 214 | executed in a blocking manner. | ||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||
| 216 | $db->delete(some_table => sub ($db, $err, $results) { | ||||||||||||||||||||||||||||||||||||
| 217 | ... | ||||||||||||||||||||||||||||||||||||
| 218 | }); | ||||||||||||||||||||||||||||||||||||
| 219 | Mojo::IOLoop->start unless Mojo::IOLoop->is_running; | ||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||
| 221 | Use all the same argument variations you would pass to the C<delete> method of | ||||||||||||||||||||||||||||||||||||
| 222 | L<SQL::Abstract>. | ||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||
| 224 | # "delete from some_table" | ||||||||||||||||||||||||||||||||||||
| 225 | $db->delete('some_table'); | ||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||
| 227 | # "delete from some_table where foo = 'bar'" | ||||||||||||||||||||||||||||||||||||
| 228 | $db->delete('some_table', {foo => 'bar'}); | ||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||
| 230 | # "delete from some_table where foo like '%test%'" | ||||||||||||||||||||||||||||||||||||
| 231 | $db->delete('some_table', {foo => {-like => '%test%'}}); | ||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||
| 233 | =head2 delete_p | ||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||
| 235 | my $promise = $db->delete_p($table, \%where, \%options); | ||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||
| 237 | Same as L</"delete"> but returns a L<Mojo::Promise> object instead of accepting | ||||||||||||||||||||||||||||||||||||
| 238 | a callback. For API compatibility with L<Mojo::Pg>; the query is still executed | ||||||||||||||||||||||||||||||||||||
| 239 | in a blocking manner. | ||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||
| 241 | $db->delete_p('some_table')->then(sub ($results) { | ||||||||||||||||||||||||||||||||||||
| 242 | ... | ||||||||||||||||||||||||||||||||||||
| 243 | })->catch(sub ($err) { | ||||||||||||||||||||||||||||||||||||
| 244 | ... | ||||||||||||||||||||||||||||||||||||
| 245 | })->wait; | ||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||
| 247 | =head2 disconnect | ||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||
| 249 | $db->disconnect; | ||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||
| 251 | Disconnect L</"dbh"> and prevent it from getting reused. | ||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||
| 253 | =head2 insert | ||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||
| 255 | my $results = $db->insert($table, \@values || \%fieldvals, \%options); | ||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||
| 257 | Generate an C<INSERT> statement with L<Mojo::SQLite/"abstract"> (usually an | ||||||||||||||||||||||||||||||||||||
| 258 | L<SQL::Abstract::Pg> object) and execute it with L</"query">. You can also | ||||||||||||||||||||||||||||||||||||
| 259 | append a callback for API compatibility with L<Mojo::Pg>; the query is still | ||||||||||||||||||||||||||||||||||||
| 260 | executed in a blocking manner. | ||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||
| 262 | $db->insert(some_table => {foo => 'bar'} => sub ($db, $err, $results) { | ||||||||||||||||||||||||||||||||||||
| 263 | ... | ||||||||||||||||||||||||||||||||||||
| 264 | }); | ||||||||||||||||||||||||||||||||||||
| 265 | Mojo::IOLoop->start unless Mojo::IOLoop->is_running; | ||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||
| 267 | Use all the same argument variations you would pass to the C<insert> method of | ||||||||||||||||||||||||||||||||||||
| 268 | L<SQL::Abstract>. | ||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||
| 270 | # "insert into some_table (foo, baz) values ('bar', 'yada')" | ||||||||||||||||||||||||||||||||||||
| 271 | $db->insert('some_table', {foo => 'bar', baz => 'yada'}); | ||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||
| 273 | =head2 insert_p | ||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||
| 275 | my $promise = $db->insert_p($table, \@values || \%fieldvals, \%options); | ||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||
| 277 | Same as L</"insert"> but returns a L<Mojo::Promise> object instead of accepting | ||||||||||||||||||||||||||||||||||||
| 278 | a callback. For API compatibility with L<Mojo::Pg>; the query is still executed | ||||||||||||||||||||||||||||||||||||
| 279 | in a blocking manner. | ||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||
| 281 | $db->insert_p(some_table => {foo => 'bar'})->then(sub ($results) { | ||||||||||||||||||||||||||||||||||||
| 282 | ... | ||||||||||||||||||||||||||||||||||||
| 283 | })->catch(sub ($err) { | ||||||||||||||||||||||||||||||||||||
| 284 | ... | ||||||||||||||||||||||||||||||||||||
| 285 | })->wait; | ||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||
| 287 | =head2 ping | ||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||
| 289 | my $bool = $db->ping; | ||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||
| 291 | Check database connection. | ||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||
| 293 | =head2 query | ||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||
| 295 | my $results = $db->query('select * from foo'); | ||||||||||||||||||||||||||||||||||||
| 296 | my $results = $db->query('insert into foo values (?, ?, ?)', @values); | ||||||||||||||||||||||||||||||||||||
| 297 | my $results = $db->query('select ? as img', {type => SQL_BLOB, value => slurp 'img.jpg'}); | ||||||||||||||||||||||||||||||||||||
| 298 | my $results = $db->query('select ? as foo', {json => {bar => 'baz'}}); | ||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||
| 300 | Execute a blocking L<SQL|http://www.postgresql.org/docs/current/static/sql.html> | ||||||||||||||||||||||||||||||||||||
| 301 | statement and return a results object based on L</"results_class"> (which is | ||||||||||||||||||||||||||||||||||||
| 302 | usually L<Mojo::SQLite::Results>) with the query results. The L<DBD::SQLite> | ||||||||||||||||||||||||||||||||||||
| 303 | statement handle will be automatically reused when it is not active anymore, to | ||||||||||||||||||||||||||||||||||||
| 304 | increase the performance of future queries. You can also append a callback for | ||||||||||||||||||||||||||||||||||||
| 305 | API compatibility with L<Mojo::Pg>; the query is still executed in a blocking | ||||||||||||||||||||||||||||||||||||
| 306 | manner. | ||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||
| 308 | $db->query('insert into foo values (?, ?, ?)' => @values => sub ($db, $err, $results) { | ||||||||||||||||||||||||||||||||||||
| 309 | ... | ||||||||||||||||||||||||||||||||||||
| 310 | }); | ||||||||||||||||||||||||||||||||||||
| 311 | Mojo::IOLoop->start unless Mojo::IOLoop->is_running; | ||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||
| 313 | Hash reference arguments containing C<type> and C<value> elements will use the | ||||||||||||||||||||||||||||||||||||
| 314 | specified bind type for the parameter, using types from L<DBI/"DBI Constants">; | ||||||||||||||||||||||||||||||||||||
| 315 | see L<DBD::SQLite/"Blobs"> and the subsequent section for more information. | ||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||
| 317 | Hash reference arguments containing a value named C<json> or C<-json> will be | ||||||||||||||||||||||||||||||||||||
| 318 | encoded to L<JSON text|http://sqlite.org/json1.html> with | ||||||||||||||||||||||||||||||||||||
| 319 | L<Mojo::JSON/"to_json">. To accomplish the reverse, you can use the method | ||||||||||||||||||||||||||||||||||||
| 320 | L<Mojo::SQLite::Results/"expand"> to decode JSON text fields to Perl values | ||||||||||||||||||||||||||||||||||||
| 321 | with L<Mojo::JSON/"from_json">. | ||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||
| 323 | # "I ♥ SQLite!" | ||||||||||||||||||||||||||||||||||||
| 324 | $db->query('select ? as foo', {json => {bar => 'I ♥ SQLite!'}}) | ||||||||||||||||||||||||||||||||||||
| 325 | ->expand(json => 'foo')->hash->{foo}{bar}; | ||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||
| 327 | =head2 query_p | ||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||
| 329 | my $promise = $db->query_p('SELECT * FROM foo'); | ||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||
| 331 | Same as L</"query"> but returns a L<Mojo::Promise> object instead of accepting | ||||||||||||||||||||||||||||||||||||
| 332 | a callback. For API compatibility with L<Mojo::Pg>; the query is still executed | ||||||||||||||||||||||||||||||||||||
| 333 | in a blocking manner. | ||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||
| 335 | $db->query_p('INSERT INTO foo VALUES (?, ?, ?)' => @values)->then(sub ($results) { | ||||||||||||||||||||||||||||||||||||
| 336 | ... | ||||||||||||||||||||||||||||||||||||
| 337 | })->catch(sub ($err) { | ||||||||||||||||||||||||||||||||||||
| 338 | ... | ||||||||||||||||||||||||||||||||||||
| 339 | })->wait; | ||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||
| 341 | =head2 select | ||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||
| 343 | my $results = $db->select($source, $fields, $where, $order); | ||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||
| 345 | Generate a C<SELECT> statement with L<Mojo::SQLite/"abstract"> (usually an | ||||||||||||||||||||||||||||||||||||
| 346 | L<SQL::Abstract::Pg> object) and execute it with L</"query">. You can also | ||||||||||||||||||||||||||||||||||||
| 347 | append a callback for API compatibility with L<Mojo::Pg>; the query is still | ||||||||||||||||||||||||||||||||||||
| 348 | executed in a blocking manner. | ||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||
| 350 | $db->select(some_table => ['foo'] => {bar => 'yada'} => sub ($db, $err, $results) { | ||||||||||||||||||||||||||||||||||||
| 351 | ... | ||||||||||||||||||||||||||||||||||||
| 352 | }); | ||||||||||||||||||||||||||||||||||||
| 353 | Mojo::IOLoop->start unless Mojo::IOLoop->is_running; | ||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||
| 355 | Use all the same argument variations you would pass to the C<select> method of | ||||||||||||||||||||||||||||||||||||
| 356 | L<SQL::Abstract>. | ||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||
| 358 | # "select * from some_table" | ||||||||||||||||||||||||||||||||||||
| 359 | $db->select('some_table'); | ||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||
| 361 | # "select id, foo from some_table" | ||||||||||||||||||||||||||||||||||||
| 362 | $db->select('some_table', ['id', 'foo']); | ||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||
| 364 | # "select * from some_table where foo = 'bar'" | ||||||||||||||||||||||||||||||||||||
| 365 | $db->select('some_table', undef, {foo => 'bar'}); | ||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||
| 367 | # "select * from some_table where foo = 'bar' order by id desc" | ||||||||||||||||||||||||||||||||||||
| 368 | $db->select('some_table', undef, {foo => 'bar'}, {-desc => 'id'}); | ||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||
| 370 | # "select * from some_table where foo like '%test%'" | ||||||||||||||||||||||||||||||||||||
| 371 | $db->select('some_table', undef, {foo => {-like => '%test%'}}); | ||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||
| 373 | =head2 select_p | ||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||
| 375 | my $promise = $db->select_p($source, $fields, $where, \%options); | ||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||
| 377 | Same as L</"select"> but returns a L<Mojo::Promise> object instead of accepting | ||||||||||||||||||||||||||||||||||||
| 378 | a callback. For API compatibility with L<Mojo::Pg>; the query is still executed | ||||||||||||||||||||||||||||||||||||
| 379 | in a blocking manner. | ||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||
| 381 | $db->select_p(some_table => ['foo'] => {bar => 'yada'})->then(sub ($results) { | ||||||||||||||||||||||||||||||||||||
| 382 | ... | ||||||||||||||||||||||||||||||||||||
| 383 | })->catch(sub ($err) { | ||||||||||||||||||||||||||||||||||||
| 384 | ... | ||||||||||||||||||||||||||||||||||||
| 385 | })->wait; | ||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||
| 387 | =head2 tables | ||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||
| 389 | my $tables = $db->tables; | ||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||
| 391 | Return table and view names for this database, that are visible to the current | ||||||||||||||||||||||||||||||||||||
| 392 | user and not internal, as an array reference. Names will be quoted and prefixed | ||||||||||||||||||||||||||||||||||||
| 393 | by a schema name of C<"main"> for standard tables, C<"temp"> for temporary | ||||||||||||||||||||||||||||||||||||
| 394 | tables, and the appropriate schema name for | ||||||||||||||||||||||||||||||||||||
| 395 | L<attached databases|http://sqlite.org/lang_attach.html>. | ||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||
| 397 | # Names of all tables | ||||||||||||||||||||||||||||||||||||
| 398 | say for @{$db->tables}; | ||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||
| 400 | =head2 update | ||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||
| 402 | my $results = $db->update($table, \%fieldvals, \%where); | ||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||
| 404 | Generate an C<UPDATE> statement with L<Mojo::SQLite/"abstract"> (usually an | ||||||||||||||||||||||||||||||||||||
| 405 | L<SQL::Abstract::Pg> object) and execute it with L</"query">. You can also | ||||||||||||||||||||||||||||||||||||
| 406 | append a callback for API compatibility with L<Mojo::Pg>; the query is still | ||||||||||||||||||||||||||||||||||||
| 407 | executed in a blocking manner. | ||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||
| 409 | $db->update(some_table => {foo => 'baz'} => {foo => 'bar'} => sub ($db, $err, $results) { | ||||||||||||||||||||||||||||||||||||
| 410 | ... | ||||||||||||||||||||||||||||||||||||
| 411 | }); | ||||||||||||||||||||||||||||||||||||
| 412 | Mojo::IOLoop->start unless Mojo::IOLoop->is_running; | ||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||
| 414 | Use all the same argument variations you would pass to the C<update> method of | ||||||||||||||||||||||||||||||||||||
| 415 | L<SQL::Abstract>. | ||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||
| 417 | # "update some_table set foo = 'bar' where id = 23" | ||||||||||||||||||||||||||||||||||||
| 418 | $db->update('some_table', {foo => 'bar'}, {id => 23}); | ||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||
| 420 | # "update some_table set foo = 'bar' where foo like '%test%'" | ||||||||||||||||||||||||||||||||||||
| 421 | $db->update('some_table', {foo => 'bar'}, {foo => {-like => '%test%'}}); | ||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||
| 423 | =head2 update_p | ||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||
| 425 | my $promise = $db->update_p($table, \%fieldvals, \%where, \%options); | ||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||
| 427 | Same as L</"update"> but returns a L<Mojo::Promise> object instead of accepting | ||||||||||||||||||||||||||||||||||||
| 428 | a callback. For API compatibility with L<Mojo::Pg>; the query is still executed | ||||||||||||||||||||||||||||||||||||
| 429 | in a blocking manner. | ||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||
| 431 | $db->update_p(some_table => {foo => 'baz'} => {foo => 'bar'})->then(sub ($results) { | ||||||||||||||||||||||||||||||||||||
| 432 | ... | ||||||||||||||||||||||||||||||||||||
| 433 | })->catch(sub ($err) { | ||||||||||||||||||||||||||||||||||||
| 434 | ... | ||||||||||||||||||||||||||||||||||||
| 435 | })->wait; | ||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||
| 437 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||
| 439 | Report any issues on the public bugtracker. | ||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||
| 441 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||
| 443 | Dan Book, C<dbook@cpan.org> | ||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||
| 445 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||
| 447 | Copyright 2015, Dan Book. | ||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||
| 449 | This library is free software; you may redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||
| 450 | the terms of the Artistic License version 2.0. | ||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||
| 452 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||
| 454 | L<Mojo::SQLite> | ||||||||||||||||||||||||||||||||||||