NAME

MKDoc::SQL::Table - Table object.

API

SCHEMA METHODS

These are the methods which are related to schema manipulation.

$class->new (%args);

MKDoc::SQL::Table->new (
    bless_into => 'Object::Class'.
    name       => $table_name,
    pk         => [ $name1 ],
    cols       => [
      { name => $name1, type => $type1 },
      { name => $name2, type => $type2 } ],
    unique     => { $name1 => [ $col1, $col2 ] }
    index      => { $name2 => [ $col2 ] }
    fk         => { foreign_table => { source_col => target_col } }
    ai         => TRUE / FALSE
);

Constructs a new MKDoc::SQL::Table object. Arguments are:

bless_into - Optional class to bless this table's records into a given class
name - This table's SQL name
pk - Optional list of SQL columns to be used as a primary key
cols - List of columns definitions for this table
unique - Optional hashref of unique indexes for this table
index - Optional hashref of non unique indexes for this table
fk - Optional hashref of foreign keys constraints
ai - Auto-Increment flag for primary key

$class->create_all();

Creates all tables.

$class->drop_all();

Drops all tables.

$class->table ($table_name);

Returns a MKDoc::SQL::Table object corresponding to $table_name.

$class->save_state ($directory);

Writes the current schema into $directory.

$class->load_state ($directory);

Loads the current schema from $directory.

$class->load_driver ($directory);

Loads the driver.pl contained in $directory.

$self->name();

Returns this table's SQL name.

$self->not_null();

Returns this table's column names which are not null as a list.

$class->not_null (@cols);

Sets this table's columns which have to be not null.

$self->referencing_tables();

Returns all the table names which reference $self through foreign key constraints.

$self->pk();

Returns the primary key as an array or array ref depending on context.

$self->pk ($array or $arrayref);

Sets the primary key.

$self->ai();

Returns wether the pk is auto-increment or not.

$self->ai ($boolean);

Sets auto-increment for the table primary key provided that it has only one field that can be auto-incremented.

If the table's primary key is not suitable for auto_increment, an exception is raised.

$self->unique();

Return all the unique indexes names for that Table.

$self->unique ($constraint_name);

Return the array of columns that are indexed for that unique constraint.

$self->unique ($constraint_name, $arrayref);

Sets the unique index for this constraint to $arrayref.

$self->unique ($name, $col1, ..., $coln);

Same thing.

$self->index();

Return all the non-unique indexes names for that Table.

$self->index ($index_name);

Return the array of columns that are indexed for that non-unique index.

$self->index ($index_name, $arrayref);

Sets the unique index for this non-unique index to $arrayref.

$self->index ($name, $col1, ..., $coln);

Same thing.

$self->fk();

Returns all the table names which $self references.

$self->fk ($foreign_table);

Returns all the $self <-> $foreign table column mapping as a hash or hashref depending upon the context.

$self->fk ($foreign_table, $mapping);

Sets the mapping from $self to $foreign_table.

$self->cols();

Returns all the column names in the order which they were defined.

$self->cols ($name);

Returns the column type for $name, undef if no such column is defined.

$self->cols ($name, $type);

Changes $type for $name, or appends column $name, $type if $name is not yet defined, or removes $name if $type is undefined.

DATA MANIPULATION METHODS

$self->get ($id);

Works only if the primary key is set to be ONE field.

Returns the record which ID is $id, or undef if no records are found.

$self->get (col1 => 'val1', col2 => 'val2'...);

Returns the first record which matches the condition defined by the hash which is passed as an argument.

$self->get ($condition);

Returns the first record that matches the MKDoc::SQL::Condition object.

$self->count():

Returns the number of records that this table handles.

$self->count (col1 => 'val1', col2 => 'val2'...);

Returns the number of records matched by the condition defined by the hash which is passed as an argument.

$self->count ($condition);

Returns the number of records matched by the MKDoc::SQL::Condition object.

$self->search():

Returns all this table's records as a list of hashrefs / objects.

$self->search (col1 => 'val1', col2 => 'val2'...);

Returns all the records matching the condition as a list of hashref / objects.

$self->search ($condition);

Returns all the records matched by the MKDoc::SQL::Condition object as a list of hashref / objects.

$self->modify ($hash or $hashref);

Modifies the record which primary key maches whatever is specified in $hashref to the values of $hashref.

Only works when a primary key is defined in the schema.

$self->select (%args);

Usage:

my $query = $table->select (
    cols     => [ qw /col1 col2 col3/ ],
    where    => $condition,
    sort     => [ qw /col1 col2/ ],
    desc     => TRUE / FALSE,
    distinct => TRUE / FALSE,
    page     => [ $slice, $thickness ]
);

Returns a MKDoc::SQL::Query object which represents this database query. Condition can be a MKDoc::SQL::Condition object or a hashref.

$self->delete ($condition);

Delete all the rows which match $condition. Performs no foreign key checks.

$self->delete_cascade ($condition);

Delete all the rows which match $condition, eventually cascading.

$self->insert ($hashref);

Insert record represented by $hashref. Returns the inserted auto-increment value if any.

$self->update ($hashref, $condition);

Sets all the rows that maches $condition to the values specified in $hashref, and returns the number of columns modified.

MISCELLEANOUS METHODS

$class->query_stack();

Returns the SQL query stack - useful for debugging.

$class->query_stack (@sql_commands);

Pushes all these stacks to the current QUERY_STACK global array.

$self->lock();

Attempts to lock the table.

$self->unlock();

Attempts to unlock the table.

$class->force_unlock();

Force unlock all tables

$class->driver ($driver_name);

Imports into the current package the driver specific subroutines. This is really wrong and should be implemented using a bridge design pattern.