Skip to main content

Run custom SQL

Luwak can implement simple CRUD database operations with zero code configuration.

At the same time, Luwak also provides an interface to execute complex SQL.

Function definition

var query = {
sql: "SQL statement with placeholder",
countSql: "Optional, if paging data is returned, statistical SQL statements with placeholders need to be provided"
}

var params = {
key: "value"
}

luwak.runSql(query, params)

Among them, the placeholder in the SQL statement is Named Binding, that is: colon plus variable name.

Luwak uses prepared statements internally to run SQL to avoid SQL injection attacks.

Example

For example, if you need to find all purchase orders that have passed quality inspection and are not completed, you can use the following code to achieve this

var query = {
sql: "select * from `purchase_order` where id in ( select purchase_order_id from inspection where is_passed = :isPassed) and purchase_order_status_id != :statusId ;"
}

//The value of the placeholder comes from the object property of the same name
var params = {
isPassed: 1,
statusId: 3,
name: "tomcat" // This value is not used in the placeholder
}

result = luwak.runSql(query, params);

limit

Luwak Prohibit execution of DDL (create tables, modify table structures, etc.)