A5 SQL DB Application Architecture
A5 SQL DB Application Architecture
SQL Database
Application Architecture
Overview............................................................................................................................. 3
Design Goals....................................................................................................................... 3
Issues................................................................................................................................... 4
Architecture......................................................................................................................... 5
Connection Strings.......................................................................................................... 5
A Simple Connection String for a SQL Server Database ........................................... 6
A Simple Connection String for an Oracle Database ................................................. 6
Database APIs................................................................................................................. 7
Portable SQL................................................................................................................... 9
How These Examples Were Created ........................................................................ 11
Syntax Handlers ............................................................................................................ 12
Functionality ............................................................................................................. 12
User Defined Syntax Handlers.................................................................................. 12
Extracting a Schema Descriptions ............................................................................ 13
Generating Create Table Statements......................................................................... 14
The XBasic Object Model ................................................................................................ 16
About Namespaces........................................................................................................ 16
Objects that Manipulate Data........................................................................................ 16
Objects that Describe Database Objects ....................................................................... 16
Objects that Parse and Execute Portable SQL .............................................................. 16
Example Scripts ................................................................................................................ 18
Function Calls on SQL Objects .................................................................................... 18
Connecting To a SQL Database.................................................................................... 18
Fetching and Displaying Data....................................................................................... 18
Using Arguments in SQL Queries ................................................................................ 19
Working with SQL::ResultSet ...................................................................................... 20
Importing Data into a dBase Table ............................................................................... 20
Exporting Data from a dBase Table to a SQL Database .............................................. 21
Updating Rows in a SQL Database .............................................................................. 21
Inserting a Row into a SQL Database........................................................................... 22
Deleting Rows from a SQL Database........................................................................... 23
Conclusion ........................................................................................................................ 24
Page 2
2/8/2007
Overview
Alpha Five is a business application development and run-time environment historically
targeted at the dBase market. Times have changed, and Alpha Software has been
retooling in recent years to support SQL databases with the same ease of use and power
that it brought to the dBase market so many years ago.
This article will discuss some of the goals, issues and architecture behind Alpha Fives
support for SQL databases. Example scripts, written in Alpha Fives Xbasic dialect of
BASIC will demonstrate how users experience the programming interface.
Note: This article assumes a basic knowledge of SQL Data Manipulation Language
(DML) and Data Definition Language (DDL) and the ability to read simple code
examples written in Xbasic.
Design Goals
Ease of use takes precedence in Alpha Five, so the first design priority is that features
work well in the simplest case and that it is possible to do complex tasks with a minimum
of code or keystrokes. It is frustrating for a new user to have functionality fail because of
something they are expected to know, but clearly dont.
Portability, in this context, is the ability to create an application that runs without code
changes when the user switches to another database vendor. It is one of those features
developers ignore until the time comes to port the application to another database server.
At the same time, when an application needs to make use of functionality unique to a
specific database, the tool should not stand in the way.
Performance is secondary, in importance, only to ease of use and portability. To get the
maximum overall balance, we want to invest an appropriate amount of attention to doing
time intensive tasks (such as importing or exporting data) quickly.
Maintainability is an indirect benefit to customers, but is a critical part of Alphas being
able to add features and database platforms in time to be useful to the marketplace. Since
Alpha Fives user interface must integrate the same architecture Xbasic programmers will
use, it is important to have dialogs and functions that do not need to be maintained when
new databases are added.
Page 3
2/8/2007
Page 4
2/8/2007
XBasic Object
Connection String
Database Driver
Before looking at the object model we will discuss database APIs and syntax handlers
and the connection strings that tell Alpha Five which ones to make use of. Because the
examples in this section use connection strings, we will discuss them first.
Connection Strings
Connecting to a database can be quite complicated. First you need to know what
API/driver you want to use. Then, in the case of generic drivers you need to decide what
syntax you want to use. All of the information required to connect to a specific database
is encoded in a single connection string as a set of name/value pairs.
A connection string can be encoded in either of two formats:
Format 1 A comma separated list enclosed in soft braces (most common usage).
{Name=Value[,Name=Value] }
Page 5
2/8/2007
Connection strings generally have a named value specifying the driver or API being used
(A5API), and (for generic drivers) a syntax to use (A5Syntax). Other values will depend
on the API and syntax selected and the options needed.
In most cases, you do not need to hand code a connection string. There are dialogs and
help interfaces (even in the script editor) that will make it easy to create them.
Page 6
2/8/2007
Type of Driver
Access
DB2
Excel
MySQL
ODBC
Oracle
OracleLite
Paradox
QuickBooks
QuickBooksOnline
SQLServer
Extension (ODBC)
Native
Extension (ODBC)
Native
Generic
Native
Extension (ODBC)
Extension (ODBC)
Extension (ODBC)
Extension (ODBC)
Extension (ODBC)
Page 7
2/8/2007
Page 8
2/8/2007
Portable
SQL Statement
Query Parser
Syntax Handler
Native
SQL Statement
Alpha Five Portable SQL must contend with such issues as formatting names properly
when they contain spaces and special characters, formats for literals and subtle
differences in join syntax. The examples below show how SQL literals and names are
translated for some of the different syntaxes:
Formatting Literals
Portable SQL
Oracle
Page 9
2/8/2007
SQLServer
Oracle
MySQL
SQLServer
Portable SQL also supports more than fifty functions for use in SQL expressions to do
things from extracting a substring to computing a standard deviation. In generating the
SQL, Alpha Five will use the native implementation where one is available. In other
cases, Alpha Five will construct SQL to generate the result using more basic functions as
building blocks. In one extreme case, for example, standard deviations are implemented
by combining SUM and COUNT functions in the appropriate expressions. The examples
below show how SQL functions are translated for some of the different syntaxes:
Formatting Functions
Portable SQL
Oracle
MySQL
SQLServer
Portable SQL also adds special syntax to handle limiting the number of rows return from
a query by using the FIRST keyword. This operation is not standard in SQL. It is
supported natively by some databases, such as Microsoft SQL Server and MySQL. In
other cases, special SQL must be generated to accomplish the desired result. The
example below shows how portable SQL using the FIRST keyword is translated by some
of the different syntax handlers:
Using the FIRST Keyword
Portable SQL
Oracle
MySQL
Page 10
2/8/2007
Page 11
2/8/2007
Page 12
2/8/2007
If you want to extract whole sets of table descriptions, the SQL::Schema object is
probably the best choice. It contains a set of SQL::TableInfos and can perform
operations on them as a group, including presenting them in order using the referential
constraints linking them together. This feature makes it easy to create or delete sets of
tables that depend on each other in the right order.
dim c as sql::connection
dim Sc as sql::Schema
c.open("{A5API='SQLServer',Server='(local)',Trusted_connection=yes,Database='DataModeling'}")
c.GetSchema(Sc,)
Note: Optional parameters for GetSchema can be used to include or exclude specific table types.
Page 13
2/8/2007
In the following examples, we are using the connection object to generate syntax by
assigning a connection string with only a syntax name. The connection object finds the
matching syntax handler and asks it to generate the appropriate code.
dim c as SQL::Connection
c.setconnectionstring("{a5syntax=oracle}")
?c.generateCreateTableStatement(ti)
= CREATE TABLE categories
(
CategoryID
integer NOT NULL,
CategoryName nvarchar2(15) NOT NULL,
Description
nclob NULL,
Picture long raw
NULL,
PRIMARY KEY (CategoryID))\
CREATE SEQUENCE A5AI_CATEGORIES START WITH 9 INCREMENT BY 1
NOMAXVALUE\
CREATE OR REPLACE TRIGGER A5TI_CATEGORIES BEFORE INSERT ON
CATEGORIES REFERENCING NEW AS new FOR EACH ROW WHEN (new.CategoryID IS
NULL) BEGIN SELECT A5AI_CATEGORIES.nextval into :new.CategoryID from dual; END
A5TI_CATEGORIES;
c.setconnectionstring("{a5syntax=mysql}")
?c.generateCreateTableStatement(ti)
= CREATE TABLE categories
(
CategoryID
int(10) NOT NULL,
CategoryName varchar(15)
NOT NULL,
Description
longtext NULL,
Picture longblob
NULL,
PRIMARY KEY (CategoryID))
c.setconnectionstring("{a5syntax=access}")
?c.generateCreateTableStatement(ti)
= CREATE TABLE categories
Page 14
2/8/2007
You would not typically use the functions we have shown here in most of your code. We
show them to explain the architecture. They are used in some of the Alpha Five user
interface and are available to any developer needing a more sophisticated interface. More
often, a table would be created using a script like the one below. Normally the
GenerateCreateTableStatement function would be invoked internally.
dim SourceConnection as sql::connection
dim TargetConnection as sql::connection
dim TableDefinition as sql::tableinfo
SourceConnection.open("{A5API=Oracle,A5ProcedureLanguage=PLSQL,Server='localhost',Port
=1521,Service='ORCL',UserName='Fred',Password=''}")
SourceConnection.GetTableInfo(ti, "categories")
TargetConnection.open({a5api=excel,filename=c:\northwind.xls})
TargetConnection.CreateTable(TableDefinition)
Page 15
2/8/2007
About Namespaces
Alpha Five classes of objects and enumerations are grouped into collections called
namespaces. The namespace SQL has been added in order to group the new object
classes that implement support for SQL databases. To dimension a variable for one of
these objects, use the syntax SQL::<object class>. To help get you used to this syntax, all
of the types are named referring to their fully qualified names. The list below includes
some of the more commonly used objects.
Page 16
2/8/2007
Page 17
2/8/2007
Page 18
2/8/2007
Page 19
2/8/2007
Page 20
2/8/2007
= "Project"
= "c:\test\Project.dbf"
Page 21
2/8/2007
Page 22
2/8/2007
Page 23
2/8/2007
Page 24
2/8/2007