Permalink
Browse files

user ids and update

  • Loading branch information...
1 parent 10250b5 commit 94c583981dd138baeaf3773c13394af43cf868d2 @rodrigolive committed Nov 1, 2011
Showing with 347 additions and 130 deletions.
  1. +45 −0 lib/Vamp/Backend/Oracle.pm
  2. +21 −10 lib/Vamp/Backend/Oracle/kv.pm
  3. +153 −6 lib/Vamp/Backend/SQLite.pm
  4. +46 −28 lib/Vamp/Collection.pm
  5. +15 −45 lib/Vamp/Database.pm
  6. +1 −1 lib/Vamp/ResultSet.pm
  7. +33 −21 t/basic.t
  8. +33 −19 t/bench2.pl
View
45 lib/Vamp/Backend/Oracle.pm
@@ -1,4 +1,49 @@
package Vamp::Backend::Oracle;
use base 'Vamp::Backend::Oracle::kv';
+sub drop_table {
+ my ($self,$table) = @_;
+ eval { $self->query("drop table $table cascade constraints") };
+ $ENV{VAMP_DEBUG} && $@ and warn $@;
+}
+
+sub _insert_kv {
+ my ($self, %args) = @_;
+ my $db_name = $self->{db_name};
+ $self->query( qq{INSERT INTO ${db_name}_kv ( id, oid, key, value, datatype, seq, version )
+ VALUES (${db_name}_kv_seq.NEXTVAL, ?,?,?,?,?,?) },
+ $args{oid}, $args{key}, $args{value}, $args{datatype}, $args{seq}, $args{version} );
+}
+
+sub _update_kv {
+ my ($self, %args) = @_;
+ my $db_name = $self->{db_name};
+ $self->query( qq{UPDATE ${db_name}_kv SET value = ? WHERE oid = ? AND key = ? }, $args{value}, $args{oid}, $args{key} );
+}
+
+sub _create {
+ my ($self , %args ) = @_;
+ my $db_name = $self->{db_name};
+ if( exists $args{data}{id} && ( my $id = delete $args{data}{id} ) ) {
+ if ( $args{serialize} ) {
+ $self->query( qq{INSERT INTO ${db_name}_obj ( id, collection, document ) VALUES (?,?,?) },
+ $id, $args{collection}, $self->_dump( $args{data} ) );
+ } else {
+ $self->query( qq{INSERT INTO ${db_name}_obj ( id, collection ) VALUES (?,?) },
+ $id, $args{collection} );
+ }
+ return $id;
+ } else {
+ if ( $args{serialize} && exists $args{data} ) {
+ $self->query( qq{INSERT INTO ${db_name}_obj ( id, collection, document )
+ VALUES ( ${db_name}_obj_seq.NEXTVAL, ?,? ) },
+ $args{collection}, $self->_dump( $args{data} ) );
+ } else {
+ $self->query( qq{INSERT INTO ${db_name}_obj ( id, collection )
+ VALUES (${db_name}_obj_seq.NEXTVAL,?) }, $args{collection} );
+ }
+ return $self->last_insert_id('','','','$self->{db_name}_obj');
+ }
+}
+
1;
View
31 lib/Vamp/Backend/Oracle/kv.pm
@@ -39,6 +39,14 @@ sub query_find_id {
return bless { st => $st, lc_columns => $self->{lc_columns} }, $self->{result_class};
}
+sub build_query_find_id {
+ my ($self, $collname, @ids ) = @_;
+ my $db_name = $self->{db_name};
+ my ($where, @binds) = $self->_abstract({ 'obj.id'=>\@ids });
+ my $sql = "SELECT * FROM ${db_name}_kv kv,${db_name}_obj obj $where and obj.id=kv.oid order by oid";
+ [ $sql, @binds ];
+}
+
sub build_query_findall {
my ($self, $collname, $where, $opts ) = @_;
my $db_name = $self->{db_name};
@@ -75,11 +83,20 @@ sub build_query_findall {
}
}
- # TODO select?
-
# where
my $where_quoted = $self->_quote_keys( $where );
my ( $wh, @binds ) = keys %$where ? $self->_abstract( $where_quoted ) : ('WHERE 1=1');
+
+ # select?
+ my $select_filter_str;
+ my $select_filter = $opts->{select} ? [ $self->_abstract( key => $opts->{select} ) ] : [];
+ if( @$select_filter && $select_filter->[0] =~ s/WHERE/and/ig ) {
+ push @binds, splice @{ $select_filter || [] },1;
+ $select_filter_str = $select_filter->[0];
+ } else {
+ $select_filter_str = '';
+ }
+
# pivots
my $pivots = join ',' => qw/oid/,
my $pivots_with = join ',' =>
@@ -105,6 +122,7 @@ sub build_query_findall {
SELECT $selects
FROM $pivots_from
$wh
+ $select_filter_str
and $pivots_outer
and obj2.id = kv2.oid
and obj2.collection = ?
@@ -135,6 +153,7 @@ sub build_query_findall {
# };
DEBUG && warn $sql;
#warn $sql;
+ #warn YAML::Dump( [ @binds, $collname ] );
[ $sql, @binds, $collname ];
}
@@ -189,14 +208,6 @@ sub deploy {
document CLOB
)});
$self->query("create sequence ${db_name}_obj_seq");
- $self->query("create trigger ${db_name}_obj_tr
- BEFORE INSERT ON ${db_name}_obj
- REFERENCING NEW AS NEW OLD AS OLD
- FOR EACH ROW
- BEGIN
- SELECT ${db_name}_obj_seq.NEXTVAL INTO :NEW.ID FROM dual;
- END;
- ");
};
try { $self->query("select count(*) from ${db_name}_kv") }
catch {
View
159 lib/Vamp/Backend/SQLite.pm
@@ -1,8 +1,112 @@
package Vamp::Backend::SQLite;
-use strict;
-use warnings;
+use Mouse;
use Try::Tiny;
use base 'Vamp::Database';
+use constant DEBUG => Vamp::DEBUG();
+
+sub last_insert_id {
+ my $self = shift;
+ return $self->dbh->sqlite_last_insert_rowid;
+}
+
+sub build_query_find_id {
+ my ($self, $collname, @ids ) = @_;
+ my $db_name = $self->{db_name};
+ my ($where, @binds) = $self->_abstract({ 'obj.id'=>\@ids });
+ my $sql = "SELECT * FROM ${db_name}_kv kv,${db_name}_obj obj $where and obj.id=kv.oid order by oid";
+ [ $sql, @binds ];
+}
+
+sub build_query_findall {
+ my ($self, $collname, $where, $opts ) = @_;
+ my $db_name = $self->{db_name};
+ #my $query_head = "SELECT DISTINCT oid FROM ${db_name}_kv ";
+ #my $coll_match = qq{ AND EXISTS ( select 1 from ${db_name}_obj vamp3
+ # where vamp3.collection='$collname' and vamp3.id=vamp_kv.oid ) };
+ $where = $self->_flatten_as_hash( $where );
+
+ my $hint = $opts->{hint};
+
+ my %pivot_cols;
+ my @order_by;
+ # where?
+ for my $key ( keys %$where ) {
+ next unless length $key;
+ $pivot_cols{ $key } = ();
+ }
+ # order_by ?
+ if( my @order_by_param = ref $opts->{order_by} eq 'ARRAY'
+ ? @{$opts->{order_by}}
+ : ( $opts->{order_by} ) ) {
+ for my $order_by_column ( @order_by_param ) {
+ next unless defined $order_by_column;
+ my $quoted = qq{"$order_by_column"};
+ # XXX cast type on hint?
+ if( my $type = $hint->{ $order_by_column } ) {
+ $quoted = "cast( $quoted as number )" if $type eq 'number';
+ }
+ # add to order by
+ push @order_by, $quoted;
+ # add to pivot column select
+ $quoted =~ s{ |DESC|ASC}{}gi;
+ $pivot_cols{ $order_by_column } = ()
+ unless exists $pivot_cols{ $order_by_column };
+ }
+ }
+
+ my $where_quoted = $self->_quote_keys( $where );
+ my ( $wh, @binds ) = keys %$where ? $self->_abstract( $where_quoted ) : ('WHERE 1=1');
+ #warn $wh;
+
+ # select?
+ my $select_filter_str;
+ my $select_filter = $opts->{select} ? [ $self->_abstract( key => $opts->{select} ) ] : [];
+ if( @$select_filter && $select_filter->[0] =~ s/WHERE/and/ig ) {
+ push @binds, splice @{ $select_filter || [] },1;
+ $select_filter_str = $select_filter->[0];
+ } else {
+ $select_filter_str = '';
+ }
+
+ my $pivots = join ',' => qw/oid/,
+ map {
+ qq{max( case when key='$_' then value else '' end) as "$_"};
+ } keys %pivot_cols;
+ my $selects = join ',' => qw/kv2.oid kv2.key kv2.value kv2.datatype/, map { qq{"$_"} } keys %pivot_cols;
+ my $order_by_pivot = @order_by ? 'order by ' . join ',', @order_by : '';
+ my $order_by = join ',', @order_by, 'kv2.oid', 'key', 'kv2.id';
+
+ my $sql = qq{
+ select $pivots
+ from vamp_kv kv,vamp_obj obj
+ where kv.oid = obj.id and obj.collection=?
+ group by oid
+ $order_by_pivot
+ };
+
+ # limit? (0 indexed)
+ my $start = $opts->{start};
+ my $limit = $opts->{limit};
+ $sql = do {
+ #my $page_num = int( $start / $limit ) + 1 ;
+ $limit ||= "9999999";
+ defined $start
+ ? "$sql LIMIT $start-1,$limit"
+ : "$sql LIMIT $limit";
+ } if defined $start || defined $limit;
+
+ $sql = qq{
+ select $selects from (
+ $sql
+ ) pivot, vamp_kv kv2
+ $wh
+ and kv2.oid = pivot.oid
+ $select_filter_str
+ order by $order_by
+ };
+ DEBUG && warn $sql;
+ [ $sql, $collname, @binds ];
+}
sub drop {
my $self = shift;
@@ -11,21 +115,64 @@ sub drop {
}
}
+sub _insert_kv {
+ my ($self, %args) = @_;
+ my $db_name = $self->{db_name};
+ $self->query( qq{INSERT INTO ${db_name}_kv ( oid, key, value, datatype, seq, version )
+ VALUES (?,?,?,?,?,?) },
+ $args{oid}, $args{key}, $args{value}, $args{datatype}, $args{seq}, $args{version} );
+}
+
+sub _update_kv {
+ my ($self, %args) = @_;
+ my $db_name = $self->{db_name};
+ $self->query( qq{UPDATE ${db_name}_kv SET value = ? WHERE oid = ? AND key = ? }, $args{value}, $args{oid}, $args{key} );
+}
+
+sub _create {
+ my ($self , %args ) = @_;
+ my $db_name = $self->{db_name};
+ if( exists $args{data}{id} && ( my $id = delete $args{data}{id} ) ) {
+ if ( $args{serialize} ) {
+ $self->query( qq{INSERT INTO ${db_name}_obj ( id, collection, document ) VALUES (?,?,?) },
+ $id, $args{collection}, $self->_dump( $args{data} ) );
+ } else {
+ $self->query( qq{INSERT INTO ${db_name}_obj ( id, collection ) VALUES (?,?) },
+ $id, $args{collection} );
+ }
+ return $id;
+ } else {
+ my $tempid = int(rand(999999999999) * $$ + time);
+ if ( $args{serialize} && exists $args{data} ) {
+ $self->query( qq{INSERT INTO ${db_name}_obj ( id, collection, document )
+ VALUES ( ?,?,? ) },
+ $tempid, $args{collection}, $self->_dump( $args{data} ) );
+ } else {
+ $self->query( qq{INSERT INTO ${db_name}_obj ( id, collection )
+ VALUES (?,?) }, $tempid, $args{collection} );
+ }
+ my $id = $self->last_insert_id('','','','$self->{db_name}_obj');
+ $self->query( qq{UPDATE ${db_name}_obj SET id=? WHERE id=?}, $id, $tempid );
+ return $id;
+ }
+}
+
sub deploy {
my $self = shift;
$self->query(q{PRAGMA foreign_keys = ON});
try { $self->query("select count(*) from $self->{db_name}_obj") }
catch {
$self->query(qq{create table $self->{db_name}_obj (
- id integer primary key,
- collection text
+ id text primary key,
+ collection text,
+ document text
)});
};
try { $self->query("select count(*) from $self->{db_name}_kv") }
catch {
$self->query(qq{create table $self->{db_name}_kv (
- id integer primary key,
- oid integer,
+ id integer primary key autoincrement,
+ oid text,
seq integer,
datatype text,
key text,
View
74 lib/Vamp/Collection.pm
@@ -16,23 +16,39 @@ sub insert {
my $self = shift;
my $data = ref $_[0] eq 'HASH' ? shift : \%{ @_ };
die 'invalid data' unless ref $data eq 'HASH';
- my $oid = $self->_obj( data=>$data );
+ my $oid = $self->db->_create( data=>$data, serialize=>$self->serialize, collection=>$self->name );
die 'no last oid' unless defined $oid;
try {
while( my ($k,$v) = each %$data ) {
$self->_serialdo( do=>'insert', oid=>$oid, k=>$k, v=>$v );
}
} catch {
- $self->rollback( oid=>$oid );
+ $self->_rollback( oid=>$oid );
die "Error inserting: " . shift();
};
}
+sub update {
+ my $self = shift;
+ my $oid = shift;
+ my $data = ref $_[0] eq 'HASH' ? shift : \%{ @_ };
+ try {
+ while( my ($k,$v) = each %$data ) {
+ $self->_serialdo( do=>'update', oid=>$oid, k=>$k, v=>$v );
+ }
+ } catch {
+ $self->_rollback( oid=>$oid );
+ die "Error updating: " . shift();
+ };
+}
+
sub find {
my $self = shift;
my ($where, $opts ) = ref $_[0] eq 'HASH'
? ( shift(), shift() )
- : ( \%{ @_ || {} } , {} );
+ : @_
+ ? ( { id=>[@_] } , {} )
+ : ({},{});
my $query = $self->db->build_query_findall( $self->name, $where, $opts );
my $rs = Vamp::ResultSet->new( db=>$self->db, query=>$query );
return wantarray ? $rs->all : $rs;
@@ -54,10 +70,17 @@ sub find_all {
wantarray ? @objs : \@objs;
}
+sub get {
+ my $self = shift;
+ my $query = $self->db->build_query_find_id( $self->name, @_ );
+ my $rs = Vamp::ResultSet->new( db=>$self->db, query=>$query );
+ @_ > 1 ? $rs : $rs->next;
+}
sub find_one {
my $self = shift;
- my $rs = $self->find( @_ );
- $rs->next;
+ !ref $_[0]
+ ? $self->get( @_ )
+ : $self->find( @_ )->next;
}
=head2 insert_from_query
@@ -129,24 +152,6 @@ sub _get {
}
-sub _obj {
- my ($self , %args ) = @_;
- my $db_name = $self->db->{db_name};
- if ( $self->serialize && exists $args{data} ) {
- $self->db->query( qq{insert into ${db_name}_obj ( collection, document ) values (?,?) },
- $self->name, $self->_dump( $args{data} ) );
- } else {
- $self->db->query( qq{insert into ${db_name}_obj ( collection ) values (?) }, $self->name );
- }
- return $self->db->last_insert_id('','','','$self->{db_name}_obj');
-}
-
-sub _dump {
- my ($self , $data ) = @_;
- require YAML::XS;
- return YAML::XS::Dump( $data );
-}
-
sub _rollback {
my ($self , %args ) = @_;
my $oid = $args{oid};
@@ -165,17 +170,30 @@ sub _serialdo {
my $ref = ref $value;
my $db_name = $self->db->{db_name};
if( ! $ref ) {
- $self->db->query( qq{insert into ${db_name}_kv ( oid, key, value, datatype, seq, version ) values (??) },
- $oid, $key, $value, $args{datatype} || 'v', $args{seq} || 1, $args{version} || 1
- );
+ if( $args{do} eq 'insert' ) {
+ $self->db->_insert_kv(
+ oid => $oid,
+ key => $key,
+ value => $value,
+ datatype => $args{datatype} || 'v',
+ seq => $args{seq} || 1,
+ version => $args{version} || 1
+ );
+ }
+ elsif( $args{do} eq 'update' ) {
+ $self->db->_update_kv( oid=>$oid, key=>$key, value=>$value );
+ }
+ else {
+ die "Invalid do operation $args{do}";
+ }
} elsif( $ref eq 'ARRAY' ) {
my $cnt = 0;
for( @$value ) {
- $self->_serialdo( do=>'insert', oid=>$oid, k=>$key, v=>$_, seq=>$cnt, datatype=>'a', prefix=>'' );
+ $self->_serialdo( do=>$args{do}, oid=>$oid, k=>$key, v=>$_, seq=>$cnt, datatype=>'a', prefix=>'' );
}
} elsif( $ref eq 'HASH' ) {
while( my ($k,$v) = each %$value ) {
- $self->_serialdo( do=>'insert', oid=>$oid, k=>$k, v=>$v, prefix=>$key );
+ $self->_serialdo( do=>$args{do}, oid=>$oid, k=>$k, v=>$v, prefix=>$key );
}
} else {
die "data type $ref not supported";
View
60 lib/Vamp/Database.pm
@@ -6,6 +6,7 @@ Base class for all backends.
package Vamp::Database;
#use Mouse;
use strict;
+#use Mouse;
use Try::Tiny;
use Vamp;
use Vamp::Util;
@@ -26,6 +27,7 @@ sub recreate {
$self->deploy;
}
+# XXX unused
sub query_find_id {
my ($self, %args) = @_;
my $db_name = $self->{db_name};
@@ -54,46 +56,10 @@ sub drop_collection {
$self->query("delete from ${db_name}_obj where collection = ?", $collname );
}
-sub query_find_abs {
- my ($self, $where, @binds ) = @_;
- my $db_name = $self->{db_name};
- my $query_head = "SELECT DISTINCT oid FROM ${db_name}_kv ";
- my @wh = $self->_flatten( $where );
- #push @wh, \[ "${db_name}_obj.id = ${db_name}_kv.oid" ];
- #my ($where,@binds) = $self->_abstract( -or => \@wh );
- #warn join',',$self->query( $query_head . $where, @binds )->flat;
- my @all_oids;
- for my $wh ( @wh ) {
- my ($where,@binds) = $self->_abstract( $wh );
- my $query = $query_head . $where;
- #warn $query;
- my @oids = $self->query( $query, @binds )->flat;
- push @all_oids, \@oids;
- }
- my @res = intersect(@all_oids);
- wantarray ? @res : \@res;
-}
-
-sub query_findall {
- my ($self, $collname, $where ) = @_;
- my $db_name = $self->{db_name};
- my $query_head = "SELECT DISTINCT oid FROM ${db_name}_kv vamp_kv";
- my $coll_match = " AND EXISTS ( select 1 from ${db_name}_obj vamp3 where vamp3.collection='$collname' and vamp3.id=vamp_kv.oid ) ";
- my @sqls;
- my @all_binds;
- my @wh = $self->_flatten( $where );
- for my $wh ( @wh ) {
- my ( $where, @binds ) = $self->_abstract( $wh );
- my $sql = $query_head . $where . $coll_match;
- push @sqls, $sql;
- push @all_binds, @binds;
- }
- my $from = join ' INTERSECT ', @sqls;
- my $sql = "SELECT DISTINCT oid FROM ( $from ) vamp2 WHERE vamp1.oid = vamp2.oid ";
- #warn $sql;
- $sql = "SELECT oid,key,value,datatype FROM ${db_name}_kv vamp1 WHERE EXISTS ( $sql ) ORDER BY vamp1.oid,id desc";
- #warn Dump $self->query( $sql, @all_binds )->hashes;
- $self->query( $sql, @all_binds );
+sub _dump {
+ my ($self , $data ) = @_;
+ require YAML::XS;
+ return YAML::XS::Dump( $data );
}
sub _is_special {
@@ -183,11 +149,15 @@ sub _abstract {
sub drop_database {
my $self = shift;
my $db_name = $self->{db_name};
- eval { $self->query("drop table ${db_name}_obj cascade constraints") };
- $ENV{VAMP_DEBUG} && $@ and warn $@;
- eval { $self->query("drop table ${db_name}_kv cascade constraints") };
- $ENV{VAMP_DEBUG} && $@ and warn $@;
- eval { $self->query("drop table ${db_name}_rel cascade constraints") };
+
+ for my $suffix ( qw/_kv _obj _rel/ ) {
+ $self->drop_table( ${db_name} . $suffix );
+ }
+}
+
+sub drop_table {
+ my ($self,$table) = @_;
+ eval { $self->query("drop table $table") };
$ENV{VAMP_DEBUG} && $@ and warn $@;
}
View
2 lib/Vamp/ResultSet.pm
@@ -47,7 +47,7 @@ sub next {
while( my $r = $self->{rs}->hash ) {
my $oid = $r->{oid};
#warn ">>>>>>>" . YAML::Dump( $r );
- if( defined $lastid && $oid != $lastid ) {
+ if( defined $lastid && $oid ne $lastid ) {
$self->{lastrow} = $r;
$self->{lastid} = $oid;
last;
View
54 t/basic.t
@@ -11,8 +11,8 @@ use VampTest;
# connect and drop
my $db = test_db();
$db->recreate;
-my $coll = $db->collection('person');
-$coll->drop;
+my $people = $db->collection('people');
+$people->drop;
my $company = $db->collection('company');
my $nums = $db->collection('numbers');
@@ -21,22 +21,22 @@ my $nums = $db->collection('numbers');
$company->insert({ name=>'joe', age=>55 });
$company->insert({ name=>'joe', age=>55 });
-$coll->insert({ name=>'joe', age=>20 });
-$coll->insert({ name=>'jack', age=>33 });
-$coll->insert({ name=>{ first=>'Susie', last=>'Doe' }, age=>25 });
-$coll->insert({ name=>'listy', age=>20, belongings=>[qw/house car boat/] });
+$people->insert({ name=>'joe', age=>20 });
+$people->insert({ name=>'jack', age=>33 });
+$people->insert({ name=>{ first=>'Susie', last=>'Doe' }, age=>25 });
+$people->insert({ name=>'listy', age=>20, belongings=>[qw/house car boat/] });
{
- my $p = $coll->find_one({ name=>'joe' });
+ my $p = $people->find_one({ name=>'joe' });
is $p->{age}, 20, 'find_one';
}
{
- my $rs = $coll->find({ age => { '>=', 25 } });
+ my $rs = $people->find({ age => { '>=', 25 } });
is ref $rs->as_query, 'ARRAY', 'as_query ok';
is $rs->count, 2, 'where compare with count';
}
{
- my $rs = $coll->find({ age => { '>=', 25 } }, { order_by=>'age' });
+ my $rs = $people->find({ age => { '>=', 25 } }, { order_by=>'age' });
is $rs->count, 2, 'where compare with count and order_by';
}
{
@@ -51,42 +51,54 @@ $coll->insert({ name=>'listy', age=>20, belongings=>[qw/house car boat/] });
}
{
my $rs = $nums->find({}, { start => 10, limit=>9, order_by=>'num', hint=>{ num=>'number' } });
- yy $rs->as_query;
+ #yy $rs->as_query;
my @all = $rs->all;
- yy \@all;
is $all[0]->{num}, 10, 'start limit ok';
is $all[8]->{num}, 18, 'start + limit ok';
is $all[9], undef, 'start + limit +1 ok';
is scalar @all, 9, 'limit at 10';
}
{
- my $p = $coll->find_one({ name=>'listy' });
- #yy $p;
+ my $p = $people->find_one({ name=>'listy' });
is ref( $p->{belongings} ), 'ARRAY', 'array ok';
is $p->{belongings}->[0], 'house', 'array find';
}
{
- my $objs = $coll->find({ age=>33 });
+ my $objs = $people->find({ age=>33 });
is $objs->first->{name}, 'jack', 'arr find';
}
{
- my $objs = $coll->find({ age=>[33,20], name=>{ '-like' => '%jac%' } });
+ my $objs = $people->find({ age=>[33,20], name=>{ '-like' => '%jac%' } });
is $objs->first->{name}, 'jack', 'age arr find';
}
{
- $coll->insert({ name=>{ first=>'bob', last=>'baz'}, age=>75, family=>{ kids=>['kyle','lucy'] } });
- $coll->insert({ name=>{ first=>'helen', last=>'baz'}, age=>71 });
- my $objs = $coll->find({ name=>{ last=>'baz' } });
+ $people->insert({ name=>{ first=>'bob', last=>'baz'}, age=>75, family=>{ kids=>['kyle','lucy'] } });
+ $people->insert({ name=>{ first=>'helen', last=>'baz'}, age=>71 });
+ my $objs = $people->find({ name=>{ last=>'baz' } });
my $row = $objs->first;
- yy $row;
is $row->{name}->{first}, 'bob', 'hash deep find';
is_deeply $row->{family}->{kids}, ['kyle', 'lucy'], 'arr deep find';
}
{
- $coll->insert({ name=>'longy', long_field_name_hard_to_store_cos_it_has_more_than_30_chars => 99 });
- my $obj = $coll->find_one({ name=>'longy' });
+ my $f = $people->find({ name=>{ first=>'bob' } }, { select=>['name.first'] })->first;
+ is $f->{name}->{first}, 'bob', 'select one field data';
+ is scalar keys %$f, 2, 'select one field'; # 2, one is the id
+}
+{
+ $people->insert({ name=>'longy', long_field_name_hard_to_store_cos_it_has_more_than_30_chars => 99 });
+ my $obj = $people->find_one({ name=>'longy' });
is $obj->{long_field_name_hard_to_store_cos_it_has_more_than_30_chars}, 99, 'long field';
}
+{
+ $people->insert({ id=>"reggie", gender=>"male", name=>'Reggie', age=>76 });
+ my $f = $people->find_one('reggie');
+ is $f->{gender}, 'male', 'custom id';
+}
+{
+ $people->update( 'reggie' => { age => 22 } );
+ my $f = $people->find_one('reggie');
+ is $f->{age}, 22, 'update';
+}
done_testing;
View
52 t/bench2.pl
@@ -8,18 +8,18 @@
use VampTest;
my $db = test_db();
-#$db->recreate;
my $jobs = $db->collection('jobs');
use Benchmark qw/:hireswallclock/;
print "Dump table tests\n";
my $k = 0;
if(0) {
+ $db->recreate;
$jobs->drop;
timethis( 1, sub{
$jobs->insert_from_query(
- query=>'select * from bali_job'
+ query=>'select * from bali_job@gbp'
);
});
};
@@ -62,7 +62,22 @@
)
*/
+ WITH pivot_num as (
+ select oid, to_char(value) as "name",NULL as "age" from vamp_kv kv,vamp_obj obj
+ where kv.oid = obj.id and obj.collection='people' and key='name'
+ UNION
+ select oid, NULL as "name",to_char(value) as "age" from vamp_kv kv3,vamp_obj obj3
+ where kv3.oid = obj3.id and obj3.collection='people' and key='age'
+ )
+ SELECT kv2.oid,kv2.key,kv2.value,kv2.datatype,"name","age"
+ FROM vamp_kv kv2,vamp_obj obj2, pivot_num
+ WHERE 1=1
+ and kv2.oid = pivot_num.oid (+)
+ and obj2.id = kv2.oid
+ and obj2.collection = 'people'
+ ORDER BY to_number( "age" ),kv2.oid,key,kv2.id
+/*
select count(*) from (
with pivot_name as (
select oid, val as "name" -- use val for half the speed
@@ -80,7 +95,7 @@
and kv2.oid = pivot_name.oid (+) and kv2.oid = pivot_status.oid (+)
order by kv2.oid,key,kv2.id
)
-
+*/
/* with pivot AS (
select oid, max( case when key='name' then to_char(value) else '' end) as "name"
@@ -101,7 +116,6 @@
});
warn "Total rows: $k";
}
-die "OK";
warn "YAML.....";
{
@@ -131,9 +145,8 @@
}
{
- my $k = 0;
timethis( 50, sub{
- my $rs = $jobs->{db}->query(q{select * from bali_job where name like '%3__'});
+ my $rs = $jobs->{db}->query(q{select * from bali_job@gbp where name like '%3__'});
#warn $rs->text('table'); return;
my @rows = $rs->hashes;
#$k += scalar map { keys %$_ } @rows;
@@ -144,23 +157,24 @@
}
-$k = 0;
print "Find_one tests\n";
+
timethis( 50, sub{
- my $rs = $jobs->find({ name=>{ -like => '%3__' } }, { start=>0, limit=>30 });
- #die yy $rs->all;
- my @rows = $rs->all;
- warn join ', ', keys %{ $rows[0] };
- $k += scalar @rows;
- # while( my $r = $rs->next ) {
- # warn $r->{name};
- # }
- #warn $r->{name};
- #my $r = $jobs->find({ name=>"me" . $k++ })->first; # ultraslow
- #is $r->{age}, $k, 'age ok';
+ my $k = 0;
+ my $rs = $jobs->find({ name=>{ -like => '%3__' } }, { start=>0, limit=>30 });
+ #die yy $rs->all;
+ my @rows = $rs->all;
+ warn join ', ', keys %{ $rows[0] };
+ $k += scalar @rows;
+ # while( my $r = $rs->next ) {
+ # warn $r->{name};
+ # }
+ #warn $r->{name};
+ #my $r = $jobs->find({ name=>"me" . $k++ })->first; # ultraslow
+ #is $r->{age}, $k, 'age ok';
+ warn "Total rows: $k";
});
-warn "Total rows: $k";
{
my $rs = $jobs->query({}, { order_by=>'name', rows=>5 });

0 comments on commit 94c5839

Please sign in to comment.