Questions tagged [dbix-class]

DBIx::Class is a Perl object-relational mapping (ORM) module. It aims to make representing queries in your code as perl-ish as possible while still providing access to as many of the capabilities of the database as possible, including retrieving related records from multiple tables in a single query, JOIN, LEFT JOIN, COUNT, DISTINCT, GROUP BY, ORDER BY and HAVING support.

from the description on CPAN:

This is an SQL to OO mapper with an object API inspired by Class::DBI (with a compatibility layer as a springboard for porting) and a resultset API that allows abstract encapsulation of database operations. It aims to make representing queries in your code as perl-ish as possible while still providing access to as many of the capabilities of the database as possible, including retrieving related records from multiple tables in a single query, JOIN, LEFT JOIN, COUNT, DISTINCT, GROUP BY, ORDER BY and HAVING support.

DBIx::Class can handle multi-column primary and foreign keys, complex queries and database-level paging, and does its best to only query the database in order to return something you've directly asked for. If a resultset is used as an iterator it only fetches rows off the statement handle as requested in order to minimise memory usage. It has auto-increment support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is known to be used in production on at least the first four, and is fork- and thread-safe out of the box (although your DBD may not be).

377 questions
23
votes
3 answers

Can I pretty-print the DBIC_TRACE output in DBIx::Class?

Setting the DBIC_TRACE environment variable to true: BEGIN { $ENV{DBIC_TRACE} = 1 } generates very helpful output, especially showing the SQL query that is being executed, but the SQL query is all on one line. Is there a way to push it through some…
mintywalker
  • 1,585
  • 3
  • 11
  • 13
14
votes
6 answers

How do I make DBIx::Class join tables using other operators than `=`?

Summary I've got a table of items that go in pairs. I'd like to self-join it so I can retrieve both sides of the pair in a single query. It's valid SQL (I think), the SQLite engine actually does accept it, but I'm having trouble getting…
JB.
  • 40,344
  • 12
  • 79
  • 106
10
votes
3 answers

How do I handle errors in methods chains in Perl?

What is the best way to deal with exceptions threw in a method chaining in Perl? I want to assign a value of 0 or undef if any of the methods chained throw an exception Code sample: my $x =…
nsbm
  • 5,842
  • 6
  • 30
  • 45
10
votes
4 answers

What's the right way to display a DBIx::Class ResultSet in my Catalyst project that uses Template Toolkit?

Given a DBIx::Class resultset, for example: my $rs = $c->model("DB::Card")->search({family_name => "Smith"}); the tutorials I've read use the stash to pass an arrayref of rows: $c->stash->{cards} = [$rs->all]; This results in the query getting…
Thelema
  • 14,257
  • 6
  • 27
  • 35
9
votes
3 answers

What is the DBIx::Class syntax for the CASE WHEN ... THEN SQL syntax?

Does anyone know what's the DBIx::Class equivalent of an SQL query such as: SELECT cdr_id, CASE WHEN service_id = 'GPRS' THEN 'KB' WHEN service_id = 'SMS' THEN 'SMS' END AS unit FROM ... Thanks
galli2000
  • 478
  • 5
  • 14
9
votes
2 answers

Does DBIx::Class do unions?

I haven't found a way to do unions with DBIx::Class other than using a view and writing out the SQL manually. This seems strange to me. I feel like there should be some way to union two ResultSets without a lot of extra work because set addition…
Eric Johnson
  • 17,502
  • 10
  • 52
  • 59
9
votes
1 answer

Moose method modifiers on DBIx::Class::Schema models in Catalyst

For any given result class MySchema::Result::Foo (built from default schema loader generated syntax which uses Moose/MooseX::nonmoose) If I add a BUILDARGS method wrapper to sanitize the constructor data for a row like so: package…
nebulous
  • 738
  • 6
  • 17
9
votes
2 answers

Can DBIx::Class be used to create tables?

I do not think I understand the scope of DBIx::Class Do I have to manually create a database with regular SQL first, then use the schemaloader (or manually code the schema/resultsets)? Or is there a way to tell DBIx::Class to go ahead and create the…
9
votes
4 answers

Does DBIx::Class have transparent caching?

In the C#/.Net world, there are ORMs such as NHibernate or ActiveRecord that includes transparent caching: database updates are transparently replicated to the cache, objects are retrieved directly from the cache when available, etc (often with…
Julien
  • 5,729
  • 4
  • 37
  • 60
8
votes
2 answers

How do I make a DBIx::Class relationship with a fixed join condition?

We have a link table that can handle multiple types of object on one side, and I can't work out how to get from one of these objects to the link table using has_many. Example: link table contains: id link_id link_table resource_id 1 1 page …
Cebjyre
  • 6,552
  • 3
  • 32
  • 57
8
votes
3 answers

How do I figure out what module is loading Moose?

I am trying to figure out which module in my CGI::Application is loading Moose. I attempted to overload "require" but I don't seem to have the syntax quite right. If someone could clean up the following code I would appreciate it: use strict; use…
Jeffrey Fuller
  • 300
  • 1
  • 2
  • 8
8
votes
1 answer

How can I get the class name for a table in DBIx::Class

Finding the table name for a class in DBIx::Class is straightforward, like this my $s = DBIx::Class::Schema::Loader->connect('dbi:SQLite:foo.db'); $s->class($class_name)->table; but how can I do the opposite, and get the class name from the name of…
simone
  • 4,667
  • 4
  • 25
  • 47
8
votes
3 answers

How can I tidy DBIx::Class::Schema::Loader's output?

We are currently introducing DBIx::Class in our team and we would like to start out with DBIx::Class::Schema::Loader. However, we have hard requirements on code style, i.e. we've got Perl::Tidy as part of our pre-commit script, since we haven't had…
Nikolai Prokoschenko
  • 8,465
  • 11
  • 58
  • 97
7
votes
1 answer

DBIx::Class - get all relationship that was used as a condition using prefetch?

Here are three tables: product, model, and product_model that maps products and models in N:M relationship. product product_model model id name product_id model_id id name ------------ ------------------- …
gypark
  • 612
  • 7
  • 26
7
votes
2 answers

How can I filter DBIX::Class resultsets with external data?

Using DBIx::Class and I have a resultset which needs to be filtered by data which cannot be generated by SQL. What I need to do is something effectively equivalent to this hypothetical example: my $resultset =…
Ovid
  • 11,580
  • 9
  • 46
  • 76
1
2 3
25 26