/extremedating/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/extremedating/trunk

« back to all changes in this revision

Viewing changes to php/db.php

  • Committer: Gustav Hatvigsson
  • Date: 2013-04-13 12:48:33 UTC
  • mfrom: (9.1.2 extremedating)
  • Revision ID: gustav.hartvigsson@gmail.com-20130413124833-hicxuqfdhae3bdv6
Merged stuff

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
    ExtremeDating - a Hackathon 2013 project.
 
4
    Copyright (C) 2013 Gustav Hartvigsson <gustav.hartvigsson@gmail.com>
 
5
    Copyright (C) 2013 Daniel Johansson <maila@danieljohansson.nu>
 
6
 
 
7
 
 
8
    This program is free software: you can redistribute it and/or modify
 
9
    it under the terms of the GNU Affero General Public License as
 
10
    published by the Free Software Foundation, either version 3 of the
 
11
    License, or (at your option) any later version.
 
12
 
 
13
    This program is distributed in the hope that it will be useful,
 
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
    GNU Affero General Public License for more details.
 
17
 
 
18
    You should have received a copy of the GNU Affero General Public License
 
19
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
 
 
21
 */
 
22
$db = new PDO("sqlite:./database.db");
 
23
 
 
24
$db->setAttribute(PDO::ATTR_ERRMODE, 
 
25
                          PDO::ERRMODE_EXCEPTION);
 
26
 
 
27
/*
 
28
Database design:
 
29
Users(_loginName_, shadow, firstName, surName, city, eMail);
 
30
Resturants(_id_, name, city, phoneNumber, theme);
 
31
# Theme is if it is, say, a pizzeria or a french resturant or something.
 
32
Interests(_interest_);
 
33
FoodType(_foodType_);
 
34
UserInterests(-loginName-, -interest-);
 
35
UserFoodTypes(-loginName-, -foodType-);
 
36
*/
 
37
 
 
38
// Create User table
 
39
$db->exec("
 
40
  CREATE TABLE IF NOT EXISTS Users(
 
41
  loginName varchar(64) PRIMARY KEY,
 
42
  shadow varchar(64) NOT NULL,
 
43
  firstName varchar(64) NOT NULL,
 
44
  surName varchar(64) NOT NULL,
 
45
  city varchar(64) NOT NULL,
 
46
  eMail varchar(64) NOT NULL,
 
47
  profileImage blob,
 
48
  UNIQUE (loginName)
 
49
  );
 
50
");
 
51
 
 
52
//Create Resturasts tabel
 
53
$db->exec("
 
54
  CREATE TABLE IF NOT EXISTS Resturants(
 
55
  id integer PRIMARY KEY,
 
56
  name varchar(64) NOT NULL,
 
57
  city varchar(64) NOT NULL,
 
58
  phoneNumber varchar(64) NOT NULL,
 
59
  theme varchar(64)
 
60
  );
 
61
");
 
62
 
 
63
//Create Interests Table - This table stores the intrests a user may have.
 
64
$db->exec("
 
65
  CREATE TABLE IF NOT EXISTS Interests(
 
66
  interest varchar(64) PRIMARY KEY
 
67
  );
 
68
");
 
69
 
 
70
//Create FoodTypes table - this table stores the different types of food thet 
 
71
// exist.
 
72
$db->exec("
 
73
  CREATE TABLE IF NOT EXISTS FoodTypes(
 
74
  foodType varchar(64) PRIMARY KEY
 
75
  );
 
76
");
 
77
 
 
78
try {
 
79
  $db->beginTransaction();
 
80
  // Relations
 
81
  $db->exec("
 
82
    CREATE TABLE IF NOT EXISTS UserIntrests(
 
83
    loginName varchar(64),
 
84
    interest varchar(64),
 
85
    FOREIGN KEY(loginName) REFERENCES Users(loginName),
 
86
    FOREIGN KEY(interest) REFERENCES Interests(interest),
 
87
    PRIMARY KEY(loginName, interest)
 
88
    );
 
89
  ");
 
90
  
 
91
  
 
92
  $db->exec("
 
93
    CREATE TABLE IF NOT EXISTS UserFoodTypes(
 
94
    loginName varchar(64),
 
95
    foodType varchar(64),
 
96
    FOREIGN KEY(loginName) REFERENCES Users(loginName),
 
97
    FOREIGN KEY(foodType) REFERENCES FoodTypes(foodType),
 
98
    PRIMARY KEY(loginName, foodType)
 
99
    );
 
100
  "); 
 
101
} catch (PDOException $err) {
 
102
  echo $err;
 
103
}
 
104
$db->commit();
 
105
 
 
106
?>