NAME
Whatbot::Database::Table - Class wrapper for a database table
SYNOPSIS
class Whatbot::Database::Table::ATable extends Whatbot::Database::Table {
sub BUILD(...) {
$table->init_table({
'name' => 'a_table',
'primary_key' => 'a_table_id',
'indexed' => ['thing'],
'columns' => {
'a_table_id' => {
'type' => 'serial'
},
'thing' => {
'type' => 'varchar',
'size' => 32
},
},
});
$table->create({
'thing' => 'Test',
});
}
}
DESCRIPTION
Whatbot::Database::Table wraps a database table into a simple class to add and return data from. To generate a class for a given table, pass 'init_table' to a new Table object with the table name and column definitions. If the table doesn't exist in the database, it will be auto created for you. Once the object is live, 'create' and 'search' methods are available to add and retrieve rows (Whatbot::Database::Table::Row) from the database. To delete or update data, perform those actions directly on the returned rows.
METHODS
- init_table( \%table_params )
-
Create a new table definition, generally called in BUILD. Possible keys are:
- name
-
Table name, as it should be referred to in the data store.
- primary_key
-
Optional. The primary key name in the table.
- columns
-
A hashref, where the key is the column name, and the value is another hashref containing a "type", and optionally, a "size". For example:
columns => { 'a_column' => { 'type' => 'varchar', 'size' => 32, } }
- defaults
-
Optional. A hashref where the key is a column name, and the value is a default value for that column on create.
- create( \%column_data )
-
Create a new row in this table. The passed hashref should contain the column names as keys, with the desired data in values. Any column not listed in the hashref will be filled by the corresponding entry in init_table's 'defaults' if available, or will be left to the database to decide. Returns a Whatbot::Database::Table::Row object if successful, undef on failure.
- find($key_id)
-
Search the table to find the given value in the primary key column. Returns nothing if not found.
- count(<\%search_data>)
-
Return the number of rows in the table. Can be filtered with a search query similar to what would be sent to search_one() or search().
- search(<\%search_data>)
-
Search the table and return results. The optional search_data hashref allows filtering of the results. Return an empty arrayref if no results are found, or an arrayref of Whatbot::Database::Table::Row instances. The search_data hashref can contain key => value pairs that correspond to a column name and a value for equal searches, otherwise, a value can be a hashref that contains a key of 'LIKE' and a value of the like query. Keys that start with underscore are reserved, and can include:
- search_one(<\%search_data>)
-
Search the table and return one Whatbot::Database::Table::Row instance, or nothing if it is not found.
LICENSE/COPYRIGHT
Be excellent to each other and party on, dudes.