|
|
Section Intro: Generic Sql Interface
Generic Sql InterfaceSynopsis:With the plethora of ANSI SQL compatible databases present on the marketplace, Meta-HTML provides a single comprehensive interface which may be used to access any of those which are supported.
Such databases include Using the Meta-HTML Generic SQL Interface (GSQL), you can more rapidly develop and inexpensively prototype your application -- then migrate to full-blown production scale without writing a single line of code! For example, you might build an in-house HR application using the MySQL database. Once you are happy with the application flow and GUI, you can port the application to Oracle 8i by changing two lines of code:
<sql::set-database-type mysql>
<set-var app::dsn = "host=localhost;database=hr">
...
<sql::set-database-type odbc>
<set-var app::dsn = "HOST=production;database=HR;SVT=Oracle 8i;...">Commands: More Information:
The Meta-HTML Generic SQL Interface works by providing all of the
underlying functionality for each specific database type at a generic
top-level interface which never has to change. You specify which type
of database that you would like to use with a single function call,
using Although there are specific low-level functions implementing the database connectivity, the higher-level routines transparently make extremely efficient use of them to handle your requests. Because the low-level functions exist, you can easily write code which copies data from one type of database (say mSQL) to another type (say an Oracle ODBC database).
Returns the complete path of the executable program which is used to
perform administration functions on the underlying SQL database.
For example,
Return the number of rows affected by the last SQL query execution, associated with the database referenced indirectly by CURSOR. This may be completely unrelated to the query which created the cursor object, depending on the underlying database which is in use.
For those underlying database engines which keep information
about the database structure within the database itself, a
request to
Called with a CURSOR (the result from a call to
If there is no result set, or if the result set is empty, the empty string is returned.
<sql::with-open-database db dsn="host=localhost;database=admin">
<set-var query="select * from users">
<sql::database-exec-query db <get-var-once query> cursor=row>
<set-var any? = <sql::database-next-record row>>
<when <get-var-once any?>>
Result: <sql::cursor-get-column row 0>
</when>
</sql::with-open-database>
Returns an association list of properties on the specified COLUMN in the table TABLE. Example: ((NAME . realname) (LENGTH . 30) (TYPE . GSQL_CHAR) (IS_UNIQUE . "") (IS_NULLABLE . true) (QUALIFIER . "") (OWNER . "") (TYPENAME . "") (PRECISION . 0) (SCALE . 0) (RADIX . 0) (TABLE . accounts))
This is actually implemented by fetching information on all
columns and selecting the specified one. This means that you should
use the command
Returns a newline separated list of the columns present in TABLENAME in the open SQL database referenced by DBVAR. The keyword RESULT=VARNAME argument specifies the name of a variable to put the result array into; if not present, the array is returned to the caller.
Analogous to
Deletes the specified record in the database referenced by
DBVAR. The record to delete is specified by KEY and
KEYNAME. An SQL You must supply values for TABLE and KEYNAME.
Executes the SQL query in QUERY-STRING by sending it to the open database connection referenced by DBVAR. QUERY-STRING can be any Meta-HTML expression which evaluates to a valid SQL query or command string.
Returns a cursor object, which can be passed to
If the keyword argument CURSOR=VARNAME is given, the returned cursor value is stored in VARNAME.
Executes the SQL query in QUERY-STRING, on the open database connection specified by DBVAR. Returns TRUE if no errors are encountered.
This function differs from
<sql::database-exec-sql
db "DELETE FROM maillists WHERE list = '<get-var-once listname>'">
Load variable values from the database referenced by DBVAR and KEY into the package specified by PACKAGENAME (defaulting to the current package if not supplied). TABLENAME and FIELDNAME must be supplied -- they specify the table and primary key field to be operated on.
Fill the variables in PACKAGENAME with the values of the next record which last satisfied the search referenced by CURSORVAR. If PACKAGENAME is not supplied, then return an alist of the values instead. Each subsequent call gets a subsequent record from the list of results, until all of the results are exhausted.
Returns If COLNAMES is supplied, then the column values of this result are bound sequentially to the comma-separated list of names in NAMELIST instead of the field names in the result set.
<sql::database-next-record cursor package=tmp
colnames="a.name, a.partnum, b.name">
<get-var-once tmp::b.name>
<get-var-once tmp::a.name>
If COLNAMES is non-null, or the database connection option
Select and optionally format records in the database referenced by DBVAR according to the criterion in FEXPR. QUERY is an SQL query, which is expected to return a list of rows. For each result row, EXPR is then evaluated, with the column values of each row bound to their corresponding column name. If the result of that evaluation is not an empty string, then that record is selected for further processing by either FORMAT, KEYS, or to return in plain text the list of keys. If FORMAT=FEXPR is present, FEXPR is an expression to evaluate in the context of the database fields, (as with EXPR). The value of "true" for EXPR is specially optimized to for those queries where no additional processing is necessary.
The example below shows an SQL query which is formatted as rows
of an HTML table. In this case, the EXPR is simply the
constant
<table border=1>
<tr> <th>Name</th> <th>Age</th> </tr>
<sql::with-open-database db dsn="DATABASE=ROLO;HOST=localhost">
<sql::database-query db true
"SELECT * FROM people
WHERE name like 'Washington' ORDER BY lastname"
format=<group <tr>
<td> <get-var lastname>, <get-var firstname> </td>
<td> <get-var-once age> </td>
</tr>>>
</sql::with-open-database>
</table>
If KEYS=VARNAME is given, then VARNAME is the name of a variable to receive the array of keys which satisfied the query. If you specify an argument for KEYS, you must also specify which column to collect the values from, using the KEYNAME=FIELDNAME keyword argument. If the optional argument COLNAMES=NAMELIST is supplied, then for each row, column values are bound sequentially to these comma separated names instead of the column names in the result set.
<sql::database-query db true <get-var-once query>
colnames="foo.name, bar.name"
format=<group FOO.NAME IS <get-var-once foo.name>
BAR.NAME IS <get-var-once bar.name>>>
If PREFIXTABLENAMES is non-null, or the database
connection option The WINDOW-START and WINDOW-LENGTH keywords may be used to limit the amount of data that is returned from the SQL server. For example, to get the 10 most recently hired employees from the database, one might write: <sql::database-query db true window-start=0 window-length=10
format=<array-append <package-to-alist> ^::results>
"SELECT * FROM employees ORDER BY hiring_date DESC">
Returns an array of association lists giving information about each column in the result set specified by CURSOR. If the keyword argument RESULT=VARNAME is given, the resultant array is placed into that variable -- otherwise it is simply returned.
Save the variables in PACKAGE associated with the value KEY in column KEYNAME in the table TABLENAME of the database referenced by DBVAR. This only saves variables which have names matching existing table column names. Package prefixes are stripped from the variables, and the symbol name is used as the column name to store the data. Symbol names and column names are treated in a case-insensitive manner.
The implementation is to first attempt to do a SQL
Example: Say we want to save some information about employee Kate Mulgrew, in a table of employees, which has a primary key field named "id", and we want to save this record with id=103:
<set-var record::name="Kate Mulgrew" record::age=45
record::salary=34000 record::dept=travel>
<set-var saved? =
<sql::database-save-package db 103 record table=employees keyname=id>>
If a variable in the package you are saving corresponds to a column with a numeric field type, and the value of the variable is the empty string, the system will attempt to store a NULL value into this field of the record. If the table does not support NULL values on that column, the operation will fail.
Write a record to the open SQL database connection referenced by DBVAR, using KEY as the value of the primary key, on the table TABLE. The keyword argument KEY is the SQL column name of the column that you are specifying as the primary key. For each VAR, treat the name of VAR as a column name, and set that column's value to the value stored within VAR.
An NOTE: If the key field for table you are using is not configured as the primary key, then it is possible to create duplicate entries in the database. If you pass in a variable VAR which does not match a column name in TABLE, that variable is silently ignored.
If you try to save character data into a numeric field, the
As always, the most recent query executed is available by
calling the function
Sets various database-specific options. Possible values for OPTION-NAME are:
Returns an array of association lists, one per table in the open SQL database referenced by DBVAR. Each alist contains database-specific information about the table, including at least the name of the table. ((name . "table_name"))
The keyword arguments starting with
The keyword RESULT=VARNAME argument specifies the name of a variable to put the result array into; if not present, the array is returned to the caller.
Return the names of the tables in the database referenced by DBVAR as a newline separated list. If VARNAME is supplied, it is the name of the variable to receive the table names.
Returns an array of association lists, one per table in the open SQL database referenced by DBVAR. Each alist contains database-specific information about the table, including at least the name of the table. ((name . "table_name"))
The keyword arguments starting with
The keyword RESULT=VARNAME argument specifies the name of a variable to put the result array into; if not present, the array is returned to the caller.
Return the type of the underlying SQL database which will be accessed by the various SQL::... functions. The possibilities are one of
Return an array of the database names present on HOST
(defaults to
This command is only meaningful to the
Return the number of rows SELECTed by the last SQL query execution, associated with the database referenced indirectly by CURSOR.
Save the variables in PACKAGE in the table TABLE of the database referenced by DBVAR. This only saves variables which have names matching existing table column names. Package prefixes are stripped from the variables, and the symbol name is used as the column name to store the data. Symbol names and column names are treated in a case-insensitive manner.
If you don't specify a METHOD, then the implementation first
attempts to do an SQL
Both the If a variable in the package you are saving corresponds to a column with a numeric field type, and the value of the variable is the empty string, the system will attempt to store a NULL value into this field of the record. If the table does not support NULL values on that column, the operation will fail.
Undocumented.
Returns the last SQL query statement that was sent to the underlying SQL database. Can be useful in debugging.
Get or set the standard search form parameters. Always returns an association list of the current search form parameters, and can optionally set them to new values. In typical usage, this function might be called as follows:
<set-var orig-params =
<sql::search-form-params DSN=<mydsn> table=CLIENTS>>
<sql::standard-search-form posted>
<set-var ignore = <sql::search-form-params <get-var-once orig-params>>>
We have provided a simple but useful generic search form for use in
your Meta-HTML pages. Calling
You should set the following variables before calling SQL::STANDARD-SEARCH-FORM:
Optionally, you can specify columns that you would like not to be offered as columns that may be searched for, in the array sql::search-ignore-cols[].
Create a command set called SQL::... which contains all of the functions
normally used for accessing ODBC, mySQL, or mSQL databases. This allows
you to write a complete application using the mySQL database,
The possible values for TYPE are
Set the current row number position in the result set from the last query. This operates on a CURSOR object.
Return the current error message from recent SQL statements.
Currently unimplemented.
Display a complete form suitable for searching for a record from
the database and table specified in Optional keyword argument HREF specifies the destination page -- it defaults to the current page if not supplied. Optional keyword FSIZE is the size of the font to use in the form -- this has the effect of growing or shrinking the form.
When this form is posted to, it gathers all of the
columns which match the specified search criteria, and then
displays the results in a table, linking each to the
sql::search-editurl. When only one result is found, redirects
to the page specified in
For more information on what happens when this form is posted to,
please see
Perform a database search on the search table using the criteria in PACKAGE. Variables in PACKAGE must include:
Produces a table with the contents of the specified column linked with an href to sql::search-defurl?. You can overide the information which is displayed for each record that matches the search criteria by creating a function called sql::search-result-name. If this function exists, it should take two arguments: the column name that was being searched for, and an association list representing the data of the current record. An example might be to always display the last and first name of any search where the column name to be searched contained the string "name": <defun sql::search-result-name col alist>
<if <match <get-var-once col> "name" caseless=true>
<concat <alist-get-var alist last_name>
", "
<alist-get-var alist first_name>>
<alist-get-var alist <get-var-once col>>>
</defun>
Return a pretty version of the column name specified in COL, by translating it through the array SQL::COLUMN-TRANSLATIONS. Each member of that array is a column name followed by a colon, followed by a pretty version of that column name. If COL doesn't appear in the list of possible names, then it is simply capitalized.
Opens the database specified by DSN (database service name) string, and stores a referent to it in DBVAR. If the connection fails, the value of DBVAR is the empty string. DSN should contain a minimum of "HOST=hostname;DATABASE=dbname" in order to connect successfully to an mSQL database. Although the older HOST=HOST DATABASE=DBNAME keyword arguments are still supported, they have been deprecated, and the DSN=DSN-STRING form is preferred.
If NOLOCK=TRUE is supplied, then no lock file on the server is
created. This is useful when you don't want to block other processes
from accessing the database for the duration of the
Edit Section
![]() The META-HTML Reference Manual V2.0 Copyright © 1995, 1998, Brian J. Fox Found a bug? Send mail to bug-manual@metahtml.org |