NAME

Acrux::DBI::Res - Results of your database queries

SYNOPSIS

use Acrux::DBI::Res;

my $res = Acrux::DBI::Res->new(sth => $sth);

$res->collection->map(sub { $_->{foo} })->shuffle->join("\n")->say;

DESCRIPTION

Class to works with results of your database queries

new

my $res = Acrux::DBI::Res->new( sth => $sth, dbi => $dbi );

Construct a new Acrux::DBI::Res object

ATTRIBUTES

This method implements the following attributes

dbi

my $dbi = $res->dbi;
$res = $res->dbi(Acrux::DBI->new);

Acrux::DBI object these results belong to.

sth

my $sth  = $res->sth;
$res = $res->sth($sth);

Acrux::DBI statement handle results are fetched from

METHODS

This class implements the following methods

affected_rows

my $affected = $res->affected_rows;

Number of affected rows by the query. For example

UPDATE testtable SET id = 1 WHERE id = 1

would return 1

array

my $array = $res->array;

Fetch one row from "sth" and return it as an array reference

# [
#   'foo', 'bar', 'baz'
# ]

See also "record" in CTK::DBI

arrays

my $arrays = $res->arrays;

Fetch all rows from "sth" and return them as an array of arrays

# [
#   [ 'foo', 'bar', 'baz' ],
#   [ 'qux', 'quux' ],
# ]

See also "table" in CTK::DBI

collection

my $collection = $res->collection;

Fetch all rows from "sth" and return them as a Mojo::Collection object containing hash references

# Process all rows at once
say $res->collection->reduce(sub { $a + $b->{money} }, 0);

collection_list

my $collection_list = $res->collection_list;

Fetch all rows from "sth" and return them as a Mojo::Collection object containing array references

# Process all rows at once
say $res->collection_list->reduce(sub { $a + $b->[3] }, 0);

columns

my $columns = $res->columns;

Return column names as an array reference

# Names of all columns
say for @{$res->columns};

err

my $err = $res->err;

Error code received

errstr

my $errstr = $res->errstr;

Error message received

finish

$res->finish;

Indicate that you are finished with "sth" and will not be fetching all the remaining rows

hash

my $hash = $res->hash;

Fetch one row from "sth" and return it as a hash reference

# {
#   'foo' => 1,
#   'bar' => 'one',
# }

See also "recordh" in CTK::DBI

hashed_by

my $hash = $res->hashed_by( $key_field );
my $hash = $res->hashed_by( 'id' );

This method returns a reference to a hash containing a key for each distinct value of the $key_field column that was fetched. For each key the corresponding value is a reference to a hash containing all the selected columns and their values, as returned by fetchrow_hashref()

For example:

my $hash = $res->hashed_by( 'id' );

# {
#   1 => {
#      'id'   => 1,
#      'name' => 'foo'
#   },
#   2 => {
#      'id'   => 2,
#      'name' => 'bar'
#   }
# }

See "fetchall_hashref" in DBI for details

See also "tableh" in CTK::DBI

hashes

my $hashes = $res->hashes;

Fetch all rows from "sth" and return them as an array containing hash references

# [
#   {
#      'id'   => 1,
#      'name' => 'foo'
#   },
#   {
#      'id'   => 2,
#      'name' => 'bar'
#   }
# ]

last_insert_id

my $last_id = $res->last_insert_id;

That value of AUTO_INCREMENT column if executed query was INSERT in a table with AUTO_INCREMENT column

more_results

do {
  my $columns = $res->columns;
  my $arrays = $res->arrays;
} while ($res->more_results);

Handle multiple results

rows

my $num = $res->rows;

Number of rows

state

my $state = $res->state;

Error state received

text

my $text = $res->text;

Fetch all rows from "sth" and turn them into a table with "tablify" in Mojo::Util.

HISTORY

See Changes file

TO DO

See TODO file

SEE ALSO

Mojo::mysql, Mojo::Pg, Mojo::DB::Connector, CTK::DBI, DBI

AUTHOR

Serż Minus (Sergey Lepenkov) https://www.serzik.com <abalama@cpan.org>

COPYRIGHT

Copyright (C) 1998-2024 D&D Corporation. All Rights Reserved

LICENSE

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See LICENSE file and https://dev.perl.org/licenses/