+ 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...
+ Is an ORM (Object Relational Mapper)
+ SQL => Object
DBIX::CLASS
+ Database tables becomes Object
+ Table data and relat...
+ 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 dat...
+ 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
...
+ make_schema_at using simple perl script
DBIX EXAMPLE: GENERATE SCHEMA
#!/usr/bin/env perl
use FindBin;
use Getopt::Std;
...
+ DBIxTest/Schema.pm
DBIX EXAMPLE: GENERATE SCHEMA
use utf8;
package DBIxTest::Schema;
# Created by DBIx::Class::Schema::L...
+ Result/User.pm (User Object or User Table)
DBIX EXAMPLE: GENERATE SCHEMA
package DBIxTest::Schema::Result::User;
use bas...
+ Result/Role.pm
DBIX EXAMPLE: GENERATE SCHEMA
package DBIxTest::Schema::Result::Role;
use strict;
use warnings;
use base ...
+ Result/UserRole.pm
DBIX EXAMPLE: GENERATE SCHEMA
package DBIxTest::Schema::Result::UserRole;
use strict;
use warnings;
u...
+ Schema creations
DBIX EXAMPLE: SCHEMA
use DBIxTest::Schema;
my $schema = DBIxTest::Schema->connect( "DBI:Pg:dbname=dbix_...
+ C: Create
DBIX EXAMPLE: CRUD
+ Create/Insert Record
my $user_01 = $schema->resultset('User')->create(
{
Name => 'JohnSam...
+ R: Read
DBIX EXAMPLE: CRUD
+ Read All records
my $all_users = $schema->resultset('User')->all;
+ Select/search query
$ac...
+ U: Update
DBIX EXAMPLE: CRUD
+ Update User Email
$user_01->update({Email => 'sheeju@exceleron.com'});
+ Update with Join...
+ D: Delete
DBIX EXAMPLE: CRUD
+ Delete Record
$user_01->delete;
my $user = $schema->resultset('User')->search(
{ Email =>...
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
Upcoming SlideShare
Loading in...5
×

DBIx::Class walkthrough @ bangalore pm

613

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
613
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
4
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

DBIx::Class walkthrough @ bangalore pm

  1. 1. + In this presentation I will walk through DBIx::Class which helps you get started DBIX::CLASS WALK THROUGH
  2. 2. ~ Sheeju Alex ~
  3. 3. DBI BACKGROUND + Perl Database Interface Module + High level layer to interact with databases
  4. 4. DBI BACKGROUND + Awesome module which can connect to any database you ask + Uniform interface to handle different types of databases
  5. 5. + Is an ORM (Object Relational Mapper) + SQL => Object DBIX::CLASS + Database tables becomes Object + Table data and relationships between tables become object methods
  6. 6. + Alternatives? DBIX::CLASS + DBIx::Class or Rose::DB::Object (Perl) + Hibernate (Java) + Yii/Zend (PHP)
  7. 7. + Founded by Matt S. Trout (mst) + Website: DBIX::CLASS http://www.dbix-class.org/
  8. 8. + 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)
  9. 9. + User Role Mangement Example DBIX EXAMPLE: GENERATE SCHEMA
  10. 10. + 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=>{.} }"
  11. 11. + 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 ], );
  12. 12. + 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;
  13. 13. + 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");
  14. 14. + 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" },
  15. 15. + 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",
  16. 16. + 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
  17. 17. + 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', } );
  18. 18. + 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"});
  19. 19. + 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'});
  20. 20. + 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);
  21. 21. DBIX EXAMPLE: METHODS YOU LIKE + find_or_create + update_or_create + $schema->deploy
  22. 22. RESULT VS RESULTSET + Result = Row + ResultSet = Query Plan ($user->active)
  23. 23. Sheeju Alex THANKS! Happy Coding :) Exceleron Inc Lead Product Developer sheeju@exceleron.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×