-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstraints.sql
171 lines (134 loc) · 4.55 KB
/
constraints.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/** Advanced SQL Commands **/
ALTER TABLE Song
ADD CONSTRAINT checkReleaseYear
CHECK (release_year <= 2023);
DELIMITER $$
CREATE TRIGGER popularityTrigger
BEFORE UPDATE ON Song
FOR EACH ROW
BEGIN
IF new.popularity > 100 THEN
SET new.popularity = 100;
ELSEIF new.popularity < 0 THEN
SET new.popularity = 0;
END IF;
END
$$
DELIMITER ;
CREATE ASSERTION maxPlaylist
CHECK (25 >= ALL
(SELECT COUNT(*)
FROM Playlist
GROUP BY user_id))
/** Database Level Security **/
/* Check, Assertion, and Trigger */
ALTER TABLE Song
ADD CONSTRAINT checkReleaseYear
CHECK (release_year <= 2023);
DELIMITER $$
CREATE TRIGGER popularityTrigger
BEFORE UPDATE ON Song
FOR EACH ROW
BEGIN
IF new.popularity > 100 THEN
SET new.popularity = 100;
ELSEIF new.popularity < 0 THEN
SET new.popularity = 0;
END IF;
END
$$
DELIMITER ;
CREATE ASSERTION maxPlaylist
CHECK (25 >= ALL
(SELECT COUNT(*)
FROM Playlist
GROUP BY user_id))
/* Grant Statements */
GRANT ALL PRIVILEGES
ON UVA_User
TO Owner;
GRANT INSERT
ON UVA_User
TO PUBLIC;
/** Application Level Security **/
-- Try-Exception method in multiple files
/* connect to the database */
try
{
// $db = new PDO("mysql:host=$hostname;dbname=db-demo", $username, $password);
$db = new PDO($dsn, $username, $password);
// dispaly a message to let us know that we are connected to the database
echo "<p>You are connected to the database: $dsn</p>";
}
catch (PDOException $e) // handle a PDO exception (errors thrown by the PDO library)
{
// Call a method from any object, use the object's name followed by -> and then method's name
// All exception objects provide a getMessage() method that returns the error message
$error_message = $e->getMessage();
echo "<p>An error occurred while connecting to the database: $error_message </p>";
}
catch (Exception $e) // handle any type of exception
{
$error_message = $e->getMessage();
echo "<p>Error message: $error_message </p>";
}
/* retrieve song by title */
try{
$song_retrieved_by_title = retrieveSongByTitle($_POST['title']);
}
catch (Exception $e){
echo $e->getMessage();
}
-- Password Hashing
if ($query->rowCount() == 0) {
$query = $db->prepare("INSERT INTO UVA_User(user_id,user_name,password) VALUES (:user_id,:user_name,:password_hash)");
$query->bindParam("user_id", $user_id, PDO::PARAM_STR);
$query->bindParam("user_name", $user_name, PDO::PARAM_STR);
$query->bindParam("password_hash", $password_hash, PDO::PARAM_STR);
$result = $query->execute();
if ($result) {
echo '<p class="success">Your registration was successful!</p>';
// echo '<div class="row mb-3 mx-3">
// <a href="index.php">
// <input type="submit" class="btn btn-outline-dark" name="actionBtn" value="Go to Our Home Page" title="click to go back to home page" />
// </a>
// </div>';
header("Location: index.php");
exit();
} else {
echo '<p class="error">Something went wrong!</p>';
}
}
-- Prepare Statements in multiple functions - below are a couple of examples
function retrieveSongByArtist($artistName){
global $db;
$query = "select *
from Song
inner join SongTitle_ArtistID
on Song.title = SongTitle_ArtistID.title and Song.artist_id = SongTitle_ArtistID.artist_id
inner join Album
on SongTitle_ArtistID.album_id = Album.album_id
natural join Song_Language
natural join Song_Genre
inner join Artist
on Artist.artist_id = Song.artist_id
where Artist.name=:artistName
";
$statement = $db->prepare($query);
$statement->bindValue(':artistName', $artistName);
$statement->execute();
$result = $statement->fetchAll();
//close cursor
$statement->closeCursor();
return $result;
}
function addTrack_AlbumName_ArtistID($artist_id, $album_name, $album_id){
global $db;
$query = "insert into AlbumName_ArtistID(artist_id,album_name,album_id) values (:artist_id,:album_name,:album_id)";
$statement = $db->prepare($query);
$statement->bindValue(':artist_id', $artist_id);
$statement->bindValue(':album_name', $album_name);
$statement->bindValue(':album_id', $album_id);
$statement->execute();
$statement->closeCursor();
}