Wrapper Functions

This file is to offer a little bit of coherence to our scripts.  Since we’ll be dealing with all sorts of environments and setups, anything we need that might vary is defined as a wrapper stub below.  All of these functions should be defined for every application implementation.

Summary
Wrapper FunctionsThis file is to offer a little bit of coherence to our scripts.
wrappers
SQL Wrapper Functions
FormatAndEscapeDataFormats and escapes data from lua to be appropriate for use in SQL.
ExecuteExecute given statement on the database.
BeginTransactionIf the SQL implementation allows it, start a transaction.
EndTransactionIf the SQL implementation allows it, end a transaction.
BeginTransactionGet the number of affected rows from the last statement executed.
File Wrapper Functions
FileExistsCheck to see if a file or folder exists.
FileReadRead a file.
FileWriteWrite to a file.
FileDeleteDelete a file or folder.
Command Wrappers

wrappers

Summary
SQL Wrapper Functions
FormatAndEscapeDataFormats and escapes data from lua to be appropriate for use in SQL.
ExecuteExecute given statement on the database.
BeginTransactionIf the SQL implementation allows it, start a transaction.
EndTransactionIf the SQL implementation allows it, end a transaction.
BeginTransactionGet the number of affected rows from the last statement executed.
File Wrapper Functions
FileExistsCheck to see if a file or folder exists.
FileReadRead a file.
FileWriteWrite to a file.
FileDeleteDelete a file or folder.
Command Wrappers

SQL Wrapper Functions

FormatAndEscapeData

function FormatAndEscapeData(data)

Formats and escapes data from lua to be appropriate for use in SQL.  This function must quote and escape strings as well as handle nil and numbers.  For numbers, you probably want to pass it right back as a string.  For nil, you probably want to return “NULL”.

Parameters

dataThe data that can be a string, number, or nil.  Any other type is an error condition.

Returns

The string of the formatted and escaped data.

Revisions

v1.00Initial.

Execute

function Execute(database_type,
statement,
key_types)

Execute given statement on the database.  Note that we assume that there is only one database, and we’re automatically connected to it.

Parameters

database_typeThe database type to execute this statement on.  See otlib.DatabaseTypes.
statementThe string to execute on the database.  If the statement doesn’t execute properly, raise an error.
key_typesAn optional table indexed by row-key string name with lua string type values (IE, “number”).  This is here in case you’re dealing with a poor SQL implementation and need to convert datatypes yourself.

Returns

Nil if it wasn’t a select operation, a list table of selected data otherwise.

Revisions

v1.00Initial.

BeginTransaction

function BeginTransaction(database_type)

If the SQL implementation allows it, start a transaction.  Otherwise, implement this as an empty function.

Revisions

v1.00Initial.

EndTransaction

function EndTransaction(database_type)

If the SQL implementation allows it, end a transaction.  Otherwise, implement this as an empty function.

Revisions

v1.00Initial.

BeginTransaction

Get the number of affected rows from the last statement executed.

Returns

The number of rows affected.

Revisions

v1.00Initial.

File Wrapper Functions

FileExists

function FileExists(file_path)

Check to see if a file or folder exists.

Parameters

file_pathThe file path string.

Returns

A boolean indicating whether or not the file or folder exists.

Revisions

v1.00Initial.

FileRead

function FileRead(file_path)

Read a file.

Parameters

file_pathThe file path string.

Returns

The string of the file contents.  Returns an empty string if the file doesn’t exists.

Revisions

v1.00Initial.

FileWrite

function FileWrite(file_path,
data)

Write to a file.  Should clear the contents of any existing file first.

Parameters

file_pathThe file path string.
dataThe string to write to the file.

Revisions

v1.00Initial.

FileDelete

function FileDelete(file_path)

Delete a file or folder.  Should only delete empty directories.

Parameters

file_pathThe file or folder path string.

Revisions

v1.00Initial.

Command Wrappers

function FormatAndEscapeData(data)
Formats and escapes data from lua to be appropriate for use in SQL.
function Execute(database_type,
statement,
key_types)
Execute given statement on the database.
function BeginTransaction(database_type)
If the SQL implementation allows it, start a transaction.
function EndTransaction(database_type)
If the SQL implementation allows it, end a transaction.
function FileExists(file_path)
Check to see if a file or folder exists.
function FileRead(file_path)
Read a file.
function FileWrite(file_path,
data)
Write to a file.
function FileDelete(file_path)
Delete a file or folder.
DatabaseTypes
Close