-
Notifications
You must be signed in to change notification settings - Fork 4
SQL Query
SQL Query objects are arrays that represent an object reference and the object's properties. This object defines what a database query will do. You create a SQL Query object by passing the name of a table to sqlquery_create. SQL Queries are stored within variables and typically only persist while the application is running.
put sqlquery_createObject("lessons") into tQueryA
You can specify the WHERE condition of a query by setting the conditions property. You can even use english words rather such as contains:
put the text of field "SearchCriteria" into tUserSearchString
sqlquery_set tQueryA, "conditions", "lessons.title contains ':1'", tUserSearchString
sqlquery_set tQueryA "order by", "lessons.title"
Use any of the following commands to retrieve data from a SQL Query object. You can get data as an array, raw data or as records. The second parameter to each command is passed by reference and will contain the data from your database after the call has completed.
If you aren't familiar with what records are in SQL Yoga you can refer to the documentation on SQL Record objects.
sqlquery_retrieveAsArray tQueryA, tArrayA
sqlquery_retrieveAsData tQueryA, tData
sqlquery_retrieveAsRecords tQueryA, tRecordsA
For further information see the documentation for sqlquery_retrieveAsArray, sqlquery_retrieveAsData and sqlquery_retrieveAsRecords.
A SQL Query object provides a quick interface for creating data in a database.
Importing: When using sqlquery_create none of the validations or callbacks you've defined for Table objects will be triggered. You must use SQL Record objects in order to use those features.
The first step is to fill in an array variable with the values you want to insert into the database. The array keys should be the names of the columns in your database table.
put "My Dog's Car" into tRowA["title"]
put false into tRowA["draft"]
Once you have created the array you just pass it as a parameter to sqlquery_create along with the name of the table you want to create the record in. SQL Yoga will generate the query for you.
sqlquery_create "lessons", tRowA
put it into tAffectedRows
A SQL Query object provides a quick interface for deleting data in a database.
Importing: When using sqlquery_delete none of the validations or callbacks you've defined for Table objects will be triggered. You must use SQL Record objects in order to use those features.
The first step is to create a SQL Query object that can be passed to sqlquery_delete.
put sqlquery_createObject("lessons") into tQueryA
When calling sqlquery_delete, the where clause property of the SQL Query object will be used to perform the delete. You define this property by setting the conditions property using sqlquery_set.
## Specify that lesson with an id of 12 should be deleted.
sqlquery_set tQueryA, "conditions", "id is :1", 12
## Perform the delete.
sqlquery_delete tQueryA
put it into tAffectedRows
After setting properties for a SQL Query object you can use sqlquery_retrieveAsArray, sqlquery_retrieveAsData and sqlquery_retrieveAsRecords to retrieve your data without having to fuss with a database cursor. You can also access the database cursor directly when needed.
A SQL Query object can open a database cursor using sqlquery_retrieve. This command generates a SQL query using the SQL Query object properties and then opens the database cursor. You are then responsible for closing the database cursor using sqlquery_close.
You can access the RevDB cursor id through the cursor property:
put sqlquery_get(theQueryA, "cursor") into tCursor
## Do something with the cursor...
sqlquery_close theQueryA
Once you have opened a database cursor there are a couple of SQL Query object handlers that help navigate the cursor. They are:
sqlquery_moveToFirstRecordsqlquery_moveToLastRecordsqlquery_moveToNextRecordsqlquery_moveToPreviousRecordsqlquery_moveToRecord
Here is an example:
put sqlquery_createObject("lessons") into tQueryA
sqlquery_set tQueryA, "conditions", "id < 4"
sqlquery_retrieve tQueryA
sqlquery_moveToNextRecord tQueryA
if the result is true then
## You are now on the second record in the result set.
end if
SQL Yoga USER GUIDE
- Home
- SQL Yoga Objects
- Database Objects
- Connection Objects
- SQL Query Objects
- SQL Record Objects
- Table Objects
- Table Object Behaviors
- Relationships
- Scopes
- Schema
- Working with User Search Strings
- SQL Query Template Objects
- Error Handling
- Migrating from SQL Yoga 1.x
- Integrating with the Levure Application Framework