diff options
author | cos <cos> | 2009-09-05 21:06:55 +0200 |
---|---|---|
committer | cos <cos> | 2009-09-05 21:06:55 +0200 |
commit | 137dc9f4d36acdcc0b3f207383e1920f0e388c96 (patch) | |
tree | 8ead4793b1bcd9bc52f59e1b63bc2d9b1976636d | |
download | mat-137dc9f4d36acdcc0b3f207383e1920f0e388c96.zip |
Initial version.
-rwxr-xr-x | gendump | 2 | ||||
-rwxr-xr-x | mat | 91 |
2 files changed, 93 insertions, 0 deletions
@@ -0,0 +1,2 @@ +#!/bin/sh +echo '.dump'|sqlite3 recipe.db > recipe_dump_`date +%Y%m%d`.txt @@ -0,0 +1,91 @@ +#!/usr/bin/perl + +use warnings; +use strict; + +use DBI; + +my $db = DBI->connect("dbi:SQLite:recipe.db", "", "", {RaiseError => 1, + AutoCommit => 1}); + +my @recipes; +my @ingredients; +my @contents; + +sub read_recipe_db { + # All recipes needs to be read to be able to pick random recipes easily + # enough. + my $all = $db->selectall_arrayref("SELECT * FROM recipes"); + foreach my $row (@$all) { + my ($id) = @$row; + $recipes[$id]->{'name'} = @$row[1]; + $recipes[$id]->{'uri'} = @$row[2]; + } + + # This is not how to do the ingredient and content reading! + # They should only be read for relevant recipes. + $all = $db->selectall_arrayref("SELECT * FROM ingredients"); + foreach my $row (@$all) { + my ($id) = @$row; + $ingredients[$id]->{'id'} = @$row[1]; + $ingredients[$id]->{'name'} = @$row[2]; + } + $all = $db->selectall_arrayref("SELECT * FROM contents"); + foreach my $row (@$all) { + my ($id) = @$row; + $contents[$id]->{'recipe_id'} = @$row[1]; + $contents[$id]->{'ingredient_id'} = @$row[2]; + $contents[$id]->{'quantity'} = @$row[2]; + $contents[$id]->{'unit'} = @$row[2]; + } +} + +sub get_random_recipe { + return int(rand(@recipes)); +} + +sub get_recipie_name ($) { + my ( $id ) = @_; + + if ($recipes[$id]->{'name'}) { + return $recipes[$id]->{'name'}; + } else { + return "NULL"; + } +} + +my @schedule = ( + { + day => "Måndag", + }, + { + day => "Tisdag", + }, + { + day => "Onsdag", + }, + { + day => "Torsdag", + }, + { + day => "Fredag", + } +); + +# MAIN PROGRAM ################################################################ + +read_recipe_db; + +for (my $i = 0; $i < 5; $i++) { + $schedule[$i]->{'recipe'} = get_random_recipe; +} + +for (my $i = 0; $i < 5; $i++) { + print $schedule[$i]->{'day'}.": "; + print get_recipie_name($schedule[$i]->{'recipe'})."\n"; + my $contents = $db->selectall_arrayref("SELECT * FROM contents WHERE ". + "recipe_id=".$schedule[$i]->{'recipe'}); + unless (@$contents) { + print "OBSERVERA: Ingredienslista saknas för denna rätt!\n\n"; + } +} |