SQL Restrictions
SQL restrictions will allow you to add ad-hoc SQL to the criteria you are building. Simple enough, but we have made great strides to make this look easy for a developer but behind the scenes we take each sql you pass and compile it so we can abstract all the native Java types for you. This means that you can use it in a similar manner to executeQuery() in CFML.

Method Signature

The method you will use for this restriction is sql()
1
/**
2
* Use arbitrary SQL to modify the resultset
3
*
4
* @sql The sql to execute, it can contain parameters via positional `?` placeholders
5
* @params This is an array of value definitions which need to be a struct of { value: , type: } or if the value is a simple value, we will try to infer it's type
6
*/
7
function sql( required string sql, array params=[] )
Copied!

Arguments

The method takes in two arguments:
  • sql - The ad-hoc query to execute. You can use positional parameters via the ? placeholder.
  • params - An array of parameters to bind the SQL with, can be simple values or a struct of a values and a supported type or a combination of both.

SQL Params

The parameters you bind the SQL with can be of two types
  1. 1.
    Plain values.
  2. 2.
    Typed values
If you use plain values, then we will INFER the type from it, which is not as accurate as using a typed value. A typed value is a struct with the value and a valid type.
1
c.sql( "isActive = true" );
2
3
// simple values
4
c.sql( "id = ?", [ 123 ] );
5
c.sql( "userName = ? and firstName like ?", [ "joe", "%joe%"] );
6
7
// strong typed values
8
c.sql( "id = ?", [ { value:123, type=c.TYPES.integer } ] );
9
c.sql( "isActive = ?", [ { value:true, type=c.TYPES.boolean } ] );
10
c.sql( "userName = ? and firstName like ?", [
11
{ value : "joe", type : "string" },
12
{ value : "%joe%", type : "string" }
13
] );
Copied!

Valid Types

Below you can see the TYPES struct available in the criteria builder object which map the CFML types to the native Hibernate Types.
1
this.TYPES = {
2
"string" : "StringType",
3
"clob" : "ClobType",
4
"text" : "TextType",
5
"char" : "ChareacterType",
6
"boolean" : "BooleanType",
7
"yesno" : "YesNoType",
8
"truefalse" : "TrueFalseType",
9
"byte" : "ByteType",
10
"short" : "ShortType",
11
"integer" : "IntegerType",
12
"long" : "LongType",
13
"float" : "FloatType",
14
"double" : "DoubleType",
15
"bigInteger" : "BigIntegerType",
16
"bigDecimal" : "BigDecimalType",
17
"timestamp" : "TimestampType",
18
"time" : "TimeType",
19
"date" : "DateType",
20
"calendar" : "CalendarType",
21
"currency" : "CurrencyType",
22
"locale" : "LocaleType",
23
"timezone" : "TimeZoneType",
24
"url" : "UrlType",
25
"class" : "ClassType",
26
"blob" : "BlobType",
27
"binary" : "BinaryType",
28
"uuid" : "UUIDCharType",
29
"serializable": "SerializableType"
30
};
Copied!

Inferred Types

The inferred types we infer are the following and in the following order.
  1. 1.
    Binary
  2. 2.
    Boolean
  3. 3.
    Time
  4. 4.
    Date
  5. 5.
    uuid
  6. 6.
    float
  7. 7.
    numeric
  8. 8.
    url
  9. 9.
    string
  10. 10.
    text
Last modified 2yr ago
Copy link
Edit on GitHub