Simple Data

Offers a wrapper around file or database I/O for simple data needs.  You’ll be able to access the data without caring what the backend is (SQL or flat-file), but each row in the table will need to have an unique, primary key and only four operations are supported on the data.  The four operations are fetch all rows (otlib.DataTable.GetAll), retrieve row by key (<otlib.DataTable.Fetch), delete row by key (<otlib.DataTable.Remove), and update row (which will happen automatically unless you’re doing transactions).  In other words, there’s no searching on anything other than the unique, primary key unless you’re willing to grab all the data (otlib.DataTable.GetAll) and search yourself (but that would be extremely slow).

Major features

  • Save to flatfile (readable text), SQLite, or MySQL.
  • Convert between formats on the fly.
  • Caching of retrieved data.
  • Can flush the cache on the fly, or disable the caching entirely.
  • Automatically and instantly saves changes.
  • Can do transactions.  This means several changes are pushed at once.
  • Optional comments are added to MySQL and the flatfile to describe your data.
Summary
Simple DataOffers a wrapper around file or database I/O for simple data needs.
otlib
preferred_database_typeThe DatabaseTypes to use by default.
CreateDataTableCreates a new data table for use.
otlib.DataTable
AddKeyAdds a key to the data table.
AddListOfKeyValuesAdds a key to the data table, whose value is going to be a table.
BeginTransactionBegins a transaction.
EndTransactionSee BeginTransaction.
ClearCacheClears any cache that may be built up in this datatable.
DisableCacheFirst clears the cache with ClearCache and disables further caching until re-enabled with EnableCache.
EnableCacheSee DisableCache.
UntrackedCopyOne of the downsides of using DataTable is that data you get out of it (except GetAll) can’t be thrown throw an iterator like pairs because of the way we track changes to the table.
ConvertToConvert the DataTable from using one database type to another.
InsertInsert (or replace existing with) a new row.
FetchFetch a row from the database.
RemoveDelets a row from the database.
GetAllFetchs all rows from the database.
EmptyEmpties all existing data for this database and clears any caches.

otlib

Summary
preferred_database_typeThe DatabaseTypes to use by default.
CreateDataTableCreates a new data table for use.

preferred_database_type

preferred_database_type

The DatabaseTypes to use by default.

CreateDataTable

function CreateDataTable(table_name,
primary_key_name,
primary_key_type,
comment,
database_type)

Creates a new data table for use.

Parameters

table_nameThe unique string of the table name.  IE, “users” for data about users.
primary_key_nameThe string of the name for the primary key of the table.  IE, “ip_address”.  For more information see DataTable.AddKey.
primary_key_typeThe string of the type for the primary key of the table.  For more information see DataTable.AddKey.
commentThe optional string of the comment to use for the primary key.
database_typeThe optional otlib.DatabaseTypes to use for this table.  Defaults to the value of preferred_database_type.

Returns

The newly created DataTable.

Revisions

v1.00Initial.

otlib.DataTable

Summary
AddKeyAdds a key to the data table.
AddListOfKeyValuesAdds a key to the data table, whose value is going to be a table.
BeginTransactionBegins a transaction.
EndTransactionSee BeginTransaction.
ClearCacheClears any cache that may be built up in this datatable.
DisableCacheFirst clears the cache with ClearCache and disables further caching until re-enabled with EnableCache.
EnableCacheSee DisableCache.
UntrackedCopyOne of the downsides of using DataTable is that data you get out of it (except GetAll) can’t be thrown throw an iterator like pairs because of the way we track changes to the table.
ConvertToConvert the DataTable from using one database type to another.
InsertInsert (or replace existing with) a new row.
FetchFetch a row from the database.
RemoveDelets a row from the database.
GetAllFetchs all rows from the database.
EmptyEmpties all existing data for this database and clears any caches.

AddKey

function DataTable:AddKey(key_name,
key_type,
comment)

Adds a key to the data table.  To help understand this, imagine that you couldn’t just do my_table.my_key = some_value in lua, that you had to define my_key first before you were able to assign to it.  That’s basically what this function is for.  You’ll need to define any keys you want to use before you use them.

This function must be used directly after you create the table using CreateDataTable and before you use functions such as Insert or Fetch.

Parameters

key_nameThe string of a key name for the table.  IE, “group”.  This name is what you’ll use to refer to the data later.
key_typeThe string of the type for this key in the table.  Valid strings to pass in are “string(<max_length_of_string)” or “number”.
commentThe optional string of the comment to use for this key.  The comment will show up in flatfiles and in MySQL.

Returns

Self.

Revisions

v1.00Initial.

AddListOfKeyValues

function DataTable:AddListOfKeyValues(list_name,
key_type,
value_type,
comment)

Adds a key to the data table, whose value is going to be a table.  To help understand this, imagine that you couldn’t just do my_table.my_key = {} in lua, that you had to define my_key first before you were able to assign a table to it.  That’s basically what this function is for.  You’ll need to define any keys you want to use before you use them.

In this case, the value of the key defined here is a table that can contain anything, as long as each key and value in the table conform to the types given, below.

This function must be used directly after you create the table using CreateDataTable and before you use functions such as Insert or Fetch.

Parameters

list_nameThe string of a list name for the table.  IE, “allow”.  This name is what you’ll use to refer to the data later.
key_typeThe string of the type for the keys in the list.  See AddKey for more information.
value_typeThe string of the type for the values in the list.  See AddKey for more information.
commentThe optional string of the comment to use for this key.  The comment will show up in flatfiles and in MySQL.

Returns

Self.

Revisions

v1.00Initial.

BeginTransaction

function DataTable:BeginTransaction()

Begins a transaction.  Between beginning and ending a transaction, data is basically built up in a buffer before getting sent off.  This is especially useful for flatfiles since it means that we won’t have to save the file (which is slow) a thousand times when data is initialized if the initialization takes advantage of transactions.

You should note that not all implementations support transactions though, so this function might be silently ignored.

Revisions

v1.00Initial.

EndTransaction

function DataTable:EndTransaction()

See BeginTransaction.

Revisions

v1.00Initial.

ClearCache

function DataTable:ClearCache()

Clears any cache that may be built up in this datatable.  This is a good function to use when you know the external data has changed.  For flatfiles this forces the file to be re-read.

Revisions

v1.00Initial.

DisableCache

function DataTable:DisableCache()

First clears the cache with ClearCache and disables further caching until re-enabled with EnableCache.  Flatfiles are a bit of a special case in that disabling the cache won’t force the file to be re-read every time we need to fetch data.

Revisions

v1.00Initial.

EnableCache

function DataTable:EnableCache()

See DisableCache.

Revisions

v1.00Initial.

UntrackedCopy

function DataTable:UntrackedCopy(data)

One of the downsides of using DataTable is that data you get out of it (except GetAll) can’t be thrown throw an iterator like pairs because of the way we track changes to the table.  To help alleviate this problem, you can create an untracked copy of the data using this function.

Parameters

dataThe table of the data you want to get an untracked copy of.  Must directly be the table you got from Insert or Fetch.  Don’t pass anything else in here.

Returns

The copied, untracked table.

Revisions

v1.00Initial.

ConvertTo

function DataTable:ConvertTo(database_type)

Convert the DataTable from using one database type to another.  Before converting the data to the new database type, it completely wipes whatever information was in the destination database type, so make sure you’re okay with losing whatever information may be there.

Parameters

database_typeThe destination otlib.DatabaseTypes.

Returns

Self.

Revisions

v1.00Initial.

Insert

function DataTable:Insert(primary_key,
data)

Insert (or replace existing with) a new row.

Parameters

primary_keyThe primary key for the new row, should be the same type as you declared it to be in CreateDataTable.
dataAn optional table of data to constitute this row.  Whatever information is required that isn’t present in this table will be added by the function.  Defaults to {}.

Returns

The table that constitutes the tracked, inserted row.  Use this new value to modify data.

Revisions

v1.00Initial.

Fetch

function DataTable:Fetch(primary_key)

Fetch a row from the database.

Parameters

primary_keyThe primary key for the new row, should be the same type as you declared it to be in CreateDataTable.

Returns

The table that constitutes the tracked, inserted row if it exists, nil if it doesn’t.

Revisions

v1.00Initial.

Remove

function DataTable:Remove(primary_key)

Delets a row from the database.

Parameters

primary_keyThe primary key for the new row, should be the same type as you declared it to be in CreateDataTable.

Returns

A boolean, true if it existed and was removed, false if the row didn’t exist.

Revisions

v1.00Initial.

GetAll

function DataTable:GetAll()

Fetchs all rows from the database.  This is similar to Fetch, except that it fetches all rows in the entire database, and all returned data is untracked.  This obviously pretty slow and should only be used if absolutely necessary.

Returns

The table that constitutes the entire database.

Revisions

v1.00Initial.

Empty

function DataTable:Empty()

Empties all existing data for this database and clears any caches.

Revisions

v1.00Initial.
preferred_database_type
The DatabaseTypes to use by default.
DatabaseTypes
function CreateDataTable(table_name,
primary_key_name,
primary_key_type,
comment,
database_type)
Creates a new data table for use.
function DataTable:AddKey(key_name,
key_type,
comment)
Adds a key to the data table.
function DataTable:AddListOfKeyValues(list_name,
key_type,
value_type,
comment)
Adds a key to the data table, whose value is going to be a table.
function DataTable:BeginTransaction()
Begins a transaction.
function DataTable:EndTransaction()
See BeginTransaction.
function DataTable:ClearCache()
Clears any cache that may be built up in this datatable.
function DataTable:DisableCache()
First clears the cache with ClearCache and disables further caching until re-enabled with EnableCache.
function DataTable:EnableCache()
See DisableCache.
function DataTable:UntrackedCopy(data)
One of the downsides of using DataTable is that data you get out of it (except GetAll) can’t be thrown throw an iterator like pairs because of the way we track changes to the table.
function DataTable:GetAll()
Fetchs all rows from the database.
function DataTable:ConvertTo(database_type)
Convert the DataTable from using one database type to another.
function DataTable:Insert(primary_key,
data)
Insert (or replace existing with) a new row.
function DataTable:Fetch(primary_key)
Fetch a row from the database.
function DataTable:Remove(primary_key)
Delets a row from the database.
function DataTable:Empty()
Empties all existing data for this database and clears any caches.
Close