Skip to content

Commit 09861be

Browse files
committed
Fix mysql fingerprint for ORDER BY FIELD()
Previously we had different fingerprints depending on the number of ids given in FIELD().
1 parent f872f65 commit 09861be

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

Diff for: lib/prosopite.rb

+2
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ def mysql_fingerprint(query)
179179

180180
query.gsub!(/\b(in|values?)(?:[\s,]*\([\s?,]*\))+/, "\\1(?+)")
181181

182+
query.gsub!(/(?<!\w)field\s*\(\s*(\S+)\s*,\s*(\?+)(?:\s*,\s*\?+)*\)/, 'field(\1, \2+)')
183+
182184
query.gsub!(/\b(select\s.*?)(?:(\sunion(?:\sall)?)\s\1)+/, "\\1 /*repeat\\2*/")
183185

184186
query.gsub!(/\blimit \?(?:, ?\?| offset \?)/, "limit ?")

Diff for: test/test_mysql_fingerprint.rb

+4
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ def test_order_by_clauses
280280
Prosopite.mysql_fingerprint("SELECT * from a_table ORDER BY foo ASC, bar DESC, baz, quux ASC"),
281281
"select * from a_table order by foo, bar desc, baz, quux"
282282
)
283+
assert_equal(
284+
Prosopite.mysql_fingerprint("SELECT * from a_table ORDER BY FIELD(id, 1, 2, 3, 4)"),
285+
"select * from a_table order by field(id, ?+)"
286+
)
283287
end
284288

285289
def test_call_procedures

0 commit comments

Comments
 (0)