line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::PObject::Driver::sqlite; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# $Id: sqlite.pm,v 1.10 2003/11/07 04:51:04 sherzodr Exp $ |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
26
|
|
6
|
|
|
|
|
|
|
#use diagnostics; |
7
|
1
|
|
|
1
|
|
4
|
use Log::Agent; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
130
|
|
8
|
1
|
|
|
1
|
|
5
|
use vars ('@ISA', '$VERSION'); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
536
|
|
9
|
|
|
|
|
|
|
require File::Path; |
10
|
|
|
|
|
|
|
require File::Basename; |
11
|
|
|
|
|
|
|
require Class::PObject::Driver::DBI; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
@ISA = ('Class::PObject::Driver::DBI'); |
14
|
|
|
|
|
|
|
$VERSION = '2.01'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub save { |
18
|
1
|
|
|
1
|
1
|
3
|
my ($self, $object_name, $props, $columns) = @_; |
19
|
|
|
|
|
|
|
|
20
|
1
|
0
|
|
|
|
3
|
my $dbh = $self->dbh($object_name, $props) or return; |
21
|
0
|
0
|
|
|
|
0
|
my $table = $self->_tablename($object_name, $props, $dbh) or return; |
22
|
0
|
|
|
|
|
0
|
my ($sql, $bind_params); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# checking if $columns->{id} exists: |
25
|
0
|
0
|
|
|
|
0
|
if ( $columns->{id} ) { |
26
|
|
|
|
|
|
|
#let's check if there is a database record for this column already |
27
|
0
|
0
|
|
|
|
0
|
if ( $self->count($object_name, $props, {id=>$columns->{id}}) ) { |
28
|
0
|
|
|
|
|
0
|
($sql, $bind_params) = $self->_prepare_update($table, $columns, {id=>$columns->{id}}); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
} |
31
|
0
|
0
|
|
|
|
0
|
unless ( $sql ) { |
32
|
0
|
|
|
|
|
0
|
($sql, $bind_params)= $self->_prepare_insert($table, $columns) |
33
|
|
|
|
|
|
|
} |
34
|
0
|
|
|
|
|
0
|
my $sth = $dbh->prepare( $sql ); |
35
|
0
|
0
|
|
|
|
0
|
unless ( $sth->execute(@$bind_params) ) { |
36
|
0
|
|
|
|
|
0
|
$self->errstr("couldn't save/update the record ($sth->{Statement}): " . $sth->errstr); |
37
|
0
|
|
|
|
|
0
|
logerr $self->errstr; |
38
|
|
|
|
|
|
|
return undef |
39
|
0
|
|
|
|
|
0
|
} |
40
|
0
|
|
|
|
|
0
|
return $dbh->func("last_insert_rowid") |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub dbh { |
52
|
1
|
|
|
1
|
0
|
2
|
my ($self, $object_name, $props) = @_; |
53
|
|
|
|
|
|
|
|
54
|
1
|
|
|
|
|
3
|
my $datasource = $props->{datasource}; |
55
|
1
|
50
|
|
|
|
5
|
if ( defined $self->stash($datasource) ) { |
56
|
0
|
|
|
|
|
0
|
return $self->stash($datasource) |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
1
|
|
|
|
|
56
|
my $basedir = File::Basename::dirname( $datasource ); |
60
|
1
|
|
|
|
|
19
|
logtrc 3, "datasource:%s, directory: %s", $datasource, $basedir; |
61
|
|
|
|
|
|
|
|
62
|
1
|
50
|
|
|
|
85
|
unless ( -e $basedir ) { |
63
|
1
|
50
|
|
|
|
154
|
unless ( File::Path::mkpath($basedir) ) { |
64
|
0
|
|
|
|
|
0
|
$self->errstr( "couldn't create '$basedir': $!" ); |
65
|
|
|
|
|
|
|
return undef |
66
|
0
|
|
|
|
|
0
|
} |
67
|
|
|
|
|
|
|
} |
68
|
1
|
|
|
|
|
330
|
require DBI; |
69
|
0
|
|
|
|
|
|
my $dbh = DBI->connect("dbi:SQLite:dbname=$datasource", "", "", {RaiseError=>0, PrintError=>0}); |
70
|
0
|
0
|
|
|
|
|
unless ( defined $dbh ) { |
71
|
0
|
|
|
|
|
|
$self->errstr("couldn't connect to 'DSN': " . $DBI::errstr); |
72
|
|
|
|
|
|
|
return undef |
73
|
0
|
|
|
|
|
|
} |
74
|
0
|
|
|
|
|
|
$dbh->{FetchHashKeyName} = 'NAME_lc'; |
75
|
0
|
|
|
|
|
|
$self->stash($datasource, $dbh); |
76
|
0
|
|
|
|
|
|
$self->stash('close', 1); |
77
|
0
|
|
|
|
|
|
return $dbh |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub _tablename { |
84
|
0
|
|
|
0
|
|
|
my ($self, $object_name, $props, $dbh) = @_; |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
my $table_name = lc $object_name; |
87
|
0
|
|
|
|
|
|
$table_name =~ s/\W+/_/g; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
{ |
90
|
0
|
|
|
|
|
|
local $^W = 0; # DBD::SQLite generates a warning |
|
0
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
my %tables = map { $_, 1 } $dbh->tables; |
|
0
|
|
|
|
|
|
|
92
|
0
|
0
|
|
|
|
|
if ( $tables{ $table_name } ) { |
93
|
0
|
|
|
|
|
|
return $table_name |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
my $sql = $self->_prepare_create_table($object_name, $table_name); |
98
|
0
|
0
|
|
|
|
|
unless ( $dbh->do( $sql ) ) { |
99
|
0
|
|
|
|
|
|
$self->errstr( $dbh->errstr ); |
100
|
|
|
|
|
|
|
return undef |
101
|
0
|
|
|
|
|
|
} |
102
|
0
|
|
|
|
|
|
return $table_name |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
1; |
106
|
|
|
|
|
|
|
__END__; |