SmartSQL is a class created by FreeVBCode.com. We've found this class extremely useful in Visual Basic so we decided to port it to REALbasic.
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.
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 or by passing a list of field names as a paramarray or array to the AddFields method.
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 Simple 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');
A More
Complex Example:
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
SELECT * FROM (Customers INNER JOIN
Orders ON Customers.ID = Orders.CustomerID)
WHERE (Customers.Age < 35) OR (Orders.Total > 5000);
Using with
REALbasic
Download the package and unzip. Import the cSmartSQL.rbo,
mSmartSQL.rbo and cInvalidValueException.rbo files on to your
REALbasic project.
You
can download the SmartSQL project files from here in the "Free
Stuff" section. The terms of use from FreeVBCode.com require us to
include the original documentation and VB class and
dll.
The REALbasic code is provided "As-Is." You can freely use and
modify the code as you see fit provided you adhere to the original
terms set forth in the FreeVBCode documentation. If you find any
bugs or ways to enhance the functionality in REALbasic, please
email us any changes at support@bkeeney.com.