Client/Sqlite.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Client/Splunk.pm ↑ Summary Client/Ssh.pm »
1 #
2 # $Id$
3 #
4 # client::sqlite Brik
5 #
6 package Metabrik::Client::Sqlite;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 db => [ qw(sqlite_file) ],
20 autocommit => [ qw(0|1) ],
21 dbh => [ qw(INTERNAL) ],
22 },
23 attributes_default => {
24 autocommit => 1,
25 },
26 commands => {
27 open => [ qw(sqlite_file|OPTIONAL) ],
28 execute => [ qw(sql_query) ],
29 create => [ qw(table_name fields_array key|OPTIONAL) ],
30 insert => [ qw(table_name data_hash) ],
31 select => [ qw(table_name fields_array|OPTIONAL key|OPTIONAL) ],
32 commit => [ ],
33 show_tables => [ ],
34 describe_table => [ ],
35 list_types => [ ],
36 close => [ ],
37 },
38 require_modules => {
39 'DBI' => [ ],
40 'DBD::SQLite' => [ ],
41 },
42 };
43 }
44  
45 sub open {
46 my $self = shift;
47 my ($db) = @_;
48  
49 $db ||= $self->db;
50 $self->brik_help_run_undef_arg('open', $db) or return;
51  
52 my $dbh = DBI->connect('dbi:SQLite:dbname='.$db,'','', {
53 AutoCommit => $self->autocommit,
54 RaiseError => 1,
55 PrintError => 0,
56 PrintWarn => 0,
57 #HandleError => sub {
58 #my ($errstr, $dbh, $arg) = @_;
59 #die("DBI: $errstr\n");
60 #},
61 });
62 if (! $dbh) {
63 return $self->log->error("open: DBI: $DBI::errstr");
64 }
65  
66 $self->dbh($dbh);
67  
68 return 1;
69 }
70  
71 sub execute {
72 my $self = shift;
73 my ($sql) = @_;
74  
75 my $dbh = $self->dbh;
76 $self->brik_help_run_undef_arg('open', $dbh) or return;
77 $self->brik_help_run_undef_arg('execute', $sql) or return;
78  
79 $self->log->debug("execute: sql[$sql]");
80  
81 my $sth = $dbh->prepare($sql);
82  
83 return $sth->execute;
84 }
85  
86 sub commit {
87 my $self = shift;
88  
89 my $dbh = $self->dbh;
90 $self->brik_help_run_undef_arg('open', $dbh) or return;
91  
92 if ($self->autocommit) {
93 $self->log->verbose("commit: skipping cause autocommit is on");
94 return 1;
95 }
96  
97 eval {
98 $dbh->commit;
99 };
100 if ($@) {
101 chomp($@);
102 return $self->log->warning("commit: $@");
103 }
104  
105 return 1;
106 }
107  
108 sub create {
109 my $self = shift;
110 my ($table, $fields, $key) = @_;
111  
112 $self->brik_help_run_undef_arg('create', $table) or return;
113 $self->brik_help_run_undef_arg('create', $fields) or return;
114 $self->brik_help_run_invalid_arg('create', $fields, 'ARRAY') or return;
115  
116 # create table TABLE (stuffid INTEGER PRIMARY KEY, field1 VARCHAR(512), field2, date DATE);
117 # insert into TABLE (field1) values ("value1");
118  
119 my $sql = 'CREATE TABLE '.$table.' (';
120 for my $field (@$fields) {
121 # Fields are table fields, we normalize them (space char not allowed)
122 $field =~ s/ /_/g;
123 $sql .= $field;
124 if (defined($key) && $field eq $key) {
125 $sql .= ' PRIMARY KEY NOT NULL';
126 }
127 $sql .= ',';
128 }
129 $sql =~ s/,$//;
130 $sql .= ');';
131  
132 $self->log->verbose("create: $sql");
133  
134 return $self->execute($sql);
135 }
136  
137 sub insert {
138 my $self = shift;
139 my ($table, $data) = @_;
140  
141 $self->brik_help_run_undef_arg('insert', $table) or return;
142 $self->brik_help_run_undef_arg('insert', $data) or return;
143  
144 my @data = ();
145 if (ref($data) eq 'ARRAY') {
146 for my $this (@$data) {
147 if (ref($this) ne 'HASH') {
148 $self->log->verbose('insert: not a hash, skipping');
149 next;
150 }
151 push @data, $this;
152 }
153 }
154 else {
155 if (ref($data) ne 'HASH') {
156 return $self->log->error("insert: Argument 'data' must be HASHREF");
157 }
158 push @data, $data;
159 }
160  
161 for my $this (@data) {
162 my $sql = 'INSERT INTO '.$table.' (';
163 # Fields are table fields, we normalize them (space char not allowed)
164 my @fields = map { s/ /_/g; $_ } keys %$this;
165 my @values = map { $_ } values %$this;
166 $sql .= join(',', @fields);
167 $sql .= ') VALUES (';
168 for (@values) {
169 $sql .= "\"$_\",";
170 }
171 $sql =~ s/,$//;
172 $sql .= ')';
173  
174 $self->log->verbose("insert: $sql");
175  
176 $self->execute($sql);
177 }
178  
179 return 1;
180 }
181  
182 sub select {
183 my $self = shift;
184 my ($table, $fields, $key) = @_;
185  
186 my $dbh = $self->dbh;
187 $fields ||= [ '*' ];
188 $self->brik_help_run_undef_arg('open', $dbh) or return;
189 $self->brik_help_run_undef_arg('select', $table) or return;
190 $self->brik_help_run_invalid_arg('select', $fields, 'ARRAY') or return;
191 $self->brik_help_run_empty_array_arg('select', $fields) or return;
192  
193 my $sql = 'SELECT ';
194 for (@$fields) {
195 # Fields are table fields, we normalize them (space char not allowed)
196 s/ /_/g;
197 $sql .= "$_,";
198 }
199 $sql =~ s/,$//;
200 $sql .= ' FROM '.$table;
201  
202 my $sth = $dbh->prepare($sql);
203 my $rv = $sth->execute;
204  
205 if (! defined($key)) {
206 return $sth->fetchall_arrayref;
207 }
208  
209 return $sth->fetchall_hashref($key);
210 }
211  
212 sub show_tables {
213 my $self = shift;
214  
215 my $dbh = $self->dbh;
216 $self->brik_help_run_undef_arg('open', $dbh) or return;
217  
218 # $dbh->table_info(undef, $schema, $table, $type, \%attr);
219 # $type := 'TABLE', 'VIEW', 'LOCAL TEMPORARY' and 'SYSTEM TABLE'
220 my $sth = $dbh->table_info(undef, 'main', '%', 'TABLE');
221  
222 my $h = $sth->fetchall_arrayref;
223 my @r = ();
224 for my $this (@$h) {
225 push @r, $this->[-1]; # Last entry is the CREATE TABLE one.
226 }
227  
228 return \@r;
229 }
230  
231 sub list_types {
232 my $self = shift;
233  
234 return [
235 'INTEGER',
236 'DATE',
237 'VARCHAR(int)',
238 ];
239 }
240  
241 # https://metacpan.org/pod/DBI#table_info
242 sub describe_table {
243 #my $sth = $dbh->column_info(undef,'table_name',undef,undef);
244 #$sth->fetchall_arrayref;
245 }
246  
247 sub close {
248 my $self = shift;
249  
250 my $dbh = $self->dbh;
251 if (defined($dbh)) {
252 $dbh->commit;
253 $dbh->disconnect;
254 $self->dbh(undef);
255 }
256  
257 return 1;
258 }
259  
260 1;
261  
262 __END__
« Client/Splunk.pm ↑ Summary Client/Ssh.pm »