DBIx-Class-ResultSet-RecursiveUpdate-v0.001/0000755000175000017500000000000011142104704016755 5ustar zbyzbyDBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/0000755000175000017500000000000011142104704017220 5ustar zbyzbyDBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/lib/0000755000175000017500000000000011142104704017766 5ustar zbyzbyDBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/lib/MySchema/0000755000175000017500000000000011142104704021474 5ustar zbyzbyDBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/lib/MySchema/Test.pm0000644000175000017500000000137111141005235022751 0ustar zbyzbypackage MySchema::Test; use strict; use warnings; use base 'DBIx::Class'; __PACKAGE__->load_components(qw/ InflateColumn::DateTime PK::Auto Core /); __PACKAGE__->table("test"); __PACKAGE__->add_columns( hidden_col => { data_type => "INTEGER" }, text_col => { data_type => "TEXT" }, password_col => { data_type => "TEXT" }, checkbox_col => { data_type => "TEXT", default_value => 0, is_nullable => 0, }, select_col => { data_type => "TEXT" }, radio_col => { data_type => "TEXT" }, radiogroup_col => { data_type => "TEXT" }, date_col => { data_type => "DATE" }, not_in_form => { data_type => "TEXT" }, ); __PACKAGE__->set_primary_key("hidden_col"); 1; DBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/lib/DBSchema.pm0000644000175000017500000000262711141005235021737 0ustar zbyzbypackage DBSchema; # Created by DBIx::Class::Schema::Loader v0.03000 @ 2006-10-02 08:24:09 use strict; use warnings; use base 'DBIx::Class::Schema'; use DateTime; __PACKAGE__->load_namespaces( default_resultset_class => '+DBIx::Class::ResultSet::RecursiveUpdate' ); sub get_test_schema { my ( $dsn, $user, $pass ) = @_; $dsn ||= 'dbi:SQLite:dbname=t/var/dvdzbr.db'; warn "testing $dsn"; my $schema = __PACKAGE__->connect( $dsn, $user, $pass, {} ); $schema->deploy({ add_drop_table => 1, }); $schema->populate('User', [ [ qw/username name password / ], [ 'jgda', 'Jonas Alves', ''], [ 'isa' , 'Isa', '', ], [ 'zby' , 'Zbyszek Lukasiak', ''], ] ); $schema->populate('Tag', [ [ qw/name file / ], [ 'comedy', '' ], [ 'dramat', '' ], [ 'australian', '' ], ] ); $schema->populate('Dvd', [ [ qw/name imdb_id owner current_borrower creation_date alter_date / ], [ 'Picnick under the Hanging Rock', 123, 1, 3, '2003-01-16 23:12:01', undef ], [ 'The Deerhunter', 1234, 1, 1, undef, undef ], [ 'Rejs', 1235, 3, 1, undef, undef ], [ 'Seksmisja', 1236, 3, 1, undef, undef ], ] ); $schema->populate( 'Dvdtag', [ [ qw/ dvd tag / ], [ 1, 2 ], [ 1, 3 ], [ 3, 1 ], [ 4, 1 ], ] ); return $schema; } 1; DBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/lib/RunTests.pm0000644000175000017500000001127211141615053022121 0ustar zbyzby# -*- perl -*- package RunTests; use Exporter 'import'; # gives you Exporter's import() method directly @EXPORT = qw(run_tests); use strict; use Test::More; sub run_tests{ my $schema = shift; plan tests => 27; my $dvd_rs = $schema->resultset( 'Dvd' ); my $user_rs = $schema->resultset( 'User' ); my $owner = $user_rs->next; my $another_owner = $user_rs->next; my $initial_user_count = $user_rs->count; my $initial_dvd_count = $dvd_rs->count; # creating new record linked to some old record my $updates; $updates = { name => 'Test name 2', viewings => [ { user_id => $owner->id } ], owner => { id => $another_owner->id }, }; my $new_dvd = $dvd_rs->recursive_update( $updates ); # my $new_dvd = $dvd_rs->create( $updates ); is ( $dvd_rs->count, $initial_dvd_count + 1, 'Dvd created' ); is ( $schema->resultset( 'User' )->count, $initial_user_count, "No new user created" ); is ( $new_dvd->name, 'Test name 2', 'Dvd name set' ); is ( $new_dvd->owner->id, $another_owner->id, 'Owner set' ); is ( $new_dvd->viewings->count, 1, 'Viewing created' ); # creating new records my $updates = { aaaa => undef, tags => [ '2', { id => '3' } ], name => 'Test name', owner => $owner, current_borrower => { name => 'temp name', username => 'temp name', password => 'temp name', }, liner_notes => { notes => 'test note', }, like_has_many => [ { key2 => 1 } ], like_has_many2 => [ { onekey => { name => 'aaaaa' }, key2 => 1 } ], }; my $dvd = $dvd_rs->recursive_update( $updates ); ; is ( $dvd_rs->count, $initial_dvd_count + 2, 'Dvd created' ); is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "One new user created" ); is ( $dvd->name, 'Test name', 'Dvd name set' ); is_deeply ( [ map {$_->id} $dvd->tags ], [ '2', '3' ], 'Tags set' ); is ( $dvd->owner->id, $owner->id, 'Owner set' ); is ( $dvd->current_borrower->name, 'temp name', 'Related record created' ); is ( $dvd->liner_notes->notes, 'test note', 'might_have record created' ); ok ( $schema->resultset( 'Twokeys' )->find( { dvd_name => 'Test name', key2 => 1 } ), 'Twokeys created' ); my $onekey = $schema->resultset( 'Onekey' )->search( name => 'aaaaa' )->first; ok ( $onekey, 'Onekey created' ); ok ( $schema->resultset( 'Twokeys_belongsto' )->find( { key1 => $onekey->id, key2 => 1 } ), 'Twokeys created' ); # changing existing records my $num_of_users = $user_rs->count; $updates = { id => $dvd->id, aaaa => undef, name => 'Test name', tags => [ ], 'owner' => $another_owner->id, current_borrower => { username => 'new name a', name => 'new name a', password => 'new password a', }, liner_notes => { notes => 'test note changed', }, }; $dvd = $dvd_rs->recursive_update( $updates ); is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "No new user created" ); is ( $dvd->name, 'Test name', 'Dvd name set' ); is ( $dvd->owner->id, $another_owner->id, 'Owner updated' ); is ( $dvd->current_borrower->name, 'new name a', 'Related record modified' ); is ( $dvd->tags->count, 0, 'Tags deleted' ); is ( $dvd->liner_notes->notes, 'test note changed', 'might_have record changed' ); # repeatable $updates = { name => 'temp name', username => 'temp username', password => 'temp username', owned_dvds =>[ { 'name' => 'temp name 1', 'tags' => [ 1, 2 ], }, { 'name' => 'temp name 2', 'tags' => [ 2, 3 ], } ] }; my $user = $user_rs->recursive_update( $updates ); is ( $schema->resultset( 'User' )->count, $initial_user_count + 2, "New user created" ); is ( $dvd_rs->count, $initial_dvd_count + 4, 'Dvds created' ); my %owned_dvds = map { $_->name => $_ } $user->owned_dvds; is( scalar keys %owned_dvds, 2, 'Has many relations created' ); ok( $owned_dvds{'temp name 1'}, 'Name in a has_many related record saved' ); my @tags = $owned_dvds{'temp name 1'}->tags; is( scalar @tags, 2, 'Tags in has_many related record saved' ); ok( $owned_dvds{'temp name 2'}, 'Second name in a has_many related record saved' ); } DBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/lib/DBSchema/0000755000175000017500000000000011142104704021374 5ustar zbyzbyDBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/lib/DBSchema/Result/0000755000175000017500000000000011142104704022652 5ustar zbyzbyDBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/lib/DBSchema/Result/User.pm0000644000175000017500000000177011141005235024131 0ustar zbyzbypackage DBSchema::Result::User; # Created by DBIx::Class::Schema::Loader v0.03000 @ 2006-10-02 08:24:09 use strict; use warnings; use base 'DBIx::Class'; #use overload '""' => sub {$_[0]->name}, fallback => 1; __PACKAGE__->load_components('Core'); __PACKAGE__->table("usr"); __PACKAGE__->add_columns( "id" => { data_type => 'integer', is_auto_increment => 1, }, "username" => { data_type => 'varchar', size => '100', }, "password" => { data_type => 'varchar', size => '100', }, "name" => { data_type => 'varchar', size => '100', }, ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->has_many("user_roles", "UserRole", { "foreign.user" => "self.id" }); __PACKAGE__->has_many("owned_dvds", "Dvd", { "foreign.owner" => "self.id" }); __PACKAGE__->has_many( "borrowed_dvds", "Dvd", { "foreign.current_borrower" => "self.id" }, ); __PACKAGE__->many_to_many('roles', 'user_roles' => 'role'); 1; DBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/lib/DBSchema/Result/Dvd.pm0000644000175000017500000000346511141613703023740 0ustar zbyzbypackage DBSchema::Result::Dvd; # Created by DBIx::Class::Schema::Loader v0.03000 @ 2006-10-02 08:24:09 use strict; use warnings; use base 'DBIx::Class'; use overload '""' => sub {$_[0]->name}, fallback => 1; use lib '../../DBIx-Class-HTML-FormFu/lib/'; __PACKAGE__->load_components(qw/IntrospectableM2M Core/); __PACKAGE__->table('dvd'); __PACKAGE__->add_columns( 'id' => { data_type => 'integer', is_auto_increment => 1 }, 'name' => { data_type => 'varchar', size => 100, is_nullable => 1, }, 'imdb_id' => { data_type => 'varchar', size => 100, is_nullable => 1, }, 'owner' => { data_type => 'integer' }, 'current_borrower' => { data_type => 'integer', is_nullable => 1, }, 'creation_date' => { data_type => 'datetime', is_nullable => 1, }, 'alter_date' => { data_type => 'datetime', is_nullable => 1, }, 'twokeysfk' => { data_type => 'integer', is_nullable => 1, }, ); __PACKAGE__->set_primary_key('id'); __PACKAGE__->belongs_to('owner', 'DBSchema::Result::User', { id => 'owner' }); __PACKAGE__->belongs_to('current_borrower', 'DBSchema::Result::User', { id => 'current_borrower' }); __PACKAGE__->has_many('dvdtags', 'Dvdtag', { 'foreign.dvd' => 'self.id' }); __PACKAGE__->has_many('viewings', 'DBSchema::Result::Viewing', { 'foreign.dvd_id' => 'self.id' }); __PACKAGE__->many_to_many('tags', 'dvdtags' => 'tag'); __PACKAGE__->might_have( liner_notes => 'DBSchema::Result::LinerNotes', undef, { proxy => [ qw/notes/ ] }, ); __PACKAGE__->add_relationship('like_has_many', 'DBSchema::Result::Twokeys', { 'foreign.dvd_name' => 'self.name' }, { accessor_name => 'like_has_many' } ); __PACKAGE__->add_relationship('like_has_many2', 'DBSchema::Result::Twokeys_belongsto', { 'foreign.key1' => 'self.twokeysfk' }, ); 1; DBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/lib/DBSchema/Result/Tag.pm0000644000175000017500000000132511141005235023722 0ustar zbyzbypackage DBSchema::Result::Tag; # Created by DBIx::Class::Schema::Loader v0.03000 @ 2006-10-02 08:24:09 use strict; use warnings; use base 'DBIx::Class'; use overload '""' => sub {$_[0]->name}, fallback => 1; __PACKAGE__->load_components("PK::Auto", "Core"); __PACKAGE__->table("tag"); __PACKAGE__->add_columns( "id" => { data_type => 'integer', is_auto_increment => 1 }, 'name' => { data_type => 'varchar', size => 100, is_nullable => 1, }, 'file' => { data_type => 'text', is_nullable => 1, } ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->has_many("dvdtags", "Dvdtag", { "foreign.tag" => "self.id" }); __PACKAGE__->many_to_many('dvds', 'dvdtags' => 'dvd'); 1; DBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/lib/DBSchema/Result/LinerNotes.pm0000644000175000017500000000061711141005235025274 0ustar zbyzbypackage # hide from PAUSE DBSchema::Result::LinerNotes; use base qw/DBIx::Class::Core/; __PACKAGE__->table('liner_notes'); __PACKAGE__->add_columns( 'liner_id' => { data_type => 'integer', }, 'notes' => { data_type => 'varchar', size => 100, }, ); __PACKAGE__->set_primary_key('liner_id'); __PACKAGE__->belongs_to( 'dvd', 'DBSchema::Result::Dvd', 'liner_id' ); 1; DBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/lib/DBSchema/Result/Dvdtag.pm0000644000175000017500000000100011141005235024406 0ustar zbyzbypackage DBSchema::Result::Dvdtag; # Created by DBIx::Class::Schema::Loader v0.03000 @ 2006-10-02 08:24:09 use strict; use warnings; use base 'DBIx::Class'; __PACKAGE__->load_components("PK::Auto", "Core"); __PACKAGE__->table("dvdtag"); __PACKAGE__->add_columns( "dvd" => { data_type => 'integer' }, "tag" => { data_type => 'integer' }, ); __PACKAGE__->set_primary_key("dvd", "tag"); __PACKAGE__->belongs_to("dvd", "Dvd", { id => "dvd" }); __PACKAGE__->belongs_to("tag", "Tag", { id => "tag" }); 1; DBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/lib/DBSchema/Result/UserRole.pm0000644000175000017500000000102011141005235024737 0ustar zbyzbypackage DBSchema::Result::UserRole; # Created by DBIx::Class::Schema::Loader v0.03000 @ 2006-10-02 08:24:09 use strict; use warnings; use base 'DBIx::Class'; __PACKAGE__->load_components("PK::Auto", "Core"); __PACKAGE__->table("user_role"); __PACKAGE__->add_columns( "user" => { data_type => 'integer' } , "role" => { data_type => 'integer' } ); __PACKAGE__->set_primary_key("user", "role"); __PACKAGE__->belongs_to("user", "User", { id => "user" }); __PACKAGE__->belongs_to("role", "Role", { id => "role" }); 1; DBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/lib/DBSchema/Result/Twokeys.pm0000644000175000017500000000104111141005235024647 0ustar zbyzbypackage DBSchema::Result::Twokeys; # Created by DBIx::Class::Schema::Loader v0.03000 @ 2006-10-02 08:24:09 use strict; use warnings; use base 'DBIx::Class'; __PACKAGE__->load_components("PK::Auto", "Core"); __PACKAGE__->table("twokeys"); __PACKAGE__->add_columns( "dvd_name" => { data_type => 'varchar', size => 100 }, "key2" => { data_type => 'integer' }, ); __PACKAGE__->set_primary_key("dvd_name", "key2"); __PACKAGE__->add_relationship('like_belongs_to', 'DBSchema::Result::Dvd', { 'foreign.name' => 'self.dvd_name' }, ); 1; DBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/lib/DBSchema/Result/Role.pm0000644000175000017500000000124011141005235024104 0ustar zbyzbypackage DBSchema::Result::Role; # Created by DBIx::Class::Schema::Loader v0.03000 @ 2006-10-02 08:24:09 use strict; use warnings; use base 'DBIx::Class'; use overload '""' => sub {$_[0]->id}, fallback => 1; __PACKAGE__->load_components("PK::Auto", "Core"); __PACKAGE__->table("role"); __PACKAGE__->add_columns( "id" => { data_type => 'integer', is_auto_increment => 1, }, "role" => { data_type => 'varchar', size => '100', } ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->has_many("user_roles", "UserRole", { "foreign.role" => "self.id" }); __PACKAGE__->many_to_many('users', 'user_roles' => 'user'); 1; DBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/lib/DBSchema/Result/Viewing.pm0000755000175000017500000000073011141005235024621 0ustar zbyzbypackage DBSchema::Result::Viewing; use base 'DBIx::Class::Core'; __PACKAGE__->table('viewing'); __PACKAGE__->add_columns( 'user_id' => { data_type => 'integer' }, 'dvd_id' => { data_type => 'integer' }, ); __PACKAGE__->set_primary_key(qw/user_id dvd_id/); __PACKAGE__->belongs_to( user => 'DBSchema::Result::User', {'foreign.id'=>'self.user_id'}, ); __PACKAGE__->belongs_to( dvd => 'DBSchema::Result::Dvd', {'foreign.id'=>'self.dvd_id'}, ); ; 1; DBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/lib/DBSchema/Result/Onekey.pm0000644000175000017500000000104711141616330024446 0ustar zbyzbypackage DBSchema::Result::Onekey; # Created by DBIx::Class::Schema::Loader v0.03000 @ 2006-10-02 08:24:09 use strict; use warnings; use base 'DBIx::Class'; __PACKAGE__->load_components("PK::Auto", "Core"); __PACKAGE__->table("onekey"); __PACKAGE__->add_columns( "id" => { data_type => 'integer', is_auto_increment => 1 }, name => { data_type => 'varchar', size => 100, is_nullable => 1 }, ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->might_have( twokeys_belongsto => 'DBSchema::Result::Twokeys_belongsto', 'key1', ); 1; DBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/lib/DBSchema/Result/Twokeys_belongsto.pm0000644000175000017500000000120411141613562026734 0ustar zbyzbypackage DBSchema::Result::Twokeys_belongsto; # Created by DBIx::Class::Schema::Loader v0.03000 @ 2006-10-02 08:24:09 use strict; use warnings; use base 'DBIx::Class'; __PACKAGE__->load_components("PK::Auto", "Core"); __PACKAGE__->table("twokeys_belongsto"); __PACKAGE__->add_columns( "key1" => { data_type => 'integer' }, "key2" => { data_type => 'integer' }, ); __PACKAGE__->set_primary_key("key1", "key2"); __PACKAGE__->add_relationship('like_belongs_to', 'DBSchema::Result::Dvd', { 'foreign.twokeysfk' => 'self.key1' }, ); __PACKAGE__->belongs_to('onekey', 'DBSchema::Result::Onekey', { 'foreign.id' => 'self.key1' }, ); 1; DBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/lib/MySchema.pm0000644000175000017500000000015611141005235022032 0ustar zbyzbypackage MySchema; use strict; use warnings; use base 'DBIx::Class::Schema'; __PACKAGE__->load_classes; 1; DBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/var/0000755000175000017500000000000011142104704020010 5ustar zbyzbyDBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/var/dvdzbr.db0000644000175000017500000011200011142104673021611 0ustar zbyzbySQLite format 3@ x ;%@!A 9 0  qZD- #temp name 2 #temp name 1 Test name #Test name 2 Seksmisja1236 Rejs1235 )The Deerhunter1234? I3Picnick under the Hanging Rock1232003-01-16 23:12:01    -zbyZbyszek Lukasiak  isaIsa #jgdaJonas Alves /test note changed ! australian  dramat  comedy pp(''temp usernametemp usernametemp name'!)!new name anew password anew name a -zbyZbyszek Lukasiak  isaIsa #jgdaJonas Alves 0~~T.1?tableuseruserCREATE TABLE user ( id INTEGER PRIMARY KEY NOT NULL, username varchar(100) NOT NULL, password varchar(100) NOT NULL, name varchar(100) NOT NULL )oCindexviewing_idx_user_id_viewingviewingCREATE INDEX viewing_idx_user_id_viewing on viewing (user_id)r,KtabledvddvdCREATE TABLE dvd ( id INTEGER PRIMARY KEY NOT NULL, name varchar(100), imdb_id varchar(100), owner integer NOT NULL, current_borrower integer, creation_date datetime, alter_date datetime, twokeysfk integer )o-Eindexdvd_idx_current_borrower_dvddvdCREATE INDEX dvd_idx_current_borrower_dvd on dvd (current_borrower)J.-cindexdvd_idx_name_dvddvdCREATE INDEX dvd_idx_name_dvd on dvd (name)Y/7windexdvd_idx_twokeysfk_dvddvdCREATE INDEX dvd_idx_twokeysfk_dvd on dvd (twokeysfk)M0/gindexdvd_idx_owner_dvddvdCREATE INDEX dvd_idx_owner_dvd on dvd (owner)  QQW=%y1MtabledvdtagdvdtagCREATE TABLE dvdtag ( dvd integer NOT NULL, tag integer NOT NULL, PRIMARY KEY (dvd, tag) )+2?indexsqlite_autoindex_dvdtag_1dvdtagY37qindexdvdtag_idx_dvd_dvdtagdvdtagCREATE INDEX dvdtag_idx_dvd_dvdtag on dvdtag (dvd)Y47qindexdvdtag_idx_tag_dvdtagdvdtagCREATE INDEX dvdtag_idx_tag_dvdtag on dvdtag (tag)5##Stableliner_notesliner_notesCREATE TABLE liner_notes ( liner_id INTEGER PRIMARY KEY NOT NULL, notes varchar(100) NOT NULL ){6I#indexliner_notes_idx_liner_id_linerliner_notesCREATE INDEX liner_notes_idx_liner_id_liner on liner_notes (liner_id)g7)tableonekeyonekeyCREATE TABLE onekey ( id INTEGER PRIMARY KEY NOT NULL, name varchar(100) )j87tableroleroleCREATE TABLE role ( id INTEGER PRIMARY KEY NOT NULL, role varchar(100) NOT NULL )k9=tabletagtagCREATE TABLE tag ( id INTEGER PRIMARY KEY NOT NULL, name varchar(100), file text )   Test name Test name   aaaaa     uu#temp name 2#temp name 1 Test name#Test name 2 SeksmisjaRejs)The Deerhunter"IPicnick under the Hanging Rock    $+2?indexsqlite_autoindex_dvdtag_1dvdtagY37qindexdvdtag_idx_dvd_dvdtagdvdtagCREATE INDEX dvdtag_idx_dvd_dvdtag on dvdtag (dvd)Y47qindexdvdtag_idx_tag_dvdtagdvdtagCREATE INDEX dvdtag_idx_tag_dvdtag on dvdtag (tag)5##Stableliner_notesliner_notesCREATE TABLE liner_notes ( liner_id INTEGER PRIMARY KEY NOT NULL, notes varchar(100) NOT NULL ){6I#indexliner_notes_idx_liner_id_linerliner_notesCREATE INDEX liner_notes_idx_liner_id_liner on liner_notes (liner_id)g7)tableonekeyonekeyCREATE TABLE onekey ( id INTEGER PRIMARY KEY NOT NULL, name varchar(100) )j87tableroleroleCREATE TABLE role ( id INTEGER PRIMARY KEY NOT NULL, role varchar(100) NOT NULL )k9=tabletagtagCREATE TABLE tag ( id INTEGER PRIMARY KEY NOT NULL, name varchar(100), file text )  Test name    ,,_EQ :qtabletwokeystwokeysCREATE TABLE twokeys ( dvd_name varchar(100) NOT NULL, key2 integer NOT NULL, PRIMARY KEY (dvd_name, key2) )-;Aindexsqlite_autoindex_twokeys_1twokeyso<E indextwokeys_idx_dvd_name_twokeystwokeysCREATE INDEX twokeys_idx_dvd_name_twokeys on twokeys (dvd_name)=//ktabletwokeys_belongstotwokeys_belongstoCREATE TABLE twokeys_belongsto ( key1 integer NOT NULL, key2 integer NOT NULL, PRIMARY KEY (key1, key2) )A>U/indexsqlite_autoindex_twokeys_belongsto_1twokeys_belongsto?I/indextwokeys_belongsto_idx_key1_twotwokeys_belongstoCREATE INDEX twokeys_belongsto_idx_key1_two on twokeys_belongsto (key1)@G/indextwokeys_belongsto_idx_key1_00twokeys_belongsto CREATE INDEX twokeys_belongsto_idx_key1_00 on twokeys_belongsto (key1)+A=tableusrusr CREATE TABLE usr ( id INTEGER PRIMARY KEY NOT NULL, username varchar(100) NOT NULL, password varchar(100) NOT NULL, name varchar(100) NOT NULL )    Og%B[tableuser_roleuser_role CREATE TABLE user_role ( user integer NOT NULL, role integer NOT NULL, PRIMARY KEY (user, role) )1CEindexsqlite_autoindex_user_role_1user_role oDEindexuser_role_idx_role_user_roleuser_role"CREATE INDEX user_role_idx_role_user_role on user_role (role)oEEindexuser_role_idx_user_user_roleuser_role#CREATE INDEX user_role_idx_user_user_role on user_role (user) FktableviewingviewingCREATE TABLE viewing ( user_id integer NOT NULL, dvd_id integer NOT NULL, PRIMARY KEY (user_id, dvd_id) )-GAindexsqlite_autoindex_viewing_1viewingiHAindexviewing_idx_dvd_id_viewingviewingCREATE INDEX viewing_idx_dvd_id_viewing on viewing (dvd_id)lICindexviewing_idx_user_id_viewingviewing$CREATE INDEX viewing_idx_user_id_viewing on viewing (user_id)DBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/pg.t0000644000175000017500000000057511141611336020025 0ustar zbyzby# -*- perl -*- use lib 't/lib'; use DBSchema; use RunTests; use Test::More; my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/}; plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test' . ' (note: creates and tables!)' unless ($dsn && $user); my $schema = DBSchema::get_test_schema( $dsn, $user, $pass ); run_tests( $schema ); DBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/pod.t0000644000175000017500000000021411141005235020162 0ustar zbyzby#!perl -T use Test::More; eval "use Test::Pod 1.14"; plan skip_all => "Test::Pod 1.14 required for testing POD" if $@; all_pod_files_ok(); DBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/sqlite.t0000644000175000017500000000021711141611336020711 0ustar zbyzby# -*- perl -*- use lib 't/lib'; use DBSchema; use RunTests; use Test::More; my $schema = DBSchema::get_test_schema(); run_tests( $schema ); DBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/00.load.t0000644000175000017500000000031111141005235020533 0ustar zbyzbyuse Test::More tests => 1; BEGIN { use_ok( 'DBIx::Class::ResultSet::RecursiveUpdate' ); } diag( "Testing DBIx::Class::ResultSet::RecursiveUpdate $DBIx::Class::ResultSet::RecursiveUpdate::VERSION" ); DBIx-Class-ResultSet-RecursiveUpdate-v0.001/t/pod-coverage.t0000644000175000017500000000025411141005235021757 0ustar zbyzby#!perl -T use Test::More; eval "use Test::Pod::Coverage 1.04"; plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" if $@; all_pod_coverage_ok(); DBIx-Class-ResultSet-RecursiveUpdate-v0.001/lib/0000755000175000017500000000000011142104704017523 5ustar zbyzbyDBIx-Class-ResultSet-RecursiveUpdate-v0.001/lib/DBIx/0000755000175000017500000000000011142104704020311 5ustar zbyzbyDBIx-Class-ResultSet-RecursiveUpdate-v0.001/lib/DBIx/Class/0000755000175000017500000000000011142104704021356 5ustar zbyzbyDBIx-Class-ResultSet-RecursiveUpdate-v0.001/lib/DBIx/Class/ResultSet/0000755000175000017500000000000011142104704023310 5ustar zbyzbyDBIx-Class-ResultSet-RecursiveUpdate-v0.001/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm0000644000175000017500000003115111142056007026763 0ustar zbyzbypackage DBIx::Class::ResultSet::RecursiveUpdate; use version; $VERSION = qv('0.001'); use warnings; use strict; use Carp; use Scalar::Util qw( blessed ); use base qw(DBIx::Class::ResultSet); sub recursive_update { my ( $self, $updates, $fixed_fields ) = @_; # warn 'entering: ' . $self->result_source->from(); if ( blessed($updates) && $updates->isa('DBIx::Class::Row') ) { return $updates; } carp 'fixed fields needs to be a hash ref' if $fixed_fields && ref($fixed_fields) ne 'HASH'; # direct column accessors my %columns; # relations that that should be done before the row is inserted into the database # like belongs_to my %pre_updates; # relations that that should be done after the row is inserted into the database # like has_many and might_have my %post_updates; my %columns_by_accessor = $self->_get_columns_by_accessor; # warn 'columns_by_accessor: ' . Dumper( \%columns_by_accessor ); use Data::Dumper; for my $name ( keys %$updates ) { my $source = $self->result_source; if ( $columns_by_accessor{$name} && !( $source->has_relationship($name) && ref( $updates->{$name} ) ) ) { $columns{$name} = $updates->{$name}; next; } next if !$source->has_relationship($name); my $info = $source->relationship_info($name); if ( _master_relation_cond( $source, $info->{cond}, $self->_get_pk_for_related($name) ) ) { $pre_updates{$name} = $updates->{$name}; } else { $post_updates{$name} = $updates->{$name}; } } # warn 'columns: ' . Dumper( \%columns ); use Data::Dumper; my $object; my @missing = grep { !exists $columns{$_} && !exists $fixed_fields->{$_} } $self->result_source->primary_columns; if ( !scalar @missing ) { $object = $self->find( \%columns, { key => 'primary' } ); } $object ||= $self->new( {} ); # first update columns and other accessors - so that later related records can be found for my $name ( keys %columns ) { $object->$name( $updates->{$name} ); } for my $name ( keys %pre_updates ) { my $info = $object->result_source->relationship_info($name); $self->_update_relation( $name, $updates, $object, $info ); } # $self->_delete_empty_auto_increment($object); # don't allow insert to recurse to related objects - we do the recursion ourselves # $object->{_rel_in_storage} = 1; $object->update_or_insert; # updating many_to_many for my $name ( keys %$updates ) { next if exists $columns{$name}; my $value = $updates->{$name}; # many to many case if ( $self->is_m2m($name) ) { my ($pk) = $self->_get_pk_for_related($name); my @rows; my $result_source = $object->$name->result_source; for my $elem ( @{ $updates->{$name} } ) { if ( ref $elem ) { push @rows, $result_source->resultset->find($elem); } else { push @rows, $result_source->resultset->find( { $pk => $elem } ); } } my $set_meth = 'set_' . $name; $object->$set_meth( \@rows ); } } for my $name ( keys %post_updates ) { my $info = $object->result_source->relationship_info($name); $self->_update_relation( $name, $updates, $object, $info ); } return $object; } sub _get_columns_by_accessor { my $self = shift; my $source = $self->result_source; my %columns; for my $name ( $source->columns ) { my $info = $source->column_info($name); $info->{name} = $name; $columns{ $info->{accessor} || $name } = $info; } return %columns; } sub _update_relation { my ( $self, $name, $updates, $object, $info ) = @_; my $related_result = $self->related_resultset($name)->result_source->resultset; my $resolved = $self->result_source->resolve_condition( $info->{cond}, $name, $object ); # warn 'resolved: ' . Dumper( $resolved ); use Data::Dumper; $resolved = undef if $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION == $resolved; if ( ref $updates->{$name} eq 'ARRAY' ) { for my $sub_updates ( @{ $updates->{$name} } ) { $sub_updates = { %$sub_updates, %$resolved } if $resolved; my $sub_object = $related_result->recursive_update( $sub_updates, $resolved ); } } else { my $sub_updates = $updates->{$name}; $sub_updates = { %$sub_updates, %$resolved } if $resolved; my $sub_object = $related_result->recursive_update( $sub_updates, $resolved ); $object->set_from_related( $name, $sub_object ); } } sub is_m2m { my ( $self, $relation ) = @_; my $rclass = $self->result_class; # DBIx::Class::IntrospectableM2M if ( $rclass->can('_m2m_metadata') ) { return $rclass->_m2m_metadata->{$relation}; } my $object = $self->new( {} ); if ( $object->can($relation) and !$self->result_source->has_relationship($relation) and $object->can( 'set_' . $relation ) ) { return 1; } return; } sub get_m2m_source { my ( $self, $relation ) = @_; my $rclass = $self->result_class; # DBIx::Class::IntrospectableM2M if ( $rclass->can('_m2m_metadata') ) { return $self->result_source->related_source( $rclass->_m2m_metadata->{$relation}{relation} ) ->related_source( $rclass->_m2m_metadata->{$relation}{foreign_relation} ); } my $object = $self->new( {} ); my $r = $object->$relation; return $r->result_source; } sub _delete_empty_auto_increment { my ( $self, $object ) = @_; for my $col ( keys %{ $object->{_column_data} } ) { if ( $object->result_source->column_info($col)->{is_auto_increment} and ( !defined $object->{_column_data}{$col} or $object->{_column_data}{$col} eq '' ) ) { delete $object->{_column_data}{$col}; } } } sub _get_pk_for_related { my ( $self, $relation ) = @_; my $result_source; if ( $self->result_source->has_relationship($relation) ) { $result_source = $self->result_source->related_source($relation); } # many to many case if ( $self->is_m2m($relation) ) { $result_source = $self->get_m2m_source($relation); } return $result_source->primary_columns; } sub _master_relation_cond { my ( $source, $cond, @foreign_ids ) = @_; my $foreign_ids_re = join '|', @foreign_ids; if ( ref $cond eq 'HASH' ) { for my $f_key ( keys %{$cond} ) { # might_have is not master my $col = $cond->{$f_key}; $col =~ s/self\.//; if ( $source->column_info($col)->{is_auto_increment} ) { return 0; } if ( $f_key =~ /^foreign\.$foreign_ids_re/ ) { return 1; } } } elsif ( ref $cond eq 'ARRAY' ) { for my $new_cond (@$cond) { return 1 if _master_relation_cond( $source, $new_cond, @foreign_ids ); } } return; } 1; # Magic true value required at end of module __END__ =head1 NAME DBIx::Class::ResultSet::RecursiveUpdate - like update_or_create - but recursive =head1 VERSION This document describes DBIx::Class::ResultSet::RecursiveUpdate version 0.001 =head1 SYNOPSIS __PACKAGE__->load_namespaces( default_resultset_class => '+DBIx::Class::ResultSet::RecursiveUpdate' ); in the Schema file (see t/lib/DBSchema.pm). Or appriopriate 'use base' in the ResultSet classes. Then: my $user = $user_rs->recursive_update( { id => 1, owned_dvds => [ { title => 'One Flew Over the Cuckoo's Nest' } ] } ); =head1 DESCRIPTION You can feed the ->create method with a recursive datastructure and have the related records created. Unfortunately you cannot do a similar thing with update_or_create - this module tries to fill that void. It is a base class for ResultSets providing just one method: recursive_update which works just like update_or_create but can recursively update or create data objects composed of multiple rows. All rows need to be identified by primary keys - so you need to provide them in the update structure (unless they can be deduced from the parent row - for example when you have a belongs_to relationship). If not all colums comprising the primary key are specified - then a new row will be created, with the expectation that the missing columns will be filled by it (as in the case of auto_increment primary keys). If the resultset itself stores an assignement for the primary key, like in the case of: my $restricted_rs = $user_rs->search( { id => 1 } ); then you need to specify that additional predicate as a second argument to the recursive_update method: my $user = $restricted_rs->recursive_update( { owned_dvds => [ { title => 'One Flew Over the Cuckoo's Nest' } ] }, { id => 1 } ); For a many_to_many (pseudo) relation you can supply a list of primary keys from the other table - and it will link the record at hand to those and only those records identified by them. This is convenient for handling web forms with check boxes (or a SELECT box with multiple choice) that let you update such (pseudo) relations. For a description how to set up base classes for ResultSets see load_namespaces in DBIx::Class::Schema. =head1 DESIGN CHOICES =head2 Treatment of many to many pseudo relations The function gets the information about m2m relations from DBIx::Class::IntrospectableM2M. If it is not loaded in the ResultSource classes - then the code relies on the fact that: if($object->can($name) and !$object->result_source->has_relationship($name) and $object->can( 'set_' . $name ) ) then $name must be a many to many pseudo relation. And that in a similarly ugly was I find out what is the ResultSource of objects from that many to many pseudo relation. =head1 INTERFACE =head1 METHODS =head2 recursive_update The method that does the work here. =head2 is_m2m $self->is_m2m( 'name ' ) - answers the question if 'name' is a many to many (pseudo) relation on $self. =head2 get_m2m_source $self->get_m2m_source( 'name' ) - returns the ResultSource linked to by the many to many (pseudo) relation 'name' from $self. =head1 DIAGNOSTICS =head1 CONFIGURATION AND ENVIRONMENT DBIx::Class::RecursiveUpdate requires no configuration files or environment variables. =head1 DEPENDENCIES DBIx::Class =head1 INCOMPATIBILITIES =for author to fill in: None reported. =head1 BUGS AND LIMITATIONS =for author to fill in: No bugs have been reported. Please report any bugs or feature requests to C, or through the web interface at L. =head1 AUTHOR Zbigniew Lukasiak C<< >> Influenced by code by Pedro Melo. =head1 LICENCE AND COPYRIGHT Copyright (c) 2008, Zbigniew Lukasiak C<< >>. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See L. =head1 DISCLAIMER OF WARRANTY BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. DBIx-Class-ResultSet-RecursiveUpdate-v0.001/README0000644000175000017500000000173611141005235017642 0ustar zbyzbyDBIx-Class-RecursiveUpdate version 0.0.1 [ REPLACE THIS... The README is used to introduce the module and provide instructions on how to install the module, any machine dependencies it may have (for example C compilers and installed libraries) and any other information that should be understood before the module is installed. A README file is required for CPAN modules since CPAN extracts the README file from a module distribution so that people browsing the archive can use it get an idea of the modules uses. It is usually a good idea to provide version information here so that people can decide whether fixes for the module are worth downloading. ] INSTALLATION To install this module, run the following commands: perl Makefile.PL make make test make install DEPENDENCIES None. COPYRIGHT AND LICENCE Copyright (C) 2008, Zbigniew Lukasiak This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. DBIx-Class-ResultSet-RecursiveUpdate-v0.001/Changes0000644000175000017500000000014711141005235020250 0ustar zbyzbyRevision history for DBIx-Class-RecursivePUT 0.0.1 Wed Jun 18 13:09:28 2008 Initial release. DBIx-Class-ResultSet-RecursiveUpdate-v0.001/Makefile.PL0000644000175000017500000000126311141005235020727 0ustar zbyzbyuse strict; use warnings; use ExtUtils::MakeMaker; WriteMakefile( NAME => 'DBIx::Class::ResultSet::RecursiveUpdate', AUTHOR => 'Zbigniew Lukasiak ', VERSION_FROM => 'lib/DBIx/Class/ResultSet/RecursiveUpdate.pm', ABSTRACT_FROM => 'lib/DBIx/Class/ResultSet/RecursiveUpdate.pm', PL_FILES => {}, PREREQ_PM => { 'Test::More' => 0, 'version' => 0, 'DBIx::Class' => 0, 'DBIx::Class::IntrospectableM2M' => 0, }, dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, clean => { FILES => 'DBIx-Class-ResultSet-RecursiveUpdate-*' }, ); DBIx-Class-ResultSet-RecursiveUpdate-v0.001/META.yml0000644000175000017500000000111311142104704020222 0ustar zbyzby--- #YAML:1.0 name: DBIx-Class-ResultSet-RecursiveUpdate version: v0.001 abstract: like update_or_create - but recursive license: ~ author: - Zbigniew Lukasiak generated_by: ExtUtils::MakeMaker version 6.40 distribution_type: module requires: DBIx::Class: 0 DBIx::Class::IntrospectableM2M: 0 Test::More: 0 version: 0 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.3.html version: 1.3 DBIx-Class-ResultSet-RecursiveUpdate-v0.001/MANIFEST0000644000175000017500000000123411142104127020105 0ustar zbyzbyChanges MANIFEST Makefile.PL README lib/DBIx/Class/ResultSet/RecursiveUpdate.pm t/00.load.t t/pod-coverage.t t/pod.t t/lib/MySchema/Test.pm t/lib/DBSchema.pm t/lib/RunTests.pm t/lib/DBSchema/Result/User.pm t/lib/DBSchema/Result/Dvd.pm t/lib/DBSchema/Result/Tag.pm t/lib/DBSchema/Result/LinerNotes.pm t/lib/DBSchema/Result/Dvdtag.pm t/lib/DBSchema/Result/UserRole.pm t/lib/DBSchema/Result/Role.pm t/lib/MySchema.pm t/var/dvdzbr.db t/pg.t t/sqlite.t META.yml Module meta-data (added by MakeMaker) t/lib/DBSchema/Result/Twokeys.pm t/lib/DBSchema/Result/Viewing.pm t/lib/DBSchema/Result/Onekey.pm t/lib/DBSchema/Result/Twokeys_belongsto.pm