ActiveRecord-Simple

 view release on metacpan or  search on metacpan

lib/ActiveRecord/Simple.pm  view on Meta::CPAN


    # 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

lib/ActiveRecord/Simple.pm  view on Meta::CPAN


    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();

=head1 CLASS METHODS

L<ActiveRecord::Simple> implements the following class methods.

=head2 new

Object's constructor.

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

=head2 connect

Connect to the database, uses DBIx::Connector if installed, if it's not - L<ActiveRecord::Simple::Connect>.
    
    __PACKAGE__->connect($dsn, $username, $password, $options);


=head2 dbh

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

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


=head2 table_name

Set table name.

    __PACKAGE__->table_name('log');


=head2 columns

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

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


=head2 primary_key

lib/ActiveRecord/Simple.pm  view on Meta::CPAN

Create a relation without foreign keys:

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


=head2 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};

=head2 mixins

Create calculated fields

    Purchase->mixins(
        sum_amount => sub {
            return 'SUM(amount)'
        }
    );

lib/ActiveRecord/Simple.pm  view on Meta::CPAN



=head2 delete

Delete object from the database

=head2 update

Update object using hashref

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


=head2 to_hash

Unbless object, get naked hash


=head2 increment

Increment fields

lib/ActiveRecord/Simple/Find.pm  view on Meta::CPAN


=head1 METHODS

L<ActiveRecord::Simple::Find> implements the following methods.

=head2 new

Object constructor, creates basic search pattern. Available from method "find"
of the base class:

    # SELECT * FROM log WHERE site_id = 1 AND level = 'error';
    my $f = Log->find({ id => 1, level => 'error' });

    # SELECT * FROM log WHERE site_id = 1 AND level IN ('error', 'warning');
    my $f = Log->find({ id => 1, level => ['error', 'warning'] });

    # SELECT * FROM customer WHERE age > 21;
    Customer->find('age > ?', 21);

    # SELECT * FROM customer WHERE id = 100;
    Customer->find(100);

    # SELECT * FROM customer WHERE id IN (100, 101, 191);
    Customer->find([100, 101, 191]);

=head2 last

Fetch last row from database:

    # get very last log:
    my $last_log = Log->find->last; 

    # get last error log of site number 1:
    my $last_log = Log->find({ level => 'error', site_id => 1 })->last;

=head2 first

Fetch first row:

    # get very first log:
    my $first_log = Log->find->first; 

    # get first error log of site number 1:
    my $first_log = Log->find({ level => 'error', site_id => 1 })->first;

=head2 count

Fetch number of records in the database:

    my $cnt = Log->find->count();
    my $cnt_warnings = Log->find({ level => 'warnings' })->count;

=head2 exists 

Check the record is exist:

    if (Log->find({ level => 'fatal' })->exists) {
        die "got fatal error log!";
    }

=head2 fetch

Fetch data from the database as objects:

    my @errors = Log->find({ level => 'error' })->fetch;
    my $errors = Log->find({ level => 'error' })->fetch; # the same, but returns ARRAY ref
    my $error = Log->find(1)->fetch; # only one record
    my @only_five_errors = Log->find({ level => 'error' })->fetch(5);

=head2 next

Fetch next n rows from the database:

    my $finder = Log->find({ level => 'info' });
    
    # get logs by lists of 10 elements:
    while (my @logs = $finder->next(10)) {
        print $_->id, "\n" for @logs;
    }

=head2 only 

Specify field names to get from database:

    # SELECT id, message FROM log;
    my @logs = Log->find->only('id', 'message');

=head2 fields

The same as "only":

    # SELECT id, message FROM log;
    my @logs = Log->find->fields('id', 'message');

=head2 order_by

Set "ORDER BY" command to the query:

    # SELECT * FROM log ORDER BY inserted_time;
    my @logs = Log->find->order_by('inserted_time');

    # SELECT * FROM log ORDER BY level, id;
    my @logs = Log->find->order_by('level', 'id');

=head2 asc

Set "ASC" to the query:

    # SELECT * FROM log ORDER BY id ASC;
    my @logs = Log->find->order_by('id')->asc;

=head2 desc

Set "DESC" to the query:

    # SELECT * FROM log ORDER BY id DESC;
    my @logs = Log->find->order_by('id')->desc;

=head2 limit

SET "LIMIT" to the query:

    # SELECT * FROM log LIMIT 100;
    my @logs = Log->find->limit(100);

=head2 offset

SET "OFFSET" to the query:

    # SELECT * FROM log LIMIT 100 OFFSET 99;
    my @logs = Log->find->limit(100)->offset(99);

=head2 group_by

Set "GROUP BY":

    my @logs = Log->find->group_by('level');

=head2 with 

Set "LEFT JOIN" command to the query:

    # SELECT l.*, s.* FROM logs l LEFT JOIN sites s ON s.id = l.site_id
    my @logs_and_sites = Log->find->with('sites');
    print $_->site->name, ": ", $_->mesage for @logs_and_sites;

=head2 left_join

The same as "with" method

=head2 uplod

Fetch object from database and load into ActiveRecord::Simple::Find object:

    my $logs = Log->find({ level => ['error', 'fatal'] });
    $logs->order_by('level')->desc;
    $logs->limit(100);
    $logs->upload;

    print $_->message for @$logs;

=head2 to_sql

Show SQL-query that genereted by ActiveRecord::Simple::Find class:

    my $finder = Log->frind->only('message')->order_by('level')->desc->limit(100);
    print $finder->to_sql; # prints: SELECT message FROM log ORDER BY level DESC LIMIT 100;


=head1 EXAMPLES

    # SELECT * FROM pizza WHERE name = 'pepperoni';
    Pizza->find({ name => 'pepperoni' });

    # SELECT first_name, last_name FORM customer WHERE age > 21 ORDER BY id DESC LIMIT 100;
    Customer->find('age > ?', 21)->only('first_name', 'last_name')->order_by('id')->desc->limit(100);



( run in 1.186 second using v1.01-cache-2.11-cpan-49f99fa48dc )