| 1 | package MYDan::Util::SQLiteDB; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | MYDan::Util::SQLiteDB - SQLite database interface | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use base qw( MYDan::Util::SQLiteDB ); | ||
| 10 | |||
| 11 | my $db = MYDan::Util::SQLiteDBI->new( '/database/file' => @table ); | ||
| 12 | |||
| 13 | $db->select( foo => 'name', [ 1, 'bar' ] ); | ||
| 14 | |||
| 15 | =cut | ||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | use Carp; | ||
| 19 | use DBI; | ||
| 20 | |||
| 21 | sub new | ||
| 22 | { | ||
| 23 | my ( $class, $db ) = splice @_, 0, 2; | ||
| 24 | |||
| 25 | $db = DBI->connect | ||
| 26 | ( | ||
| 27 | "DBI:SQLite:dbname=$db", '', '', | ||
| 28 | { RaiseError => 1, PrintWarn => 0, PrintError => 0 } | ||
| 29 | ); | ||
| 30 | |||
| 31 | my $self = bless { db => $db }, ref $class || $class; | ||
| 32 | my @define = $self->define(); | ||
| 33 | $self->{column} = [ map { $define[ $_ << 1 ] } 0 .. @define / 2 - 1 ]; | ||
| 34 | |||
| 35 | my %exist = $self->exist(); | ||
| 36 | map { $self->create( $_ ) } @_, keys %exist; | ||
| 37 | return $self; | ||
| 38 | } | ||
| 39 | |||
| 40 | =head1 METHODS | ||
| 41 | |||
| 42 | =head3 column() | ||
| 43 | |||
| 44 | Returns table columns. | ||
| 45 | |||
| 46 | =cut | ||
| 47 | sub column | ||
| 48 | { | ||
| 49 | my $self = shift; | ||
| 50 | return @{ $self->{column} }; | ||
| 51 | } | ||
| 52 | |||
| 53 | =head3 table() | ||
| 54 | |||
| 55 | Returns table names. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | sub table | ||
| 59 | { | ||
| 60 | my $self = shift; | ||
| 61 | my %table = $self->exist(); | ||
| 62 | keys %table; | ||
| 63 | } | ||
| 64 | |||
| 65 | =head3 create( $table ) | ||
| 66 | |||
| 67 | Create $table. | ||
| 68 | |||
| 69 | =cut | ||
| 70 | sub create | ||
| 71 | { | ||
| 72 | my ( $self, $table ) = splice @_; | ||
| 73 | my %exist = $self->exist(); | ||
| 74 | my %column = $self->define(); | ||
| 75 | my @column = $self->column(); | ||
| 76 | |||
| 77 | my $db = $self->{db}; | ||
| 78 | my $neat = DBI::neat( $table ); | ||
| 79 | |||
| 80 | unless ( $exist{$table} ) | ||
| 81 | { | ||
| 82 | $db->do | ||
| 83 | ( | ||
| 84 | sprintf "CREATE TABLE $neat ( %s )", | ||
| 85 | join ', ', map { "$_ $column{$_}" } @column | ||
| 86 | ) | ||
| 87 | } | ||
| 88 | |||
| 89 | my $stmt = $self->{stmt}{$table} = {}; | ||
| 90 | |||
| 91 | =head3 insert( $table, @record ) | ||
| 92 | |||
| 93 | Inserts @record into $table. | ||
| 94 | |||
| 95 | =cut | ||
| 96 | $stmt->{insert} = $db->prepare | ||
| 97 | ( | ||
| 98 | sprintf "INSERT OR REPLACE INTO $neat ( %s ) VALUES ( %s )", | ||
| 99 | join( ',', @column ), join( ',', map { '?' } @column ) | ||
| 100 | ); | ||
| 101 | |||
| 102 | =head3 dump( $table ) | ||
| 103 | |||
| 104 | Dump all records from $table. | ||
| 105 | |||
| 106 | =cut | ||
| 107 | $stmt->{dump} = $db->prepare( "SELECT * FROM $neat" ); | ||
| 108 | |||
| 109 | =head3 truncate( $table ) | ||
| 110 | |||
| 111 | Delete all records from $table. | ||
| 112 | |||
| 113 | =cut | ||
| 114 | $stmt->{truncate} = $db->prepare( "DELETE FROM $neat" ); | ||
| 115 | |||
| 116 | =head3 drop( $table ) | ||
| 117 | |||
| 118 | Drop $table. | ||
| 119 | |||
| 120 | =cut | ||
| 121 | $stmt->{drop} = $db->prepare( "DROP TABLE $neat" ); | ||
| 122 | return $self; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub AUTOLOAD | ||
| 126 | { | ||
| 127 | my ( $self, $table ) = splice @_, 0, 2; | ||
| 128 | return unless my $stmt = $self->{stmt}{$table}; | ||
| 129 | return unless our $AUTOLOAD =~ /::(\w+)$/ && ( $stmt = $stmt->{$1} ); | ||
| 130 | @{ $self->execute( $stmt, @_ )->fetchall_arrayref }; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub DESTROY | ||
| 134 | { | ||
| 135 | my $self = shift; | ||
| 136 | %$self = (); | ||
| 137 | } | ||
| 138 | |||
| 139 | =head3 select( $table, $column, %query ) | ||
| 140 | |||
| 141 | Select $column from $table. | ||
| 142 | |||
| 143 | =cut | ||
| 144 | sub select | ||
| 145 | { | ||
| 146 | my ( $self, $table, $column ) = splice @_, 0, 3; | ||
| 147 | return unless $self->{stmt}{$table}; | ||
| 148 | my $stmt = "SELECT $column FROM ". DBI::neat( $table ). $self->query( @_ ); | ||
| 149 | return @{ $self->do( $stmt )->fetchall_arrayref }; | ||
| 150 | } | ||
| 151 | |||
| 152 | =head3 delete( $table, %query ) | ||
| 153 | |||
| 154 | Delete records from $table. | ||
| 155 | |||
| 156 | =cut | ||
| 157 | sub delete | ||
| 158 | { | ||
| 159 | my ( $self, $table ) = splice @_, 0, 2; | ||
| 160 | return unless $self->{stmt}{$table}; | ||
| 161 | $self->do( 'DELETE FROM '. DBI::neat( $table ). $self->query( @_ ) ); | ||
| 162 | } | ||
| 163 | |||
| 164 | =head1 QUERY | ||
| 165 | |||
| 166 | I<%query> consists of ARRAY indexed by I<column>. The first array element | ||
| 167 | is a boolean value that indcates if I<column> is IN or NOT IN the rest of | ||
| 168 | the array elements. i.e. | ||
| 169 | |||
| 170 | x => [ 0, 'foo', 'bar' ], y => [ 1, 'bar', 'baz' ] | ||
| 171 | |||
| 172 | means: | ||
| 173 | |||
| 174 | "WHERE x NOT IN ( 'foo','bar' ) AND y IN ( 'bar','baz' )" | ||
| 175 | |||
| 176 | =cut | ||
| 177 | sub query | ||
| 178 | { | ||
| 179 | my ( $self, %cond, @cond ) = splice @_; | ||
| 180 | |||
| 181 | while ( my ( $col, $cond ) = each %cond ) | ||
| 182 | { | ||
| 183 | my $in = $cond->[0] eq '1' ? 'IN' : $cond->[0] eq '0' ?'NOT IN' : $cond->[0]; | ||
| 184 | push @cond, sprintf "$col $in ( %s )", | ||
| 185 | join ',', map { DBI::neat( $cond->[$_] ) } 1 .. @$cond - 1; | ||
| 186 | } | ||
| 187 | return @cond ? ' WHERE ' . join ' AND ', @cond : ''; | ||
| 188 | } | ||
| 189 | |||
| 190 | ## return a flattened hash of existing tables | ||
| 191 | sub exist | ||
| 192 | { | ||
| 193 | my $self = shift; | ||
| 194 | my $exist = $self->{db}->table_info( undef, undef, undef, 'TABLE' ) | ||
| 195 | ->fetchall_hashref( 'TABLE_NAME' ); | ||
| 196 | return %$exist; | ||
| 197 | } | ||
| 198 | |||
| 199 | ## execute a statement | ||
| 200 | sub do | ||
| 201 | { | ||
| 202 | my $self = shift; | ||
| 203 | $self->execute( $self->{db}->prepare( @_ ) ); | ||
| 204 | } | ||
| 205 | |||
| 206 | ## execute a prepared statement | ||
| 207 | sub execute | ||
| 208 | { | ||
| 209 | my ( $self, $stmt ) = splice @_, 0, 2; | ||
| 210 | while ( $stmt ) | ||
| 211 | { | ||
| 212 | eval { $stmt->execute( @_ ) }; | ||
| 213 | last unless $@; | ||
| 214 | confess $@ if $@ !~ /locked/; | ||
| 215 | } | ||
| 216 | return $stmt; | ||
| 217 | } | ||
| 218 | |||
| 219 | 1; |