RDF/Trine/Store/DBI/mysql.pm

filter: Statementstmt 19.0% 20 / 105 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 54.5% 6 / 11 Podpod 100% 3 / 3 totaltotal 18.3% 29 / 158 CC 20 SCAR 38.9
CC
20
Cov
14%
init
90.0
add_statement
56.0
_add_node
42.0
CRAP 48.8
« RDF/Trine/Parser/Redland.pm ↑ Summary RDF/Trine/Parser/Turtle/Lexer.pm »
1 =head1 NAME
2  
3 RDF::Trine::Store::DBI::mysql - Mysql subclass of DBI store
4  
5 =head1 VERSION
6  
7 This document describes RDF::Trine::Store::DBI::mysql version 1.019
8  
9 =head1 SYNOPSIS
10  
11 use RDF::Trine::Store::DBI::mysql;
12  
13 =head1 DESCRIPTION
14  
15 =cut
16  
17 package RDF::Trine::Store::DBI::mysql;
18  
19 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 68 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 68 ▶use base qw(RDF::Trine::Store::DBI);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 68 ▶use Scalar::Util qw(blessed reftype refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 68 our $VERSION;
27 68 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
28 68 $VERSION = "1.019";
29 68 my $class = __PACKAGE__;
30 68 $RDF::Trine::Store::STORE_CLASSES{ $class } = $VERSION;
31 }
32  
33  
34 0 ▶sub _config_meta {
Subroutine _config_meta: not called CC=1 SCAR=6.9
35 return {
36 0 required_keys => [qw(dsn username password name)],
37 fields => {
38 name => { description => 'Model Name', type => 'string' },
39 dsn => { description => 'DSN', type => 'string', template => 'DBI:mysql:database=[%database%]' },
40 database => { description => 'Database Name', type => 'string' },
41 username => { description => 'Username', type => 'string' },
42 password => { description => 'Password', type => 'password' },
43 driver => { description => 'Driver', type => 'string', value => 'mysql' },
44 },
45 }
46 }
47  
48 =head1 METHODS
49  
50 Beyond the methods documented below, this class inherits methods from the
51 L<RDF::Trine::Store::DBI> class.
52  
53 =over 4
54  
55 =item C<< new_with_config ( \%config ) >>
56  
57 Returns a new RDF::Trine::Store object based on the supplied configuration hashref.
58  
59 =cut
60  
61 0 ▶sub new_with_config {
Subroutine new_with_config: not called CC=1 SCAR=6.9
Pod documented
62 0 my $proto = shift;
63 0 my $config = shift;
64 0 $config->{storetype} = 'DBI::mysql';
65 0 return $proto->SUPER::new_with_config( $config );
66 }
67  
68 =item C<< add_statement ( $statement [, $context] ) >>
69  
70 Adds the specified C<$statement> to the underlying model.
71  
72 =cut
73  
74 0 ▶sub add_statement {
Subroutine add_statement: not called CC=7 SCAR=40.3
Pod documented
75 0 my $self = shift;
76 0 my $stmt = shift;
77 0 my $context = shift;
78  
79 0 my $dbh = $self->dbh;
80 # Carp::confess unless (blessed($stmt));
81 0 my $stable = $self->statements_table;
82 0 ▶ unless (blessed($stmt) and $stmt->can('nodes')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($stmt) and $stmt->can("nodes")) 0 0 0
Condition0%0/3
builtin::blessed($stmt) and $stmt->can("nodes")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($stmt) and $stmt->can("nodes")
builtin::blessed($stmt) $stmt->can("nodes")
Truth table0%0/3
A and B
A= builtin::blessed($stmt)B= $stmt->can("nodes")
ABresult
0X0
100
111
83 0 Carp::confess "No statement passed to add_statement";
84 }
85 0 my @nodes = $stmt->nodes;
86 0 foreach my $n (@nodes) {
87 0 $self->_add_node( $n );
88 }
89
90 0 my @values = map { $self->_mysql_node_hash( $_ ) } @nodes;
91 0 ▶ if ($stmt->isa('RDF::Trine::Statement::Quad')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt->isa('RDF::Trine::Statement::Quad')) { } 0 0 0
92 0 ▶ if (blessed($context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($context)) 0 0 0
93 0 throw RDF::Trine::Error::MethodInvocationError -text => "add_statement cannot be called with both a quad and a context";
94 }
95 0 $context = $stmt->context;
96 } else {
97 0 my $cid = do {
98 0 ▶ if ($context) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context) { } 0 0 0
99 0 $self->_add_node( $context );
100 0 $self->_mysql_node_hash( $context );
101 } else {
102 0 0
103 }
104 };
105 0 push(@values, $cid);
106 }
107 0 my $sql = sprintf( "INSERT IGNORE INTO ${stable} (Subject, Predicate, Object, Context) VALUES (?,?,?,?)" );
108 0 my $sth = $dbh->prepare( $sql );
109 0 $sth->execute(@values);
110 }
111  
112 0 ▶sub _add_node {
Subroutine _add_node: not called CC=6 SCAR=37.4
113 0 my $self = shift;
114 0 my $node = shift;
115 0 my $hash = $self->_mysql_node_hash( $node );
116 0 my $dbh = $self->dbh;
117
118 0 my @cols;
119 my $table;
120 0 my %values;
121 # Carp::confess unless (blessed($node));
122 0 ▶ if ($node->is_blank) {
Branch: 3 branches
BranchTrueFalseTotal
if ($node->is_blank) { } 0 0 0
elsif ($node->is_resource) { } 0 0 0
elsif ($node->isa('RDF::Trine::Node::Literal')) { } 0 0 0
123 0 $table = "Bnodes";
124 0 @cols = qw(ID Name);
125 0 @values{ @cols } = ($hash, $node->blank_identifier);
126 } elsif ($node->is_resource) {
127 0 $table = "Resources";
128 0 @cols = qw(ID URI);
129 0 @values{ @cols } = ($hash, $node->uri_value);
130 } elsif ($node->isa('RDF::Trine::Node::Literal')) {
131 0 $table = "Literals";
132 0 @cols = qw(ID Value);
133 0 @values{ @cols } = ($hash, $node->literal_value);
134 0 $values{ 'Datatype' } = '';
135 0 $values{ 'Language' } = '';
136 0 ▶ if ($node->has_language) {
Branch: 2 branches
BranchTrueFalseTotal
if ($node->has_language) { } 0 0 0
elsif ($node->has_datatype) { } 0 0 0
137 0 push(@cols, 'Language');
138 0 $values{ 'Language' } = $node->literal_value_language;
139 } elsif ($node->has_datatype) {
140 0 push(@cols, 'Datatype');
141 0 $values{ 'Datatype' } = $node->literal_datatype;
142 }
143 }
144
145 0 my $sql = "INSERT IGNORE INTO ${table} (" . join(', ', @cols) . ") VALUES (" . join(',',('?')x scalar(@cols)) . ")";
146 0 my $sth = $dbh->prepare( $sql );
147 0 $sth->execute( map "$_", @values{ @cols } );
148 0 return $hash;
149 }
150  
151 =item C<< init >>
152  
153 Creates the necessary tables in the underlying database.
154  
155 =cut
156  
157 0 ▶sub init {
Subroutine init: not called CC=9 SCAR=45.0
Pod documented
158 0 my $self = shift;
159 0 my $dbh = $self->dbh;
160 0 my $name = $self->model_name;
161 0 my $id = RDF::Trine::Store::DBI::_mysql_hash( $name );
162 0 local($dbh->{AutoCommit}) = 0;
163 0 ▶ unless ($self->_table_exists("Literals")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_table_exists("Literals")) 0 0 0
164 0 ▶ $dbh->do( <<"END" ) || do { $dbh->rollback; return };
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do("\t\t\tCREATE TABLE IF NOT EXISTS Literals (\n\t\t\t\tID bigint unsigned PRIMARY KEY,\n\t\t\t\tValue longtext NOT NULL,\n\t\t\t\tLanguage text NOT NULL,\n\t\t\t\tDatatype text NOT NULL\n\t\t\t) CHARACTER SET utf8 COLLATE utf8_bin;\n") or do { $dbh->rollback; return } 0 0 0
165 CREATE TABLE IF NOT EXISTS Literals (
166 ID bigint unsigned PRIMARY KEY,
167 Value longtext NOT NULL,
168 Language text NOT NULL,
169 Datatype text NOT NULL
170 ) CHARACTER SET utf8 COLLATE utf8_bin;
171 END
172  
173 0 ▶ $dbh->do( <<"END" ) || do { $dbh->rollback; return };
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do("\t\t\tCREATE TABLE IF NOT EXISTS Resources (\n\t\t\t\tID bigint unsigned PRIMARY KEY,\n\t\t\t\tURI text NOT NULL\n\t\t\t);\n") or do { $dbh->rollback; return } 0 0 0
174 CREATE TABLE IF NOT EXISTS Resources (
175 ID bigint unsigned PRIMARY KEY,
176 URI text NOT NULL
177 );
178 END
179 0 ▶ $dbh->do( <<"END" ) || do { $dbh->rollback; return };
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do("\t\t\tCREATE TABLE IF NOT EXISTS Bnodes (\n\t\t\t\tID bigint unsigned PRIMARY KEY,\n\t\t\t\tName text NOT NULL\n\t\t\t);\n") or do { $dbh->rollback; return } 0 0 0
180 CREATE TABLE IF NOT EXISTS Bnodes (
181 ID bigint unsigned PRIMARY KEY,
182 Name text NOT NULL
183 );
184 END
185 0 ▶ $dbh->do( <<"END" ) || do { $dbh->rollback; return };
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do("\t\t\tCREATE TABLE IF NOT EXISTS Models (\n\t\t\t\tID bigint unsigned PRIMARY KEY,\n\t\t\t\tName text NOT NULL\n\t\t\t);\n") or do { $dbh->rollback; return } 0 0 0
186 CREATE TABLE IF NOT EXISTS Models (
187 ID bigint unsigned PRIMARY KEY,
188 Name text NOT NULL
189 );
190 END
191 0 ▶ $dbh->commit or warn $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$dbh->commit or warn $dbh->errstr 0 0 0
192 }
193  
194 0 ▶ unless ($self->_table_exists("Statements${id}")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_table_exists("Statements$id")) 0 0 0
195 0 ▶ $dbh->do( <<"END" ) || do { $dbh->rollback; return };
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do("\t\t\tCREATE TABLE IF NOT EXISTS Statements$id (\n\t\t\t\tSubject bigint unsigned NOT NULL,\n\t\t\t\tPredicate bigint unsigned NOT NULL,\n\t\t\t\tObject bigint unsigned NOT NULL,\n\t\t\t\tContext bigint unsigned NOT NULL DEFAULT 0,\n\t\t\t\tPRIMARY KEY (Subject, Predicate, Object, Context)\n\t\t\t);\n") or do { $dbh->rollback; return } 0 0 0
196 CREATE TABLE IF NOT EXISTS Statements${id} (
197 Subject bigint unsigned NOT NULL,
198 Predicate bigint unsigned NOT NULL,
199 Object bigint unsigned NOT NULL,
200 Context bigint unsigned NOT NULL DEFAULT 0,
201 PRIMARY KEY (Subject, Predicate, Object, Context)
202 );
203 END
204  
205 # $dbh->do( "DROP TABLE IF EXISTS Statements${id}" ) || do { $dbh->rollback; return };
206  
207  
208 # $dbh->do( "CREATE INDEX idx_${name}_spog ON Statements${id} (Subject,Predicate,Object,Context);", undef, $name ); # || do { $dbh->rollback; return };
209 0 $dbh->do( "CREATE INDEX `idx_${name}_pogs` ON Statements${id} (Predicate,Object,Context,Subject);", undef, $name ); # || do { $dbh->rollback; return };
210 0 $dbh->do( "CREATE INDEX `idx_${name}_opcs` ON Statements${id} (Object,Predicate,Context,Subject);", undef, $name ); # || do { $dbh->rollback; return };
211 0 $dbh->do( "CREATE INDEX `idx_${name}_cpos` ON Statements${id} (Context,Predicate,Object,Subject);", undef, $name ); # || do { $dbh->rollback; return };
212  
213 # $dbh->do( "DELETE FROM Models WHERE ID = ${id}") || do { $dbh->rollback; return };
214 0 $dbh->do( "INSERT INTO Models (ID, Name) VALUES (${id}, ?)", undef, $name );
215
216 0 $dbh->commit;
217 }
218 }
219  
220  
221 68 1; # Magic true value required at end of module
222 __END__
« RDF/Trine/Parser/Redland.pm ↑ Summary RDF/Trine/Parser/Turtle/Lexer.pm »