1
0
Fork 0
mirror of https://github.com/DBD-SQLite/DBD-SQLite synced 2025-06-08 14:48:32 -04:00

implemented ReadOnly attribute

This commit is contained in:
Kenichi Ishigaki 2016-01-10 09:16:38 +09:00
parent 376a2243ec
commit 8845364fba
2 changed files with 60 additions and 1 deletions

View file

@ -414,9 +414,18 @@ sqlite_db_login6(SV *dbh, imp_dbh_t *imp_dbh, char *dbname, char *user, char *pa
val = hv_fetch(hv, "sqlite_extended_result_codes", 28, 0);
extended = (val && SvOK(*val)) ? !(!SvTRUE(*val)) : 0;
}
if (hv_exists(hv, "ReadOnly", 8)) {
val = hv_fetch(hv, "ReadOnly", 8, 0);
if ((val && SvOK(*val)) ? SvIV(*val) : 0) {
flag |= SQLITE_OPEN_READONLY;
}
}
if (hv_exists(hv, "sqlite_open_flags", 17)) {
val = hv_fetch(hv, "sqlite_open_flags", 17, 0);
flag = (val && SvOK(*val)) ? SvIV(*val) : 0;
flag |= (val && SvOK(*val)) ? SvIV(*val) : 0;
if (flag & SQLITE_OPEN_READONLY) {
hv_stores(hv, "ReadOnly", newSViv(1));
}
}
}
rc = sqlite_open2(dbname, &(imp_dbh->db), flag, extended);
@ -694,6 +703,12 @@ sqlite_db_STORE_attrib(SV *dbh, imp_dbh_t *imp_dbh, SV *keysv, SV *valuesv)
DBIc_set(imp_dbh, DBIcf_AutoCommit, SvTRUE(valuesv));
return TRUE;
}
if (strEQ(key, "ReadOnly")) {
if (SvTRUE(valuesv) && !sqlite3_db_readonly(imp_dbh->db, "main")) {
sqlite_error(dbh, 0, "ReadOnly is set but it's only advisory");
}
return FALSE;
}
if (strEQ(key, "sqlite_allow_multiple_statements")) {
imp_dbh->allow_multiple_statements = !(! SvTRUE(valuesv));
return TRUE;

44
t/60_readonly.t Normal file
View file

@ -0,0 +1,44 @@
#!/usr/bin/perl
use strict;
BEGIN {
$| = 1;
$^W = 1;
}
use t::lib::Test qw/connect_ok/;
use Test::More;
use DBD::SQLite::Constants qw/SQLITE_OPEN_READONLY/;
use Test::NoWarnings;
plan tests => 14;
{
my $dbh = connect_ok(
sqlite_open_flags => SQLITE_OPEN_READONLY,
RaiseError => 0,
PrintError => 0,
);
ok $dbh->{ReadOnly};
ok !$dbh->do('CREATE TABLE foo (id)');
like $dbh->errstr => qr/attempt to write a readonly database/;
}
{
my $dbh = connect_ok(ReadOnly => 1, PrintError => 0, RaiseError => 0);
ok $dbh->{ReadOnly};
ok !$dbh->do('CREATE TABLE foo (id)');
like $dbh->errstr => qr/attempt to write a readonly database/;
}
{
my $dbh = connect_ok(PrintWarn => 0, PrintError => 0, RaiseError => 0);
$dbh->{ReadOnly} = 1;
ok !$dbh->err;
like $dbh->errstr => qr/ReadOnly is set but/;
ok $dbh->{ReadOnly};
# this is ok because $dbh is not actually readonly (though we
# told so)
ok $dbh->do('CREATE TABLE foo (id)');
}