Mojo::...Results(3pm)

πŸ’… NAME πŸ’…

okay so welcome to the Mojo::SQLite::Results(3pm) page, part of the User Contributed Perl Documentation situation. The NAME is literally Mojo::SQLite::Results β€” Results. That's it. That's the whole name. πŸ’–

✨ SYNOPSIS ✨

bestie, here's the vibe in code form:

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. That's the whole personality: it holds the statement-handle situation so the database can do its thing. ✨ main character energy ✨

🫢 ATTRIBUTES 🫢

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

πŸ¦‹ db πŸ¦‹

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

The Mojo::SQLite::Database object these results belong to. It's giving "whose results are these?" and the answer is this db. πŸ‘‘

πŸ¦‹ sth πŸ¦‹

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

The DBD::SQLite statement handle results are fetched from. the source of the rows, fr fr. πŸ“

⭐ METHODS ⭐

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

🌟 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. fresh object, fresh vibes. πŸ’…

🌟 array 🌟

my $array = $results->array;

Fetch next row from sth and return it as an array reference.

⚠️ note: finish needs to be called if you are not fetching all the possible rows. don't leave the sth hanging, bestie.
# 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. iykyk, exact match only. πŸŽ€

# 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. closure, but for statement handles. periodt. πŸ’€

🌟 hash 🌟

my $hash = $results->hash;

Fetch next row from sth and return it as a hash reference.

⚠️ note: finish needs to be called if you are not fetching all the possible rows. same rule, bestie.
# 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 (https://www.sqlite.org/c3ref/last_insert_rowid.html) of the most recent successful INSERT. that's the id of the newest successful insert, no cap. πŸ”₯

🌟 rows 🌟

my $num = $results->rows;

Number of rows.

⚠️ note: for SELECT statements, this count will not be accurate until all rows have been fetched. so don't trust the count too early, bestie. πŸ™ˆ

🌟 text 🌟

my $text = $results->text;

Fetch all rows from sth and turn them into a table with tablify in Mojo::Util. cute little text table moment. 🌈

πŸ› BUGS πŸ›

Report any issues on the public bugtracker. if something's cursed, that's where it goes. vibe check. πŸ’œ

πŸ‘‘ AUTHOR πŸ‘‘

Dan Book, dbook@cpan.org. iconic. πŸ’…

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. go read that too, bestie. 🫢


later, bestie β€” this was Mojo::SQLite::Results(3pm) from perl v5.42.0, dated 2022-03-23. stay slaying. ✨

πŸ“¦ where this page comes from