DBD-SQLite
view release on metacpan or search on metacpan
lib/DBD/SQLite.pm view on Meta::CPAN
package DBD::SQLite;
use 5.006;
use strict;
use DBI 1.57 ();
use XSLoader ();
our $VERSION = '1.78';
# sqlite_version cache (set in the XS bootstrap)
our ($sqlite_version, $sqlite_version_number);
# not sure if we still need these...
our ($err, $errstr);
XSLoader::load('DBD::SQLite', $VERSION);
# New or old API?
use constant NEWAPI => ($DBI::VERSION >= 1.608);
# global registry of collation functions, initialized with 2 builtins
our %COLLATION;
tie %COLLATION, 'DBD::SQLite::_WriteOnceHash';
$COLLATION{perl} = sub { $_[0] cmp $_[1] };
$COLLATION{perllocale} = sub { use locale; $_[0] cmp $_[1] };
our $drh;
my $methods_are_installed = 0;
sub driver {
return $drh if $drh;
if (!$methods_are_installed && DBD::SQLite::NEWAPI ) {
DBI->setup_driver('DBD::SQLite');
DBD::SQLite::db->install_method('sqlite_last_insert_rowid');
DBD::SQLite::db->install_method('sqlite_busy_timeout');
DBD::SQLite::db->install_method('sqlite_create_function');
DBD::SQLite::db->install_method('sqlite_create_aggregate');
DBD::SQLite::db->install_method('sqlite_create_collation');
DBD::SQLite::db->install_method('sqlite_collation_needed');
DBD::SQLite::db->install_method('sqlite_progress_handler');
DBD::SQLite::db->install_method('sqlite_commit_hook');
DBD::SQLite::db->install_method('sqlite_rollback_hook');
DBD::SQLite::db->install_method('sqlite_update_hook');
DBD::SQLite::db->install_method('sqlite_set_authorizer');
DBD::SQLite::db->install_method('sqlite_backup_from_file');
DBD::SQLite::db->install_method('sqlite_backup_to_file');
DBD::SQLite::db->install_method('sqlite_backup_from_dbh');
DBD::SQLite::db->install_method('sqlite_backup_to_dbh');
DBD::SQLite::db->install_method('sqlite_enable_load_extension');
DBD::SQLite::db->install_method('sqlite_load_extension');
DBD::SQLite::db->install_method('sqlite_register_fts3_perl_tokenizer');
DBD::SQLite::db->install_method('sqlite_trace', { O => 0x0004 });
DBD::SQLite::db->install_method('sqlite_profile', { O => 0x0004 });
DBD::SQLite::db->install_method('sqlite_table_column_metadata', { O => 0x0004 });
DBD::SQLite::db->install_method('sqlite_db_filename', { O => 0x0004 });
DBD::SQLite::db->install_method('sqlite_db_status', { O => 0x0004 });
DBD::SQLite::st->install_method('sqlite_st_status', { O => 0x0004 });
DBD::SQLite::db->install_method('sqlite_create_module');
DBD::SQLite::db->install_method('sqlite_limit');
DBD::SQLite::db->install_method('sqlite_db_config');
DBD::SQLite::db->install_method('sqlite_get_autocommit');
DBD::SQLite::db->install_method('sqlite_txn_state');
DBD::SQLite::db->install_method('sqlite_error_offset');
$methods_are_installed++;
}
$drh = DBI::_new_drh( "$_[0]::dr", {
Name => 'SQLite',
Version => $VERSION,
Attribution => 'DBD::SQLite by Matt Sergeant et al',
} );
return $drh;
}
sub CLONE {
undef $drh;
}
package # hide from PAUSE
DBD::SQLite::dr;
sub connect {
my ($drh, $dbname, $user, $auth, $attr) = @_;
# Default PrintWarn to the value of $^W
# unless ( defined $attr->{PrintWarn} ) {
# $attr->{PrintWarn} = $^W ? 1 : 0;
# }
my $dbh = DBI::_new_dbh( $drh, {
Name => $dbname,
} );
my $real = $dbname;
if ( $dbname =~ /=/ ) {
foreach my $attrib ( split(/;/, $dbname) ) {
my ($key, $value) = split(/=/, $attrib, 2);
if ( $key =~ /^(?:db(?:name)?|database)$/ ) {
$real = $value;
} elsif ( $key eq 'uri' ) {
$real = $value;
$attr->{sqlite_open_flags} |= DBD::SQLite::OPEN_URI();
} else {
$attr->{$key} = $value;
}
lib/DBD/SQLite.pm view on Meta::CPAN
=item $rowid
is the unique 64-bit signed integer key of the affected row within
that table.
=back
=head2 $dbh->sqlite_set_authorizer( $code_ref )
This method registers an authorizer callback to be invoked whenever
SQL statements are being compiled by the L<DBI/prepare> method. The
authorizer callback should return C<DBD::SQLite::OK> to allow the
action, C<DBD::SQLite::IGNORE> to disallow the specific action but
allow the SQL statement to continue to be compiled, or
C<DBD::SQLite::DENY> to cause the entire SQL statement to be rejected
with an error. If the authorizer callback returns any other value,
then C<prepare> call that triggered the authorizer will fail with
an error message.
An authorizer is used when preparing SQL statements from an untrusted
source, to ensure that the SQL statements do not try to access data
they are not allowed to see, or that they do not try to execute
malicious statements that damage the database. For example, an
application may allow a user to enter arbitrary SQL queries for
evaluation by a database. But the application does not want the user
to be able to make arbitrary changes to the database. An authorizer
could then be put in place while the user-entered SQL is being
prepared that disallows everything except SELECT statements.
The callback will be called as
$code_ref->($action_code, $string1, $string2, $database, $trigger_or_view)
where
=over
=item $action_code
is an integer that specifies what action is being authorized
(see L</"Action Codes">).
=item $string1, $string2
are strings that depend on the action code
(see L</"Action Codes">).
=item $database
is the name of the database (C<main>, C<temp>, etc.) if applicable.
=item $trigger_or_view
is the name of the inner-most trigger or view that is responsible for
the access attempt, or C<undef> if this access attempt is directly from
top-level SQL code.
=back
=head2 $dbh->sqlite_backup_from_file( $filename )
This method accesses the SQLite Online Backup API, and will take a backup of
the named database file, copying it to, and overwriting, your current database
connection. This can be particularly handy if your current connection is to the
special :memory: database, and you wish to populate it from an existing DB.
=head2 $dbh->sqlite_backup_to_file( $filename )
This method accesses the SQLite Online Backup API, and will take a backup of
the currently connected database, and write it out to the named file.
=head2 $dbh->sqlite_backup_from_dbh( $another_dbh )
This method accesses the SQLite Online Backup API, and will take a backup of
the database for the passed handle, copying it to, and overwriting, your current database
connection. This can be particularly handy if your current connection is to the
special :memory: database, and you wish to populate it from an existing DB.
You can use this to backup from an in-memory database to another in-memory database.
=head2 $dbh->sqlite_backup_to_dbh( $another_dbh )
This method accesses the SQLite Online Backup API, and will take a backup of
the currently connected database, and write it out to the passed database handle.
=head2 $dbh->sqlite_enable_load_extension( $bool )
Calling this method with a true value enables loading (external)
SQLite3 extensions. After the call, you can load extensions like this:
$dbh->sqlite_enable_load_extension(1);
$sth = $dbh->prepare("select load_extension('libmemvfs.so')")
or die "Cannot prepare: " . $dbh->errstr();
=head2 $dbh->sqlite_load_extension( $file, $proc )
Loading an extension by a select statement (with the "load_extension" SQLite3 function like above) has some limitations. If the extension you want to use creates other functions that are not native to SQLite, use this method instead. $file (a path to...
$dbh->sqlite_enable_load_extension(1);
$dbh->sqlite_load_extension('libsqlitefunctions.so')
or die "Cannot load extension: " . $dbh->errstr();
If the extension uses SQLite mutex functions like C<sqlite3_mutex_enter>, then
the extension should be compiled with the same C<SQLITE_THREADSAFE> compile-time
setting as this module, see C<DBD::SQLite::compile_options()>.
=head2 $dbh->sqlite_trace( $code_ref )
This method registers a trace callback to be invoked whenever
SQL statements are being run.
The callback will be called as
$code_ref->($statement)
where
=over
=item $statement
is a UTF-8 rendering of the SQL statement text as the statement
first begins executing.
=back
Additional callbacks might occur as each triggered subprogram is
entered. The callbacks for triggers contain a UTF-8 SQL comment
that identifies the trigger.
See also L<DBI/TRACING> for better tracing options.
=head2 $dbh->sqlite_profile( $code_ref )
This method registers a profile callback to be invoked whenever
a SQL statement finishes.
The callback will be called as
$code_ref->($statement, $elapsed_time)
where
( run in 2.443 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )