Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/grinnz/mojo-db-results-role-struct
- Owner: Grinnz
- License: other
- Created: 2019-09-20T17:58:21.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-06-17T02:58:04.000Z (over 3 years ago)
- Last Synced: 2024-11-13T05:42:02.991Z (about 2 months ago)
- Language: Perl
- Homepage: https://metacpan.org/pod/Mojo::DB::Results::Role::Struct
- Size: 40 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.pod
- Changelog: Changes
- Contributing: CONTRIBUTING.md
- License: LICENSE
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" existsuse 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