ActiveRecord-Simple

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

        + Dependancy on SQL::Translator now is optional (thanks to @kberov)
        + Added Credits - list of contributors (see README)
        * Fixed bugs
        * Improved tests

0.70    2015-08-14
        + Added ARS_TRACE
        + Created method "update" for quick objects update
        + New mars command "--upload"
        * Improved `find` and `count` methods, now you can use find({ id => [1, 2, 3] }) as '.. where id in (1, 2, 3)'
        * Improved error handling
        * Method `new` now takes simple hashes (not only hashrefs)
        * Improved documentation

0.80    2016-01-05
        + Added method "abstract"
        + Added method "select"
        + Added method "update"
        + Added method "abstract"
        + Added method "next"
        + Added "where in ... " condition to find
        + Added method "connect"
        * Improved error handling
        * Improved "new" method
        + Added LEFT JOIN
        * Optimization of data fetch
        * Improved documentation
        + Added package method "load_info"
        + "Smart accessors"
        + Added cookbook
        * Improved tests

0.84    2016-07-13

Changes  view on Meta::CPAN

        * Renamed method "load_info" to "autoload"
        * Renamed "use_smart_saving" to "autosave"
        * Fixed typos

0.92    2017-08-20
        * Improve "next" method in favor less memory usage
        * Fixes

0.93    2017-08-21
        + Method "next" not takes a number of given objects. Default is 1
        * Fixed auto_load error
        * Fixed error messages
        * Different fixes

0.95    2017-09-22
        + Method "sql_fetch_all"
        + Method "sql_fetch_row"

0.96    2017-09-25
        + Method "all"
        + Mixins now can get $class
        + Added class Meta.pm, access via method "META" in ARS::Model

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

    $class->primary_key($primary_key) if $primary_key;
    $class->columns(@columns) if @columns;
}

sub connect {
    my ($class, $dsn, $username, $password, $options) = @_;

    eval { require DBIx::Connector };

    $options->{HandleError} = sub {
        my ($error_message, $DBI_st) = @_;

        $error_message or return;
        croak $error_message;

    } if ! exists $options->{HandleError};

    if ($@) {
        $connector = ActiveRecord::Simple::Connect->new($dsn, $username, $password, $options);
        $connector->db_connect;
    }
    else {
        $connector = DBIx::Connector->new($dsn, $username, $password, $options);
    }

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

    no strict 'refs';
    RELATION_NAME:
    for my $relation_name ( keys %{ $relations }) {
        my $pkg_method_name = $class . '::' . $relation_name;
        next RELATION_NAME if $class->can($pkg_method_name); ### FIXME: orrrr $relation_name???

        my $relation           = $relations->{$relation_name};
        my $full_relation_type = _get_relation_type($class, $relation);
        my $related_class      = _get_related_class($relation);

        ### TODO: check for error if returns undef
        my $pk = $relation->{params}{pk};
        my $fk = $relation->{params}{fk};

        my $instance_name = "relation_instance_$relation_name";

        if (grep { $full_relation_type eq $_ } qw/one_to_many one_to_one one_to_only/) {
            *{$pkg_method_name} = sub {
                my ($self, @args) = @_;
                if (@args) {
                    my $object = shift @args;

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

}

sub DESTROY { }

sub AUTOLOAD {
    my $call = $AUTOLOAD;
    my $self = shift;
    my $class = ref $self;

    $call =~ s/.*:://;
    my $error = "Can't call method `$call` on class $class.\nPerhaps you have forgotten to fetch your object?";

    croak $error;
}

1;

__END__;


=head1 NAME

ActiveRecord::Simple::Find

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;

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

    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:

t/07-auto_load.t  view on Meta::CPAN


use Test::More;


ok my $customer = Customer->new();
eval { $customer->first_name };
ok ! $@, 'loaded accessor `first_name`';
eval { $customer->id };
ok ! $@, 'loaded accessor `id`';
eval { $customer->foo };
ok $@, 'error load undefined accessor';

is(Customer->_get_table_name, 'customer', 'loaded table name');
is(Customer->_get_primary_key, 'id', 'loaded primary key');


done_testing();




( run in 0.527 second using v1.01-cache-2.11-cpan-65fba6d93b7 )