-
-
16 Jun 2021 03:58:26 UTC
- Distribution: Mojo-DB-Results-Role-Struct
- Module version: v0.1.1
- Source (raw)
- Browse (raw)
- Changes
- Homepage
- How to Contribute
- Repository
- Issues (0)
- Testers
- Kwalitee
Bus factor: 1- 100.00% Coverage
- License: artistic_2
- Perl: v5.10.1
- Activity
24 month- Tools
- Download (13.32KB)
- MetaCPAN Explorer
- Permissions
- Subscribe to distribution
- Permalinks
- This version
- Latest version
++ed by:1 non-PAUSE userNAME
Mojo::DB::Results::Role::Struct - Database query results as structs
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 }
DESCRIPTION
This role can be applied to a results object for Mojo::Pg or similar database APIs. It provides "struct" and "structs" methods which return Struct::Dumb 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 "with_roles" in Mojo::Base, or apply it to all results objects created by a database manager using Mojo::DB::Role::ResultsRoles as shown in the synopsis.
METHODS
Mojo::DB::Results::Role::Struct composes the following methods.
struct
my $struct = $results->struct;
Fetch next row from the statement handle with the result object's
array
method, and return it as a struct.structs
my $collection = $results->structs;
Fetch all rows from the statement handle with the result object's
arrays
method, and return them as a Mojo::Collection object containing structs.BUGS
Report any issues on the public bugtracker.
AUTHOR
Dan Book <dbook@cpan.org>
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)
SEE ALSO
Mojo::DB::Role::ResultsRoles, Mojo::Pg, Mojo::SQLite, Mojo::mysql
Module Install Instructions
To install Mojo::DB::Results::Role::Struct, copy and paste the appropriate command in to your terminal.
cpanm Mojo::DB::Results::Role::Struct
perl -MCPAN -e shell install Mojo::DB::Results::Role::Struct
For more information on module installation, please visit the detailed CPAN module installation guide.