Improve recipe retrieval query

This commit is contained in:
Chris 2016-12-29 23:55:44 +00:00
parent c620310e77
commit 5a49a9e579

View File

@ -310,14 +310,24 @@ sub get_recipe {
$self -> clear_error(); $self -> clear_error();
my $query = "SELECT `r`.*,
`s`.`name` AS `status`,
`t`.`name` AS `type`,
`c`.`username` AS `creatoruser`, `c`,`realname` AS `creatorname`
`u`.`username` AS `updateuser`, `u`,`realname` AS `updatename`
FROM `".$self -> {"settings"} -> {"database"} -> {"recipes"}."` AS `r`
LEFT JOIN `".$self -> {"settings"} -> {"database"} -> {"states"}."` AS `s`
ON `s`.`id` = `r`.`status_id`
LEFT JOIN `".$self -> {"settings"} -> {"database"} -> {"types"}."` AS `t`
ON `t`.`id` = `r`.`type_id`
LEFT JOIN `".$self -> {"settings"} -> {"database"} -> {"users"}."` AS `c`
ON `c`.`user_id` = `r`.`creator_id`
LEFT JOIN `".$self -> {"settings"} -> {"database"} -> {"users"}."` AS `u`
ON `u`.`user_id` = `r`.`updater_id`
WHERE `r`.`id` = ?";
# Fetch the recipe itself, along with names of singular relations # Fetch the recipe itself, along with names of singular relations
my $recipeh = $self -> {"dbh"} -> prepare("SELECT `r`.*, `s`.`name` AS `status`, `t`.`name` AS `type`, `u`.`user_id`, `u`,`username`, `u`,`realname` my $recipeh = $self -> {"dbh"} -> prepare($query);
FROM `".$self -> {"settings"} -> {"database"} -> {"recipes"}."` AS `r`,
`".$self -> {"settings"} -> {"database"} -> {"states"}."` AS `s`,
`".$self -> {"settings"} -> {"database"} -> {"types"}."` AS `t`
WHERE `s`.`id` = `r`.`status_id`
AND `t`.`id` = `r`.`type_id`
AND `r`.`id` = ?");
$recipeh -> execute($recipeid) $recipeh -> execute($recipeid)
or return $self -> self_error("Unable to perform recipe lookup: ".$self -> {"dbh"} -> errstr); or return $self -> self_error("Unable to perform recipe lookup: ".$self -> {"dbh"} -> errstr);