DBIx::Class walkthrough @ bangalore pm
Upcoming SlideShare
Loading in...5
×
 

DBIx::Class walkthrough @ bangalore pm

on

  • 624 views

 

Statistics

Views

Total Views
624
Views on SlideShare
617
Embed Views
7

Actions

Likes
1
Downloads
1
Comments
0

2 Embeds 7

http://sheeju.github.io 4
http://localhost 3

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

DBIx::Class walkthrough @ bangalore pm DBIx::Class walkthrough @ bangalore pm Presentation Transcript

  • + In this presentation I will walk through DBIx::Class which helps you get started DBIX::CLASS WALK THROUGH
  • ~ Sheeju Alex ~
  • DBI BACKGROUND + Perl Database Interface Module + High level layer to interact with databases
  • DBI BACKGROUND + Awesome module which can connect to any database you ask + Uniform interface to handle different types of databases
  • + Is an ORM (Object Relational Mapper) + SQL => Object DBIX::CLASS + Database tables becomes Object + Table data and relationships between tables become object methods
  • + Alternatives? DBIX::CLASS + DBIx::Class or Rose::DB::Object (Perl) + Hibernate (Java) + Yii/Zend (PHP)
  • + Founded by Matt S. Trout (mst) + Website: DBIX::CLASS http://www.dbix-class.org/
  • + Benefits? DBIX::CLASS + No more writing SQL + Make your coding easier + Cross DB: Majority of code should run on all databases + SQL statements are executed when it is needed (perfoma)
  • + User Role Mangement Example DBIX EXAMPLE: GENERATE SCHEMA
  • + dbicdump DBIX EXAMPLE: GENERATE SCHEMA dbicdump -o dump_directory=./lib DBIxTest::Schema dbi:Pg:dbname=dbix_test sheeju "{quote_char=>q{"}, quote_field_names=>{0}, name_sep=>{.} }"
  • + make_schema_at using simple perl script DBIX EXAMPLE: GENERATE SCHEMA #!/usr/bin/env perl use FindBin; use Getopt::Std; use Data::Dumper; use lib "$FindBin::Bin/../lib"; use DBIx::Class::Schema::Loader 'make_schema_at'; our ($opt_F, $opt_d); getopts('Fd'); make_schema_at('DBIxTest::Schema', { debug => !!($opt_d), really_erase_my_files => !!($opt_F), dump_directory=>"$FindBin::Bin/lib", overwrite_modifications=>1, preserve_case=>1, }, ['dbi:Pg:dbname=dbix_test','sheeju','sheeju', {'quote_char' => '"', 'quote_field_names' => '0', 'name ], );
  • + DBIxTest/Schema.pm DBIX EXAMPLE: GENERATE SCHEMA use utf8; package DBIxTest::Schema; # Created by DBIx::Class::Schema::Loader # DO NOT MODIFY THE FIRST PART OF THIS FILE use strict; use warnings; use base 'DBIx::Class::Schema'; __PACKAGE__->load_namespaces; 1;
  • + Result/User.pm (User Object or User Table) DBIX EXAMPLE: GENERATE SCHEMA package DBIxTest::Schema::Result::User; use base 'DBIx::Class::Core'; __PACKAGE__->table("User"); __PACKAGE__->add_columns( "Id", { accessor => "id", data_type => "integer", is_auto_increment => 1, is_nullable => 0, sequence => "User_Id_seq", }, "Name", { accessor => "name", data_type => "varchar", is_nullable => 0, size => 255 }, "Email", { accessor => "email", data_type => "varchar", is_nullable => 0, size => 255 }, "PasswordSalt", { accessor => "password_salt", data_type => "bytea", is_nullable => 0 }, "PasswordHash", { accessor => "password_hash", data_type => "bytea", is_nullable => 0 }, "Status", { accessor => "status", data_type => "varchar", default_value => "Active", is_nullable => 0, size => 64 }, ); __PACKAGE__->set_primary_key("Id"); __PACKAGE__->add_unique_constraint("User_Email_key", ["Email"]); __PACKAGE__->has_many( "user_roles", "DBIxTest::Schema::Result::UserRole", { "foreign.UserId" => "self.Id" }, ); __PACKAGE__->many_to_many("roles", "user_roles", "role");
  • + Result/Role.pm DBIX EXAMPLE: GENERATE SCHEMA package DBIxTest::Schema::Result::Role; use strict; use warnings; use base 'DBIx::Class::Core'; __PACKAGE__->table("Role"); __PACKAGE__->add_columns( "Id", { accessor => "id", data_type => "integer", is_auto_increment => 1, is_nullable => 0, sequence => "Role_Id_seq", }, "Name", { accessor => "name", data_type => "varchar", is_nullable => 0, size => 255 }, ); __PACKAGE__->set_primary_key("Id"); __PACKAGE__->has_many( "user_roles", "DBIxTest::Schema::Result::UserRole", { "foreign.RoleId" => "self.Id" },
  • + Result/UserRole.pm DBIX EXAMPLE: GENERATE SCHEMA package DBIxTest::Schema::Result::UserRole; use strict; use warnings; use base 'DBIx::Class::Core'; __PACKAGE__->table("UserRole"); __PACKAGE__->add_columns( "UserId", { accessor => "user_id", data_type => "integer", is_foreign_key => 1, is_nullable }, "RoleId", { accessor => "role_id", data_type => "integer", is_foreign_key => 1, is_nullable }, ); __PACKAGE__->set_primary_key("UserId", "RoleId"); __PACKAGE__->belongs_to( "role", "DBIxTest::Schema::Result::Role",
  • + Schema creations DBIX EXAMPLE: SCHEMA use DBIxTest::Schema; my $schema = DBIxTest::Schema->connect( "DBI:Pg:dbname=dbix_test", "sheeju", "sheeju", { RaiseError => 1, PrintError => 1, 'quote_char' => '"', 'quote
  • + C: Create DBIX EXAMPLE: CRUD + Create/Insert Record my $user_01 = $schema->resultset('User')->create( { Name => 'JohnSample', Email => 'john@sample.com', PasswordSalt => 'sheeju', PasswordHash => 'sheeju', Status => 'Active', } );
  • + R: Read DBIX EXAMPLE: CRUD + Read All records my $all_users = $schema->resultset('User')->all; + Select/search query $active_users = $schema->resultset('User')->search({"Status" => "Active"}); + JOIN and Search query $active_users = $schema->resultset('User')->search( {"user_roles.RoleId" => 1}, {"join" => "user_roles"});
  • + U: Update DBIX EXAMPLE: CRUD + Update User Email $user_01->update({Email => 'sheeju@exceleron.com'}); + Update with Join $active_users = $schema->resultset('User')->search( {"user_roles.RoleId" => 1}, {"join" => "user_roles"}); $active_users->update({Status => 'InActive'});
  • + D: Delete DBIX EXAMPLE: CRUD + Delete Record $user_01->delete; my $user = $schema->resultset('User')->search( { Email => 'jeremy@purepwnage.com' } )->first; $user->delete if($user);
  • DBIX EXAMPLE: METHODS YOU LIKE + find_or_create + update_or_create + $schema->deploy
  • RESULT VS RESULTSET + Result = Row + ResultSet = Query Plan ($user->active)
  • Sheeju Alex THANKS! Happy Coding :) Exceleron Inc Lead Product Developer sheeju@exceleron.com