Mad::Ma...::Custom(3)

πŸ“¦ version 0.09, in fedora 44

πŸ’– 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 (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 via perl v5.42.2, dated 2026-06-09, on the Mad::Mapper::Guides::Custom(3) page from the User Contributed Perl Documentation. periodt. πŸ’…βœ¨
πŸ“¦ where this page comes from