Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/grinnz/mojo-db-results-role-struct

Mojo::DB::Results::Role::Struct - Database query results as structs
https://github.com/grinnz/mojo-db-results-role-struct

Last synced: 5 days ago
JSON representation

Mojo::DB::Results::Role::Struct - Database query results as structs

Awesome Lists containing this project

README

        

=pod

=head1 NAME

Mojo::DB::Results::Role::Struct - Database query results as structs

=head1 SYNOPSIS

use Mojo::SQLite;
my $sqlite = Mojo::SQLite->new(...);
my $results = $sqlite->db->query('SELECT * FROM "table" WHERE "foo" = ?', 42);
my $struct = $results->with_roles('Mojo::DB::Results::Role::Struct')->structs->first;
my $bar = $struct->bar; # dies unless column "bar" exists

use Mojo::Pg;
my $pg = Mojo::Pg->new(...)->with_roles('Mojo::DB::Role::ResultsRoles');
push @{$pg->results_roles}, 'Mojo::DB::Results::Role::Struct';
my $results = $pg->db->query('SELECT "foo", "bar" FROM "table"');
foreach my $row ($results->structs->each) {
my $foo = $row->foo;
my $bar = $row->baz; # dies
}

=head1 DESCRIPTION

This role can be applied to a results object for L or similar
database APIs. It provides L"struct"> and L"structs"> methods which return
L records, providing read-only accessors only for the expected
column names. Note that a column name that is not a valid identifier is
trickier to access in this manner.

my $row = $results->struct;
my $col_name = 'foo.bar';
my $val = $row->$col_name;
# or
my $val = $row->${\'foo.bar'};

You can apply the role to a results object using L,
or apply it to all results objects created by a database manager using
L as shown in the synopsis.

=head1 METHODS

L composes the following methods.

=head2 struct

my $struct = $results->struct;

Fetch next row from the statement handle with the result object's C
method, and return it as a struct.

=head2 structs

my $collection = $results->structs;

Fetch all rows from the statement handle with the result object's C
method, and return them as a L object containing structs.

=head1 BUGS

Report any issues on the public bugtracker.

=head1 AUTHOR

Dan Book

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2019 by Dan Book.

This is free software, licensed under:

The Artistic License 2.0 (GPL Compatible)

=head1 SEE ALSO

L, L, L, L

=cut