-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixing #5884 #5885
Fixing #5884 #5885
Conversation
lib/impure/db_mysql.nim
Outdated
## Database sanitizes the SQLInput | ||
case s.kind: | ||
of SQLStringKind: | ||
result = "'" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it might be worth using newStringOfCap
with the length of s.str
+ 2 initially?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, yeah that would probably be good.
…new size as the capacity of the string.
This is a breaking change and inconsistent with the other a) move db_ modules into its own Nimble package. |
Well, breaking, but breaking broken things. I wrote my server implementation first, then rewrote the SQL escaping afterwards making sure that my server didn't break in the process. Sure it won't work for anything that can be converted to a string, but it really shouldn't be able to. This makes it more explicit if you try to do something that's really not a good idea to do, and makes it hard for un-escaped things to be added to the SQL statement. As for inconsistency, it's all based on SQL but the problem is that SQL isn't one consistent system. So modelling it as such might not be appropriate. I can see why the current system is used, but it simply isn't safe. As for the "DB value" abstraction the type I created here should be serviceable for most common cases, however there are some special cases like timestamps and such which could be added (but those might different between platforms). Another thing to add to make sure we don't miss anything for a certain database implementation is an "Unsafe" or "Unescaped" kind which will allow the user to add a string of unescaped data to their query with the express warning that this should be done at their own risk. The reason why I started messing around with this was also because I couldn't do what I needed to do for my query. I had a simple |
The real thing you're supposed to use is |
But that pattern is horrible. You shouldn't concatenate variables with a SQL pattern without passing it through an escape system. Sure if offset is an int (and I would hope it is and not a |
Well no, what is broken here is SQL's overly complicated escape system full of special cases that you need to work around sometimes and I showed you the workaround. |
No, the workaround should be done by the system you use to execute your SQL with. Like in the code I've implemented here. You pass it strings and numbers and whatnot and the procedure that handles the escaping takes care of it. Almost impossible for the user to mess it up (unless we add an Unescaped kind, which would need to be called explicitly) by following the pattern they see in examples. As I said, your code might work as long as |
Well I'm not gonna break db_mysql for everybody just because it's an imperfect solution. It needs a proper migration path and workarounds for your problem do exist. Sorry. |
What about adding a concept for things convertible to a string and a toSQLInput that takes anything of that concept. This way everything that would previously be supported is now still supported, but strings would now be properly escaped and ints and floats would be supported in all positions. This could obviously be implemented for all the other SQL based DBs, by just editing the dbQuote procedure, and particularly the string escaping rules. I think they would be pretty similar to MySQL but I'm not 100% sure. |
You should use mysql_stmt_bind_param function instead of manually escaping values. |
Yeah I know, but I didn't want this version to differ too much from the other DB wrappings. mysql_stmt_bind_param isn't a universal thing so I went with something that could easily be implemented for the other wrappers. |
Both SQLite and PostgreSQL have simmilar methods: sqlite3_bind_* and PQexecParams. So, binding can be implemented for all supported I've done some PoC for db_sqlite. Exported |
Ah, then this is definitely the way to go. I don't have time to implement this right now since I'm working on my masters but I will have a look at it once I find the time if no-one does it in the meantime. |
Added more robust MySQL escaping.
Also changed from simple strings to more types that should be supported when inserting into SQL statements.
This fixes nim-lang/db_connector#13