oleghnidets / OHMySQL

Swift + MySQL = ❤️
https://oleghnidets.github.io/OHMySQL/documentation/ohmysql/
MIT License
235 stars 42 forks source link

Provide an escape function #4

Closed goodman-capu closed 7 years ago

goodman-capu commented 7 years ago

The building of query string lacks escaping of special characters. I suggest adding a public function like this and apply it to NSString (SQLQueries) functions.

oleghnidets commented 7 years ago

I guess I will use the native MySQL C API functions: mysql_real_escape_string and/or mysql_real_escape_string_quote.

oleghnidets commented 7 years ago

Hi there, Check the latest release. I added a public method -escapedUsingEncoding. Although, I am still in progress due to lack of the time. The next step is applying the method to a query.

goodman-capu commented 7 years ago

There are problem with this function: Not every character's length is 2. For example, if you run this code: NSLog(@"%@", [@"好" escapedUsingEncoding:NSUTF8StringEncoding]); or this: NSLog(@"%@", [@"😁" escapedUsingEncoding:NSUTF8StringEncoding]); The app would crash, for some Chinese characters' length is 3, and for some Emoji it's 4.

oleghnidets commented 7 years ago

You passed an incorrect enum type. Due to different char sets support I had to define own type CharsetEncoding. Please, try CharsetEncodingUTF8.

goodman-capu commented 7 years ago

Opps, my bad. It worked fine.

oleghnidets commented 7 years ago

Well, public method added. The developer is responsible to call -escapedUsingEncoding if needed (like in MySQL C API mysql_real_escape_string()). The issue is closed.