Introduction
Interpolation refers to inserting values directly into the SQL code without parametrization. In order to prevent SQL injection attacks, Latitude parameterizes all queries to your db, by default. Parameter interpolation bypasses this protection and users should use it at their own risk. Read more about it in the SQL Syntax section.Direct interpolation can lead to SQL injection vulnerabilities, allowing users to perform critical actions such as deleting a table.
Why use interpolation?
There are some cases where you might want to interpolate a value directly into the SQL query.-
unparameterizable values: Depending on the database engine, some values cannot be parameterized. For example, table names cannot be parameterized in PostgreSQL.
For example:
Running this query in PostgreSQL will throw an error because the column name cannot be parameterized. To fix it, we must interpolate the column name directly into the query:
-
Dynamic query keywords: Most database engines only let you parameterize values, not keywords. For example, you cannot parameterize the
DESC
orASC
keyword in some engines.In order to fix this, you can interpolate the keyword directly into the query:
Syntax
interpolate()
allows you to send any parenthesized logic directly to the database. Let’s look at some examples:
Example
Without interpolation
Original query:$1 = 'Latitude'
This process is done differently depending on the database engine you are using, but all of them are secure and prevent SQL injection attacks.
With interpolation
Original query:Other uses
You can useinterpolate()
with any logic, for example with param()
which is an interesting use case to send the value from URL or inputs in the raw SQL code.