DBIx::Browse - Perl extension to browse tables.
use DBIx::Browse;
my ($dbh, $dbb, $q);
$dbh = DBI->connect("DBI:Pg:dbname=enterprise")
or croak "Can't connect to database: $@";
$dbixbr = new DBIx::Browse({
dbh => $dbh,
table => 'employee',
proper_fields => [ qw ( name fname ) ],
linked_fields => [ qw ( department category office ) ],
linked_tables => [ qw ( department category office ) ],
linked_values => [ qw ( name name phone ) ],
linked_refs => [ qw ( id id ide ) ],
aliases => [ qw ( name fname department category phone )],
primary_key => 'id'
});
## Insert a record
$dbixbr->add({
name => 'John',
department => 'Sales',
category => 'Sales Representant',
phone => '1114'
});
## Update a record
$dbixbr->update({
record => { phone => '1113', category => 'Sales Manager' }
where => 'id = 123 '
});
...etc
The purpose of DBIx::Browse is to handle the browsing of relational tables.
DBIx::Browse transparently translates SELECTs, UPDATEs, DELETEs and INSERTs from the desired ``human view'' to the values needed for the table. This is the case when you have related tables (1 to n) where the detail table has a reference (FOREIGN KEY) to a generic table (i.e. Customers and Bills) with some index (tipically an integer).
If present, linked_tables, linked_values and linked_refs must have the same number of elements than linked_fields.
my $dbixbr = new DBIx::Browse({
table => 'employee',
proper_fields => 'name',
linked_fields => ['departament','category']
})
(...)
$my $sth = $dbixbr->prepare({
where => "departament = 'Adminitstration' AND age < 35",
order => "name ASC, departament ASC"
}
instead of:
$my $sth = $dbh->prepare(
"SELECT employee.name AS name,
departament.name AS departament,
category.name AS category
FROM employee, departament, category
WHERE departament.id = employee.departament AND
category.id = employee.category AND
departament.name = 'Administration' AND
employee.age < 35
ORDER BY employee.name ASC, departament.name ASC"
);
All parameters are passed in a hash reference containig the following fields (all optional):
The last column will always be the declared primary key for the main table. The column name will be generated with the pkey_name method.
field_number: An index indicating the field number (as declared in the new method). If the field is a linked field (related to other table) it will return the values of the related table (as described by linked_table, and linked_values in the new method).
The DBI driver to use MUST allow to set AutoCommit to zero.
The syntax construction of queries have only been tested against PostgreSQL and MySQL.
Not all the clauses are supported by all DBI drivers. In particular, the ``LIMIT'' and ``OFFSET'' ones are non SQL-standard and have been only tested in PostgresSQL and MySQL (in this later case, no especific OFFSET clause exists but the DBIx::Browse simulates it by setting accordingly the ``LIMIT'' clause).
Evilio José del Río Silván, edelrio@icm.csic.es
perl(1), DBI(3).