.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" 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 .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . 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 .\" ======================================================================== .\" .IX Title "DBD::SQLite::VirtualTable::PerlData 3pm" .TH DBD::SQLite::VirtualTable::PerlData 3pm 2024-03-07 "perl v5.38.2" "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 DBD::SQLite::VirtualTable::PerlData \-\- virtual table hooked to Perl data .SH SYNOPSIS .IX Header "SYNOPSIS" Within Perl : .PP .Vb 1 \& $dbh\->sqlite_create_module(perl => "DBD::SQLite::VirtualTable::PerlData"); .Ve .PP Then, within SQL : .PP .Vb 2 \& CREATE VIRTUAL TABLE atbl USING perl(foo, bar, etc, \& arrayrefs="some::global::var::aref") \& \& CREATE VIRTUAL TABLE htbl USING perl(foo, bar, etc, \& hashrefs="some::global::var::href") \& \& CREATE VIRTUAL TABLE ctbl USING perl(single_col \& colref="some::global::var::ref") \& \& \& SELECT foo, bar FROM atbl WHERE ...; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" A \f(CW\*(C`PerlData\*(C'\fR virtual table is a database view on some datastructure within a Perl program. The data can be read or modified both from SQL and from Perl. This is useful for simple import/export operations, for debugging purposes, for joining data from different sources, etc. .SH PARAMETERS .IX Header "PARAMETERS" Parameters for creating a \f(CW\*(C`PerlData\*(C'\fR virtual table are specified within the \f(CW\*(C`CREATE VIRTUAL TABLE\*(C'\fR statement, mixed with regular column declarations, but with an '=' sign. .PP The only authorized (and mandatory) parameter is the one that specifies the Perl datastructure to which the virtual table is bound. It must be given as the fully qualified name of a global variable; the parameter can be one of three different kinds : .ie n .IP """arrayrefs""" 4 .el .IP \f(CWarrayrefs\fR 4 .IX Item "arrayrefs" arrayref that contains an arrayref for each row. Each such row will have a size equivalent to the number of columns declared for the virtual table. .ie n .IP """hashrefs""" 4 .el .IP \f(CWhashrefs\fR 4 .IX Item "hashrefs" arrayref that contains a hashref for each row. Keys in each hashref should correspond to the columns declared for the virtual table. .ie n .IP """colref""" 4 .el .IP \f(CWcolref\fR 4 .IX Item "colref" arrayref that contains a single scalar for each row; obviously, this is a single-column virtual table. .SH USAGE .IX Header "USAGE" .SS "Common part of all examples : declaring the module" .IX Subsection "Common part of all examples : declaring the module" In all examples below, the common part is that the Perl program should connect to the database and then declare the \&\f(CW\*(C`PerlData\*(C'\fR virtual table module, like this .PP .Vb 4 \& # connect to the database \& my $dbh = DBI\->connect("dbi:SQLite:dbname=$dbfile", \*(Aq\*(Aq, \*(Aq\*(Aq, \& {RaiseError => 1, AutoCommit => 1}); \& # or any other options suitable to your needs \& \& # register the module \& $dbh\->sqlite_create_module(perl => "DBD::SQLite::VirtualTable::PerlData"); .Ve .PP Then create a global arrayref variable, using \f(CW\*(C`our\*(C'\fR instead of \f(CW\*(C`my\*(C'\fR, so that the variable is stored in the symbol table of the enclosing module. .PP .Vb 2 \& package Foo::Bar; # could as well be just "main" \& our $rows = [ ... ]; .Ve .PP Finally, create the virtual table and bind it to the global variable (here we assume that \f(CW@$rows\fR contains arrayrefs) : .PP .Vb 3 \& $dbh\->do(\*(AqCREATE VIRTUAL TABLE temp.vtab\*(Aq \& .\*(Aq USING perl(col1 INT, col2 TEXT, etc, \& arrayrefs="Foo::Bar::rows\*(Aq); .Ve .PP In most cases, the virtual table will be for temporary use, which is the reason why this example prepends \f(CW\*(C`temp.\*(C'\fR in front of the table name : this tells SQLite to cleanup that table when the database handle will be disconnected, without the need to emit an explicit DROP statement. .PP Column names (and optionally their types) are specified in the virtual table declaration, just like for any regular table. .SS "Arrayref example : statistics from files" .IX Subsection "Arrayref example : statistics from files" Let's suppose we want to perform some searches over a collection of files, where search constraints may be based on some of the fields returned by stat, such as the size of the file or its last modify time. Here is a way to do it with a virtual table : .PP .Vb 1 \& my @files = ... ; # list of files to inspect \& \& # apply the L function to each file \& our $file_stats = [ map { [ $_, stat $_ ] } @files]; \& \& # create a temporary virtual table \& $dbh\->do(<<""); \& CREATE VIRTUAL TABLE temp.file_stats\*(Aq \& USING perl(path, dev, ino, mode, nlink, uid, gid, rdev, size, \& atime, mtime, ctime, blksize, blocks, \& arrayrefs="main::file_stats"); \& \& # search files \& my $sth = $dbh\->prepare(<<""); \& SELECT * FROM file_stats \& WHERE mtime BETWEEN ? AND ? \& AND uid IN (...) .Ve .SS "Hashref example : unicode characters" .IX Subsection "Hashref example : unicode characters" Given any unicode character, the "charinfo" in Unicode::UCD function returns a hashref with various bits of information about that character. So this can be exploited in a virtual table : .PP .Vb 2 \& use Unicode::UCD \*(Aqcharinfo\*(Aq; \& our $chars = [map {charinfo($_)} 0x300..0x400]; # arbitrary subrange \& \& # create a temporary virtual table \& $dbh\->do(<<""); \& CREATE VIRTUAL TABLE charinfo USING perl( \& code, name, block, script, category, \& hashrefs="main::chars" \& ) \& \& # search characters \& my $sth = $dbh\->prepare(<<""); \& SELECT * FROM charinfo \& WHERE script=\*(AqGreek\*(Aq \& AND name LIKE \*(Aq%SIGMA%\*(Aq .Ve .SS "Colref example: SELECT WHERE ... IN ..." .IX Subsection "Colref example: SELECT WHERE ... IN ..." \&\fINote: The idea for the following example is borrowed from the \&\fR\f(CI\*(C`test_intarray.h\*(C'\fR\fI file in SQLite's source ().\fR .PP A \f(CW\*(C`colref\*(C'\fR virtual table is designed to facilitate using an array of values as the right-hand side of an IN operator. The usual syntax for IN is to prepare a statement like this: .PP .Vb 1 \& SELECT * FROM table WHERE x IN (?,?,?,...,?); .Ve .PP and then bind individual values to each of the ? slots; but this has the disadvantage that the number of values must be known in advance. Instead, we can store values in a Perl array, bind that array to a virtual table, and then write a statement like this .PP .Vb 1 \& SELECT * FROM table WHERE x IN perl_array; .Ve .PP Here is how such a program would look like : .PP .Vb 3 \& # connect to the database \& my $dbh = DBI\->connect("dbi:SQLite:dbname=$dbfile", \*(Aq\*(Aq, \*(Aq\*(Aq, \& {RaiseError => 1, AutoCommit => 1}); \& \& # Declare a global arrayref containing the values. Here we assume \& # they are taken from @ARGV, but any other datasource would do. \& # Note the use of "our" instead of "my". \& our $values = \e@ARGV; \& \& # register the module and declare the virtual table \& $dbh\->sqlite_create_module(perl => "DBD::SQLite::VirtualTable::PerlData"); \& $dbh\->do(\*(AqCREATE VIRTUAL TABLE temp.intarray\*(Aq \& .\*(Aq USING perl(i INT, colref="main::values\*(Aq); \& \& # now we can SELECT from another table, using the intarray as a constraint \& my $sql = "SELECT * FROM some_table WHERE some_col IN intarray"; \& my $result = $dbh\->selectall_arrayref($sql); .Ve .PP Beware that the virtual table is read-write, so the statement below would push 99 into \f(CW@ARGV\fR ! .PP .Vb 1 \& INSERT INTO intarray VALUES (99); .Ve .SH AUTHOR .IX Header "AUTHOR" Laurent Dami .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright Laurent Dami, 2014. .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.