Module SQLSerializable
In: sql-serialize.rb

The generic serializable module. This is never included directly, rather database-specific versions of it are included. MySQLSerializable for mysql, PgSQLSerializable for postgresql, etc.

Methods
delete    insert    load    select    sql_assoc_id    sql_basic_delete    sql_basic_insert    sql_basic_update    sql_create_objects    sql_decrease_refcount    sql_delete    sql_delete_assocs    sql_exist_in_db?    sql_fetch_next_id    sql_fetch_primary_key_value    sql_fetch_variable_names    sql_fetch_variable_values    sql_increase_refcount    sql_insert    sql_insert_array    sql_insert_assoc    sql_insert_hash    sql_refcount    sql_select    sql_select_assocs    sql_select_from_id    sql_update    store    update   
Attributes
sql_assoc_id  [W] 

the sql_assoc_id is world-writable for use in SQLSerializableList and in the sql_delete_assocs method. Application programmers should not set this value. The sql_exist_in_db? method is the closest thing to a sql_assoc_id getter.

Public Class methods
sql_create_objects(dbi)
Public Instance methods
sql_exist_in_db?(dbi)

Returns sql_assoc_id of self if it exists in the database or false if it does not. This is the closest thing to a sql_assoc_id getter.

insert(dbi, new_transaction = true)

Insert the object and all associated objects. The new_transaction parameter is used internally to make sure that a new transaction isn’t started when insert is called recursively. It should not be set when calling insert from application code.

dbi is a DBI object connected to a database. AutoCommit should be disabled on this database handle.

update(dbi, new_transaction = true)

Update the object and all associated objects. The new_transaction parameter is used internally to make sure that a new transaction isn’t started when update is called recursively. It should not be set when calling update from application code.

store(dbi)

Store an object and all associated objects.

delete(dbi, new_transaction = true)

Delete an object (and thereby all associated objects) if the reference count is low enough, otherwise just decrease the reference count. The new_transaction parameter is used internally to make sure that a new transaction isn’t started when delete is called recursively. It should not be set when calling delete from application code.

new_transaction has a second purpose: since it is never called by application code it also determines whether trying to delete an object with more than one reference to it should cause an exception. When called internally (though recursive delete) it is ok to decrease the reference count. When called by application code trying to delete an object with references to it should raise an exception

select(dbi)

Select/load an object

This method is also aliased as load
load(dbi)

Alias for select

Protected Instance methods
sql_insert(dbi)

Insert basic datatypes into entity table. This method will generate and execute sql to insert basic datatypes. All Arrays, Hashes, and anything that responds to sql_insert.

TODO:if primary key is nil then select max on it from the database, hoping that it is a sequence or autoincrement value. This would require that the sql_create code in the database specific modules reflect this autoincrementing behavior.
sql_insert_array(dbi)

Generate and execute sql to insert arrays. This includes executing sql to insert each member of the array. Objects are inserted into the database if they don’t already exist, in which case their reference count will be incremented in stead of adding a new copy (which would be impossible due to primary key uniqueness constraints).

sql_insert_hash(dbi)

Generate and execute sql to insert hashes. This includes executing sql to insert each key and value member of the hash. Both keys and values will become objects in the database since anything can be a key or a value. Objects are inserted into the database if they don’t already exist, in which case their reference count will be incremented in stead of adding a new copy (which would be impossible due to primary key uniqueness constraints).

sql_insert_assoc(dbi)

insert associated objects (that is, call insert on them and insert a reference into the classname_assoc table). Associated objects are inserted into the database if they don’t already exist, in which case their reference count will be incremented in stead of adding a new copy (which would be impossible due to primary key uniqueness constraints).

This method also takes care of built in classes that need special attention, such as TrueClass and FalseClass. This is done using basic_insert and an optional parameter specifying table name.

sql_delete(dbi)

Delete from sql_ids since that should be enough to casade delete everything. Consequences are deletion of the entity itself (don’t we all just love ON DELETE CASADE).

Prerequisite: primary key or sql_assoc_id must be set.

sql_delete_assocs(dbi)

Delete arrays and hashes and associated objects.

sql_update(dbi)

Generate and execute update sql for simple attributes

sql_select(dbi)

Simple select and load all variables from database. There are two modes of operation in this method: Either selection is done on the primary key (if it is set), or on sql_assoc_id (if primary key isn’t set). @sql_primary_key must hold the primary key symbol.

This will fail horribly if neither primary key not @sql_assoc_id is set.

sql_select_from_id(dbi, assoc_id)

Simple select and load all variables from database. Selection is done on assoc_id. Returns the newly loaded object. Beware: where sql_select loads into self sql_select_from_id loads into a new object.

sql_select_assocs(dbi)

Select and load all arrays, hashes and associated objects.

Private Instance methods
sql_fetch_variable_names(quote_names = true)

Fetch variable names in this class instance. Prepend sql_assoc_id to this list, since that is also going to go in the db. If quote_name is true then escape the names using sql_quote_name.

TODO:If the primary key is nil then do not include it and assume that it is a sequence or an autoincrement value. This would require that the sql_create code in the database specific modules reflect this autoincrementing behavior.
sql_fetch_variable_values(dbi)

Fetch values of all variables in this class instance. Prepend sql_assoc_id to this list, since that is also going to go in the db.

sql_fetch_primary_key_value()
sql_fetch_next_id(dbi)

Fetch the next sql_assoc_id from the database

The function depends on the exsistence of a sql_meta table with an id column.

This method increments and fetches the next sql_assoc_id from the database if this instance does not have an sql_assoc_id yet. Otherwise it just returns the current sql_assoc_id. It is implemented in the most database independent manner.

Database specific versions of this method should be implemented for databases that support faster methods for achieving the same results - sequences for instance.

sql_assoc_id(dbi)

Return the sql_assoc_id. If it isn’t already defined try to load it from the database using the primary key. If that fails then increment the global sql_assoc_id counter and return the new sql_assoc_id.

sql_increase_refcount(dbi, assoc_id)

Increase the reference count on the assoc_id. Objects with a reference count above 0 will not be deleted.

sql_decrease_refcount(dbi, assoc_id)

Decrease the reference count on the assoc_id. Objects with a reference count above 0 will not be deleted.

sql_refcount(dbi, assoc_id)

Return the number of references to assoc_id

sql_basic_insert(dbi, obj, classname = nil)

methods for serializing "basic" datatypes which do not have their own SQLSeriabliable mixin

sql_basic_update(dbi, obj, assoc_id)
sql_basic_delete(dbi, assoc_id)