-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise_test.erl
62 lines (50 loc) · 1.75 KB
/
exercise_test.erl
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
-module(exercise_test).
-export([setup_db/0, add_abuse/2, add_tips/3, add_users/3, read_info/1, read_info_abuse/1, read_info_tips/1, select_all/0]).
-record(users, {name, email_address :: string(), password}).
-record(tips, {site_url, description, date_of_review}).
-record(abuse, {ip_address, number_of_site_visits}).
setup_db()->
mnesia:create_table(users,[{attributes, record_info(fields, users)},{disc_copies, [node(), nodes()]}]),
mnesia:create_table(tips, [{attributes, record_info(fields, tips)}, {disc_copies, [node(), nodes()]}]),
mnesia:create_table(abuse, [{attributes, record_info(fields, abuse)}, {disc_copies, [node(), nodes()]}]).
%Routines to write to the tables
add_users(Name, Email, Password) ->
Row = #users{name = Name, email_address = Email, password = Password},
F = fun() ->
mnesia:write(Row)
end,
mnesia:transaction(F).
add_tips(Url, Desc, Date) ->
Row = #tips{site_url = Url, description = Desc, date_of_review = Date},
F = fun() ->
mnesia:write(Row)
end,
mnesia:transaction(F).
add_abuse(IP, Visit) ->
Row = #abuse{ip_address = IP, number_of_site_visits = Visit},
F = fun() ->
mnesia:write(Row)
end,
mnesia:transaction(F).
read_info(Username) ->
F = fun () ->
mnesia:read({users, Username})
end,
mnesia:transaction(F).
read_info_abuse(Name) ->
F = fun () ->
mnesia:read({abuse, Name})
end,
mnesia:transaction(F).
read_info_tips(Url) ->
F = fun () ->
mnesia:read({tips, Url})
end,
mnesia:transaction(F).
select_all() ->
mnesia:transaction(
fun() ->
P=qlc:e(qlc:q([E || E <- mnesia:table(users)])), %query to select all data from table named 'tableName'
io:format(" ~p ~n ", [P]) % Prints table data on terminal
%to_file("fileName.txt",P) % to_file method writes the data to file
end ).