Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Introduction to   DBIx::Lite    id:motemen
me• id:motemen• motemen 美顔器• Application engineer • DBIx::MoCo maintainer
ORM and me• DBIx::MoCo• DBIx::Skinny• Data::Model• Teng
DBIx::Lite• AUTHOR cpan:AAR• “Chained and minimal ORM”• cf. DBIx::Class
SYNOPSIS
Preparation• No classes required to declare • e.g. schemas, row classes• Just use
Initialization• Passing $dbh  my $dbix = DBIx::Lite->new(dbh => $dbh);• Using DBIx::Connector  my $dbix = DBIx::Lite->conn...
CRUD  my $entries = $dbix->table(entries);• Call table() to start building query• Returns ResultSet object
SELECT• Build ResultSet by method chain  my $entries_rs = $dbix->table(entries)      ->search({ author_id => 1 })      ->o...
INSERTmy $entry = $dbix->table(entries)->insert({    author_id => 47,    body => …,    created_at => time(),});# INSERT IN...
UPDATE/DELETE$dbix->table(entries)    ->search({ id => [ 2, 3, 5 ] })    ->update({ body => … });# UPDATE entries AS me SE...
Components
• DBIx::Lite • DBIx::Lite::ResultSet • DBIx::Lite::Row • DBIx::Lite::Schema • DBIx::Lite::Schema::Table
DBIx::Lite• Represents a database (connection)• $dbix->{connector} • isa DBIx::Connector• $dbix->{schema} • isa DBIx::Lite...
DBIL::ResultSet• Represents a set of database rows• Has most major methods• Does not hold actual data in itself • To retri...
DBIL::ResultSet• Chain methods to build specific  result set  $rs   =   $dbix->table(…);  $rs   =   $rs->search(%where);   ...
DBIL::Row•Represents a database row•Simple structure •$row->{data}, $row->{dbix_lite} •$row->hashref•No inflates/deflates•AU...
DBIL::Row• Row operation methods • $row->update(%cols) • $row->delete• pk needed in schema (next)
DBIL::Schema& DBIL::Schema::Table• Represents metadata of tables • (Auto-increment) primary keys • Row classes, ResultSet ...
DBIL::Schema& DBIL::Schema::Table• Setting primary key lets row objects’  update methods to work • Use autopk() for auto-i...
DBIL::Schema& DBIL::Schema::Table• Register has-many relations  $dbix->schema->one_to_many(      authors.id => entries.aut...
Other features
Paging• Paging by offset/limit  my $entries_rs = $dbix->table(entries)      ->rows_per_page(10)->page(3);  $entries_rs->al...
JOIN$dbix->table(entries)->left_join(authors, { author_id => id })->select_also([ authors.name => author_name ])->all;# SE...
Raw DBI accessmy $now = $dbix->dbh_do(sub {    $_->selectcol_arrayref(SELECT NOW())->[0]});• Internally uses DBIx::Connect...
CAVEATS• “SELECT me.* FROM …” queries • Sometimes need to prefix “me.” to   field names• Row classes are not automatically  ...
HACKING• Auto-upgrade connection to master• Need to alter DBIx::Lite and  produced DBIL::ResultSet• Use Role::Tiny• gist:3...
Sum up• Simple APIs• ResultSet• No need to declare classes,  easy to start• Extending may need tricks
Questions?
Upcoming SlideShare
Loading in …5
×

Introduction to DBIx::Lite - Kyoto.pm tech talk #2

4,316 views

Published on

Published in: Technology
  • DOWNLOAD THAT BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book that can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer that is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story That Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money That the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths that Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

Introduction to DBIx::Lite - Kyoto.pm tech talk #2

  1. 1. Introduction to DBIx::Lite id:motemen
  2. 2. me• id:motemen• motemen 美顔器• Application engineer • DBIx::MoCo maintainer
  3. 3. ORM and me• DBIx::MoCo• DBIx::Skinny• Data::Model• Teng
  4. 4. DBIx::Lite• AUTHOR cpan:AAR• “Chained and minimal ORM”• cf. DBIx::Class
  5. 5. SYNOPSIS
  6. 6. Preparation• No classes required to declare • e.g. schemas, row classes• Just use
  7. 7. Initialization• Passing $dbh my $dbix = DBIx::Lite->new(dbh => $dbh);• Using DBIx::Connector my $dbix = DBIx::Lite->connect( dbi:mysql:dbname=..., $username, $password, { mysql_enable_utf8 => 1, RootClass => DBIx::Sunny, }, );
  8. 8. CRUD my $entries = $dbix->table(entries);• Call table() to start building query• Returns ResultSet object
  9. 9. SELECT• Build ResultSet by method chain my $entries_rs = $dbix->table(entries) ->search({ author_id => 1 }) ->order_by(created_at);• Finally retrieve row objects my @entries = $entries_rs->all; # SELECT me.* FROM entries AS me WHERE ( author_id = 1 ) ORDER BY created_at my $entry = $entries_rs->limit(1)->single; # SELECT me.* FROM entries AS me WHERE ( author_id = 1 ) ORDER BY created_at LIMIT 1 OFFSET 0
  10. 10. INSERTmy $entry = $dbix->table(entries)->insert({ author_id => 47, body => …, created_at => time(),});# INSERT INTO entries ( author_id, body, created_at)VALUES ( 47, …, 1345196410 )• If the table has an auto- incremented pkey, LAST_INSERT_ID is filled in
  11. 11. UPDATE/DELETE$dbix->table(entries) ->search({ id => [ 2, 3, 5 ] }) ->update({ body => … });# UPDATE entries AS me SET body = … WHERE ( ( id = 2 ORid = 3 OR id = 5 ) )$dbix->schema->table(entries)->pk(id);$entry->update({ body => … });# UPDATE entries AS me SET body = … WHERE ( id = 1 )
  12. 12. Components
  13. 13. • DBIx::Lite • DBIx::Lite::ResultSet • DBIx::Lite::Row • DBIx::Lite::Schema • DBIx::Lite::Schema::Table
  14. 14. DBIx::Lite• Represents a database (connection)• $dbix->{connector} • isa DBIx::Connector• $dbix->{schema} • isa DBIx::Lite::Schema• Generates ResultSet (next)
  15. 15. DBIL::ResultSet• Represents a set of database rows• Has most major methods• Does not hold actual data in itself • To retrieve row objects: my @all = $rs->all; my $row = $rs->single;
  16. 16. DBIL::ResultSet• Chain methods to build specific result set $rs = $dbix->table(…); $rs = $rs->search(%where); # WHERE $rs = $rs->order_by($col); # ORDER BY $rs = $rs->limit($n); # LIMIT $rs = $rs->select(@cols); # SELECT• search() uses SQL::Abstract• Multiple search()’es joined by “AND”
  17. 17. DBIL::Row•Represents a database row•Simple structure •$row->{data}, $row->{dbix_lite} •$row->hashref•No inflates/deflates•AUTOLOAD method names
  18. 18. DBIL::Row• Row operation methods • $row->update(%cols) • $row->delete• pk needed in schema (next)
  19. 19. DBIL::Schema& DBIL::Schema::Table• Represents metadata of tables • (Auto-increment) primary keys • Row classes, ResultSet classes • Has-a, has-many relationships
  20. 20. DBIL::Schema& DBIL::Schema::Table• Setting primary key lets row objects’ update methods to work • Use autopk() for auto-inc pk $dbix->schema->table(entries)->pk(id);• Give row objects custom methods from the package $dbix->schema->table(entries) ->class(My::Row::Entry);
  21. 21. DBIL::Schema& DBIL::Schema::Table• Register has-many relations $dbix->schema->one_to_many( authors.id => entries.author_id );• ResultSet gains relation method my @entries = $dbix->table(authors) ->search({ name => motemen }) ->entries->all; # SELECT entries.* FROM authors AS me INNER JOIN entries ON ( me.id = entries.author_id ) WHERE ( name = motemen )
  22. 22. Other features
  23. 23. Paging• Paging by offset/limit my $entries_rs = $dbix->table(entries) ->rows_per_page(10)->page(3); $entries_rs->all; # SELECT COUNT(*) FROM entries AS me LIMIT 10 OFFSET 0 # SELECT me.* FROM entries AS me LIMIT 10 OFFSET 20 my $pager = $entries_rs->pager; # => Data::Page
  24. 24. JOIN$dbix->table(entries)->left_join(authors, { author_id => id })->select_also([ authors.name => author_name ])->all;# SELECT me.*, authors.name AS `author_name` FROM entries AS me LEFT OUTER JOIN authors ON ( me.author_id = authors.id )• Use $rs->select_also() to fetch joined tables
  25. 25. Raw DBI accessmy $now = $dbix->dbh_do(sub { $_->selectcol_arrayref(SELECT NOW())->[0]});• Internally uses DBIx::Connector#run
  26. 26. CAVEATS• “SELECT me.* FROM …” queries • Sometimes need to prefix “me.” to field names• Row classes are not automatically require’d
  27. 27. HACKING• Auto-upgrade connection to master• Need to alter DBIx::Lite and produced DBIL::ResultSet• Use Role::Tiny• gist:3378389
  28. 28. Sum up• Simple APIs• ResultSet• No need to declare classes, easy to start• Extending may need tricks
  29. 29. Questions?

×