APLX Help : Help on APL language : System Functions & Variables : ⎕SQL Interface to External Database
|
||||||||||||||||||||||||||||||||||||
![]() |
|
|||||||||||||||||||||||||||||||||||
The system function To use In most cases, To make full use of Note: Because APLX ships on multiple platforms, and SyntaxIn most cases, If you are connecting simultaneously to more than one database, then you also need to supply a left argument to ResultIn general, the result of
It is recommended that you always use multiple assignment to split the result from (RC ERRMSG DATA)←⎕SQL 'Connect' 'aplxdb2' RC 3 ¯1024 0 0 ERRMSG [IBM][CLI Driver] SQL1024N A database connection does not exist. SQLSTATE=08003 In this example, an error has been reported by IBM's DB2 driver software. This method makes it very easy to check for errors by seeing if the first element of RC is non-zero. Connecting to a databaseSyntax: Before carrying out any database operation, you need to cause APLX to load the appropriate interface library, and connect to the underlying database. This is done using the The first parameter is the name of the interface driver. This is a shared library which interfaces between APLX and the database manager. It can be either a simple shared-library name (such as 'aplxodbc'), or a full path name (such as 'c:\apl\drivers\aplxodbc.dll'). In the former case, which is normally preferred because it is operating-system independent, APLX will load the driver from the bin directory of the APLX installation; the shared-library extension (.dll for Windows, .so for Linux and AIX, or .bundle for Mac OS X) will be added automatically. In the latter case, the file name will be used as supplied, and you need to provide the file extension. If the library cannot be loaded, either because the APLX interface driver is not found, or because the underlying database manager library is not properly installed, APLX will report the error LOGICAL UNIT NOT FOUND. Normally, you will use one of the drivers supplied as standard with APLX. These are currently: For 32-bit versions of APLX:
For APLX64:
The second and subsequent parameters depend on the exact database interface you are using, and on how your system is set up. If you are using ODBC, you can specify either a 'Connection String' or a 'Data Source Name' (DSN). Other database interfaces use a Database Alias, which is similar to a DSN. Specifying the database with a Connection String:A Connection String is a text vector containing a series of semicolon-delimited phrases, each of which is of the form 'keyword=value;' . These phrases fully or partially define the connection to the database. The parameters typically include the driver name, user name, connection parameters and so on, plus any driver-specific information such as the name of an Excel spreadsheet, the name of the database alias, or the network address of the database server. (Technical note: This method of connection uses the underlying SQLDriverConnect ODBC call.) Under Windows, if the Connection String is incomplete (i.e. if it does not fully define all the parameters needed to make the connection), the ODBC sub-system will display a dialog which allows you to set up the remaining parameters of the connection interactively. You can even specify an empty vector, in which case you can choose the ODBC driver, database, and all other parameters which are required. For MacOS, Linux, and AIX you need to specify a full connection string. On successful connection, For example, suppose you want to use
(RC ERRMSG CNS) ← ⎕SQL 'Connect' 'aplxodbc' '' A dialog should appear. Select the 'File DSN' tab, and press the New... button. Select the driver 'Microsoft Excel Driver', and save the DSN in some suitable scratch directory. When you click Next.. , the ODBC Excel driver should ask you for the name of the spreadsheet containing the data. (You can use the sample spreadsheet 'alcohol.xls' which is supplied with APLX in the 'ws' directory of the APLX installation, usually C:\Program Files\MicroAPL\APLX\ws). This should establish a connection to the Excel spreadsheet as though it were a database, and the CNS variable defined above should contain the full connection string which you can use to repeat the connection without the dialog: CNS DBQ=C:\Program Files\MicroAPL\APLX\ws\alcohol.xls;DefaultDir=C:\Program Files\MicroAPL\ APLX\;Driver={Microsoft Excel Driver (*.xls)};DriverId=790;FIL=excel 8.0;FI LEDSN=C:\Program Files\Common Files\ODBC\Data Sources\sample.dsn;MaxBufferSi ze=2048;MaxScanRows=8;PageTimeout=5;ReadOnly=1;SafeTransactions=0;Threads= 3;UID=admin;UserCommitSync=Yes; You should now be able to access the data using SQL commands, as described below: (RC ERRMSG DATA) ← ⎕SQL 'Do' 'select * from alcohol' RC 0 0 0 0 ⍴DATA 30 18 3 8↑DATA Australia 9.4 11.6 12.9 11.7 10.5 10 9.7 Austria 10.9 13.9 13.8 12.1 12.6 12.8 12.1 Belgium 8.9 12.3 14 12.9 12.1 11.5 11.7 Specifying the database with a Data Source Name:If the third parameter to 'Connect' is a text vector which does not end in a semi-colon and is not empty, APLX will assume that it represents a Data Source Name (DSN). A DSN is effectively a pre-defined Connection String, which encapsulates all the details needed to connect to the database. It can be stored in a text file as a "File DSN", or in the registry (or UnixODBC configuration file) as a "User DSN" or "System DSN". It is identified by a simple name. In some cases, the User Name and Password have to be entered as extra parameters. (Technical note: This method of connection uses the underlying SQLConnect ODBC call.)
Under Windows, you set up DSNs using Microsoft's 'ODBC Data Source Administrator' dialog. Under Linux or MacOS, you set them up using UnixODBC or iODBC configuration files. See the appropriate ODBC documentation for details, which will depend on the database you are using and where the data is held. Example using ODBC to access data from an Oracle Database. The Data Source Name has been configured in advance to be 'Oracle Sample', but you need to specify the user name and password: ⎕SQL 'Connect' 'aplxodbc' 'Oracle Sample' 'scott' 'tiger' 0 0 0 0 Specifying the database with a Database Alias:For DB2 and other database-specific (non-ODBC) drivers, the second parameter to Connect is the database alias, which is similar to a DSN. The third and fourth parameters are the User ID and Password, if these are required by the database. Example using IBM's DB2, assuming you have a database alias called 'DB2' defined: ⎕SQL 'Connect' 'aplxdb2' 'DB2' 0 0 0 0 Connecting to multiple databasesIf you want to connect to more than one database simultaneously, you will need to specify an integer left argument to Checking which connections are openSyntax: This returns an integer vector of the Disconnecting from a databaseSyntax: You must always disconnect from the database when you have finished using it, otherwise you may continue to tie up resources and in some cases may prevent other users from accessing the database. Disconnecting automatically closes any database statements you have open. Depending on the underlying driver, it will usually also roll back any uncommitted transactions. You can disconnect from all active databases using the statement: 0 0⍴(⎕SQL 'Connections') ⎕SQL ¨⊂'Disconnect' Executing simple database operationsSyntax: The 'Do' keyword allows you to execute almost any arbitrary SQL operation immediately. It takes a single parameter, which is a text vector containing the SQL statement you want to perform. For example, this can be a SELECT or UPDATE, or a more specialized operation such as GRANT. (However, it should not be a COMMIT or ROLLBACK, since these are done using the specific syntax described below). Any data returned by the database will be returned in the third element of the result from For example: ⎕DISPLAY ⎕SQL 'DO' 'SELECT * FROM CL_SCHED' ┌→────────────────────────────────────────────────────┐ │ ┌→──────┐ ┌⊖┐ ┌→──────────────────────────────────┐ │ │ │0 0 0 0│ │ │ ↓ ┌→──────┐ ┌→───────┐ ┌→───────┐ │ │ │ └~──────┘ └─┘ │ │CL3RPN │ 3 │09:00:00│ │10:30:00│ │ │ │ │ └───────┘ └────────┘ └────────┘ │ │ │ │ ┌→──────┐ ┌→───────┐ ┌→───────┐ │ │ │ │ │CL4SDM │ 3 │11:00:00│ │12:30:00│ │ │ │ │ └───────┘ └────────┘ └────────┘ │ │ │ │ ┌→──────┐ ┌→───────┐ ┌→───────┐ │ │ │ │ │CL4DSE │ 2 │09:00:00│ │10:30:00│ │ │ │ │ └───────┘ └────────┘ └────────┘ │ │ │ └∊──────────────────────────────────┘ │ └∊────────────────────────────────────────────────────┘ In this example, Using the 'Do' keyword has the advantage of being very simple. However, it has two disadvantages which mean that it will often be better to use the multi-stage 'Prepare' and 'Execute' operations described below. These are, firstly, that you have no control over the amount of data which is returned to you, and secondly that you cannot 'bind' APL data to your SQL operation at run-time - any parameters need to be hard-coded as text in the SQL statement which you execute. For these reasons, we recommend that you do not use the 'Do' keyword in applications for executing SQL 'Select' statements, unless you are certain that the data set will always be small and that there is therefore no danger of a WS FULL error being generated at some time in the future when the database may have grown. Dynamic SQL statementsAs an alternative to executing a statement directly using the 'Do' keyword, you can prepare a statement in advance, optionally bind parameters to it, and then execute it, retrieving any results in chunks rather than all in one go. To do this, you carry out the following steps:
Preparing the dynamic SQL statementSyntax: This operation creates a new SQL statement. The parameter Name is a character vector which is the internal name by which you will refer to the statement subsequently (names are case-sensitive). The parameter Statement is the SQL statement you want to execute. This can optionally include ? characters; these act as placeholders for APL data which will be substituted when the statement is executed. For example: ⎕SQL 'Prepare' 'cs1' "UPDATE CL_SCHED SET DAY=? WHERE CLASS_CODE=?" 0 0 0 0 In this example, 'cs1' is the name of the statement, which will be used to identify it subsequently. The SQL statement is an UPDATE, with two parameters which will be filled in when it is executed. Executing the prepared statementSyntax: This causes the prepared statement to be submitted to the database for execution. Name is the name of the statement (i.e. the name you supplied when it was prepared). If there were any ? placeholders in the statement, you need to provide one element for each such placeholder; these will be substituted at this stage. For example: ⎕SQL 'Execute' 'cs1' 2 'CL4DSE' 0 0 0 0 SQL_CURSH200C4 In this example, the numeric data 2 is substituted for the first ? in the SQL statement, the character vector 'CL4DSE' is substituted for the second, and the statement is executed. The result of the operation follows the standard pattern. If a data set is available (i.e. the statement is something like a SELECT), a 1 will be placed in the fourth element of the return-code part of the result - you can retrieve the data using the 'Fetch' keyword. When you use the 'Execute' keyword, the third element of the result of Fetching the result setSyntax: If the statement you have executed returns a result set, you can fetch the data in chunks, using the 'Fetch' keyword. Name is the name of the statement you have just executed. The optional Count parameter is the maximum number of rows to return (if you omit this, APLX will try to return all the rows). The result follows the standard pattern, with a 1 in the fourth element of the return code vector indicating that more data might be available, so you should call Fetch again. The data itself is returned as a matrix (usually nested), as the third element of the result. For example, suppose we have a table with just three rows. We can retrieve these immediately using the 'Do' keyword: ⎕SQL 'DO' 'SELECT * FROM CL_SCHED' 0 0 0 0 CL3RPN 3 09:00:00 10:30:00 CL4SDM 3 11:00:00 12:30:00 CL4DSE 2 09:00:00 10:30:00 Alternatively, we can use dynamic SQL with parameter substitution as follows, in this case retrieving one row at a time: ⎕SQL 'Prepare' 'cs2' "SELECT * FROM CL_SCHED where Starting=?" 0 0 0 0 ⎕SQL 'Execute' 'cs2' '9:00' 0 0 0 1 SQL_CURSH200C4 ⎕SQL 'Fetch' 'cs2' 1 0 0 0 1 CL3RPN 3 09:00:00 10:30:00 ⎕SQL 'Fetch' 'cs2' 1 0 0 0 1 CL4DSE 2 09:00:00 10:30:00 ⎕SQL 'Fetch' 'cs2' 1 0 0 0 0 ⎕SQL 'Close' 'cs2' 0 0 0 0 In this example, we have selected only those rows where the column 'Starting' (a time) is 9:00. The first time we call 'Fetch', we get the first row only because we have set the maximum number of rows to be returned to be 1. The return code indicates that more data might be available, so we call 'Fetch' again to retrieve the next row. Finally, the call to 'Fetch' returns an empty matrix as the third element (because we have now successfully read all the data), and the 'more' indicator is 0, so there is no more data to be read. We 'Close' the statement. In a real example, using a large database, there might be many thousands of rows returned. For efficiency, you would probably retrieve these in blocks of several hundred rows (depending on the workspace available and your processing requirements). Note that the 'more' indicator of 1 means only that more data might be available; when you come to fetch it, the database may report that you are in fact at the end of the data set, and return no more rows. Once this happens, the 'more' indicator will revert to 0. Describing the result setSyntax: After you have prepared or executed a statement, you can use the 'Describe' keyword to determine the types of the columns (if any) which will be returned. (Doing this before the staement is executed is valid, but may be inefficient, depending on the underlying database). The data description is returned as a four-row matrix as the third element of the result of ⎕SQL 'Prepare' 'cs1' 'SELECT * FROM CL_SCHED' 0 0 0 0 ⎕SQL 'Execute' 'cs1' 0 0 0 1 SQL_CURSH200C4 ⎕SQL 'Describe' 'cs1' 0 0 0 0 CLASS_CODE DAY STARTING ENDING CLASS_CODE DAY STARTING ENDING CHAR 7 SMALLINT TIME TIME 0 1 1 1 In this example, the first column of the data set is called CLASS_CODE, and its label is also CLASS_CODE. It is of type CHAR, with a length of 7 characters. It cannot be NULL. The second column is called DAY, and is a small integer, which can be NULL. The third and fourth columns are called STARTING and ENDING, are of type TIME, and can be NULL. Closing the prepared statementSyntax: This closes the prepared statement and releases resources associated with it. Transactions and Isolation LevelsOne of the key features of most modern relational database systems is the ability to provide a consistent view of the data even when multiple users are changing it simultaneously, and to roll-back any changes if an error occurs within a sequence of updates. Syntax: This feature allows you to specify whether each SQL operation should be regarded as a separate transaction. If you set Value to 1 (which is the default), a COMMIT will implicitly be executed after every SQL operation you perform. This means that you will not be able to roll-back changes if an error occurs. If Value is set to 0, you will need to use the 'Commit' or 'Rollback' keywords explicitly when you make any changes to the database. If you omit Value, the current state of the parameter is returned as the third element of the result of Caution: If you set 'Autocommit' to 0, you must remember to Commit any changes before ending the session, otherwise they will be rolled back when you disconnect from the database. Syntax: Causes any pending changes to the database to be permanently committed. This has no effect if 'Autocommit' is set to 1. Syntax: Causes any pending changes to the database to be thrown away, and the database returned to the state it was in at the time of the most recent COMMIT. This has no effect if 'Autocommit' is set to 1, since in this case all changes will automatically have already been committed. Syntax: Allows you to set or query the method used to handle simultaneous changes to the database by multiple users (if this is supported by the underlying database). The parameter Code is a two-character string, which sets the isolation level as follows:
If you omit the Code parameter, the current setting is returned as the third element of the result. Note that not all database systems and drivers support all these isolation levels, or any at all; if you try to use an unsupported case, an SQL error will be returned. For example, the Excel driver under ODBC does not support isolation levels and rollbacks: ⎕SQL 'Isolation' 3 106 0 0 [Microsoft][ODBC Excel Driver]Optional feature not implemented Determining available Data Source NamesSyntax: This call allows you to determine which Data Sources are available. It takes a single parameter, which is the name of the APLX database interface (usually 'aplxodbc' for 32-bit versions of APLX, or 'aplx64odbc' for APLX64). The data returned as the third element of the result is a nested matrix of two columns. The first column is the Data Source Name (i.e. the name you should supply to 'Connect'), the second the driver description. The results will depend on what User and System Data Sources you have set up on your system. In this example, we have five DSNs defined: ⎕DISPLAY ⎕SQL 'Datasources' 'aplxodbc' ┌→───────────────────────────────────────────────────────────────────────────┐ │ ┌→──────┐ ┌⊖┐ ┌→─────────────────────────────────────────────────────────┐ │ │ │0 0 0 0│ │ │ ↓ ┌→─────────────────┐ ┌→──────────────────────────────┐ │ │ │ └~──────┘ └─┘ │ │MS Access Database│ │Microsoft Access Driver (*.mdb)│ │ │ │ │ └──────────────────┘ └───────────────────────────────┘ │ │ │ │ ┌→──────────┐ ┌→─────────────────────────────┐ │ │ │ │ │dBASE Files│ │Microsoft dBase Driver (*.dbf)│ │ │ │ │ └───────────┘ └──────────────────────────────┘ │ │ │ │ ┌→───────────┐ ┌→─────────────────────────────┐ │ │ │ │ │Excel Sample│ │Microsoft Excel Driver (*.xls)│ │ │ │ │ └────────────┘ └──────────────────────────────┘ │ │ │ │ ┌→───┐ ┌→─────────┐ │ │ │ │ │MQIS│ │SQL Server│ │ │ │ │ └────┘ └──────────┘ │ │ │ │ ┌→──┐ ┌→──────────────────┐ │ │ │ │ │DB2│ │IBM DB2 ODBC DRIVER│ │ │ │ │ └───┘ └───────────────────┘ │ │ │ └∊─────────────────────────────────────────────────────────┘ │ └∊───────────────────────────────────────────────────────────────────────────┘ Note: Microsoft's ODBC driver will not allow you to use this call once you have connected to a data source. Examining the catalog of tablesSyntax: Returns (as the third element of the result) a nested array giving details of the tables and pseudo-tables available in the database (including system tables and views). The details returned are contained in a five-column array, where each row represents a table in the database. The columns are:
The result from this call can be very large, so in most cases you should provide parameters to limit the scope of the enquiry. This is done by providing up to four character vectors as parameters. The first three are pattern-matching strings for the Catalog, Schema and Table name respectively. An underscore (_) character stands for any single character, a percent (%) character stands for any sequence of zero or more characters, and other characters stand for themselves. The case of a letter is significant. An empty vector matches against any string. The fourth parameter contains a list of table types you are interested in, separated by commas if there are more than one. Valid table type identifiers are: TABLE, VIEW, SYSTEM TABLE, ALIAS, SYNONYM. If this parameter is an empty vector, this is equivalent to specifying all of the possibilities for the table type identifier. For example, this statement returns all user tables, but not Views or System tables: ⎕DISPLAY ⎕SQL 'TABLES' '' '' '' 'TABLE' ┌→─────────────────────────────────────────────────────┐ │ ┌→──────┐ ┌⊖┐ ┌→───────────────────────────────────┐ │ │ │0 0 0 0│ │ │ ↓ ┌⊖┐ ┌→──┐ ┌→───────┐ ┌→────┐ ┌⊖┐ │ │ │ └~──────┘ └─┘ │ │0│ │RPN│ │CL_SCHED│ │TABLE│ │0│ │ │ │ │ └~┘ └───┘ └────────┘ └─────┘ └~┘ │ │ │ │ ┌⊖┐ ┌→──┐ ┌→─────────┐ ┌→────┐ ┌⊖┐ │ │ │ │ │0│ │RPN│ │DEPARTMENT│ │TABLE│ │0│ │ │ │ │ └~┘ └───┘ └──────────┘ └─────┘ └~┘ │ │ │ │ ┌⊖┐ ┌→──┐ ┌→──────┐ ┌→────┐ ┌⊖┐ │ │ │ │ │0│ │RPN│ │EMP_ACT│ │TABLE│ │0│ │ │ │ │ └~┘ └───┘ └───────┘ └─────┘ └~┘ │ │ │ │ ┌⊖┐ ┌→──┐ ┌→────────┐ ┌→────┐ ┌⊖┐ │ │ │ │ │0│ │RPN│ │EMP_PHOTO│ │TABLE│ │0│ │ │ │ │ └~┘ └───┘ └─────────┘ └─────┘ └~┘ │ │ │ │ ┌⊖┐ ┌→──┐ ┌→─────────┐ ┌→────┐ ┌⊖┐ │ │ │ │ │0│ │RPN│ │EMP_RESUME│ │TABLE│ │0│ │ │ │ │ └~┘ └───┘ └──────────┘ └─────┘ └~┘ │ │ │ │ ┌⊖┐ ┌→──┐ ┌→───────┐ ┌→────┐ ┌⊖┐ │ │ │ │ │0│ │RPN│ │EMPLOYEE│ │TABLE│ │0│ │ │ │ │ └~┘ └───┘ └────────┘ └─────┘ └~┘ │ │ │ │ ┌⊖┐ ┌→──┐ ┌→──────┐ ┌→────┐ ┌⊖┐ │ │ │ │ │0│ │RPN│ │IN_TRAY│ │TABLE│ │0│ │ │ │ │ └~┘ └───┘ └───────┘ └─────┘ └~┘ │ │ │ │ ┌⊖┐ ┌→──┐ ┌→──┐ ┌→────┐ ┌⊖┐ │ │ │ │ │0│ │RPN│ │ORG│ │TABLE│ │0│ │ │ │ │ └~┘ └───┘ └───┘ └─────┘ └~┘ │ │ │ │ ┌⊖┐ ┌→──┐ ┌→──────┐ ┌→────┐ ┌⊖┐ │ │ │ │ │0│ │RPN│ │PROJECT│ │TABLE│ │0│ │ │ │ │ └~┘ └───┘ └───────┘ └─────┘ └~┘ │ │ │ │ ┌⊖┐ ┌→──┐ ┌→────┐ ┌→────┐ ┌⊖┐ │ │ │ │ │0│ │RPN│ │SALES│ │TABLE│ │0│ │ │ │ │ └~┘ └───┘ └─────┘ └─────┘ └~┘ │ │ │ │ ┌⊖┐ ┌→──┐ ┌→────┐ ┌→────┐ ┌⊖┐ │ │ │ │ │0│ │RPN│ │STAFF│ │TABLE│ │0│ │ │ │ │ └~┘ └───┘ └─────┘ └─────┘ └~┘ │ │ │ └∊───────────────────────────────────┘ │ └∊─────────────────────────────────────────────────────┘ The following variant limits the selection further, to tables whose name begins with 'EMP': ⎕DISPLAY ⎕SQL 'TABLES' '' '' 'EMP%' 'TABLE' ┌→─────────────────────────────────────────────────────┐ │ ┌→──────┐ ┌⊖┐ ┌→───────────────────────────────────┐ │ │ │0 0 0 0│ │ │ ↓ ┌⊖┐ ┌→──┐ ┌→──────┐ ┌→────┐ ┌⊖┐ │ │ │ └~──────┘ └─┘ │ │0│ │RPN│ │EMP_ACT│ │TABLE│ │0│ │ │ │ │ └~┘ └───┘ └───────┘ └─────┘ └~┘ │ │ │ │ ┌⊖┐ ┌→──┐ ┌→────────┐ ┌→────┐ ┌⊖┐ │ │ │ │ │0│ │RPN│ │EMP_PHOTO│ │TABLE│ │0│ │ │ │ │ └~┘ └───┘ └─────────┘ └─────┘ └~┘ │ │ │ │ ┌⊖┐ ┌→──┐ ┌→─────────┐ ┌→────┐ ┌⊖┐ │ │ │ │ │0│ │RPN│ │EMP_RESUME│ │TABLE│ │0│ │ │ │ │ └~┘ └───┘ └──────────┘ └─────┘ └~┘ │ │ │ │ ┌⊖┐ ┌→──┐ ┌→───────┐ ┌→────┐ ┌⊖┐ │ │ │ │ │0│ │RPN│ │EMPLOYEE│ │TABLE│ │0│ │ │ │ │ └~┘ └───┘ └────────┘ └─────┘ └~┘ │ │ │ └∊───────────────────────────────────┘ │ └∊─────────────────────────────────────────────────────┘ Examining the catalog of columnsSyntax: This works in a similar way to the 'Tables' keyword. Again, you should normally use parameters to limit the size of the returned result. All the parameters are pattern-matching strings, following the same rules as for 'Tables'. The result has 18 columns, as follows:
For example: ⎕DISPLAY 3⊃⎕SQL 'COLUMNS' '' '' 'CL%' ┌→──────────────────────────────────────────────────────────────────────────────────┐ ↓ ┌⊖┐ ┌→──┐ ┌→───────┐ ┌→───────┐ ┌→───┐ ┌⊖┐ ┌⊖┐ ┌⊖┐ ┌⊖┐ ┌⊖┐ ┌→──┐ │ │ │0│ │RPN│ │CL_SCHED│ │CLASS_CD│ 1 │CHAR│ 7 7 │0│ │0│ 1 │0│ │0│ 1 │0│ 7 1 │NO │ │ │ └~┘ └───┘ └────────┘ └────────┘ └────┘ └~┘ └~┘ └~┘ └~┘ └~┘ └───┘ │ │ ┌⊖┐ ┌→──┐ ┌→───────┐ ┌→──┐ ┌→──┐ ┌⊖┐ ┌⊖┐ ┌⊖┐ ┌⊖┐ ┌→──┐ │ │ │0│ │RPN│ │CL_SCHED│ │DAY│ 5 │INT│ 5 2 0 10 1 │0│ │0│ 5 │0│ │0│ 2 │YES│ │ │ └~┘ └───┘ └────────┘ └───┘ └───┘ └~┘ └~┘ └~┘ └~┘ └───┘ │ │ ┌⊖┐ ┌→──┐ ┌→───────┐ ┌→───────┐ ┌→───┐ ┌⊖┐ ┌⊖┐ ┌⊖┐ ┌⊖┐ ┌→──┐ │ │ │0│ │RPN│ │CL_SCHED│ │STARTING│ 92 │TIME│ 8 6 0 │0│ 1 │0│ │0│ 9 2 │0│ 3 │YES│ │ │ └~┘ └───┘ └────────┘ └────────┘ └────┘ └~┘ └~┘ └~┘ └~┘ └───┘ │ │ ┌⊖┐ ┌→──┐ ┌→───────┐ ┌→─────┐ ┌→───┐ ┌⊖┐ ┌⊖┐ ┌⊖┐ ┌⊖┐ ┌→──┐ │ │ │0│ │RPN│ │CL_SCHED│ │ENDING│ 92 │TIME│ 8 6 0 │0│ 1 │0│ │0│ 9 2 │0│ 4 │YES│ │ │ └~┘ └───┘ └────────┘ └──────┘ └────┘ └~┘ └~┘ └~┘ └~┘ └───┘ │ └∊──────────────────────────────────────────────────────────────────────────────────┘ Examining details of the connectionSyntax: This uses the same keyword 'Describe' which is used to provide details of a result set (see above). However, if no cursor name is supplied as a parameter, APLX returns details of the connection as a nested vector of 9 elements, as follows:
The following are the minimum sets of features available at each conformance level (each level includes the features of the lower levels, and a specific driver may optionally implement further features): Minimum SQL Grammar:
Core SQL Grammar:
Extended SQL Grammar:
For example, DB2 provides full SQL facilities: ⊃ 3⊃⎕SQL 'DESCRIBE' SAMPLE 03.00 DB2/NT 08.01.0003 DB2CLI.DLL 08.01.0003 SAMPLE ALICE Extended SQL Grammar Retrieving the Version number of the APLX database driverSyntax: This call returns (as the third element of the explicit result) an integer scalar of the APLX database driver version; 100 corresponds to version 1.00. The Interface parameter is the same as that of the Connect keyword, for example 'aplxodbc'. Retrieving the database connection and statement handlesSometimes, you may need to access the underlying ODBC, CLI or other database driver directly, for example using Syntax: The information returned (as the third element of the explicit result) is system-specific; for ODBC, if you do not specify a statement name, you get a two-element result of the Environment and Database Connection handles. If you specify a statement, you get a scalar which is the statement handle: ⎕DISPLAY ⎕SQL 'Handle' ┌→──────────────────────────────────┐ │ ┌→──────┐ ┌⊖┐ ┌→────────────────┐ │ │ │0 0 0 0│ │ │ │52237280 52237480│ │ │ └~──────┘ └─┘ └~────────────────┘ │ └∊──────────────────────────────────┘ ⎕DISPLAY ⎕SQL 'Handle' 'cs1' ┌→───────────────────────┐ │ ┌→──────┐ ┌⊖┐ │ │ │0 0 0 0│ │ │ 52238968 │ │ └~──────┘ └─┘ │ └∊───────────────────────┘ Data ConversionsBecause the underlying database will usually support a set of data type for columns which are more extensive than those available in APL, APLX automatically converts data types as follows:
See also NULL handlingAn important concept in SQL is that of NULL data items, i.e. items which have no value. A NULL is handled in a very special (and often counter-intuitive) way in SQL statements; for example, in a SELECT statement, a NULL does not match anything, including another NULL. For a character field, a NULL is not the same as a zero-length string.
Summary of
|
||||||||||||||||||||||||||||||||||||
APLX Help : Help on APL language : System Functions & Variables : ⎕SQL Interface to External Database
|
Copyright © 1996-2010 MicroAPL Ltd