The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Mojo::SQLite::Results - Results

SYNOPSIS

  use Mojo::SQLite::Results;

  my $results = Mojo::SQLite::Results->new(sth => $sth);
  $results->hashes->map(sub { $_->{foo} })->shuffle->join("\n")->say;

DESCRIPTION

Mojo::SQLite::Results is a container for DBD::SQLite statement handles used by Mojo::SQLite::Database.

ATTRIBUTES

Mojo::SQLite::Results implements the following attributes.

db

  my $db   = $results->db;
  $results = $results->db(Mojo::SQLite::Database->new);

Mojo::SQLite::Database object these results belong to.

sth

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

DBD::SQLite statement handle results are fetched from.

METHODS

Mojo::SQLite::Results inherits all methods from Mojo::Base and implements the following new ones.

new

  my $results = Mojo::SQLite::Results->new;
  my $results = Mojo::SQLite::Results->new(sth => $sth);
  my $results = Mojo::SQLite::Results->new({sth => $sth});

Construct a new Mojo::SQLite::Results object.

array

  my $array = $results->array;

Fetch next row from "sth" and return it as an array reference. Note that "finish" needs to be called if you are not fetching all the possible rows.

  # Process one row at a time
  while (my $next = $results->array) {
    say $next->[3];
  }

arrays

  my $collection = $results->arrays;

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

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

columns

  my $columns = $results->columns;

Return column names as an array reference.

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

expand

  $results = $results->expand(json => 'some_json');
  $results = $results->expand(json => ['some_json','other_json']);

Decode specified fields from a particular format to Perl values for all rows. Currently only the json text format is recognized. The names must exactly match the column names as returned by "columns"; it is recommended to use explicit aliases in the query for consistent column names.

  # Expand JSON
  $results->expand(json => 'json_field')->hashes->map(sub { $_->{foo}{bar} })->join("\n")->say;

finish

  $results->finish;

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

hash

  my $hash = $results->hash;

Fetch next row from "sth" and return it as a hash reference. Note that "finish" needs to be called if you are not fetching all the possible rows.

  # Process one row at a time
  while (my $next = $results->hash) {
    say $next->{money};
  }

hashes

  my $collection = $results->hashes;

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

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

last_insert_id

  my $id = $results->last_insert_id;

Returns the rowid of the most recent successful INSERT.

rows

  my $num = $results->rows;

Number of rows. Note that for SELECT statements, this count will not be accurate until all rows have been fetched.

text

  my $text = $results->text;

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

BUGS

Report any issues on the public bugtracker.

AUTHOR

Dan Book, dbook@cpan.org

COPYRIGHT AND LICENSE

Copyright 2015, Dan Book.

This library is free software; you may redistribute it and/or modify it under the terms of the Artistic License version 2.0.

SEE ALSO

Mojo::SQLite