dreel has asked for the wisdom of the Perl Monks concerning the following question:
But it dosen't work! I produce my class MyClass.pm But without __PACKAGE__->table('fake_table'); It wont startup and says: Can't locate object method "result_source_instance" via package "My::Schema::MyClass" What is it? Wrong example in cookbook!^??? As i see there is no method to exec custom RAW SQL trough DBIx-Class. For executing raw sql cookbook give us a method with little hack - author annotate us generated query will be paste in "SELECT FROM __YOUR_SQL___ " In my case this is not working because MSSQL Sever don't understand this and it always will be mistaken query for the server. What about it? What do you think? As an alternative way I'm using combination of DBI for call Stored proc saving results in table and ResultSorce to represent table via DBIx. Maybe we can enhance DBIx to executing RAW QUERYS?package My::Schema::User; use base qw/DBIx::Class/; # ->load_components, ->table, ->add_columns, etc. # Make a new ResultSource based on the User class my $source = __PACKAGE__->result_source_instance(); my $new_source = $source->new( $source ); $new_source->source_name( 'UserFriendsComplex' ); # Hand in your query as a scalar reference # It will be added as a sub-select after FROM, # so pay attention to the surrounding brackets! $new_source->name( \<<SQL ); ( SELECT u.* FROM user u INNER JOIN user_friends f ON u.id = f.user_id WHERE f.friend_user_id = ? UNION SELECT u.* FROM user u INNER JOIN user_friends f ON u.id = f.friend_user_id WHERE f.user_id = ? ) SQL # Finally, register your new ResultSource with your Schema My::Schema->register_source( 'UserFriendsComplex' => $new_source ); Next, you can execute your complex query using bind parameters like th +is: my $friends = [ $schema->resultset( 'UserFriendsComplex' )->search( +{}, { bind => [ 12345, 12345 ] } ) ]; ... and you'll get back a perfect L<DBIx::Class::ResultSet>.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: [DBIX::Class] problem with Arbitrary SQL through a custom ResultSource
by semifor (Initiate) on Aug 22, 2007 at 15:22 UTC | |
by selkovjr (Initiate) on Dec 20, 2007 at 18:02 UTC | |
by Tommy (Chaplain) on Dec 16, 2009 at 16:51 UTC | |
by Anonymous Monk on Mar 26, 2009 at 15:37 UTC | |
by dreel (Sexton) on Aug 24, 2007 at 07:34 UTC |