blib/lib/DBD/SQLcipher/VirtualTable.pm untested

filter: Statementstmt 0.0% 0 / 96 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 37 Podpod 0.0% 0 / 32 totaltotal 0.0% 0 / 171 CC 4 SCAR 10.8
CC
4
Cov
0%
_SQLITE_UPDATE
12.0
_PREPARE_SELF
6.0
CREATE_MODULE
2.0
CRAP 3.0
1 #======================================================================
2 package DBD::SQLcipher::VirtualTable;
3 #======================================================================
4 use strict;
5 use warnings;
6 use Scalar::Util qw/weaken/;
7  
8 our $VERSION = "0.03_0019";
9 our @ISA;
10  
11  
12 #----------------------------------------------------------------------
13 # methods for registering/destroying the module
14 #----------------------------------------------------------------------
15  
16 sub CREATE_MODULE { my ($class, $mod_name) = @_; }
17 sub DESTROY_MODULE { my ($class, $mod_name) = @_; }
18  
19 #----------------------------------------------------------------------
20 # methods for creating/destroying instances
21 #----------------------------------------------------------------------
22  
23 sub CREATE { my $class = shift; return $class->NEW(@_); }
24 sub CONNECT { my $class = shift; return $class->NEW(@_); }
25  
26 sub _PREPARE_SELF {
27 my ($class, $dbh_ref, $module_name, $db_name, $vtab_name, @args) = @_;
28  
29 my @columns;
30 my %options;
31  
32 # args containing '=' are options; others are column declarations
33 foreach my $arg (@args) {
34 if ($arg =~ /^([^=\s]+)\s*=\s*(.*)/) {
35 my ($key, $val) = ($1, $2);
36 $val =~ s/^"(.*)"$/$1/;
37 $options{$key} = $val;
38 }
39 else {
40 push @columns, $arg;
41 }
42 }
43  
44 # build $self
45 my $self = {
46 dbh_ref => $dbh_ref,
47 module_name => $module_name,
48 db_name => $db_name,
49 vtab_name => $vtab_name,
50 columns => \@columns,
51 options => \%options,
52 };
53 weaken $self->{dbh_ref};
54  
55 return $self;
56 }
57  
58 sub NEW {
59 my $class = shift;
60  
61 my $self = $class->_PREPARE_SELF(@_);
62 bless $self, $class;
63 }
64  
65  
66 sub VTAB_TO_DECLARE {
67 my $self = shift;
68  
69 local $" = ", ";
70 my $sql = "CREATE TABLE $self->{vtab_name}(@{$self->{columns}})";
71  
72 return $sql;
73 }
74  
75 sub DROP { my $self = shift; }
76 sub DISCONNECT { my $self = shift; }
77  
78  
79 #----------------------------------------------------------------------
80 # methods for initiating a search
81 #----------------------------------------------------------------------
82  
83 sub BEST_INDEX {
84 my ($self, $constraints, $order_by) = @_;
85  
86 my $ix = 0;
87 foreach my $constraint (grep {$_->{usable}} @$constraints) {
88 $constraint->{argvIndex} = $ix++;
89 $constraint->{omit} = 0;
90 }
91  
92 # stupid default values -- subclasses should put real values instead
93 my $outputs = {
94 idxNum => 1,
95 idxStr => "",
96 orderByConsumed => 0,
97 estimatedCost => 1.0,
98 estimatedRows => undef,
99 };
100  
101 return $outputs;
102 }
103  
104  
105 sub OPEN {
106 my $self = shift;
107 my $class = ref $self;
108  
109 my $cursor_class = $class . "::Cursor";
110 return $cursor_class->NEW($self, @_);
111 }
112  
113  
114 #----------------------------------------------------------------------
115 # methods for insert/delete/update
116 #----------------------------------------------------------------------
117  
118 sub _SQLITE_UPDATE {
119 my ($self, $old_rowid, $new_rowid, @values) = @_;
120  
121 if (! defined $old_rowid) {
122 return $self->INSERT($new_rowid, @values);
123 }
124 elsif (!@values) {
125 return $self->DELETE($old_rowid);
126 }
127 else {
128 return $self->UPDATE($old_rowid, $new_rowid, @values);
129 }
130 }
131  
132 sub INSERT {
133 my ($self, $new_rowid, @values) = @_;
134  
135 die "INSERT() should be redefined in subclass";
136 }
137  
138 sub DELETE {
139 my ($self, $old_rowid) = @_;
140  
141 die "DELETE() should be redefined in subclass";
142 }
143  
144 sub UPDATE {
145 my ($self, $old_rowid, $new_rowid, @values) = @_;
146  
147 die "UPDATE() should be redefined in subclass";
148 }
149  
150 #----------------------------------------------------------------------
151 # remaining methods of the sqlite API
152 #----------------------------------------------------------------------
153  
154 sub BEGIN_TRANSACTION {return 0}
155 sub SYNC_TRANSACTION {return 0}
156 sub COMMIT_TRANSACTION {return 0}
157 sub ROLLBACK_TRANSACTION {return 0}
158 sub SAVEPOINT {return 0}
159 sub RELEASE {return 0}
160 sub ROLLBACK_TO {return 0}
161 sub FIND_FUNCTION {return 0}
162 sub RENAME {return 0}
163  
164  
165 #----------------------------------------------------------------------
166 # utility methods
167 #----------------------------------------------------------------------
168  
169 sub dbh {
170 my $self = shift;
171 return ${$self->{dbh_ref}};
172 }
173  
174  
175 sub sqlite_table_info {
176 my $self = shift;
177  
178 my $sql = "PRAGMA table_info($self->{vtab_name})";
179 return $self->dbh->selectall_arrayref($sql, {Slice => {}});
180 }
181  
182 #======================================================================
183 package DBD::SQLcipher::VirtualTable::Cursor;
184 #======================================================================
185 use strict;
186 use warnings;
187  
188 sub NEW {
189 my ($class, $vtable, @args) = @_;
190 my $self = {vtable => $vtable,
191 args => \@args};
192 bless $self, $class;
193 }
194  
195  
196 sub FILTER {
197 my ($self, $idxNum, $idxStr, @values) = @_;
198 die "FILTER() should be redefined in cursor subclass";
199 }
200  
201 sub EOF {
202 my ($self) = @_;
203 die "EOF() should be redefined in cursor subclass";
204 }
205  
206 sub NEXT {
207 my ($self) = @_;
208 die "NEXT() should be redefined in cursor subclass";
209 }
210  
211 sub COLUMN {
212 my ($self, $idxCol) = @_;
213 die "COLUMN() should be redefined in cursor subclass";
214 }
215  
216 sub ROWID {
217 my ($self) = @_;
218 die "ROWID() should be redefined in cursor subclass";
219 }
220  
221  
222 1;
223  
224 __END__