/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 Hartvigsson
  • Date: 2013-04-15 17:15:56 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20130415171556-q5zcc5mdle93vm1q
code cleanup

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, profileImage, userType);
 
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
  userType INTEGER,
 
49
  UNIQUE (loginName)
 
50
  );
 
51
");
 
52
 
 
53
//Create Resturants tabel
 
54
$db->exec("
 
55
  CREATE TABLE IF NOT EXISTS Resturants(
 
56
  id integer PRIMARY KEY,
 
57
  name varchar(64) NOT NULL,
 
58
  city varchar(64) NOT NULL,
 
59
  phoneNumber varchar(64) NOT NULL,
 
60
  theme varchar(64)
 
61
  );
 
62
");
 
63
 
 
64
//Create Interests Table - This table stores the intrests a user may have.
 
65
$db->exec("
 
66
  CREATE TABLE IF NOT EXISTS Interests(
 
67
  interest varchar(64) PRIMARY KEY
 
68
  );
 
69
");
 
70
 
 
71
//Create FoodTypes table - this table stores the different types of food thet 
 
72
// exist.
 
73
$db->exec("
 
74
  CREATE TABLE IF NOT EXISTS FoodTypes(
 
75
  foodType varchar(64) PRIMARY KEY
 
76
  );
 
77
");
 
78
 
 
79
//Populate foodTypes
 
80
$db->beginTransaction();
 
81
try {
 
82
  $arr = array('husman','kina','thai','mex','italienskt','franskt','grekisk',
 
83
  'fika','efterrätter');
 
84
  foreach ($arr as $i) {
 
85
    //echo $i.'\n';
 
86
    $query = "
 
87
       INSERT OR IGNORE INTO FoodTypes VALUES('{$i}');
 
88
    ";
 
89
    $db->exec($query);
 
90
  }
 
91
  $db->commit();
 
92
  sleep(.5);
 
93
} catch (PDOException $err) {
 
94
  var_dump($err);
 
95
  echo "\n";
 
96
}
 
97
 
 
98
try {
 
99
  $db->beginTransaction();
 
100
  $arr = array('php','sql','GNU/Linux','Nix','HP','mat','3d','hen-gris');
 
101
  foreach ($arr as $i) {
 
102
    //echo $i.'\n';
 
103
    $query = "
 
104
       INSERT OR IGNORE INTO Interests VALUES('{$i}');
 
105
    ";
 
106
    $db->exec($query);
 
107
  }
 
108
  $db->commit();
 
109
  sleep(.5);
 
110
} catch (PDOException $err) {
 
111
  var_dump($err);
 
112
}
 
113
 
 
114
 
 
115
try {
 
116
  $db->beginTransaction();
 
117
  // Relations
 
118
  $db->exec("
 
119
    CREATE TABLE IF NOT EXISTS UserIntrests(
 
120
    loginName varchar(64),
 
121
    interest varchar(64),
 
122
    FOREIGN KEY(loginName) REFERENCES Users(loginName),
 
123
    FOREIGN KEY(interest) REFERENCES Interests(interest),
 
124
    PRIMARY KEY(loginName, interest)
 
125
    );
 
126
  ");
 
127
  
 
128
  
 
129
  $db->exec("
 
130
    CREATE TABLE IF NOT EXISTS UserFoodTypes(
 
131
    loginName varchar(64),
 
132
    foodType varchar(64),
 
133
    FOREIGN KEY(loginName) REFERENCES Users(loginName),
 
134
    FOREIGN KEY(foodType) REFERENCES FoodTypes(foodType),
 
135
    PRIMARY KEY(loginName, foodType)
 
136
    );
 
137
  "); 
 
138
  
 
139
        $db->exec("
 
140
                CREATE TABLE IF NOT EXISTS dateInterests(
 
141
                user1 varchar(64),
 
142
                user2 varchar(64),
 
143
                FOREIGN KEY(user1) REFERENCES Users(loginName),
 
144
                FOREIGN KEY(user2) REFERENCES Users(loginName),
 
145
                PRIMARY KEY(user1, user2)
 
146
                );
 
147
        ");
 
148
        
 
149
        $db->exec("
 
150
                CREATE TABLE IF NOT EXISTS planLunch(
 
151
                user1 varchar(64),
 
152
                user2 varchar(64),
 
153
                message varchar(1024),
 
154
                date datetime,
 
155
                accepted bool,
 
156
                FOREIGN KEY(user1) REFERENCES Users(loginName),
 
157
                FOREIGN KEY(user2) REFERENCES Users(loginName),
 
158
                PRIMARY KEY(user1, user2)
 
159
                );
 
160
        ");
 
161
  $db->commit();
 
162
} catch (PDOException $err) {
 
163
  echo $err;
 
164
}
 
165
 
 
166
?>