-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDAL.php
177 lines (161 loc) · 5.05 KB
/
DAL.php
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
172
173
174
175
176
177
<?php
$CardTable = 'mtg_cards';
$MapTable = 'mtg_map';
$LogTable = 'mtg_log';
$db_now = 'utc_timestamp()';
$logging = true;
function sqlString($s, $db){
return trim($db->real_escape_string($s));
}
function DB_create_card($card, $db){
$name = $card->get('name');
$mana_cost = $card->get('mana_cost');
$converted_mana_cost = $card->get('converted_mana_cost');
$types = $card->get('types');
$card_text = $card->get('card_text');
$flavor_text = $card->get('flavor_text');
$power_toughness = $card->get('power_toughness');
$expansion = $card->get('expansion');
$rarity = $card->get('rarity');
$card_number = $card->get('card_number');
$artist = $card->get('artist');
return DB_insert_card($name, $mana_cost, $converted_mana_cost, $types, $card_text, $flavor_text, $power_toughness, $expansion, $rarity, $card_number, $artist, $db);
}
function DB_insert_card($name, $mana_cost, $converted_mana_cost, $types, $card_text, $flavor_text, $power_toughness, $expansion, $rarity, $card_number, $artist, $db){
$result = $db->query( SQL_create_card($name, $mana_cost, $converted_mana_cost, $types, $card_text, $flavor_text, $power_toughness, $expansion, $rarity, $card_number, $artist, $db) );
if ($result){
$id = $db->insert_id;
return $id;
}
else {
echo " [[ {$db->error} ]] ";
return false;
}
}
function SQL_create_card($name, $mana_cost, $converted_mana_cost, $types, $card_text, $flavor_text, $power_toughness, $expansion, $rarity, $card_number, $artist, $db){
global $CardTable;
$name = sqlString($name, $db);
$mana_cost = sqlString($mana_cost, $db);
$converted_mana_cost = sqlString($converted_mana_cost, $db);
$types = sqlString($types, $db);
$card_text = sqlString($card_text, $db);
$flavor_text = sqlString($flavor_text, $db);
$power_toughness = sqlString($power_toughness, $db);
$expansion = sqlString($expansion, $db);
$rarity = sqlString($rarity, $db);
$card_number = sqlString($card_number, $db);
$artist = sqlString($artist, $db);
$SQL = "insert into $CardTable(name, mana_cost, converted_mana_cost, types, card_text, flavor_text, power_toughness, expansion, rarity, card_number, artist)
values( '$name', '$mana_cost', '$converted_mana_cost', '$types', '$card_text', '$flavor_text', '$power_toughness', '$expansion', '$rarity', '$card_number', '$artist' );";
//echo $SQL;
//die();
return $SQL;
}
function DB_card_with_id($id, $db){
$result = $db->query(SQL_card_with_id($id, $db));
return process_card_result($result);
}
function SQL_card_with_id($id, $db){
global $CardTable;
return "select * from $CardTable
where ID = '$id'";
}
function DB_card_with_name($name, $db){
$result = $db->query(SQL_card_with_name($name, $db));
return process_card_result($result);
}
function SQL_card_with_name($name, $db){
global $CardTable;
$name = sqlString($name, $db);
return "select * from $CardTable
where name = '$name'";
}
function process_card_result($result){
switch ($result->num_rows){
case 0:
return false;
break;
default:
$fetchedArray = (array) $result->fetch_object();
return new card($fetchedArray);
}
}
function DB_maps_like($search, $db){
//Reject very short searches
if (strlen($search) < 4){ return false; }
$result = $db->query(SQL_maps_like($search, $db));
switch ($result->num_rows){
case 0:
return false;
break;
default:
$names = array();
while ($name = $result->fetch_object()){
$names[] = $name->name;
}
return $names;
}
}
function SQL_maps_like($search, $db){
global $MapTable, $CardTable;
$search = sqlString($search, $db);
$SQL = "SELECT distinct card.name FROM $MapTable as map
join $CardTable as card
on map.card_id = card.id
where map.search like '%$search%'";
return $SQL;
}
function DB_mapped_id($name, $db){
$result = $db->query(SQL_mapped_id($name, $db));
switch ($result->num_rows){
case 0:
return false;
break;
default:
$cardArray = (array) $result->fetch_object();
return $cardArray['card_id'];
}
}
function SQL_mapped_id($name, $db){
global $MapTable;
$name = sqlString($name, $db);
$SQL = "select card_id from $MapTable
where search = '$name'";
return $SQL;
}
function DB_add_to_map($search, $id, $db){
$result = $db->query(SQL_add_to_map($search, $id, $db));
if ($result){
return true;
}
else {
echo " [[ {$db->error} ]] ";
return false;
}
}
function SQL_add_to_map($search, $id, $db){
global $MapTable, $db_now;
$search = sqlString($search, $db);
$SQL = "insert into $MapTable values ('$search', $id, $db_now)";
return $SQL;
}
function DB_add_to_log($event, $search, $db){
global $LogTable, $db_now, $logging;
if ($logging){
$event = sqlString($event, $db);
$search = sqlString($search, $db);
$SQL = "insert into $LogTable (event, search, happened) values ('$event', '$search', $db_now)";
$result = $db->query($SQL);
if ($result){
return true;
}
else {
return "Error: {$db->error}";
}
}
else{
//If logging is turned off, succeed transparently
return true;
}
}
?>