toSQL

converts the value of QueryBuilder to an SQL query

object toSQL( [object config,object rules] );

Parameters

configobjectoptional, a configuration object of the SQL query
rulesobjectoptional, an object of rules for filtering

Returns

objectan object with an SQL query string and an array of values

Example

// calling the method with default parameters 
// ({placeholders:false}, $$("qb").getValue()[0])
var sql = $$("qb").toSQL();

Related samples

Details

Configuring the returned object

The configuration object contains the placeholders attribute with the boolean value:

var sql = $$("qb").toSQL({placeholders:true});

It provides two variants of converting the rules object of the Query Builder value. For example, let's take the following object:

{
   "glue": "and",
   "rules": [
       {
          "key": "fname",
          "value": "Alex",
          "rule": "equal"
       }
    ]
};
  • if the placeholders attribute is set to true, the rules object of the Query Builder value is converted into an SQL query with placeholders instead of values, as follows:
{
    code: "fname = ?",
    values: [ "Alex" ]   
};
  • if the placeholders attribute is set to false, the rules object of the Query Builder value is converted into an SQL query with real values, like this:
{
    code: "fname = Alex",
    values: [ "Alex" ]   
};

The returned object has two properties:

  • code - an SQL query string
  • values - an array of values

Referring to the rules object

As a second parameter the method can take an object of rules used for filtering. To refer to the rules object, you should get the first element of the value array returned by the getValue method. For example:

// converts Query Builder value into SQL query with placeholders instead of real values
var sql = $$("qb").toSQL({placeholders:true},$$("qb").getValue()[0]);
See also
Back to top