Using Advanced Database ActionsTransaction and Direct DBMS Actions, and Joining of Database TablesYou can put several database actions together to create a single transaction that manages the work being performed. Using Begin Transaction and End Transaction actions you can specify where to begin, commit, and rollback database changes. (You cannot use transactions with FileMaker Pro data sources.) You can use the Direct DBMS action to execute specified SQL statements and return any results generated. Relational databases let you specify joins to permit searches involving more than one table. A join tells the database how the tables are related. A standard join preserves only those rows from a search in which a match exists with the joined table. An outer join preserves all the rows in one of the tables, even if there is no match with the other table. The topics covered in this chapter include:
Creating Database TransactionsTango supports special database actions that allow you to specify where to begin, commit, and rollback database changes. Using the Begin Transaction and End Transaction actions, you can create a well-defined single transaction. Normally, actions executed by Tango Server that change the content of databases (Insert, Update, Delete, and Direct DBMS actions) cause an immediate change to the database. This is because Tango automatically sends a COMMIT command as the final step in its execution of these actions. However, Transaction actions let you control when database changes are made permanent and also let you undo (or ROLLBACK) the effects of actions that have been executed. To perform a Transaction action, Tango maintains a database connection longer than it would for other actions. You should consider the impact this may have on your server and database resources before deciding to use Transactions in your application file.
Setting Up a Transaction ActionBegin TransactionThe Begin Transaction action indicates the beginning of a transaction on a particular data source. To set up a Begin Transaction action |
|
For more information, see "Setting Data Sources for Actions". For more information, see "Connecting to Large Data Sources".. |
End Transaction
The End Transaction action marks the end of the transaction and either commits it (saves all the changes) or rolls it back (discards all the changes). To set up an End Transaction action
Executing a Transaction ActionIf Tango Server detects that an End Transaction action is being executed without first executing a Begin Transaction action, it reports a runtime error. It is also an error to begin another transaction before an existing transaction is committed or rolled back. Database actions on data sources that are not the transaction data source are automatically committed. If the application file ends without executing an associated End Transaction action or a Return action, then a Rollback End Transaction action executes automatically.
Executing SQLThe Direct DBMS action executes specified SQL statements and returns any results generated. For FileMaker Pro data sources, this action type allows you to execute a FileMaker Pro script: see "Executing a FileMaker Pro Script (Direct DBMS Action)". Setting Up a Direct DBMS ActionTo set up a Direct DBMS action |
For more information, see "Connecting to Large Data Sources". |
The Direct DBMS Action Editing WindowSQL Tab
Click the SQL tab to display the Direct DBMS action SQL text area in which to enter the SQL to be executed. |
For information on constructing SQL statements, consult your database or ODBC driver documentation. |
All statements are executed against the database specified in the data source associated with the Direct DBMS action. You can easily enter column or table names by dragging them from the Data Sources Workspace. If you drag multiple columns, they are separated with commas. You can also perform standard editing operations in the Direct DBMS action editing window by one of the following methods: You can reference any value-returning Tango meta tags in your SQL. To insert a meta tag in the Direct DBMS window |
For information on filling in the Insert Meta Tag dialog box, see "Inserting Meta Tags". |
The Insert Meta Tag dialog box appears, allowing you to specify a meta tag, and inserts it at the insertion point in the SQL text area. |
For an example of executing different SQL based on the type of data source in use, see "<@DSNUM>" in the Meta Tags and Configuration Variables manual. |
You can use the <@IF>, <@IFEQUAL>, and <@IFEMPTY> meta tags in the SQL text to include or exclude SQL based on the result of a comparison at execution time. For example, you could use this capability to execute different SQL based on the type of data source in use. Direct DBMS SQL Auto-Encoding |
For more information, see "SQL" in the Meta Tags and Configuration Variables manual. |
Tango Server automatically performs SQL encoding on meta tag values substituted in Direct DBMS SQL. For example, if a variable called myName contains "O'Brien": SELECT * FROM customer WHERE cust_name = '<@VAR NAME=myName>' SELECT * FROM customer WHERE cust_name = 'O''Brien' If Tango did not do this, the result would be: |
For more information, see "Encoding Attribute" in the Meta Tags and Configuration Variables manual. |
If your Direct DBMS SQL contains meta tags that evaluate to an entire (or partial) SQL statement constructed elsewhere, the quote-doubling may cause DBMS errors. This is because all single quotes are doubled, even those meant to delimit a string. In this case, the solution is to modify the meta tag(s) returning your SQL by adding the ENCODING=none attribute. For example: <@VAR NAME=mySQL ENCODING=none> Results TabClick the Results tab to display the results options you can set for the Direct DBMS action. You can specify options for the maximum number of records to retrieve from the data source and at which result record number retrieval begins. Number of rows to retrieve
Start retrieval at match numberUse this option to skip some of the matching records. Enter "1" (the default) to start retrieval with the first matching record. When a value other than "1" is entered into this field, the Direct DBMS action returns records starting at that number, skipping any records before it. Both of these fields can contain meta tags which return values. Executing a Direct DBMS ActionWhen Tango Server executes a Direct DBMS action, the specified SQL is sent to the data source for execution. Any result rows are returned to Tango and may be
accessed in the action's Results HTML. As with the Search action, a
<@ROWS> You can use <@COLUMN> to refer to your columns by name for ODBC data sources, but for non-ODBC data sources, you must refer to your columns in Results HTML by number, using the <@COL> meta tag. For example, if your Direct DBMS action executed the following statements with a non-ODBC data source: SELECT maintable.price,
maintable.classification, maintable.manufacturer the following Results HTML would print the database results: <@ROWS> When you perform an SQL query where you are selecting an aggregate function or calculated column, the column name depends on the database: If the Direct DBMS action generates no database results, and you have specified No Results HTML for the action, that HTML is processed instead of the Results HTML. You can use bound values in SQL by using the <@BIND> meta tag. For more information, see "<@BIND>" in the Meta Tags and Configuration Variables manual.
Executing a FileMaker Pro Script (Direct DBMS Action)
You can use the Direct DBMS action to execute a FileMaker Pro script. To execute a FileMaker Pro script
When Tango Server executes a Direct DBMS action for a FileMaker Pro database, the records specified by the script are returned. Joining Database Tables |
For more information on joins, consult your DBMS documentation, such as, The Practical SQL Handbook (J.S. Bowman, et. al., ISBN: 0-201-62623-3), or any other good SQL reference guide. |
To understand how joins work, consider a database with company and user information in two different tables. You want to create a search to find the company for any given user, and display in your browser the company information with the corresponding user's name. Because the user table contains only the company's identifier, you have to join the two tables to get full information on the company.
The customer table (COMPANY) has a record for each company including a company name, address, manager, and Web site. The user table (USERS) has a record for each user including their company, name, and telephone number. The COMPANY table is related to the USERS table by an identifier in the COMPANY column: both tables have COMPANY columns. |
For more information, see "Creating a Join in a Search Action". |
Using a Tango search, you select the columns you want to relate and define the type of join in the joins tab of the Search action or Search builder. In addition to the standard join, you can define an outer join, which can be left or right. A left outer join means all rows in the left-specified table are returned, including those with no match in the right-specified table. A right outer join means all rows in the right-specified table are returned, including those with no match in the left-specified table. For this example, you would select the COMPANY column in the left table, COMPANY, and the COMPANY column in the right table, USER. Then, from the pop-up menu, you select the type of join you want to use.
Working With JoinsTo work with joins, you must first have your Search action or Search Builder action editing window open. You can include columns from more than one table in a search, if you define joins for the tables. If you select columns from more than one table in a search, a message appears telling you to define a join. Choose either Define to create the join definition now or Later if you want to define the join at a later time. Choosing Define opens the Joins window of the current dialog box. You create, modify, and delete joins using the Joins tab of the Search editing window or Search Builder. When you define the join, it adds the columns to the search. In the Search Builder, you must define the join before you build the actions for the search or save the application file.
Creating a Join in a Search ActionTo create a join, drag the columns you want to include in the search from the Data Sources Workspace into the Joins window.
To create a new join in a Search action
Inserting a JoinTo insert a join definition
Editing a JoinTo edit a join definition
The field changes to a pop-up menu so you can choose a different entry. Deleting a JoinTo delete a join definition
Creating a Join in the Search BuilderYou create, edit, and delete joins in the Joins window the same way you do for a Search action. See "Creating a Join in a Search Action". The Search and Record List option groups of the Search Builder share the same join information because they both apply to the same generated Search action. You can specify separate join information for the Record Detail option group. You switch from the Search, Record List, or Record Detail window to the associated Joins window by clicking the General and Joins tabs, respectively, at the top of the main Search Builder window.
|
Copyright © 1999, Pervasive Software. All rights reserved.