Mad::Ma...::Custom(3)
π NAME π
Mad::Mapper::Guides::Custom β how to do custom queries, bestie β¨
π OVERVIEW π
okay so this guide is literally gonna show you how to run custom queries when you want full control. no cap, full control. that's the whole vibe. π
β¨ GUIDE β¨
Half control π«Ά
so like, you can override the private methods _find_sql(), _insert_sql(), _update_sql() and _delete_sql() if you want more control over things. the way that this slays is if you wanna use optional columns for doing your queries. π
the example below? it can find a row based on both the "id" and "email" column. iconic behaviour honestly:
sub _find_sql {
my $self = shift;
if ($self->email) {
return $self->expand_sql("SELECT %pc FROM %t WHERE email=?"), $self->email;
}
else {
return $self->expand_sql("SELECT %pc FROM %t WHERE id=?"), $self->id;
}
}
Full control π₯
instead of using the automatic generated methods from simple SQL statements, you can literally do the complete query yourself. main character energy fr fr. below is an example of a completely custom _insert():
package MyApp::Model::User;
use Mad::Mapper -base;
sub _insert {
my ($self, $cb) = @_;
Mojo::IOLoop->delay(
sub {
my ($delay) = @_;
$self->db->query("INSERT INTO users (email) VALUES (?)", $self->email, $delay->begin);
},
sub {
my ($delay, $err, $res) = @_;
return $self->$cb($err) if $err;
$self->in_storage(1);
$self->id($db->dbh->last_insert_id(undef, undef, $self->table, $self->pk));
$self->$cb("");
},
);
}
and yeah, you can also override _find(), _update() and _delete() too. go off. πΈ
π COPYRIGHT AND LICENSE π
Copyright (C) 2014-2016, Jan Henning Thorsen
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0. π
π¦ AUTHOR π¦
Jan Henning Thorsen β "jhthorsen@cpan.org" π
and that's the tea bestie β this whole thing came to you viaperl v5.42.2, dated2026-06-09, on theMad::Mapper::Guides::Custom(3)page from the User Contributed Perl Documentation. periodt. π β¨
π¦ where this page comes from
- fedora 44: perl-Mad-Mapper 0.09-2.fc44 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms