NAME

ActiveRecord::Simple - Simple to use lightweight implementation of ActiveRecord pattern.

DESCRIPTION

ActiveRecord::Simple is a simple lightweight implementation of ActiveRecord pattern. It's fast, very simple and very light.

SYNOPSIS

    package Model;

    use parent 'ActiveRecord::Simple';

    # connect to the database:
    __PACKAGE__->connect($dsn, $opts);


    package Customer;

    use parent 'Model';

    __PACKAGE__->table_name('customer');
    __PACKAGE__->columns(qw/id first_name last_login/);
    __PACKAGE__->primary_key('id');

    __PACKAGE__->has_many(purchases => 'Purchase');


    package Purchase;

    use parent 'Model';

    __PACKAGE__->auto_load(); ### load table_name, columns and primary key from the database automatically

    __PACKAGE__->belongs_to(customer => 'Customer');


    package main;

    # get customer with id = 1:
    my $customer = Customer->objects->find({ id => 1 })->fetch(); 

    # or (the same):
    my $customer = Customer->objects->get(1);

    print $customer->first_name; # print first name
    $customer->last_login(\'NOW()'); # to use built-in database function just send it as a SCALAR ref
    $customer->save(); # save in the database

    # get all purchases of $customer:
    my @purchases = Purchase->objects->find(customer => $customer)->fetch();

    # or (the same):
    my @purchases = $customer->purchases->fetch();

    # order, group and limit:
    my @purchases = $customer->purchases->order_by('paid')->desc->group_by('kind')->limit(10)->fetch();

CLASS METHODS

ActiveRecord::Simple implements the following class methods.

new

Object's constructor.

    my $log = Log->new(message => 'hello', level => 'info');

connect

Connect to the database, uses DBIx::Connector if installed, if it's not - ActiveRecord::Simple::Connect.

    __PACKAGE__->connect($dsn, $username, $password, $options);

dbh

Access to the database handler. Undef if it's not connected.

    __PACKAGE__->dbh->do('SELECT 1');

table_name

Set table name.

    __PACKAGE__->table_name('log');

columns

Set columns. Make accessors if make_columns_accessors not 0 (default is 1)

    __PACKAGE__->columns('id', 'time');

primary_key

Set primary key. Optional parameter.

    __PACKAGE__->primary_key('id');

secondary_key

Set secondary key.

    __PACKAGE__->secondary_key('time');

auto_load

Load table_name, columns and primary_key from table_info (automatically from database).

    __PACKAGE__->auto_load();

has_many

Create a ralation to another table (many-to-many, many-to-one).

    Customer->has_many(purchases => 'Purchase');
    # if you need to set a many-to-many relation, you have to 
    # specify a third table using "via" key:
    Pizza->has_many(toppings => 'Topping', { via => 'pizza_topping' });

belongs_to

Create a relation to another table (one-to-many, one-to-one). Foreign key is an optional parameter, default is <table tane>_id.

    Purchase->belongs_to(customer => 'Customer');
    # or
    Purchase->belong_to(customer => 'Customer', { fk => 'customer_id' });

has_one

Create a relation to another table (one-to-one).

    Customer->has_one(address => 'Address');

generic

Create a relation without foreign keys:

    Meal->generic(critical_t => 'Weather', { t_max => 't' });

make_columns_accessors

Set to 0 before method 'columns' if you don't want to make accessors to columns:

    __PACKAGE__->make_columns_accessors(0);
    __PACKAGE__->columns('id', 'time'); # now you can't get $log->id and $log->time, only $log->{id} and $log->{time};

mixins

Create calculated fields

    Purchase->mixins(
        sum_amount => sub {
            return 'SUM(amount)'
        }
    );
    # and then
    my $purchase = Purchase->find({ id => 1 })->fields('id', 'title', 'amount', 'sum_amount')->fetch;

relations

Make a relation. The method is aoutdated.

objects

Returns instance of ActiveRecord::Simple::QueryManager.

find [DEPRECATED]

Returns ActiveRecord::Simple::Find object.

    my $finder = Customer->find(); # it's like ActiveRecord::Simple::Find->new();
    $finder->order_by('id');
    my @customers = $finder->fetch;

all [DEPRECATED]

Same as __PACKAGE__->find->fetch;

get [DEPRECATED]

Get object by primary_key

    my $customer = Customer->get(1);
    # same as Customer->find({ id => 1 })->fetch;

count

Get number of rows

    my $cnt = Customer->count('age > ?', 21);

exists

Check if row is exists in the database

    warn "Got Barak!" 
        if Customer->exists({ name => 'Barak Obama' })

OBJECT METHODS

ActiveRecord::Simple implements the following object methods.

is_defined

Check object is defined

save

Save object to the database

delete

Delete object from the database

update

Update object using hashref

    $user->update({ last_login => \'NOW()' });

to_hash

Unbless object, get naked hash

increment

Increment fields

    $customer->increment('age')->save;

decrement

Decrement fields

    $customer->decrement('age')->save;

    

AUTHOR

shootnix, <shootnix at cpan.org>

BUGS

Please report any bugs or feature requests to shootnix@cpan.org, or through the github: https://github.com/shootnix/activerecord-simple/issues

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc ActiveRecord::Simple

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

Copyright 2013-2018 shootnix.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.