![]() |
![]()
| ![]() |
![]()
NAMEDBIx::Lite - Chained and minimal ORM VERSIONversion 0.36 SYNOPSISuse DBIx::Lite; my $dbix = DBIx::Lite->new(driver_name => 'Pg'); # disconnected mode my $dbix = DBIx::Lite->new(dbh => $dbh); my $dbix = DBIx::Lite->connect("dbi:Pg:dbname=$db", $user, $passwd, {pg_enable_utf8 => 1}); # build queries using chained methods -- no schema definition required my $authors_rs = $dbix->table('authors'); my $authors_rs = $dbix->table('authors')->search({ country => 'IT' }); my $books_rs = $dbix ->table('books') ->select('id', 'title', 'year') ->left_join('authors', { author_id => 'id' }) ->select_also(['authors.name' => 'author_name']) ->order_by('year'); # retrieve rows and columns -- still no schema definition required my @authors = $authors_rs->all; my $author = $authors_rs->search({ id => 1 })->single; while (my $book = $books_rs->next) { printf "%s (%s)\n", $book->title, $book->author_name; # automatic accessor methods } my @author_names = $authors_rs->get_column('name'); my $book_count = $books_rs->count; # manipulate rows my $book = $dbix->table('books')->insert({ name => 'Camel Tales', year => 2012 }); $books_rs->search({ year => { '<' => 1920 } })->update({ very_old => 1 }); $authors_rs->search({ age => { '>' => 99 } })->delete; # define a primary key and get more features $dbix->schema->table('authors')->autopk('id'); my $author = $dbix_lite->table('authors')->find(2); $author->update({ age => 40 }); $author->delete; # define relationships $dbix->schema->one_to_many('authors.id' => 'books.author_id', 'author'); my $author = $books->author; my $books_rs = $author->books->search({ year => 2012 }); my $book = $author->insert_related('books', { title => "A Camel's Life" }); # define custom object classes $dbix->schema ->table('subjects') ->class('My::Subject') ->resultset_class('My::Subject::ResultSet'); ABSTRACTMany ORMs and DBI abstraction layers are available on CPAN, one of the most notables being DBIx::Class which provides the most powerful features to handle database contents using OOP. DBIx::Lite was written with some goals in mind, that no other available module provides. Such goals/key features are:
METHODSInstantiating a DBIx::Lite object isn't more difficult than just writing: my $dbix = DBIx::Lite->new(driver_name => 'Pg'); Driver name is the name of the DBI module you expect to use. We need to specify it as the generated SQL will depend on the driver. This constructor will give you an unconnected object, that you can use to generate SQL commands using the select_sql(), insert_sql(), update_sql() and delete_sql() methods without executing it. If you want to connect to a database you can pass a pre-connected database handle with the "dbh" argument or you can supply your connection options to the connect() method. All arguments passed to connect() will be just passed to DBIx::Connector which will be used to manage your connection under the hood. my $dbix = DBIx::Lite->new(dbh => $dbh); my $dbix = DBIx::Lite->connect("dbi:Pg:dbname=$db", $user, $passwd, {pg_enable_utf8 => 1}); Note that connect() can be called as an object method too, if you want to connect an unconnected DBIx::Lite object at a later stage: my $dbix = DBIx::Lite->new; $dbix->connect("dbi:Pg:dbname=$db", $user, $passwd); newThis class method may accept the following optional arguments:
connectThis methods accepts a list of arguments that are passed to DBIx::Connector. It returns the DBIx::Lite object. It can be called either as class or object method. tableThis method accepts a table name and returns a DBIx::Lite::ResultSet object on which you can chain its methods to build your query. my $rs = $dbix->table('books'); schemaThis method returns our DBIx::Lite::Schema object which may hold the definitions required for some advanced feature of DBIx::Lite. You can call then call its methods: $dbix->schema->table('authors')->autopk('id'); See the DBIx::Lite::Schema documentation for an explanation of its methods. dbhThis method returns a DBI database handle that you can use to perform manual queries. txnThis method accepts a coderef which will be run inside a transaction. $dbix->txn(sub { $dbix->table('books')->update({ year => 2015 }); }); AUTHORAlessandro Ranellucci <aar@cpan.org> COPYRIGHT AND LICENSEThis software is copyright (c) 2024 by Alessandro Ranellucci. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
|