my $statement = $datasource->createStatement;
$statement->refine(...);
$statement->prepare(...);
$statement->execute(...);
while (my $row = $statement->next) {...}
where $datasource can be a Table or a View. In your case, define a View
with your special SQL, and then use a statement as shown above. The doc
is in DBIx::DataModel::Doc::Design/"STATEMENT OBJECTS". If needed you
can access the underlying $dbh handle at the DBI level.
use strict;
use warnings;
use DBIx::DataModel;
my $with_clause = <<"";
WITH RECURSIVE nodetree(level, id, pid, sort) AS (
SELECT 1, id, parent, '{1}'::int[] FROM nodes WHERE parent IS NULL
UNION
SELECT level+1,p.id, parent, sort||p.id FROM nodetree pr
JOIN nodes p ON p.parent = pr.id
)
DBIx::DataModel->Schema('Tst')
->Table(qw/Nodetree nodetree id/);
my $stmt = Tst::Nodetree->createStatement(
-postSQL => sub {my $sql = shift; $sql =~ s/^/$with_clause/; return
$sql, @_ },
-orderBy => 'sort',
);
$stmt->sqlize;
print join "\n", "SQL IS:", $stmt->sql;
# now you can do : $stmt->prepare; $stmt->execute;