-
Notifications
You must be signed in to change notification settings - Fork 3
camlsql.prepare method
Home » camlsql.prepare Method
Prepare is the bread and butter of camlsql. This is basically always your starting point.
Prepare your query and bind parameters
CamlSqlQuery camlsql.prepare ( sql_query[, parameters[] ] )
-
sql_query
is your query. Refer to SQL-Query for syntax details -
parameters
is an array of parameter values - Returns a CamlSqlQuery object
Parameters can be bound in two ways - by name or by parameter index.
Pass an array as parameter to bind by index
CamlSqlQuery camlsql.prepare(sql_query, object[] );
For simple queries you can use ?
as a macro in your SQL query to indicate the use of a parameter. The first ?
will use the first parameter from the parameter array, the second ?
will use the second and so on.
camlsql.prepare("SELECT * FROM [Pages] WHERE Created < ? AND Title LIKE ?", [
camlsql.today(), // This is the "Created < ?" parameter
"%fun%" // This is the "Title LIKE ?" parameter
]).getXml();
Notice the getXml method. This is a method of the returned CamlSqlQuery object.
Pass an object as parameter to bind by index
camlsql.prepare(sql_query, object);
If you have a lot of parameters, or need to use the same value many times in your query, then the query is easier to read if you use named queries.
camlsql.prepare("SELECT * FROM [Pages] WHERE Title LIKE @query OR Description LIKE @query OR Field3 LIKE @query",
{"@query" : "%cheese%"}
).getXml()
The prepare
method will return a CamlSqlQuery object from which you can execute the query immediatly, or simply retreive the generated CAML Xml.