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