[Dbix-class] how to add a method that performs a database lookup?

Hu Hailin i at h2l.name
Mon Dec 15 15:48:31 GMT 2008


you can write code like this:
---
Package OneTable;
use base 'DBIx::Class';

sub massive_fetching {
  my $self = shift;
  my $dbh = $self->result_source->storage->dbh;
  # then you can do whatever you want
}
---

and use it like this
---
my $a_file = $schema->resultset('OneTable')->find($id);
my $data = $a_file->massive_fetching;
---

hope this does help.

On Tue, Dec 16, 2008 at 12:13 AM, Adam Witney <awitney at sgul.ac.uk> wrote:
>>>
>>> I am new to DBIx::Class, but have so far been impressed with how easy it
>>> is to use, so thanks to those that have developed and continue to develop
>>> it.
>>>
>>> I wanted to know, is there a way of adding a method to a class that
>>> performs a separate database lookup. I have used the Cookbook example for
>>> "Arbitrary SQL through a custom ResultSource", but this returns a query that
>>> is plugged into a sub-select. Can i access the database connection handle at
>>> all (eg $dbh) to build my own SQL query?
>>>
>>> thanks for any help
>>>
>>> adam
>>>
>>>
>>
>> You can, but before I tell you how I'm going to ask what kind of query you
>> want. Give us SQL and we'll tell you if you can do it without going via the
>> raw DBH.
>
> ok i have several places i think I might need this!
>
> I have two tables containing data that originated from files; one table
> contains the filename, format etc and the other table contains the data
> itself. The data table contains about 100 million rows now and the data
> needs only be retrieved in chunks (about 5k-10k rows) corresponding to the
> whole file (one row of first table), although the required columns may very
> between queries. I was thinking that creating a class and hence an object
> per row would make this slower, so i thought i could write a method  that
> pulls back the whole data in one go for the main class/object.
>
> The second place i might need this is that i have some data in a second
> database and so i would have to interact with this using dblink (I am using
> PostgreSQL). This seems easier to just write the query in a method?
>
> Again thanks for your help and apologies if my approach is extremely daft!
> :-)
>
> adam
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive:
> http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk
>



-- 
islue



More information about the DBIx-Class mailing list