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

NAME

CallBackery::Database - database access helpers

SYNOPSIS

 use CallBackery::Database;
 my $db = CallBackery::Database->new(app=>$self->config);
 my ($fields,$values) = $self->map2sql(table,data);
 my $selWhere = $self->map2where(table,data);
 my $rowHash = $self->fetchRow(table,{field=>val,field=>val},selExpression?);
 my $value = $self->fetchValue(table,{field=>val,field=>val},column);
 my $id = $self->matchData(table,{field=>val,field=>val});
 my $id = $self->lookUp(table,field,value);
 my $id = $self->updateOrInsertData(table,{dataField=>val,...},{matchField=>val,...}?);
 my $id = $self->insertIfNew(table,{field=>val,field=>val});

DESCRIPTION

Database access helpers.

config

object needs access to the system config to get access to the database

dhb

a dbi database handle

my($fields,$values) = $self->map2sql(table,data);

Provide two hash pointers and quote the field names for inclusion into an SQL expression. Build field names according to the table_field rule.

my $sqlWhere = $self->map2where(table,data);

build a where statement Find a record matching the given data in a table the data is a map. Quote field names and values. Build field names according to the table_field rule.

$hashRef = $self->getMap(table,column);

Get an array of hashes with model and label tags:

 [{model: x, label: y},{id ...},...]
$hashRef = $self->getRowHash(table,{key=value,....},$selectExpr?)>;

Get a hash with record index as key. Optionally with a list of columns to return.

 {
   2 => { a=>x, b=>y },
   3 => { a=>k, b=>r }
 }
$id = $self->fetchRow(table,{key=value,key=>value},$selectExp ?)>;

Find a record matching the given data in a table and return a hash of the matching record.

$id = $self->fetchValue(table,{key=value,key=>value},column)>;

Find a record matching the given data in a table and returns the value in column.

$id = $self->matchData(table,data);

Find a record matching the given data in a table the data is a map.

$id = $self->lookUp(table,column,value)

Lookup the value in table in table_column and return table_id. Throw an exception if this fails. Use matchData if you are just looking.

$id = $self->updateOrInsertData(table,data,match?)

Insert the given data into the table. If a match map is given, try an update first with the given match only insert when update has 0 hits.

$id = $self->insertIfNew(table,data)

Lookup the given data. If it is new, insert a record. Returns the matching Id.

$id = $self->deleteData(table,id)

Delete data from table. Given the record id. Returns true if the record was deleted.

$id = $self->deleteDataWhere(table,{key=val,key=>val})>

Delete data from table. Given the column title and the matching value. Returns true if the record was deleted.

getConfigValue($key)

return a raw data value from the config table

setConfigValue($key,$value)

write a config value

COPYRIGHT

Copyright (c) 2015 by OETIKER+PARTNER AG. All rights reserved.

AUTHOR

Tobi Oetiker <tobi@oetiker.ch>

HISTORY

 2010-06-12 to 1.0 initial
 2013-11-19 to 1.1 converted to mojo