summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcos <cos>2009-09-05 21:06:55 +0200
committercos <cos>2009-09-05 21:06:55 +0200
commit137dc9f4d36acdcc0b3f207383e1920f0e388c96 (patch)
tree8ead4793b1bcd9bc52f59e1b63bc2d9b1976636d
downloadmat-137dc9f4d36acdcc0b3f207383e1920f0e388c96.zip
Initial version.
-rwxr-xr-xgendump2
-rwxr-xr-xmat91
2 files changed, 93 insertions, 0 deletions
diff --git a/gendump b/gendump
new file mode 100755
index 0000000..13caab3
--- /dev/null
+++ b/gendump
@@ -0,0 +1,2 @@
+#!/bin/sh
+echo '.dump'|sqlite3 recipe.db > recipe_dump_`date +%Y%m%d`.txt
diff --git a/mat b/mat
new file mode 100755
index 0000000..e1de21d
--- /dev/null
+++ b/mat
@@ -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";
+ }
+}