4 use DBIx::Data::Store::CRUD;
5 use DBIx::Data::Store::Raw;
8 use Scalar::Util qw(refaddr);
11 sort { $a->{name} cmp $b->{name} } @_
14 my $dsn = 'dbi:SQLite:tmp.db';
18 return DBI->connect($dsn, undef, undef, { RaiseError => 1 })
25 id INTEGER NOT NULL PRIMARY KEY,
26 name VARCHAR(255) NOT NULL
29 #my $pop = $dbh->prepare(q{INSERT INTO person (name) VALUES (?)});
30 #my @names = qw(Joe Jim Bob Pterry);
31 #$pop->execute($_) for @names;
33 # my $id = 0; map +{ id => ++$id, name => $_ }, @names
37 my $db_store = DBIx::Data::Store::Raw->connect($dsn);
39 sub raw_store { $db_store }
43 DBIx::Data::Store::CRUD->new({
46 select_all => 'SELECT id, name FROM names',
47 delete_all => 'DELETE FROM names',
48 select_one => 'SELECT id, name FROM names WHERE id = ?',
50 my ($store, undef, $dbh, $args) = @_;
51 $store->_sth_for($dbh, 'INSERT INTO names (name) VALUES (?)', $args);
52 [ $dbh->last_insert_id(undef,undef,undef,undef), @$args ];
54 update_one => 'UPDATE names SET name = ? WHERE id = ?',
55 delete_one => 'DELETE FROM names WHERE id = ?',
63 my $store = make_store;
65 is_deeply([$store->flatten], [], 'Empty set');
67 is_deeply($store->add(['Bob']), [1,'Bob'], 'Add record');
69 is_deeply([$store->flatten], [[1,'Bob']], 'One member');
71 is_deeply($store->get([1]), [1,'Bob'], 'Retrieve by key');
73 $store->replace([1],['Robert']);
75 is_deeply([$store->flatten], [[1,'Robert']], 'Name changed (all)');
77 is_deeply($store->get([1]), [1,'Robert'], 'Retrieve by key');
79 $store->add([$_]) for qw(Joe James Jim);
82 [ sort { $a->[0] <=> $b->[0] } $store->flatten ]
85 is_deeply($flatsort->(), [
86 [1,'Robert'],[2,'Joe'],[3,'James'],[4,'Jim']
89 is_deeply($store->get([3]),[3,'James'], 'Retrieve by key');
93 is_deeply($flatsort->(), [
94 [1,'Robert'],[2,'Joe'],[4,'Jim']
95 ], 'Three members left');
100 run_tests unless caller;