News for hackers. Stuff that matters.
Re:Writing a SQLite clone from scratch in C by anton_gogolev on Monday September 04, 2017 @02:13PM (#15169873)
I highly doubt that placeholders are interpolated into the SQL statement. Doing so will defeat the purpose of having them in the first place. Rather, they are tranferred out-of-band to the SQL "engine", as it were, and are used as straight up variables.
Re:Writing a SQLite clone from scratch in C by wruza on Monday September 04, 2017 @03:24PM (#15170307)
I checked it for PostgreSQL right now, and it seems that libpq can send them OOB via PQexecParams() and networked integer buffers. But that's not the case with OpenResty's pgmoon driver, which doesn't have such an interface. E.g. Lapis framework built on top of that simply escape_literal()-izes all arguments and interpolates right into query text. So, YMMW with specific (or generic) drivers.

Placeholders are there for those who tries to concatenate queries by hand; having them properly escaped in protocol should not defeat client-side security purpose.

Re:Writing a SQLite clone from scratch in C by anton_gogolev on Tuesday September 05, 2017 @01:38AM (#15172763)
You can never properly escape user input, so this escape_literal() is a ticking timebomb.
Re:Writing a SQLite clone from scratch in C by icebraining on Tuesday September 05, 2017 @04:21AM (#15173362)
How come it's impossible to properly escape user input? I get that it's hard if the format is complex, but I don't see what makes it categorically impossible, especially for a simple format like an SQL string. You just double every single quote and then surround the whole thing with single quotes.
Re:Writing a SQLite clone from scratch in C by anton_gogolev on Tuesday September 05, 2017 @10:45AM (#15175680)
> ...every single quote...

And then you come across \u0027 and you're screwed.

Re:Writing a SQLite clone from scratch in C by wruza on Tuesday September 05, 2017 @06:41PM (#15179688)
Do you know any decent server that interprets 6-char \uxxxx sequences as part of its top-level query syntax? I mean, how do you safely pass '\u0027' literal to it anyway? If you can't, then its literals and escaping are broken by design, so it is good to know from the start of using it.

Edit: removed double backslash from literal to not confuse it with host language's escaping.

Re:Writing a SQLite clone from scratch in C by icebraining on Tuesday September 05, 2017 @11:01AM (#15175866)
If you use a broken RDBMS, maybe. The standard is quite clear, and that certainly doesn't work in PortgreSQL.
Re:Writing a SQLite clone from scratch in C by xeeeeeeeeeeenu on Monday September 04, 2017 @03:22PM (#15170297)
Depends. Server-side prepare works the way you described while client-side prepare interpolates placeholder values. Both are widely used.
Re:Writing a SQLite clone from scratch in C by im3w1l on Monday September 04, 2017 @08:14PM (#15171646)
Having the interpolation done in one central place and coded by someone experienced sounds as good as OOB to me.
Re:Writing a SQLite clone from scratch in C by anton_gogolev on Tuesday September 05, 2017 @01:38AM (#15172764)
With that central place being?
Re:Writing a SQLite clone from scratch in C by wruza on Tuesday September 05, 2017 @02:59AM (#15173052)
Lapis db:query("? ?", a, b)

Edit: this call is most low-level db interface in lapis apps.