Skip to content

Commit 50d5794

Browse files
committed
Hex escape blob input on MySQL
This doesn't appear to be necessary on the MySQL side, but if you mix utf-8 input strings and blobs in the same string on ruby 1.9+, you can end up with encoding errors on the ruby side. Hex encoding the blobs can work around that, and doesn't appear to cause an harm other than bloating the SQL string used. Performance sensitive code should be using prepared statements for large blob input anyway, so that doesn't seem like a major downside.
1 parent 9cff859 commit 50d5794

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

CHANGELOG

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
=== HEAD
22

3+
* Hex escape blob input on MySQL (jeremyevans)
4+
35
* Handle more disconnect errors when using the postgres adapter with the postgres-pr driver (jeremyevans)
46

57
* Model#setter_methods private method now accepts 1 argument instead of 2 (jeremyevans)

lib/sequel/adapters/shared/mysql.rb

+7
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,8 @@ module DatasetMethods
563563
EXPLAIN_EXTENDED = 'EXPLAIN EXTENDED '.freeze
564564
BACKSLASH_RE = /\\/.freeze
565565
QUAD_BACKSLASH = "\\\\\\\\".freeze
566+
BLOB_START = "0x".freeze
567+
HSTAR = "H*".freeze
566568

567569
# MySQL specific syntax for LIKE/REGEXP searches, as well as
568570
# string concatenation.
@@ -893,6 +895,11 @@ def limit_sql(sql)
893895
alias delete_limit_sql limit_sql
894896
alias update_limit_sql limit_sql
895897

898+
# MySQL uses a preceding X for hex escaping strings
899+
def literal_blob_append(sql, v)
900+
sql << BLOB_START << v.unpack(HSTAR).first
901+
end
902+
896903
# Use 0 for false on MySQL
897904
def literal_false
898905
BOOL_FALSE

0 commit comments

Comments
 (0)