Skip to content

Commit

Permalink
converted to item centric subclassing
Browse files Browse the repository at this point in the history
  • Loading branch information
rizen committed Dec 21, 2009
1 parent 5fdcc12 commit 88faac3
Show file tree
Hide file tree
Showing 15 changed files with 401 additions and 505 deletions.
7 changes: 6 additions & 1 deletion Changes
@@ -1,4 +1,9 @@
0.0201 - 2009-12-25
0.0300 - 2009-12-20
- Rethought the whole subclassing approach. Now subclassing the result rather than the domain, and everything is much cooler, and is much closer to DBIx::Class. Check out the docs for API changes.
- While rewriting discovered that all relationship and attribute methods were global (all classes in the schema got them), which is a huge bug. This problemhas been eliminated in this version as well.
- Added Sub::Name to the prereqs.

0.0201 - 2009-12-19
- Some documentation fixes.
- Cleaned up relationship code.
- Attempted in-item indexed relationships, but abandoned it due to race conditions. Check out commits a3c651797a5d3924be129300bf08500e0e3c6f2d and 04709b014317d9c15dc265323cde71a647e072d9 to investigate in the future.
Expand Down
46 changes: 23 additions & 23 deletions author.t/04.Select.t
Expand Up @@ -12,15 +12,15 @@ unless (defined $access && defined $secret) {
}


use Foo;
use Foo ();

my $foo = Foo->new(secret_key=>$secret, access_key=>$access, cache_servers=>[{host=>'127.0.0.1', port=>11211}]);

my $domain = $foo->domain('foo_domain');

use_ok( 'SimpleDB::Class::SQL' );
my $select = SimpleDB::Class::SQL->new(
domain => $domain,
item_class => $domain->item_class,
);

isa_ok($select, 'SimpleDB::Class::SQL');
Expand All @@ -35,133 +35,133 @@ is($select->format_value('unknown', 'that'), q{'that'}, "format a string");
is($select->format_int(45), q{1000000045}, "format a number");

$select = SimpleDB::Class::SQL->new(
domain => $domain,
item_class => $domain->item_class,
output => 'count(*)',
);
is($select->to_sql, 'select count(*) from `foo_domain`', "count query");

$select = SimpleDB::Class::SQL->new(
domain => $domain,
item_class => $domain->item_class,
output => 'color',
);
is($select->to_sql, 'select `color` from `foo_domain`', "single item output query");

$select = SimpleDB::Class::SQL->new(
domain => $domain,
item_class => $domain->item_class,
output => ['color','size'],
);
is($select->to_sql, 'select `color`, `size` from `foo_domain`', "multi-item output query");

$select = SimpleDB::Class::SQL->new(
domain => $domain,
item_class => $domain->item_class,
limit => 44,
);
is($select->to_sql, 'select * from `foo_domain` limit 44', "limit query");

$select = SimpleDB::Class::SQL->new(
domain => $domain,
item_class => $domain->item_class,
order_by => 'color',
);
is($select->to_sql, 'select * from `foo_domain` order by `color` asc', "sort query");

$select = SimpleDB::Class::SQL->new(
domain => $domain,
item_class => $domain->item_class,
order_by => ['color','desc'],
);
is($select->to_sql, 'select * from `foo_domain` order by `color` desc', "sort query descending");

$select = SimpleDB::Class::SQL->new(
domain => $domain,
item_class => $domain->item_class,
order_by => ['color'],
);
is($select->to_sql, 'select * from `foo_domain` order by `color` desc', "sort query implied descending");

$select = SimpleDB::Class::SQL->new(
domain => $domain,
item_class => $domain->item_class,
where => { 'quantity' => ['>', 3]},
);
is($select->to_sql, "select * from `foo_domain` where `quantity`>'1000000003'", "query with < where");

my $dt = DateTime->now;
$select = SimpleDB::Class::SQL->new(
domain => $domain,
item_class => $domain->item_class,
where => { 'start_date' => ['<', $dt]},
);
is($select->to_sql, "select * from `foo_domain` where `start_date`<'".DateTime::Format::Strptime::strftime('%Y-%m-%d %H:%M:%S %N %z',$dt)."'", "query with < where");

$select = SimpleDB::Class::SQL->new(
domain => $domain,
item_class => $domain->item_class,
where => { 'quantity' => ['>=', -99999]},
);
is($select->to_sql, "select * from `foo_domain` where `quantity`>='0999900001'", "query with >= where");

$select = SimpleDB::Class::SQL->new(
domain => $domain,
item_class => $domain->item_class,
where => { 'color' => ['<=', '3']},
);
is($select->to_sql, "select * from `foo_domain` where `color`<='3'", "query with <= where");

$select = SimpleDB::Class::SQL->new(
domain => $domain,
item_class => $domain->item_class,
where => { 'color' => ['!=', '3']},
);
is($select->to_sql, "select * from `foo_domain` where `color`!='3'", "query with != where");

$select = SimpleDB::Class::SQL->new(
domain => $domain,
item_class => $domain->item_class,
where => { 'color' => ['like', '3%']},
);
is($select->to_sql, "select * from `foo_domain` where `color` like '3%'", "query with like where");

$select = SimpleDB::Class::SQL->new(
domain => $domain,
item_class => $domain->item_class,
where => { 'color' => ['not like', '3%']},
);
is($select->to_sql, "select * from `foo_domain` where `color` not like '3%'", "query with not like where");

$select = SimpleDB::Class::SQL->new(
domain => $domain,
item_class => $domain->item_class,
where => { 'color' => ['between', 2,5]},
);
is($select->to_sql, "select * from `foo_domain` where `color` between '2' and '5'", "query with between where");

$select = SimpleDB::Class::SQL->new(
domain => $domain,
item_class => $domain->item_class,
where => { 'color' => ['in', 2,5,7]},
);
is($select->to_sql, "select * from `foo_domain` where `color` in ('2', '5', '7')", "query with in where");

$select = SimpleDB::Class::SQL->new(
domain => $domain,
item_class => $domain->item_class,
where => { 'color' => ['every', 2,5,7]},
);
is($select->to_sql, "select * from `foo_domain` where every(`color`) in ('2', '5', '7')", "query with every where");

tie my %intersection, 'Tie::IxHash', color=>2, size=>'this';
$select = SimpleDB::Class::SQL->new(
domain => $domain,
item_class => $domain->item_class,
where => { '-intersection' => \%intersection},
);
is($select->to_sql, "select * from `foo_domain` where (`color`='2' intersection `size`='this')", "query with or where");

tie my %or, 'Tie::IxHash', color=>2, size=>'this';
$select = SimpleDB::Class::SQL->new(
domain => $domain,
item_class => $domain->item_class,
where => { '-or' => \%or},
);
is($select->to_sql, "select * from `foo_domain` where (`color`='2' or `size`='this')", "query with or where");

tie my %and, 'Tie::IxHash', size=>'this', that=>1;
tie my %or, 'Tie::IxHash', color=>2, '-and'=>\%and;
$select = SimpleDB::Class::SQL->new(
domain => $domain,
item_class => $domain->item_class,
where => { '-or' => \%or},
);
is($select->to_sql, "select * from `foo_domain` where (`color`='2' or (`size`='this' and `that`='1'))", "query with or where");

tie my %where, 'Tie::IxHash', color=>2, size=>'this';
$select = SimpleDB::Class::SQL->new(
domain => $domain,
item_class => $domain->item_class,
order_by => ['color'],
limit => 44,
where => \%where,
Expand Down
10 changes: 5 additions & 5 deletions author.t/05.Domain_and_Item.t
@@ -1,4 +1,4 @@
use Test::More tests => 17;
use Test::More tests => 18;
use Test::Deep;
use lib ('../lib', 'lib');

Expand All @@ -10,13 +10,12 @@ unless (defined $access && defined $secret) {
die "You need to set environment variables SIMPLEDB_ACCESS_KEY and SIMPLEDB_SECRET_KEY to run these tests.";
}


use Foo;

my $foo = Foo->new(secret_key=>$secret, access_key=>$access, cache_servers=>[{host=>'127.0.0.1', port=>11211}]);
$foo->cache->flush;
my $domain = $foo->domain('foo_domain');
isa_ok($domain,'Foo::Domain');
isa_ok($domain,'SimpleDB::Class::Domain');
isa_ok($domain->simpledb,'SimpleDB::Class');

my $parent = $foo->domain('foo_parent');
Expand All @@ -34,12 +33,13 @@ ok($domain->insert({color=>'blue',size=>'small',parentId=>'two'}), 'adding item
is($domain->find('largered')->size, 'large', 'find() works');

my $x = $domain->insert({color=>'orange',size=>'large',parentId=>'one'});
cmp_deeply($x->to_hashref, {color=>'orange',size=>'large',parentId=>'one'}, 'to_hashref()');
isa_ok($x, 'Foo::Domain');
cmp_deeply($x->to_hashref, {color=>'orange',size=>'large',parentId=>'one', start_date=>undef, quantity=>undef}, 'to_hashref()');
$domain->insert({color=>'green',size=>'small',parentId=>'two'});
$domain->insert({color=>'black',size=>'huge',parentId=>'one'});
my $foos = $domain->search({size=>'small'});
isa_ok($foos, 'SimpleDB::Class::ResultSet');
isa_ok($foos->next, 'SimpleDB::Class::Item');
isa_ok($foos->next, 'Foo::Domain');
is($foos->next->size, 'small', 'fetched an item from the result set');

my $child = $foo->domain('foo_child');
Expand Down
4 changes: 2 additions & 2 deletions author.t/lib/Foo/Child.pm
@@ -1,9 +1,9 @@
package Foo::Child;

use Moose;
extends 'SimpleDB::Class::Domain';
extends 'SimpleDB::Class::Item';

__PACKAGE__->set_name('foo_child');
__PACKAGE__->set_domain_name('foo_child');
__PACKAGE__->add_attributes(domainId=>{isa=>'Str'});
__PACKAGE__->belongs_to('domain', 'Foo::Domain', 'domainId');

Expand Down
5 changes: 3 additions & 2 deletions author.t/lib/Foo/Domain.pm
@@ -1,9 +1,9 @@
package Foo::Domain;

use Moose;
extends 'SimpleDB::Class::Domain';
extends 'SimpleDB::Class::Item';

__PACKAGE__->set_name('foo_domain');
__PACKAGE__->set_domain_name('foo_domain');
__PACKAGE__->add_attributes(
color =>{isa=>'Str'},
size =>{isa=>'Str'},
Expand All @@ -14,5 +14,6 @@ __PACKAGE__->add_attributes(
__PACKAGE__->has_many('children', 'Foo::Child', 'domainId');
__PACKAGE__->belongs_to('parent', 'Foo::Parent', 'parentId');


1;

4 changes: 2 additions & 2 deletions author.t/lib/Foo/Parent.pm
@@ -1,9 +1,9 @@
package Foo::Parent;

use Moose;
extends 'SimpleDB::Class::Domain';
extends 'SimpleDB::Class::Item';

__PACKAGE__->set_name('foo_parent');
__PACKAGE__->set_domain_name('foo_parent');
__PACKAGE__->add_attributes(title=>{isa=>'Str'});
__PACKAGE__->has_many('domains', 'Foo::Domain', 'parentId');

Expand Down
3 changes: 2 additions & 1 deletion dist.ini
@@ -1,5 +1,5 @@
name = SimpleDB-Class
version = 0.0201
version = 0.0300
author = JT Smith <RIZEN@cpan.org>
license = Perl_5
copyright_holder = Plain Black Corporation
Expand All @@ -13,6 +13,7 @@ DateTime = 0
DateTime::Format::Strptime = 0
Moose = 0.93
MooseX::ClassAttribute = 0
Sub::Name = 0.04
Digest::SHA = 0
XML::Simple = 0
URI = 0
Expand Down

0 comments on commit 88faac3

Please sign in to comment.