Skip to content
Browse files

add DBIx::DataModel

  • Loading branch information...
1 parent b2608e2 commit b114038d33c53c09b50819dc80472f9e6805dc4b Terrence Brannon committed Aug 3, 2009
View
33 lib/ORM/DBIx/DataModel/Quickstart/emps.ddl
@@ -0,0 +1,33 @@
+DROP TABLE t_employee ;
+ DROP TABLE t_department ;
+ DROP TABLE t_department ;
+ DROP TABLE t_activity ;
+ DROP TABLE t_skill ;
+ DROP TABLE t_employee_skill ;
+
+CREATE TABLE t_employee (
+ emp_id INTEGER AUTO_INCREMENT PRIMARY KEY,
+ lastname TEXT NOT NULL,
+ firstname TEXT,
+ d_birth DATE
+ );
+ CREATE TABLE t_department (
+ dpt_code VARCHAR(5) PRIMARY KEY,
+ dpt_name TEXT NOT NULL
+ );
+ CREATE TABLE t_activity (
+ act_id INTEGER AUTO_INCREMENT PRIMARY KEY,
+ emp_id INTEGER NOT NULL REFERENCES t_employee(emp_id),
+ dpt_code VARCHAR(5) NOT NULL REFERENCES t_department(dpt_code),
+ d_begin DATE NOT NULL,
+ d_end DATE
+ );
+ CREATE TABLE t_skill (
+ skill_code VARCHAR(2) PRIMARY KEY,
+ skill_name TEXT NOT NULL
+ );
+ CREATE TABLE t_employee_skill (
+ emp_id INTEGER NOT NULL REFERENCES t_employee(emp_id),
+ skill_code VARCHAR(2) NOT NULL REFERENCES t_skill(skill_code)
+
+ );
View
82 lib/ORM/DBIx/DataModel/Quickstart/emps.pl
@@ -0,0 +1,82 @@
+use strict;
+use warnings;
+
+use DBI;
+use DBIx::DataModel;
+DBIx::DataModel->Schema('HR');
+
+
+
+
+
+
+HR->Table(qw/Employee t_employee emp_id/);
+ HR->Table(qw/Department t_department dpt_code/);
+ HR->Table(qw/Activity t_activity act_id/);
+ HR->Table(qw/Skill t_skill skill_code/);
+ HR->Table(qw/EmployeeSkill t_employee_skill emp_id skill_code/);
+
+ HR->ColumnType(Date =>
+ fromDB => sub {$_[0] =~ s/(\d\d\d\d)-(\d\d)-(\d\d)/$3.$2.$1/ if $_[0]},
+ toDB => sub {$_[0] =~ s/(\d\d)\.(\d\d)\.(\d\d\d\d)/$3-$2-$1/ if $_[0]},
+ validate => sub {$_[0] =~ m/\d\d\.\d\d\.\d\d\d\d/});
+
+ HR::Employee->ColumnType(Date => qw/d_birth/);
+ HR::Activity->ColumnType(Date => qw/d_begin d_end/);
+
+
+ HR->Association([qw/Department department 1 /],
+ [qw/Activity activities * /]);
+
+
+HR->Composition([qw/Employee employee 1 /],
+ [qw/Activity activities * /]);
+
+
+
+ HR->Association([qw/Employee employee 1 /],
+ [qw/EmployeeSkill emp_skills * /]);
+
+ HR->Association([qw/Skill skill 1 /],
+ [qw/EmployeeSkill emp_skills * /]);
+
+
+ HR->Association([qw/Employee employees * emp_skills employee/],
+ [qw/Skill skills * emp_skills skill /]);
+
+
+
+
+my $dbh = DBI->connect( 'dbi:SQLite:emps.sqlite3', '', '' , {RaiseError => 1});
+ HR->dbh($dbh); # give $dbh handle to the schema
+
+
+ my ($bach_id, $berlioz_id, $monteverdi_id) =
+ HR::Employee->insert({firstname => "Johann", lastname => "Bach" },
+ {firstname => "Hector", lastname => "Berlioz" },
+ {firstname => "Claudio", lastname => "Monteverdi"});
+
+ HR::Department->insert({dpt_code => "CPT", dpt_name => "Counterpoint" },
+ {dpt_code => "ORCH", dpt_name => "Orchestration"});
+
+ HR::Skill->insert({skill_code => "VL", skill_name => "Violin" },
+ {skill_code => "KB", skill_name => "Keyboard"},
+ {skill_code => "GT", skill_name => "Guitar"});
+
+
+HR::Employee->update($bach_id => {firstname => "Johann Sebastian"});
+
+ my $bach = HR::Employee->fetch($bach_id); # get single record from prim.key
+
+ $bach->insert_into_activities({d_begin => '01.01.1695',
+ d_end => '18.07.1750',
+ dpt_code => 'CPT'});
+
+ $bach->insert_into_emp_skills({skill_code => 'VL'},
+ {skill_code => 'KB'});
+
+ HR::Employee->insert({firstname => "Richard",
+ lastname => "Strauss",
+ activities => [ {d_begin => '01.01.1874',
+ d_end => '08.09.1949',
+ dpt_code => 'ORCH' } ]});
View
BIN lib/ORM/DBIx/DataModel/Quickstart/emps.sqlite3
Binary file not shown.
View
36 lib/ORM/DBIx/Skinny/mymodel.pl
@@ -21,18 +21,52 @@ sub add_title {
}
+# class-based update
+sub find_and_fix0 {
+ my $rows = MyModel->update( authors => { au_lname => 'Bright' }, { au_lname => 'Dull' } ) ;
+
+ warn "$rows";
+
+}
+
+# set then update
sub find_and_fix {
my $row = MyModel->find_or_create( authors => { au_lname => 'Wall' } ) ;
$row->set(au_lname => 'Ball');
$row->update;
}
+# update row object
sub find_and_fix2 {
my $row = MyModel->single( authors => { au_lname => 'White' } ) ;
$row->update( { au_lname => 'Kite' } );
}
-find_and_fix2();
+# update column via scalar expression
+sub find_and_fix3 {
+ my $row = MyModel->single( authors => { au_lname => 'Straight' } ) ;
+
+
+}
+
+# class-based delete
+sub del0 {
+ my $rows = MyModel->delete( authors => { au_lname => '8' } ) ;
+
+ warn "$rows";
+
+}
+
+# row-based delete
+sub del2 {
+ my $row = MyModel->single( authors => { city => 'Nashville' } );
+
+ $row->delete;
+
+}
+
+
+del2;
View
BIN lib/ORM/DBIx/Skinny/test
Binary file not shown.

0 comments on commit b114038

Please sign in to comment.
Something went wrong with that request. Please try again.