You are on page 1of 16

SmartSQL Class For REALbasic

version 1.0
Translated by BKeeney Software, Inc.
http://www.bkeeney.com
Original Code by FreeVBCode.com
http://www.freevbcode.com
Disclaimer: The SmartSQL class (the package) is provided as-is by BKeeney Software, Inc which is not
responsible for any harm to computer systems or software which it is used. You are free to modify the code and/or
use the package yourself in any way you see fit. You are free to redistribute the package under the conditions below.
Disclaimer: The SmartSQL class and SmartSQL.dll (the package) is provided as-is by FreeVbCode.com, which
is not responsible for any harm to computer systems or software with which it is used. You are free to modify the
code and/or use the package yourself in any way you see fit. You are free to redistribute the package under the
following conditions: as 1) appropriate credit is given to FreeVBCode.com, 2) this documentation is provided
unaltered and in full, and 3) you do not charge any kind of fee for the code or binary version of the class without the
express permission of FreeVBCode.com.
Dec 2007 Page 1 of 16
SmartSQL Class For REALbasic Documentation
I. Overview
The SmartSQL class is designed to simplify the generation of SQL statements. In projects that require the use of
complex SQL statements, developers sometimes have to use (often convoluted) if-then or select logic to concatenate
and generate the statement. The SmartSQL class eases this process by allowing the developer to use properties and
functions to set up the statement, instead of keeping track of a long string.
In addition, the class offers flexibility in terms of how to generate the statement. For example, the field list of a
select statement can be specified by repeatedly calling the AddField method, by passing a list of field names as a
paramarray or array to the AddFields method.
Finally, the class offers the ability to easily re-use aspects of a previously generated statement. For instance, the class
allows a developer to easily modify the where clause of a previously generated SQL statement, while maintaining
the table list, field list, join clause and/or order clause.
An Introductory Example:
dim oSQL as new cSmartSQL
oSQL.SetupJoin "Customers", "ID", "Orders", "CustomerID", eClauseOp.Equal,
eJoinType.Left

oSQL.AddFields "Customers.Name", "Customers.Phone", "Orders.ID",
"Orders.ShippingAddress"
oSQL.AddSimpleWhereClause "Total", 1000, "Orders", eClauseOp.GreaterThan
oSQL.AddSimpleWhereClause "Customers.State", "CA"
MsgBox oSQL.SQL
The message box in the above example will display the following:
SELECT Customers.Name,Customers.Phone,Orders.ID,Orders.ShippingAddress FROM (Customers LEFT JOIN
Orders ON Customers.ID = Orders.CustomerID) WHERE Orders.Total > 1000 AND Customers.State = 'CA';
II. Included In This Package
1) SmartSQL.rbp: SmartSQL code in a REALbasic project file.
2) cSmartSQL.rbo: SmartSQL class object for REALbasic
3) mSmartSQL.rbo: Supporting code object for REALbasic.
4) cInvalidValueException.rbo: Supporting code class object for REALbasic
5) SmartSQL.pdf: This document.
III. Using With REALbasic
Drag and drop or use the Import menu command to import the cSmartSQL.rbo, mSmartSQL and
cInvalidValueException files on to your REALbasic project.
IV. Usage

A. Overview
Supported Statement Types: The SmartSQL class supports SELECT, INSERT, UPDATE, and DELETE queries.
The Statement Type is determined by the StatementType Property, which can be set to one of the following enum
values:
Dec 2007 Page 2 of 16
eStatementType:
Type_Select
Type_Insert
Type_Update
Type_Delete
The default value is Type_Select, and if you are using a select query through the lifecycle of the clsSmartSQL
object, you do not need to set this property.
The SQL Property: The SQL property will contains the SQL statement that is generated after the objects properties
are set and/or methods are called (see Introductory Example, above, for an illustration). SQL is a read-only
property.
Reset Method: This clears the SQL statement and (with a few exceptions, identified below), sets all properties that
have defaults back to their defaults.
Example:
Dim oSQL as new cSmartSQL
oSQL.AddTable "Customers"
sSQL = oSQL.SQL //sSQL = "SELECT * FROM Customers;
oSQL.reset
sSQL = oSQL.SQL //sSQL = empty string
It is also possible to clear certain parts of the SQL statement only. This technique is discussed later.

B. Working with SELECT Statements
1) Specifying Tables: The SmartSQL class provides three ways to specify the tables in a SELECT statement: 1)
Calling the AddTable method, 2) Calling the SetupJoin method, 3) Setting the ComplexFromClause property. These
methods are mutually exclusive any time you call one, you will nullify the effects of any previous calls to the other
two.
a) Public Sub AddTable(TableName As String)
Use this method for simple SQL queries that dont involve any join clauses.
Example:
Dim oSQL as new cSmartSQL
oSQL.AddTable "Order Items"
oSQL.AddFields "ItemCost", "Tax", "ShippingCost", "TotalCost"
oSQL.AddSimpleWhereClause "OrderID", 1
sSQL = oSQL.SQL
sSQL = SELECT ItemCost,Tax,ShippingCost,TotalCost FROM [Order Items] WHERE OrderID = 1;
Note that the brackets for the table name are automatically added, because it has a space. However, if you
added the brackets yourself, the resulting SQL would still be the same.
Though this method will primarily be used for queries against one table, it can also be used for queries
against multiple tables that are not qualified by a join statement. For example, if the following were added
to the above code:
oSQL.ClearWhereClause
oSQL.AddTable Orders
Dec 2007 Page 3 of 16
oSQL.SQL would equal SELECT ItemCost,Tax,ShippingCost,TotalCost FROM [Order Items], Orders;
b) Sub SetupJoin(sTable1 as string, sField1 as string, sTable2 as string, sField2 as
string, clause as eClauseOp, join as eJoinType)
Use these method in SQL queries that are based on a two-table join.
Parameters:
sTable1: The first table in the join.
sField1: The first field in the join; should be a field in Table1
sTable2: The first table in the join.
sField2: The first field in the join; should be a field in Table2
eClauseOp: (Optional) The operator used for to join table1.field1 and table2.field2. Must be one
of the following enum values:
eClauseOp:
Equal
Like
GreaterThan
GreaterThanOrEqual
LessThan
LessThanOrEqual
NotEqual
StartWith
EndWith
The default value is CLAUSE_EQUALS. See the description of the AddSimpleWhereClause
method, below, for more information about this enum
eJoinType: (Optional) The join type. Must be one of the following enum values:
eJoinType:
Inner
Left
Right
Full
This join type for the query can also be set separately, via the eJoinType property.
Example: See the introductory example, on page 3 of this document for an example of the use of
this method.
c) Public Property (Set) ComplexFromClause(NewValue As String)
Public Property (Get) ComplexFromClause() As String
Use this method to add a from clause that is not supported by the AddTable or SetupJoin method
for instance, for a join clause that contains more than two tables.
Example:
Dim oSQL as new cSmartSQL

oSQL.AddFields "Name", "Total", "Product Name"
sSQL = "(Customers JOIN Orders ON Customers.ID "
sSQL = sSQL + " = Orders.CustomerID) JOIN OrderItems "
Dec 2007 Page 4 of 16
sSQL = sSQL + "ON Orders.ID = OrderItems.OrderID"

oSQL.ComplexFromClause = sSQL
MsgBox oSQL.SQL
The message box in the above example will display:
SELECT Name,Total,[Product Name] FROM (Customers JOIN Orders ON Customers.ID =
Orders.CustomerID) JOIN OrderItems ON Orders.ID = OrderItems.OrderID;

2) Specifying the Field List: The SmartSQL class provides three ways to add field names to a SELECT query: 1)
The AddField method, used to add field names one at a time, 2) the AddFields method, used to specify a list of field
names via an array, and 3) The AddFields method used to pass a paramarray that contains a list of field names.
If you dont add any fields using one of the methods, the field list defaults to * (i.e., a statement that selects data
from all fields is constructed).
a) Public Sub AddField(sFieldName as string, sTableName as string = "")
Use this sub to add one field name to the select query. Optionally, you can specify the table name as well
(you should do this if your query involves multiple table names and the field name is ambiguous).
Alternatively, you can add the table name as part of the fieldname argument
Aliases: This method allows you to specify aliases for the field by including the alias as part of the
fieldname argument. However, for this to work, you must either: 1) add brackets around the fieldname part
of the argument (e.g., [ID] As MyID) , or 2) Set the AutoBracket property (which is discussed later in this
document) to false.
Aggregates: If you want to include use aggregates in your SQL statement, you can use this function,
passing the aggregate in the field name argument. However, the Auto Bracketing feature (whereby database
objects with spaces are automatically bracketed will not work with aggregates. For example, if you want to
add the sum of a field called Order Totals to the field list, you must pass Sum([Order Totals]) to the
FieldName argument.
Example:
Dim oSQL as new cSmartSQL
Dim sSQL as string

oSQL.SetupJoin "Customers", "ID", "Orders", "CustomerID"
oSQL.AddField "[ID] As CustID", "Customers"
oSQL.AddField "[ID] As OrderID", "Orders"
oSQL.AddField "FirstName", "Customers"
oSQL.AddField "Customers.LastName"
oSQL.AddField "Address"
msgbox.oSQL SQL
The message box in the above example will display:
SELECT Customers.[ID] As CustID,Orders.[ID] As OrderID, Customers.FirstName,
Customers.LastName, Address FROM (Customers INNER JOIN Orders ON Customers.ID =
Orders.CustomerID);
b) Public Sub AddFields(ParamArray arsField() as string)
Dec 2007 Page 5 of 16
Instead of adding field names by repeatedly calling the AddField method, you can pass one comma-
delimited list of field names to this function. For example, the following code will produces the same SQL
as the example given for the AddField method.
Dim oSQL as new cSmartSQL
Dim sSQL as string

oSQL.SetupJoin "Customers", "ID", "Orders", "CustomerID"

oSQL.AddFields "Customers.[ID] As CustID", "Orders.[ID] As
OrderID", "Customers.FirstName", "Customers.LastName", "Address"

c) Public Sub AddFields(arsField() as string, sTableName as string = "")
Another way to add fields is pass an array to the AddFields function. Specifying a Table for the optional
TableName argument will qualify each element in the list as a field in the given table.
Example:
Dim oSQL as new cSmartSQL
Dim sSQL as string

dim asCustomers(3) as string
Dim asOrders(2) As String

oSQL.SetupJoin "Customers", "ID", "Orders", "CustomerID"

asCustomers(0) = "[ID] as CustID"
asCustomers(1) = "FirstName"
asCustomers(2) = "LastName"
asCustomers(3) = "Address"

asOrders(0) = "[ID] As OrdID"
asOrders(1) = "SubTotal"
asOrders(2) = "Tax"

oSQL.AddFields asCustomers, "Customers"
oSQL.AddFields asOrders, "Orders"
MsgBox oSQL.SQL
The message box displays: SELECT Customers.[ID] as
CustID,Customers.FirstName,Customers.LastName,Customers.Address,Orders.[ID] As
OrdID,Orders.SubTotal,Orders.Tax FROM (Customers INNER JOIN Orders ON Customers.ID =
Orders.CustomerID);
6) Adding Where Clauses: The two functions that allow you to add where clauses to the statement are
AddSimpleWhereClause and AddComplexWhereClause.
a) Public Sub AddSimpleWhereClause(sFieldName as string, Value as Variant,
sTableName as string, clause as eClauseOp, logic as eWhereClauseLogic)
The AddSimpleWhereClause method is for adding clauses which contain only one comparison operator
(e.g., WHERE STATE = CA).
Parameters:
Dec 2007 Page 6 of 16
ByVal FieldName As String: The field name (STATE in the above example).
ByVal Value as Variant: The Value: (CA) in the above example. The function will automatically add
single quotes around the value if the variable type is a string, or leave off those quotes if the variable type is
a numeric value (although you can change this behavior by setting the AutoQuote property, which is
discussed at the end of this document).
Optional ByVal TableName As String: If you want to specify a table name for the field, you can pass it
using this optional parameter. Alternatively, you can also specify a table by qualifying the FieldName
argument.
In other words, the following code would have the same effect:
Dim oSQL as new cSmartSQL

oSQL.AddSimpleWhereClause "State", "CA", "Customers"
and
Dim oSQL as new cSmartSQL

oSQL.AddSimpleWhereClause "Customers.State", "CA"
Optional Op As CLAUSE_OPERATOR = CLAUSE_EQUALS: By default, the comparison operator
for the where clause is = . If you want to change this, set this parameter to one of the following enum
values:
eClauseOp:
Equal
Like
GreaterThan
GreaterThanOrEqual
LessThan
LessThanOrEqual
NotEqual
StartWith
EndWith
The meanings of most of these enum values should be clear from their names. eClauseOp.Like,
eClauseOp.StartWith, and eClauseOp.EndWith all produce a where clause with a LIKE
operator. The difference is that eClauseOp.Like produces a query for records where the field contains
the value, eClauseOp.StartWith produces a query for records where the field begins with the value,
and eClauseOp.EndWith produces a query for records where the field ends with the value:
Examples:
oSQL.AddSimpleWhereClause "Name", "La", "State", eClauseOp.like
where clause = WHERE State.Name LIKE '%La%';
oSQL.AddSimpleWhereClause "Name", "La", "State", eClauseOp.StartWith
where clause = WHERE State.Name LIKE 'La%';
oSQL.AddSimpleWhereClause "Name", "La","State", eClauseOp.EndWith
where clause = WHERE state.Name LIKE '%La';
Dec 2007 Page 7 of 16
Optional Logic As eWhereClauseLogic = LOGIC_AND
This parameter only needs to be set if you are adding more than one where clause and you want to use OR
logic to connect the clause to the rest of the statement.
Where clauses are added to the statement in the order you add them to the object, so this parameter has no
effect for the first where clause added.
The value of the parameter can be one of the following enum values: LOGIC_AND (the default) or
LOGIC_OR.
Example:
1) Simple Example:
If you SQL statement is simple, you dont have to concern yourself with the optional parameters in this
function, and you can just do something like the following:
Dim oSQL as new cSmartSQL

oSQL.AddTable "Customers"
oSQL.AddSimpleWhereClause "Age", 35
MsgBox oSQL.SQL
The Message Box in the above example will display: SELECT * FROM Customers WHERE Age = 35;
2) More Involved Example:
The following example illustrates the use of all the parameters:
Dim oSQL as new cSmartSQL

oSQL.SetupJoin "Customers", "ID", "Orders", "CustomerID"

oSQL.AddSimpleWhereClause "Age", 35, "Customers", eClauseOp.LessThan

oSQL.AddSimpleWhereClause "Total", 5000, "Orders",
clauseop.GreaterThan, eWhereClauseLogic.Logic_Or
MsgBox oSQL.SQL
The message box in the above example displays the following: SELECT * FROM (Customers INNER
JOIN Orders ON Customers.ID = Orders.CustomerID) WHERE Customers.Age < 35 OR Orders.Total >
5000;
b) Public Sub AddComplexWhereClause(sClause as string, logic as
eWhereClauseLogic)
This method can be used to add where clauses that are not supported by the AddSimpleWhereClause
method, including:

Where clauses that include a Boolean operator WITHIN the clause, as opposed to a Boolean
connector to another clause.

Where clauses that include comparison operators that are not supported by the
AddSimpleWhereClause method, such as the between operator.
Dec 2007 Page 8 of 16
The following examples illustrates the use of AddSimpleWhereClause and AddComplexWhereClause
together:
Dim oSQL as new cSmartSQL
Dim sSQL as string

oSQL.SetupJoin "Customers", "ID", "Orders", "CustomerID"

oSQL.AddSimpleWhereClause "State", "CA", "Customers"

oSQL.AddComplexWhereClause "Customers.State = 'NY' AND Customers.City =
'NY'", eWhereClauseLogic.Logic_Or

oSQL.AddComplexWhereClause "Orders.Total BETWEEN 0 AND 500",
eWhereClauseLogic.Logic_Or
MsgBox oSQL.SQL
The message box above will display: SELECT * FROM Customers JOIN Orders ON Customers.ID =
Orders.CustomerID WHERE (Customers.State = 'CA') OR (Customers.State = 'NY' AND Customers.City
= 'NY') OR (Orders.Total BETWEEN 0 AND 500)
4) Adding an Order Clause: You add order clauses by using the AddOrderClause method:
Public Sub AddOrderClause(sFieldName as string, bDesc as boolean = false,
sTableName as string = "")
If you want the statement to order items in descending order, set the optional OrderDesc parameter to true.
You can also optionally pass the table name, or you can include it in the FieldName argument if you want.
(e.g., AddOrderClause Customers.Zip has the same effect as AddOrderClause Zip,,Customers.)
You must add multiple order clauses in the order of their precedence, as the example below illustrates.
Example:
Dim oSQL as new cSmartSQL

oSQL.AddTable "Customers"
oSQL.AddOrderClause "NumOrders", True
oSQL.AddOrderClause "LastName"
msgbox oSQL.sql
The message box in this case will display SELECT * FROM Customers ORDER BY NumOrders DESC,
LastName
5) Re-Using aspects of the Select Statement:
You can re-use parts of the SQL statement generated by the SmartSQL object by clearing the part(s) you no
longer want to use, while retaining those parts you do want to reuse.
To do this, you call one of the following methods:
Public Sub ClearFromClause()
Clears tables previously added via the AddTable method, any JoinClause previously added via the
SetupJoin Method, and any From clause previously added via the ComplexFromClause method.
Public Sub ClearWhereClause()
Dec 2007 Page 9 of 16
Clears where clauses previously added via the AddSimpleWhereClause or the AddComplexWhereClause
methods.
Public Sub ClearOrderClause()
Clears order clauses previously added via the AddOrderClause method.
Public Sub ClearFields()
Clears the field list (i.e., fields previously added via the AddField, AddFields, or ListAddFields methods).
Example 1: Field list is cleared, where and order clause are re-used.
Dim oSQL as new cSmartSQL
Dim sSQL as string

oSQL.AddTable "Customers"
oSQL.AddFields "Name", "Income", "Address", "City", "State", "Zip"
oSQL.AddSimpleWhereClause "Income", 100000, "Customers",
eClauseOp.GreaterThan
oSQL.AddOrderClause "Income", True
MsgBox oSQL.SQL
Displays SELECT Name, Income, Address, City, State, Zip FROM Customers WHERE (Income >
100000) ORDER BY Income DESC
oSQL.ClearFields
oSQL.AddFields "EmailAddress", "Fax", "HomePhone", "WorkPhone",
"Income"
MsgBox oSQL.SQL
Displays SELECT EmailAddress, Fax, HomePhone, WorkPhone, Income FROM Customers WHERE
(Income > 100000) ORDER BY Income DESC
Example 2: Where clause changed, field list and join clause retained.
oSQL.SetupJoin "Customers", "ID", "Orders", "CustomerID"
oSQL.AddFields "Name", "Address", "City", "State", "Zip", "Orders.NumItems",
"Orders.Total"
oSQL.AddSimpleWhereClause "State", "CA"
MsgBox oSQL.SQL
Displays SELECT Name, Address, City, State, Zip, Orders.NumItems, Orders.Total FROM Customers JOIN
Orders ON Customers.ID = Orders.CustomerID WHERE (State = 'CA')
oSQL.ClearWhereClause
oSQL.AddSimpleWhereClause "Income", 100000, "Customer", eClauseOp.GreaterThan
MsgBox oSQL.SQL
Displays SELECT Name, Address, City, State, Zip, Orders.NumItems, Orders.Total FROM Customers JOIN
Orders ON Customers.ID = Orders.CustomerID WHERE (Customer.Income > 100000)
D. Working with UPDATE Statements
Dec 2007 Page 10 of 16
Update statements include a table, a field list, a values list, and (optionally), one or more where clause criteria.
Setting the object to use an UPDATE statement: Set the StatementType Property (described at the beginning of
the document) to eStatementType.Type_Update.
Adding the Table: Use the AddTable method, described above under Working with Select Statements. Only 1
table is permitted, and if you add more than one, only the first one you added will be used.
Adding Fields: Use the AddField, AddFields, and/or the ListAddFields methods described above under Working
with Select Statements.
Adding Where Clauses: Use the AddSimpleWhereClause and/or AddComplexWhereClause methods described
earlier in this document.
Adding Values: Use one of the following methods to add values:
Public Sub AddValue(value as Variant) : Adds a value to the value list.
Public Sub AddValues(value0 as Variant, ParamArray arvRest() as Variant): Adds a
comma-delimited list of values to the value list.
Public Sub AddValues(arvValue() as Variant): Adds the values contained in the parameter
arvValue array to the value list.
You must add values in the same order you added the corresponding fields. For example, if you added name,
address, and phone, in that order, you must be sure to add the values John, 12 Oak Street, and 555-1212, in
that order.
Reusing aspects of the UPDATE Statement: To re-use aspects of the update statement, you can use the
ClearFromClause (in this case, used to clear the table), ClearFields, and ClearWhereClause methods when the
statement type is set to TYPE_UPDATE. These methods are described earlier under Working With Select
Statements.
You can also use the ClearValues method, which is described in the next section, Working with Insert Statements.
When StatementType = TYPE_UPDATE, invoking methods that are not relevant to update statements (e.g.,
SetupJoin, AddOrderClause) will have no effect.
Example:
Dim oSQL as new cSmartSQL
Dim sSQL as string

oSQL.StatementType = eStatementType.Type_Update
oSQL.AddTable "Customers"
oSQL.AddFields "City", "State"
oSQL.AddValues "New York", "New York"
oSQL.AddSimpleWhereClause "State", "NY"
oSQL.AddSimpleWhereClause "City", "NY"
MsgBox oSQL.SQL
Displays UPDATE Customers SET City = 'New York', State = 'New York' WHERE (State = 'NY') AND (City =
'NY')
E. Working with INSERT Statements:
Insert Statements have 1 table name, a values list, and optionally a fields list.
Dec 2007 Page 11 of 16
To set the object to use an INSERT statement, set the StatementType Property (described at the beginning of the
document) to eStatementType.Type_Insert
The procedures for setting the table name, the values list, and the fields list are identical as those for UPDATE
Statements. See Working with UPDATE Statements, above, for more information.
You can also re-use aspects of the statement by using the ClearFromStatement, ClearFields, and ClearValues
method(s). The ClearFromStatement and ClearFields methods have already been described. The ClearValues
method is similar in concept; it allows you to clear the values list while retaining the statements table name and
fields list:
Example:
Dim oSQL as new cSmartSQL

oSQL.StatementType = eStatementType.Type_Insert
oSQL.AddTable "Customers"
oSQL.AddFields "GroupID", "Name", "Address", "City", "State", "Zip"
oSQL.AddValues 5, "Bill Smith", "12 Elm St.", "LA", "CA", "90210"
MsgBox oSQL.SQL
Displays INSERT INTO Customers (GroupID, Name, Address, City, State, Zip) VALUES (5, 'Bill Smith', '12 Elm
St.', 'LA', 'CA', '90210')
oSQL.ClearValues
oSQL.AddValues 7, "Donald Duck", "12 Oak St", "NY", "NY", "10012"
MsgBox oSQL.SQL
Displays INSERT INTO Customers (GroupID, Name, Address, City, State, Zip) VALUES (7, 'Donald Duck', '12
Oak St', 'NY', 'NY', '10012')
F. Working with DELETE Statements
To make the object generate a DELETE statement, set the StatementType property to TYPE_DELETE. Delete
statements can have one table and (optionally) one or more where clause. The methods for adding the one table
(AddTable) and the where clauses (AddSimpleWhereClause and AddComplexWhereClause) are described
elsewhere in the document.
By using the ClearFromClause or ClearWhereClause methods, you can re-use the where clause while changing its
table (or vice versa).
Example:
Dim oSQL as new cSmartSQL

oSQL.StatementType = eStatementType.Type_Delete

oSQL.AddTable "Customers"
oSQL.AddSimpleWhereClause "GroupID", 5
oSQL.AddSimpleWhereClause "State", "CA"

MsgBox oSQL.SQL
Displays DELETE FROM Customers WHERE (GroupID = 5) AND (State = 'CA')
Dec 2007 Page 12 of 16
oSQL.ClearFromClause
oSQL.AddTable "Prospects"
MsgBox oSQL.SQL
Displays DELETE FROM Prospects WHERE (GroupID = 5) AND (State = 'CA')
G: Changing the Statement Type
The ability to re-use aspects of the SQL statement extends to the statement type. For example, if you change the
StatementType property from eStatementType.Type_Delete to eStatementType.SelectStmt, your
SELECT statement will retain all the properties (e.g., From clause and Where clause) of the previous DELETE
statement. Moreover, its possible that, once you change the StatementType property, other properties that had been
inapplicable become applicable.
The following example illustrates this point:
Dim oSQL as new cSmartSQL

oSQL.StatementType = eStatementType.Type_Delete

oSQL.AddTable "Customers"
oSQL.AddSimpleWhereClause "GroupID", 5
oSQL.AddSimpleWhereClause "State", "CA"

//Has no effect when StatementType = TYPE DELETE
oSQL.AddOrderClause "Name", True
MsgBox oSQL.SQL
Displays DELETE FROM Customers WHERE (GroupID = 5) AND (State = 'CA')
oSQL.StatementType = eStatementType.SelectStmt
//Order clause kicks in now
MsgBox oSQL.SQL
Displays SELECT * FROM Customers WHERE (GroupID = 5) AND (State = 'CA') ORDER BY Name DESC
H. Miscellaneous Properties
Public Property (Get) AutoBracket() As Boolean
Public Property (Set) AutoBracket( NewValue As Boolean)
If AutoBracket is set to true, table and field names with spaces are automatically bracketed, if no brackets
are already included. The default value for this property true.
Though keeping AutoBracket set to true is preferable in most cases, there are some cases where it can cause
a problem. For instance, there are issues related to the use aliases and aggregates as field name arguments
when AutoBracket is set to true. Refer to the documentation for the AddField method, above, for more
information.
If you set AutoBracket to false, it is not set back to true when you call the Reset method.
Public Property (Get) AutoQuote() As Boolean
Public Property (Set) AutoQuote( NewValue As Boolean)
If AutoQuote is set to true, the class will detect the variable type of values in Update, Insert, and Where
clauses, and add the ' character at the beginning of end of string values. It will also replace this character
Dec 2007 Page 13 of 16
with two '' characters if it occurs within the value. The default value of AutoQuote is true, and, unless you
are so used to adding the ' character to string values in SQL statements that you do it without thinking, it is
recommended that you keep it set to true.
If you set AutoQuote to false, it will not be set back to true if you call the Reset statement.

Public Property (Get) AutoLike() As Boolean
Public Property (Set) AutoLike( NewValue As Boolean)
This property only applies to the use of the AddSimpleWhereClause method. If this property is true, the a
default wild-card character (%) at the beginning and end of the value argument, if the value is a string
and the Op argument is set to CLAUSE_LIKE. The default value is true, and if changed to false, it is not
set back to true by the Reset method.
Set this property to false only if you use LIKE clauses with different wild-card characters than the default
described above.
Public Property (Get) TableCount() As Long
This read-only property returns the number of tables included in the SQL statement. This may be useful if
you want to track how many tables have been added via the AddTable method, or how many distinct tables
are included in a statement added via the AddComplexFromClause method.
H: Limitations of the Class:
1) The class does not check the validity of your SQL statement as it relates to a particular data source: The
SmartSQL class is designed to ease the process of generating and re-using SQL statements. Though it generates
valid SQL, this does not necessarily mean the statement it generates will be valid for the data source you are using.
For example, if you add a table called Customers via the AddTable method, the SQL statement generated wont
work if there is not table called Customers in your database.
2) The class does not support all of SQL: Version 1.0 of this class was designed to help REALbasic developers form
the more commonly used SQL statements. It does not support the full range of SQL. For instance, there is no
support for the GROUP BY or the HAVING clauses.
If you need support for more statements offered by the class, you can still use it to generate those aspects of the
statement that it does automatically generate (e.g., the table and the field list). Then, you can work with the string
returned by the SQL property to append or insert the additional statements you need.
APPENDIX: SmartSQL Public Interface
Public Enum eJoinType
Inner
Left
Right
Full
End Enum
Public Enum eClauseOP
Equal
Like
GreaterThan
GreaterThanOrEqual
LessThan
LessThanOrEqual
NotEqual
StartWith
EndWith
Dec 2007 Page 14 of 16
End Enum
Public Enum eWhereClauseLogic
Logic_And
Logic_Or
End Enum
Public Enum eStatementType
Type_Select
Type_Insert
Type_Update
Type_Delete
End Enum
Public Sub AddComplexWhereClause(sClause as string)
Public Sub AddComplexWhereClause(sClause as string, logic as
eWhereClauseLogic)
Public Sub AddField(sFieldName as string, sTableName as string = "")
Public Sub AddFields(ParamArray arsField() as string)
Public Sub AddOrderClause(sFieldName as string, bDesc as boolean = false,
sTableName as string = "")
Public Sub AddSimpleWhereClause(sFieldName as string, Value as Variant,
sTableName as string, clause as eClauseOp, logic as eWhereClauseLogic)
Public Sub AddSimpleWhereClause(sFieldName as string, Value as Variant,
sTableName as string, clause as eClauseOp)
Public Sub AddSimpleWhereClause(sFieldName as string, Value as Variant,
sTableName as string)
Public Sub AddSimpleWhereClause(sFieldName as string, Value as Variant)
Public Sub AddTable(sTableName as string)
Public Sub AddValues(arvValue() as Variant)
Public Sub AddValues(value0 as Variant, ParamArray arvRest() as Variant)
Public Sub AddValue(value as Variant)
Public Sub ClearFields()
Public Sub ClearFromClause()
Public Sub ClearOrderClause()
Public Sub ClearValues()
Public Sub ClearWhereClause()
Public Sub AddFields(arsField() as string, sTableName as string = "")
Public Sub Reset()
Public Sub SetupJoin(sTable1 as string, sField1 as string, sTable2 as string,
sField2 as string, clause as eClauseOp, join as eJoinType)
Public Sub SetupJoin(sTable1 as string, sField1 as string, sTable2 as string,
sField2 as string, clause as eClauseOp)
Dec 2007 Page 15 of 16
Public Sub SetupJoin(sTable1 as string, sField1 as string, sTable2 as string,
sField2 as string)
Public Sub Join(sTable1 as string, sField1 as string, sTable2 as string,
sField2 as string, clause as eClauseOp, join as eJoinType)
Public Function SQL() as string
Public Function AutoBracket As boolean
Public Function AutoLike As boolean
Public Function AutoQuote As boolean
Public Function Statement As StatementType
Dec 2007 Page 16 of 16

You might also like