The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Norma::DB - Easy interface to fundamental data access and table definition operations in DBI

SYNOPSIS

  use Norma::DB;

  my $db = Norma::DB->initialize(
      dsn => 'dbi:mysql:database=testdb',
      username => 'dbuser',
      password => 'dbpassword',
  );

  my $id = $db->insert(
      table_name => 'recipes',
      values => {
          title => "Scrambled Eggs",
          instructions => 'Break two eggs into a bowl...',
          date_added => sub { 'NOW()' },
          ...
      }
  );

  $db->update(
      table_name => 'recipes',
      values => { title => "Delicious Scrambled Eggs" },
      where => "id = 1",
  );

  my $recipe = $db->select(
      table_name => 'recipes', 
      where => "id = 1",
  );

  $db->delete(
      table_name => 'recipes',
      where => "id = 1",
  );

METHODS

initialize( dsn => $dsn, username => $username, password => $password )

Set up an instance, given some connection criteria and authentication info. Alternatively, pass in an existing database handle as "dbh" if you already have one.

insert( table_name => $table_name, values => {...} )

Insert a row, given a table name and values for the row. Values will be escaped using DBI::quote except for subref values. Values that are references to subs will be executed, and their return value subtituted in for that column, useful for calling database functions (e.g. date_created => sub { 'NOW()' }). Returns the primary id for the inserted row.

merge( table_name => $table_name, values => {...} )

Similar to insert, but only insert the row if it's not already there (i.e., if it doesn't break a unique constraint). Values are processed as they are in insert(). Return criteria to select the row, whether it was inserted or already there. Implemented nicely in a single query for MySQL, less elegantly for SQLite.

update( table_name => $table_name, values => {...}, where => $where_condition )

Update rows in a table, given values to update, and matching criteria. Values are processed as they are in insert() Returns the number of rows affected.

delete( table_name => $table_name, where => $where_condition )

Delete rows in a table, given criteria. Returns number of rows affected.

select( table_name => $table_name, where => $where_condition )

Select rows from a table, given criteria.

get_table_definition( table_name => $table_name )

Get a table definition, given a table name

get_table_primary_key_field_names( table_name => $table_name )

Get primary key field names, given a table name.

get_table_key_field_names( table_name => $table_name )

Get names of columns that have single-column unique/primary indexes, given a table name

Database-Specific Drivers

DBI is great, but it doesn't cover everything. Norma::DB is augmented by database-specific drivers in order to support some bits of functionality like merge() and get_table_key_field_names()

Norma::DB::MySQL provides support for MySQL Norma::DB::SQLite provides support for SQLite3

AUTHOR

David Chester <davidchester@gmx.net>

COPYRIGHT AND LICENSE

This software is copyright (c) 2010-2011 by David Chester.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.