NAME

DBIx::Meld::Traits::Abstract - Melds SQL::Abstract with DBIx::Meld.

DESCRIPTION

This trait is a wrapper around SQL::Abstract::Limit, which itself is a lightweight wrapper around SQL::Abstract by automatically calling prepare() and execute() on the generated SQL.

The arguments to the various methods are identical to the SQL::Abstract methods, excepts for the extra clauses RETURNING, ORDER BY, LIMIT, and OFFSET. In these cases the format of the argument has been changed to be a hashref so that more clauses can be added in the future, such as GROUP BY, HAVING, etc.

ATTRIBUTES

abstract

The SQL::Abstract::Limit (a subclass of SQL::Abstract that adds LIMIT/OFFSET support) object.

METHODS

insert

$meld->insert(
    'users',                                            # table name
    { user_name=>'bob2003', email=>'bob@example.com' }, # fields to insert
    { returning => 'user_id' },                         # extra clauses
);

This accepts the same arguments as SQL::Abstract's insert() method accepts.

update

$meld->update(
    'users',                 # table name
    { phone => '555-1234' }, # fields to update
    { user_id => $uid },     # where clause
);

This accepts the same arguments as SQL::Abstract's update() method accepts.

delete

$meld->delete(
    'users',             # table name
    { user_id => $uid }, # where clause
);

This accepts the same arguments as SQL::Abstract's delete() method accepts.

array_row

my $user = $meld->array_row(
    'users',                                  # table name
    ['user_id', 'created', 'email', 'phone'], # fields to retrieve
    { user_name => $uname },                  # where clause
);

hash_row

my $user = $meld->hash_row(
    'users',                    # table name
    ['user_id', 'created'],     # fields to retrieve
    { user_name => 'bob2003' }, # where clause
);

array_of_array_rows

my $disabled_users = $meld->array_of_array_rows(
    'users',                       # table name
    ['user_id', 'email', 'phone'], # fields to retrieve
    { status => 0 },               # where clause
    { order_by => 'status' },      # extra clauses
);
print $disabled_users->[2]->[1];

Returns an array ref of array refs, one for each row returned.

array_of_hash_rows

my $disabled_users = $meld->array_of_hash_rows(
    'users',                       # table name
    ['user_id', 'email', 'phone'], # fields to retrieve
    { status => 0 },               # where clause
    { order_by => 'user_name' },   # extra clauses
);
print $disabled_users->[2]->{email};

hash_of_hash_rows

my $disabled_users = $meld->hash_of_hash_rows(
    'user_name',                   # column to index the hash by
    'users',                       # table name
    ['user_id', 'email', 'phone'], # fields to retrieve
    { status => 0 },               # where clause
    { limit => 20 },               # extra clauses
);
print $disabled_users->{jsmith}->{email};

count

my $enabled_users_count = $meld->count(
    'users',        # table name
    { status => 1}, # where clause
);

column

my $user_ids = $meld->column(
    'users',                            # table name
    'user_id',                          # column to retrieve
    { status => 1},                     # where clause
    { limit=>10, order_by=>'user_id' }, # extra clauses
);

select_sth

my ($sth, @bind) = $meld->select_sth(
    'users',                  # table name
    ['user_name', 'user_id'], # fields to retrieve
    {status => 1 },           # where clause
);
$sth->execute( @bind );
$sth->bind_columns( \my( $user_name, $user_id ) );
while ($sth->fetch()) { ... }

If you want a little more power, or want you DB access a little more effecient for your particular situation, then you might want to get at the select sth.

insert_sth

my $insert_sth;
foreach my $user_name (qw( jsmith bthompson gfillman )) {
    my $fields = {
        user_name => $user_name,
        email     => $user_name . '@mycompany.com',
    };

    $insert_sth ||= $meld->insert_sth(
        'users', # table name
        $fields, # fields to insert
    );

    $insert_sth->execute(
        $meld->bind_values( $fields ),
    );
}

If you're going to insert a *lot* of records you probably don't want to be re-generating the SQL every time you call $meld->insert().

bind_values

This mehtod is a non-modifying wrapper around SQL::Abstract's values() method to be used in conjunction with insert_sth().

AUTHOR

Aran Clary Deltac <bluefeet@gmail.com>

LICENSE

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