| 1 | package DBIx::CouchLike::SQLite; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use base qw/ DBIx::CouchLike /; | ||
| 6 | |||
| 7 | my @sql_format = ( | ||
| 8 | q|CREATE TABLE IF NOT EXISTS %s_data (id text not null primary key, value text)|, | ||
| 9 | q|CREATE TABLE IF NOT EXISTS %s_map (design_id text not null, id text not null, key text not null, value text)|, | ||
| 10 | q|CREATE INDEX %s_map_idx ON %s_map (design_id, key)|, | ||
| 11 | ); | ||
| 12 | |||
| 13 | my @sql_format_v = ( | ||
| 14 | q|CREATE TABLE IF NOT EXISTS %s_data (id text not null primary key, value text, version integer not null default 0)|, | ||
| 15 | q|CREATE TABLE IF NOT EXISTS %s_map (design_id text not null, id text not null, key text not null, value text)|, | ||
| 16 | q|CREATE INDEX %s_map_idx ON %s_map (design_id, key)|, | ||
| 17 | ); | ||
| 18 | |||
| 19 | sub create_table { | ||
| 20 | my $class = shift; | ||
| 21 | my $dbh = shift; | ||
| 22 | my $name = shift; | ||
| 23 | my $v = shift; | ||
| 24 | for my $f ( $v ? @sql_format_v : @sql_format ) { | ||
| 25 | $dbh->do( sprintf $f, $name, $name ); | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | sub _start_with { | ||
| 30 | my ( $self, $key, $value ) = @_; | ||
| 31 | my $tail = substr($value, -1, 1); | ||
| 32 | $tail = chr( ord($tail) + 1 ); | ||
| 33 | my $next_value = $value; | ||
| 34 | $next_value =~ s/.\z/$tail/; | ||
| 35 | |||
| 36 | return ( " ($key >= ? AND $key < ?)", $value, $next_value ); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub _offset_limit_sql { | ||
| 40 | my ( $class, $sql, $query, $param ) = @_; | ||
| 41 | |||
| 42 | if ( defined $query->{offset} && $query->{limit} ) { | ||
| 43 | $sql .= q{ LIMIT ?,? }; | ||
| 44 | push @$param, $query->{offset}; | ||
| 45 | push @$param, $query->{limit}; | ||
| 46 | } | ||
| 47 | elsif ( defined $query->{limit} ) { | ||
| 48 | $sql .= q{ LIMIT ? }; | ||
| 49 | push @$param, $query->{limit}; | ||
| 50 | } | ||
| 51 | return $sql; | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | 1; |