Skip to content

Commit 1ffd7a1

Browse files
committed
Updated sql properly this time (including fixed tests)
1 parent ec04c72 commit 1ffd7a1

7 files changed

+103
-82
lines changed

db/views/unified_restrictions_v04.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ SELECT
2525
CASE WHEN restrictions_cards_banned.restriction_id IS NOT NULL THEN true ELSE false END AS is_banned,
2626
CASE WHEN restrictions_cards_restricted.restriction_id IS NOT NULL THEN true ELSE false END AS is_restricted,
2727
COALESCE(restrictions_cards_points.value, 0) AS eternal_points,
28+
CASE WHEN restrictions_cards_global_penalty.restriction_id IS NOT NULL THEN true ELSE false END AS has_global_penalty,
2829
COALESCE(restrictions_cards_universal_faction_cost.value, 0) AS universal_faction_cost
2930
FROM
3031
cards_cross_restrictions_and_snapshots

lib/card_search_parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class CardSearchParser < Parslet::Parser
2828
str('eternal_points') |
2929
str('faction') |
3030
str('format') |
31-
str('global_penalty') |
31+
str('has_global_penalty') |
3232
str('illustrator') |
3333
str('in_restriction') |
3434
str('influence_cost') |

lib/card_search_query_builder.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ class CardSearchQueryBuilder
33
@@boolean_keywords = [
44
'b',
55
'banlist',
6+
'has_global_penalty',
67
'in_restriction',
78
'is_banned',
89
'is_restricted',
@@ -16,7 +17,6 @@ class CardSearchQueryBuilder
1617
'cost',
1718
'eternal_points',
1819
'g',
19-
'global_penalty',
2020
'h',
2121
'influence_cost',
2222
'l',
@@ -75,7 +75,7 @@ class CardSearchQueryBuilder
7575
'faction' => 'cards.faction_id',
7676
'format' => 'unified_restrictions.format_id',
7777
'g' => 'cards.advancement_requirement',
78-
'global_penalty' => 'unified_restrictions.global_penalty',
78+
'has_global_penalty' => 'unified_restrictions.has_global_penalty',
7979
'h' => 'cards.trash_cost',
8080
'in_restriction' => 'unified_restrictions.in_restriction',
8181
'influence_cost' => 'cards.influence_cost',
@@ -106,7 +106,7 @@ class CardSearchQueryBuilder
106106
'card_pool' => :card_pool_cards,
107107
'card_subtype' => :card_subtypes,
108108
'eternal_points' => :unified_restrictions,
109-
'global_penalty' => :unified_restrictions,
109+
'has_global_penalty' => :unified_restrictions,
110110
'in_restriction' => :unified_restrictions,
111111
'is_banned' => :unified_restrictions,
112112
'is_restricted' => :unified_restrictions,
@@ -119,7 +119,7 @@ def initialize(query)
119119
@query = query
120120
@parse_error = nil
121121
@parse_tree = nil
122-
@left_joins = Set.new
122+
@left_joins = Set.new
123123
@where = ''
124124
@where_values = []
125125
begin
@@ -133,8 +133,8 @@ def initialize(query)
133133
constraints = []
134134
where = []
135135
# TODO(plural): build in explicit support for requirements
136-
# {is_banned,is_restricted,eternal_points,global_penalty,universal_faction_cost} all require restriction_id, would be good to have card_pool_id as well.
137-
# TODO(plural): build in explicit support for smart defaults, like restriction_id should imply is_banned = false. card_pool_id should imply the latest restriction list.
136+
# {is_banned,is_restricted,eternal_points,has_global_penalty,universal_faction_cost} all require restriction_id, would be good to have card_pool_id as well.
137+
# TODO(plural): build in explicit support for smart defaults, like restriction_id should imply is_banned = false. card_pool_id should imply the latest restriction list.
138138
@parse_tree[:fragments].each {|f|
139139
if f.include?(:search_term)
140140
keyword = f[:search_term][:keyword].to_s
@@ -158,7 +158,7 @@ def initialize(query)
158158
if !value.match?(/\A\d+\Z/)
159159
@parse_error = 'Invalid value "%s" for integer field "%s"' % [value, keyword]
160160
return
161-
end
161+
end
162162
operator = ''
163163
if @@numeric_operators.include?(match_type)
164164
operator = @@numeric_operators[match_type]
@@ -167,10 +167,10 @@ def initialize(query)
167167
return
168168
end
169169
constraints << '%s %s ?' % [@@term_to_field_map[keyword], operator]
170-
where << value
170+
where << value
171171
else
172172
# String fields only support : and !, resolving to to {,NOT} LIKE %value%.
173-
# TODO(plural): consider ~ for regex matches.
173+
# TODO(plural): consider ~ for regex matches.
174174
operator = ''
175175
if @@string_operators.include?(match_type)
176176
operator = @@string_operators[match_type]
@@ -183,7 +183,7 @@ def initialize(query)
183183
end
184184
if @@term_to_left_join_map.include?(keyword)
185185
@left_joins << @@term_to_left_join_map[keyword]
186-
end
186+
end
187187
end
188188

189189
# bare/quoted words in the query are automatically mapped to stripped_title
@@ -192,7 +192,7 @@ def initialize(query)
192192
operator = value.start_with?('!') ? 'NOT LIKE' : 'LIKE'
193193
value = value.start_with?('!') ? value[1..] : value
194194
constraints << 'lower(cards.stripped_title) %s ?' % operator
195-
where << '%%%s%%' % value
195+
where << '%%%s%%' % value
196196
end
197197
}
198198
@where = constraints.join(' AND ')

lib/printing_search_parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class PrintingSearchParser < Parslet::Parser
3131
str('faction') |
3232
str('flavor') |
3333
str('format') |
34-
str('global_penalty') |
34+
str('has_global_penalty') |
3535
str('illustrator') |
3636
str('in_restriction') |
3737
str('influence_cost') |

lib/printing_search_query_builder.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ class PrintingSearchQueryBuilder
33
@@boolean_keywords = [
44
'b',
55
'banlist',
6+
'has_global_penalty',
67
'in_restriction',
78
'is_banned',
89
'is_restricted',
@@ -20,7 +21,6 @@ class PrintingSearchQueryBuilder
2021
'cost',
2122
'eternal_points',
2223
'g',
23-
'global_penalty',
2424
'h',
2525
'influence_cost',
2626
'l',
@@ -99,7 +99,7 @@ class PrintingSearchQueryBuilder
9999
'flavor' => 'printings.flavor',
100100
'format' => 'unified_restrictions.format_id',
101101
'g' => 'cards.advancement_requirement',
102-
'global_penalty' => 'unified_restrictions.global_penalty',
102+
'has_global_penalty' => 'unified_restrictions.has_global_penalty',
103103
'h' => 'cards.trash_cost',
104104
'i' => 'illustrators.name',
105105
'illustrator' => 'illustrators.name',
@@ -150,7 +150,7 @@ class PrintingSearchQueryBuilder
150150
'faction' => :card,
151151
'format' => :unified_restrictions,
152152
'g' => :card,
153-
'global_penalty' => :unified_restrictions,
153+
'has_global_penalty' => :unified_restrictions,
154154
'h' => :card,
155155
'i' => :illustrators,
156156
'illustrator' => :illustrators,
@@ -183,7 +183,7 @@ def initialize(query)
183183
@query = query
184184
@parse_error = nil
185185
@parse_tree = nil
186-
@left_joins = Set.new
186+
@left_joins = Set.new
187187
@where = ''
188188
@where_values = []
189189
begin
@@ -197,8 +197,8 @@ def initialize(query)
197197
constraints = []
198198
where = []
199199
# TODO(plural): build in explicit support for requirements
200-
# {is_banned,is_restricted,eternal_points,global_penalty,universal_faction_cost} all require restriction_id, would be good to have card_pool_id as well.
201-
# TODO(plural): build in explicit support for smart defaults, like restriction_id should imply is_banned = false. card_pool_id should imply the latest restriction list.
200+
# {is_banned,is_restricted,eternal_points,has_global_penalty,universal_faction_cost} all require restriction_id, would be good to have card_pool_id as well.
201+
# TODO(plural): build in explicit support for smart defaults, like restriction_id should imply is_banned = false. card_pool_id should imply the latest restriction list.
202202
@parse_tree[:fragments].each {|f|
203203
if f.include?(:search_term)
204204
keyword = f[:search_term][:keyword].to_s
@@ -222,7 +222,7 @@ def initialize(query)
222222
if !value.match?(/\A(\d{4}-\d{2}-\d{2}|\d{8})\Z/)
223223
@parse_error = 'Invalid value "%s" for date field "%s" - only YYYY-MM-DD or YYYYMMDD are supported.' % [value, keyword]
224224
return
225-
end
225+
end
226226
operator = ''
227227
if @@date_operators.include?(match_type)
228228
operator = @@date_operators[match_type]
@@ -231,12 +231,12 @@ def initialize(query)
231231
return
232232
end
233233
constraints << '%s %s ?' % [@@term_to_field_map[keyword], operator]
234-
where << value
234+
where << value
235235
elsif @@numeric_keywords.include?(keyword)
236236
if !value.match?(/\A\d+\Z/)
237237
@parse_error = 'Invalid value "%s" for integer field "%s"' % [value, keyword]
238238
return
239-
end
239+
end
240240
operator = ''
241241
if @@numeric_operators.include?(match_type)
242242
operator = @@numeric_operators[match_type]
@@ -245,10 +245,10 @@ def initialize(query)
245245
return
246246
end
247247
constraints << '%s %s ?' % [@@term_to_field_map[keyword], operator]
248-
where << value
248+
where << value
249249
else
250250
# String fields only support : and !, resolving to to {,NOT} LIKE %value%.
251-
# TODO(plural): consider ~ for regex matches.
251+
# TODO(plural): consider ~ for regex matches.
252252
operator = ''
253253
if @@string_operators.include?(match_type)
254254
operator = @@string_operators[match_type]
@@ -261,7 +261,7 @@ def initialize(query)
261261
end
262262
if @@term_to_left_join_map.include?(keyword)
263263
@left_joins << @@term_to_left_join_map[keyword]
264-
end
264+
end
265265
end
266266

267267
# bare/quoted words in the query are automatically mapped to stripped_title
@@ -270,7 +270,7 @@ def initialize(query)
270270
operator = value.start_with?('!') ? 'NOT LIKE' : 'LIKE'
271271
value = value.start_with?('!') ? value[1..] : value
272272
constraints << 'lower(cards.stripped_title) %s ?' % operator
273-
where << '%%%s%%' % value
273+
where << '%%%s%%' % value
274274
end
275275
}
276276
@where = constraints.join(' AND ')

0 commit comments

Comments
 (0)