.\" Automatically generated by Pod::Man 4.10 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Mojo::SQLite::Database 3pm" .TH Mojo::SQLite::Database 3pm "2019-02-03" "perl v5.28.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Mojo::SQLite::Database \- Database .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Mojo::SQLite::Database; \& \& my $db = Mojo::SQLite::Database\->new(sqlite => $sql, dbh => $dbh); \& $db\->query(\*(Aqselect * from foo\*(Aq) \& \->hashes\->map(sub { $_\->{bar} })\->join("\en")\->say; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Mojo::SQLite::Database is a container for DBD::SQLite database handles used by Mojo::SQLite. .SH "ATTRIBUTES" .IX Header "ATTRIBUTES" Mojo::SQLite::Database implements the following attributes. .SS "dbh" .IX Subsection "dbh" .Vb 2 \& my $dbh = $db\->dbh; \& $db = $db\->dbh($dbh); .Ve .PP DBD::SQLite database handle used for all queries. .PP .Vb 2 \& # Use DBI utility methods \& my $quoted = $db\->dbh\->quote_identifier(\*(Aqfoo.bar\*(Aq); .Ve .SS "results_class" .IX Subsection "results_class" .Vb 2 \& my $class = $db\->results_class; \& $db = $db\->results_class(\*(AqMyApp::Results\*(Aq); .Ve .PP Class to be used by \*(L"query\*(R", defaults to Mojo::SQLite::Results. Note that this class needs to have already been loaded before \*(L"query\*(R" is called. .SS "sqlite" .IX Subsection "sqlite" .Vb 2 \& my $sql = $db\->sqlite; \& $db = $db\->sqlite(Mojo::SQLite\->new); .Ve .PP Mojo::SQLite object this database belongs to. .SH "METHODS" .IX Header "METHODS" Mojo::SQLite::Database inherits all methods from Mojo::Base and implements the following new ones. .SS "begin" .IX Subsection "begin" .Vb 2 \& my $tx = $db\->begin; \& my $tx = $db\->begin(\*(Aqexclusive\*(Aq); .Ve .PP Begin transaction and return Mojo::SQLite::Transaction object, which will automatically roll back the transaction unless \&\*(L"commit\*(R" in Mojo::SQLite::Transaction has been called before it is destroyed. .PP .Vb 8 \& # Insert rows in a transaction \& eval { \& my $tx = $db\->begin; \& $db\->insert(\*(Aqframeworks\*(Aq, {name => \*(AqCatalyst\*(Aq}); \& $db\->insert(\*(Aqframeworks\*(Aq, {name => \*(AqMojolicious\*(Aq}); \& $tx\->commit; \& }; \& say $@ if $@; .Ve .PP A transaction locking behavior of \f(CW\*(C`deferred\*(C'\fR, \f(CW\*(C`immediate\*(C'\fR, or \f(CW\*(C`exclusive\*(C'\fR may optionally be passed; the default in DBD::SQLite is currently \&\f(CW\*(C`immediate\*(C'\fR. See \*(L"Transaction and Database Locking\*(R" in DBD::SQLite for more details. .SS "delete" .IX Subsection "delete" .Vb 1 \& my $results = $db\->delete($table, \e%where); .Ve .PP Generate a \f(CW\*(C`DELETE\*(C'\fR statement with \*(L"abstract\*(R" in Mojo::SQLite (usually an SQL::Abstract object) and execute it with \*(L"query\*(R". You can also append a callback for \s-1API\s0 compatibility with Mojo::Pg; the query is still executed in a blocking manner. .PP .Vb 5 \& $db\->delete(some_table => sub { \& my ($db, $err, $results) = @_; \& ... \& }); \& Mojo::IOLoop\->start unless Mojo::IOLoop\->is_running; .Ve .PP Use all the same argument variations you would pass to the \f(CW\*(C`delete\*(C'\fR method of SQL::Abstract. .PP .Vb 2 \& # "delete from some_table" \& $db\->delete(\*(Aqsome_table\*(Aq); \& \& # "delete from some_table where foo = \*(Aqbar\*(Aq" \& $db\->delete(\*(Aqsome_table\*(Aq, {foo => \*(Aqbar\*(Aq}); \& \& # "delete from some_table where foo like \*(Aq%test%\*(Aq" \& $db\->delete(\*(Aqsome_table\*(Aq, {foo => {\-like => \*(Aq%test%\*(Aq}}); .Ve .SS "disconnect" .IX Subsection "disconnect" .Vb 1 \& $db\->disconnect; .Ve .PP Disconnect \*(L"dbh\*(R" and prevent it from getting reused. .SS "insert" .IX Subsection "insert" .Vb 1 \& my $results = $db\->insert($table, \e@values || \e%fieldvals, \e%options); .Ve .PP Generate an \f(CW\*(C`INSERT\*(C'\fR statement with \*(L"abstract\*(R" in Mojo::SQLite (usually an SQL::Abstract object) and execute it with \*(L"query\*(R". You can also append a callback for \s-1API\s0 compatibility with Mojo::Pg; the query is still executed in a blocking manner. .PP .Vb 5 \& $db\->insert(some_table => {foo => \*(Aqbar\*(Aq} => sub { \& my ($db, $err, $results) = @_; \& ... \& }); \& Mojo::IOLoop\->start unless Mojo::IOLoop\->is_running; .Ve .PP Use all the same argument variations you would pass to the \f(CW\*(C`insert\*(C'\fR method of SQL::Abstract. .PP .Vb 2 \& # "insert into some_table (foo, baz) values (\*(Aqbar\*(Aq, \*(Aqyada\*(Aq)" \& $db\->insert(\*(Aqsome_table\*(Aq, {foo => \*(Aqbar\*(Aq, baz => \*(Aqyada\*(Aq}); .Ve .SS "ping" .IX Subsection "ping" .Vb 1 \& my $bool = $db\->ping; .Ve .PP Check database connection. .SS "query" .IX Subsection "query" .Vb 4 \& my $results = $db\->query(\*(Aqselect * from foo\*(Aq); \& my $results = $db\->query(\*(Aqinsert into foo values (?, ?, ?)\*(Aq, @values); \& my $results = $db\->query(\*(Aqselect ? as img\*(Aq, {type => SQL_BLOB, value => slurp \*(Aqimg.jpg\*(Aq}); \& my $results = $db\->query(\*(Aqselect ? as foo\*(Aq, {json => {bar => \*(Aqbaz\*(Aq}}); .Ve .PP Execute a blocking \s-1SQL\s0 statement and return a results object based on \*(L"results_class\*(R" (which is usually Mojo::SQLite::Results) with the query results. The DBD::SQLite statement handle will be automatically reused when it is not active anymore, to increase the performance of future queries. You can also append a callback for \&\s-1API\s0 compatibility with Mojo::Pg; the query is still executed in a blocking manner. .PP .Vb 5 \& $db\->query(\*(Aqinsert into foo values (?, ?, ?)\*(Aq => @values => sub { \& my ($db, $err, $results) = @_; \& ... \& }); \& Mojo::IOLoop\->start unless Mojo::IOLoop\->is_running; .Ve .PP Hash reference arguments containing \f(CW\*(C`type\*(C'\fR and \f(CW\*(C`value\*(C'\fR elements will use the specified bind type for the parameter, using types from \*(L"\s-1DBI\s0 Constants\*(R" in \s-1DBI\s0; see \*(L"Blobs\*(R" in DBD::SQLite and the subsequent section for more information. .PP Hash reference arguments containing a value named \f(CW\*(C`json\*(C'\fR will be encoded to \&\s-1JSON\s0 text with \*(L"to_json\*(R" in Mojo::JSON. To accomplish the reverse, you can use the method \*(L"expand\*(R" in Mojo::SQLite::Results to decode \s-1JSON\s0 text fields to Perl values with \*(L"from_json\*(R" in Mojo::JSON. .PP .Vb 3 \& # "I X SQLite!" \& $db\->query(\*(Aqselect ? as foo\*(Aq, {json => {bar => \*(AqI X SQLite!\*(Aq}}) \& \->expand(json => \*(Aqfoo\*(Aq)\->hash\->{foo}{bar}; .Ve .SS "select" .IX Subsection "select" .Vb 1 \& my $results = $db\->select($source, $fields, $where, $order); .Ve .PP Generate a \f(CW\*(C`SELECT\*(C'\fR statement with \*(L"abstract\*(R" in Mojo::SQLite (usually an SQL::Abstract object) and execute it with \*(L"query\*(R". You can also append a callback for \s-1API\s0 compatibility with Mojo::Pg; the query is still executed in a blocking manner. .PP .Vb 5 \& $db\->select(some_table => [\*(Aqfoo\*(Aq] => {bar => \*(Aqyada\*(Aq} => sub { \& my ($db, $err, $results) = @_; \& ... \& }); \& Mojo::IOLoop\->start unless Mojo::IOLoop\->is_running; .Ve .PP Use all the same argument variations you would pass to the \f(CW\*(C`select\*(C'\fR method of SQL::Abstract. .PP .Vb 2 \& # "select * from some_table" \& $db\->select(\*(Aqsome_table\*(Aq); \& \& # "select id, foo from some_table" \& $db\->select(\*(Aqsome_table\*(Aq, [\*(Aqid\*(Aq, \*(Aqfoo\*(Aq]); \& \& # "select * from some_table where foo = \*(Aqbar\*(Aq" \& $db\->select(\*(Aqsome_table\*(Aq, undef, {foo => \*(Aqbar\*(Aq}); \& \& # "select * from some_table where foo = \*(Aqbar\*(Aq order by id desc" \& $db\->select(\*(Aqsome_table\*(Aq, undef, {foo => \*(Aqbar\*(Aq}, {\-desc => \*(Aqid\*(Aq}); \& \& # "select * from some_table where foo like \*(Aq%test%\*(Aq" \& $db\->select(\*(Aqsome_table\*(Aq, undef, {foo => {\-like => \*(Aq%test%\*(Aq}}); .Ve .SS "tables" .IX Subsection "tables" .Vb 1 \& my $tables = $db\->tables; .Ve .PP Return table and view names for this database, that are visible to the current user and not internal, as an array reference. Names will be quoted and prefixed by a schema name of \f(CW"main"\fR for standard tables, \f(CW"temp"\fR for temporary tables, and the appropriate schema name for attached databases . .PP .Vb 2 \& # Names of all tables \& say for @{$db\->tables}; .Ve .SS "update" .IX Subsection "update" .Vb 1 \& my $results = $db\->update($table, \e%fieldvals, \e%where); .Ve .PP Generate an \f(CW\*(C`UPDATE\*(C'\fR statement with \*(L"abstract\*(R" in Mojo::SQLite (usually an SQL::Abstract object) and execute it with \*(L"query\*(R". You can also append a callback for \s-1API\s0 compatibility with Mojo::Pg; the query is still executed in a blocking manner. .PP .Vb 5 \& $db\->update(some_table => {foo => \*(Aqbaz\*(Aq} => {foo => \*(Aqbar\*(Aq} => sub { \& my ($db, $err, $results) = @_; \& ... \& }); \& Mojo::IOLoop\->start unless Mojo::IOLoop\->is_running; .Ve .PP Use all the same argument variations you would pass to the \f(CW\*(C`update\*(C'\fR method of SQL::Abstract. .PP .Vb 2 \& # "update some_table set foo = \*(Aqbar\*(Aq where id = 23" \& $db\->update(\*(Aqsome_table\*(Aq, {foo => \*(Aqbar\*(Aq}, {id => 23}); \& \& # "update some_table set foo = \*(Aqbar\*(Aq where foo like \*(Aq%test%\*(Aq" \& $db\->update(\*(Aqsome_table\*(Aq, {foo => \*(Aqbar\*(Aq}, {foo => {\-like => \*(Aq%test%\*(Aq}}); .Ve .SH "BUGS" .IX Header "BUGS" Report any issues on the public bugtracker. .SH "AUTHOR" .IX Header "AUTHOR" Dan Book, \f(CW\*(C`dbook@cpan.org\*(C'\fR .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright 2015, Dan Book. .PP This library is free software; you may redistribute it and/or modify it under the terms of the Artistic License version 2.0. .SH "SEE ALSO" .IX Header "SEE ALSO" Mojo::SQLite