2 ms·
Proper SQL parameterization is a great example of where library/language design has a lot to offer. For example, in Java // IMPROPER PreparedStatement
by paulddraper 9y ago
Proper SQL parameterization is a great example of where library/language design has a lot to offer.
For example, in Java
// IMPROPER
PreparedStatement statement = connection.prepareStatement(
"SELECT * FROM users where email = '" + email + '"'
);
statement.execute();
vs
// PROPER
PreparedStatement statement = connection.prepareStatement(
"SELECT * FROM users WHERE email = ?"
);
statement.setString(1, email);
statement.execute();
The first code is easier and simpler than the second code -- especially if have lots of parameters, or if your queries start mixing arbitrary data and variable parts of the SQL. So people are sometimes going to write the first code.
---
In contrast, Scala has more sophisticated facilities than Java. I contribute to https://github.com/lucidsoftware/relate https://github.com/lucidsoftware/relate where the example would simply be
// GOOD
sql"SELECT * FROM users WHERE email = $email".asSeq[User]()
It's just so easy that no one is ever even _tempted_ to do the wrong thing. Use developers' laziness to your advantage, not your detriment :)
You can keep the full power of SQL (index hints, hand-tuned locking, DBMS-specific features), without constantly fighting the propensity towards SQL injection.
- rhizome31 9y agoDjango ORM is also less error-prone than the Rails example shown: User.objects.get(email=email_input) Raw Python DB-API, on the other hand, can easily be misused, as I've see on several occasions.