| 1 | =head1 NAME | ||||||||||
| 2 | |||||||||||
| 3 | RDF::Trine::Store::DBI::SQLite - SQLite subclass of DBI store | ||||||||||
| 4 | |||||||||||
| 5 | =head1 VERSION | ||||||||||
| 6 | |||||||||||
| 7 | This document describes RDF::Trine::Store::DBI::SQLite version 1.019 | ||||||||||
| 8 | |||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||
| 10 | |||||||||||
| 11 | use RDF::Trine::Store::DBI::SQLite; | ||||||||||
| 12 | my $store = RDF::Trine::Store->new({ | ||||||||||
| 13 | storetype => 'DBI', | ||||||||||
| 14 | name => 'test', | ||||||||||
| 15 | dsn => "dbi:SQLite:dbname=test.db", | ||||||||||
| 16 | username => '', | ||||||||||
| 17 | password => '' | ||||||||||
| 18 | }); | ||||||||||
| 19 | |||||||||||
| 20 | |||||||||||
| 21 | =head1 CHANGES IN VERSION 1.014 | ||||||||||
| 22 | |||||||||||
| 23 | The schema used to encode RDF data in SQLite changed in RDF::Trine version | ||||||||||
| 24 | 1.014 to fix a bug that was causing data loss. This change is not backwards | ||||||||||
| 25 | compatible, and is not compatible with the shared schema used by the other | ||||||||||
| 26 | database backends supported by RDF::Trine (PostgreSQL and MySQL). | ||||||||||
| 27 | |||||||||||
| 28 | To exchange data between SQLite and other databases, the data will require | ||||||||||
| 29 | export to an RDF serialization and re-import to the new database. | ||||||||||
| 30 | |||||||||||
| 31 | =cut | ||||||||||
| 32 | |||||||||||
| 33 | package RDF::Trine::Store::DBI::SQLite; | ||||||||||
| 34 | |||||||||||
| 35 | 68 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 68 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 68 | ▶ | no warnings 'redefine'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 68 | ▶ | use base qw(RDF::Trine::Store::DBI); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | |||||||||||
| 40 | |||||||||||
| 41 | 68 | ▶ | use Scalar::Util qw(blessed reftype refaddr); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 68 | ▶ | use Encode; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 68 | ▶ | use Digest::MD5 ('md5'); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 68 | ▶ | use Math::BigInt; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | |||||||||||
| 46 | 68 | our $VERSION; | |||||||||
| 47 | 68 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 68 | $VERSION = "1.019"; | |||||||||
| 49 | 68 | my $class = __PACKAGE__; | |||||||||
| 50 | 68 | $RDF::Trine::Store::STORE_CLASSES{ $class } = $VERSION; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | |||||||||||
| 54 | 0 | ▶ | sub _config_meta { | ||||||||
|
Subroutine
_config_meta: not called CC=1 SCAR=6.9
| |||||||||||
| 55 | return { | ||||||||||
| 56 | 0 | required_keys => [qw(dsn username password name)], | |||||||||
| 57 | fields => { | ||||||||||
| 58 | name => { description => 'Model Name', type => 'string' }, | ||||||||||
| 59 | dsn => { description => 'DSN', type => 'string', template => 'DBI:SQLite:dbname=[%filename%]' }, | ||||||||||
| 60 | filename => { description => 'SQLite Database Filename', type => 'filename' }, | ||||||||||
| 61 | username => { description => 'Username', type => 'string', value => '' }, | ||||||||||
| 62 | password => { description => 'Password', type => 'password', value => '' }, | ||||||||||
| 63 | driver => { description => 'Driver', type => 'string', value => 'SQLite' }, | ||||||||||
| 64 | }, | ||||||||||
| 65 | } | ||||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | =head1 METHODS | ||||||||||
| 69 | |||||||||||
| 70 | Beyond the methods documented below, this class inherits methods from the | ||||||||||
| 71 | L<RDF::Trine::Store::DBI> class. | ||||||||||
| 72 | |||||||||||
| 73 | =over 4 | ||||||||||
| 74 | |||||||||||
| 75 | =cut | ||||||||||
| 76 | |||||||||||
| 77 | =item C<< new_with_config ( \%config ) >> | ||||||||||
| 78 | |||||||||||
| 79 | Returns a new RDF::Trine::Store object based on the supplied configuration hashref. | ||||||||||
| 80 | |||||||||||
| 81 | =cut | ||||||||||
| 82 | |||||||||||
| 83 | 0 | ▶ | sub new_with_config { | ||||||||
|
Subroutine
new_with_config: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 84 | 0 | my $proto = shift; | |||||||||
| 85 | 0 | my $config = shift; | |||||||||
| 86 | 0 | $config->{storetype} = 'DBI::SQLite'; | |||||||||
| 87 | 0 | my $exists = (-r $config->{filename}); | |||||||||
| 88 | 0 | my $self = $proto->SUPER::new_with_config( $config ); | |||||||||
| 89 | 0 | ▶ | unless ($exists) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 0 | $self->init(); | |||||||||
| 91 | } | ||||||||||
| 92 | 0 | return $self; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | # SQLite only supports 64-bit SIGNED integers, so this hash function masks out | ||||||||||
| 96 | # the high-bit on hash values (unlike the superclass which produces full 64-bit | ||||||||||
| 97 | # integers) | ||||||||||
| 98 | 3539 | ▶ | sub _mysql_hash { | ||||||||
|
Subroutine
_mysql_hash: called CC=4 SCAR=13.9
| |||||||||||
| 99 | 3539 | ▶ | if (ref($_[0])) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 3538 | my $self = shift; | |||||||||
| 101 | } | ||||||||||
| 102 | 3539 | ▶ | Carp::confess unless scalar(@_); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 3539 | my $data = encode('utf8', shift); | |||||||||
| 104 | 3539 | my @data = unpack('C*', md5( $data )); | |||||||||
| 105 | 3539 | my $sum = Math::BigInt->new('0'); | |||||||||
| 106 | # CHANGE: 7 -> 6, Smaller numbers for Sqlite which does not support real 64-bit :( | ||||||||||
| 107 | 3539 | foreach my $count (0 .. 7) { | |||||||||
| 108 | 28312 | my $data = Math::BigInt->new( $data[ $count ] ); #shift(@data); | |||||||||
| 109 | 28312 | my $part = $data << (8 * $count); | |||||||||
| 110 | # warn "+ $part\n"; | ||||||||||
| 111 | 28312 | $sum += $part; | |||||||||
| 112 | } | ||||||||||
| 113 | # warn "= $sum\n"; | ||||||||||
| 114 | 3539 | $sum = $sum->band(Math::BigInt->new('0x7fff_ffff_ffff_ffff')); | |||||||||
| 115 | 3539 | $sum =~ s/\D//; # get rid of the extraneous '+' that pops up under perl 5.6 | |||||||||
| 116 | 3539 | return $sum; | |||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | =item C<< init >> | ||||||||||
| 120 | |||||||||||
| 121 | Creates the necessary tables in the underlying database. | ||||||||||
| 122 | |||||||||||
| 123 | =cut | ||||||||||
| 124 | |||||||||||
| 125 | 17 | ▶ | sub init { | ||||||||
|
Subroutine
init: called CC=6 SCAR=29.4
Pod
documented
| |||||||||||
| 126 | 17 | my $self = shift; | |||||||||
| 127 | 17 | my $dbh = $self->dbh; | |||||||||
| 128 | 17 | my $name = $self->model_name; | |||||||||
| 129 | 17 | $self->SUPER::init(); | |||||||||
| 130 | 17 | my $id = $self->_mysql_hash( $name ); | |||||||||
| 131 | |||||||||||
| 132 | 17 | my $table = "Statements${id}"; | |||||||||
| 133 | 17 | local($dbh->{AutoCommit}) = 0; | |||||||||
| 134 | 17 | ▶ | unless ($self->_table_exists($table)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 0 | ▶ | $dbh->do( "CREATE INDEX idx_${name}_spog ON Statements${id} (Subject,Predicate,Object,Context);" ) || do { $dbh->rollback; return }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 0 | ▶ | $dbh->do( "CREATE INDEX idx_${name}_pogs ON Statements${id} (Predicate,Object,Context,Subject);" ) || do { $dbh->rollback; return }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | 0 | ▶ | $dbh->do( "CREATE INDEX idx_${name}_opcs ON Statements${id} (Object,Predicate,Context,Subject);" ) || do { $dbh->rollback; return }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | 0 | ▶ | $dbh->do( "CREATE INDEX idx_${name}_cpos ON Statements${id} (Context,Predicate,Object,Subject);" ) || do { $dbh->rollback; return }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 139 | 0 | $dbh->commit; | |||||||||
| 140 | } | ||||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | |||||||||||
| 144 | 68 | 1; # Magic true value required at end of module | |||||||||
| 145 | __END__ | ||||||||||