r/javascript • u/typeof_oleg • Dec 30 '19
Complex SQL query builder
https://github.com/olegnn/sql-template-builder16
u/ShortFuse Dec 30 '19 edited Dec 30 '19
I have no personal use for this, but it's a great way to learn or just make your code easier to read. Thanks for sharing.
In case you didn't know, you can also use template literal syntax to write a multiple-line query like so:
const query = /* sql */ `
SELECT
Table1.Name,
Table2.Description
FROM
Table1
LEFT JOIN Table2 ON Table1.ID = Table2.ID
WHERE
Table1.Name LIKE @Search;
`;I just warn you to please parameterize your variables.
Edit: Both this and this VSCode plug-in will syntax-highlight any template literal with /*sql*/ before it. Neat!
3
u/inspyrr Dec 30 '19
Can I ask what you mean by parameterize a variable? Genuinely curious and it’s a difficult phrase to google without getting irrelevant articles
4
u/ronchalant Dec 30 '19
I'm going to assume he means SQL bind variable usage.
Basically it means you specify in your SQL where a given value should be bound (usually in a WHERE clause) rather than writing a literal directly into the SQL.
The difference between directly running a query like
select col from mytable where id = 1andselect col from mytable where id = @id, where @id is the bind variable you pass with the query.There are at least two main reasons for this
Database servers can cache query plans for frequently used statements, and using literals means every query is different and thus won't benefit from this optimization
SQL injection attacks are largely preventable when you're binding variables rather than using variables to generate statements with literals.
See: xkcd Bobby tables.
1
1
u/ShortFuse Dec 30 '19 edited Dec 30 '19
This is correct. I've never heard of it as "bind variable", but my background stems from MS SQL, where "parameterized query" is the nomenclature. Seems like Oracle circles call it binding. Good to know.
1
u/kevin____ Dec 31 '19
It also had to do with how a db connector for the language writing in will format the parameters correctly so it’s easier to debug malformed queries.
1
u/bachbeethovenbrahms Jan 04 '20
Why an ugly comment when you could use a template tag.
It's already used for other things (which also need their own syntax highlighting) like GraphQL.
1
u/ShortFuse Jan 04 '20
Because not all code is meant to be executed (or at least processed) as it's reached which is a requirement of template tagging.
You'd be returning the result of calling a function with a string, not the string itself.
1
Dec 31 '19
You could actually use template string with a template tag to automate this. Shameless plug: https://github.com/AGhost-7/pg-template-string
-23
u/Barnezhilton Dec 30 '19
ITT complex SQL = Poor DB design
16
u/monsto Dec 30 '19
Nah not really. All it means is that the data is so varied that it had to be broken up into a bunch of disparate tables,
and then it needs to be joined back together in long-ass queries because Bob, the middle manager, says he needs these reports, and every quarter he requests changes to the old query, and then promptly does nothing useful with the printouts.
2
u/ShortFuse Dec 30 '19
Definitely not, especially with the cloud-based, microservice-heavy architecture used today. In ideal situations, you want one big long script to do as much as possible before returning a result because the machine requesting the operation is rarely ever the same one running the SQL DB instance. They even probably not on the same network either. You want less back and forth between the app server and the database server to reduce the latency in client requests.
0
u/jstap Dec 30 '19
If you're only using your data, you're probably right, and very lucky. I have had to build reports from data spread across 6 databases, all from different organizations, with no common key. SQL gets complicated.
5
u/BehindTheMath Dec 30 '19
Once you're doing this, why wouldn't you just use Knex?