Created
July 6, 2009 08:54
-
-
Save shelling/141356 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package MyApp::DB; | |
my $dsn = "dbi:SQLite:dbname=test.sqlite3"; | |
sub driver { | |
my $class = shift; | |
my $mode = shift; | |
if ($mode == "default") { | |
Data::Model::Driver::DBI->new( dsn => $dsn ); | |
} | |
} | |
sub make_schema { | |
my $dbh = DBI->connect($dsn); | |
for (qw(Users Posts)) { | |
$dbh->do($_->as_sqls); | |
} | |
$dbh->disconnect(); | |
} | |
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Posts; | |
use base Data::Model; | |
use Data::Model::Schema; | |
base_driver(MyApp::DB->driver("default")); | |
install_model "posts" => schema { | |
key "id"; | |
column "id" => "int" => { | |
auto_increment => 1, | |
required => 1, | |
unsigned => 1, | |
}; | |
columns qw(title content); | |
}; | |
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Data::Model; | |
use Data::Model::Driver::DBI; | |
use MyApp::DB; | |
use Users; | |
use Posts; | |
MyApp::DB->make_schema(); | |
$user_model = Users->new; | |
my $row = $user_model->set( | |
users => { | |
name => 'shelling', | |
email => 'shelling@cpan.org', | |
} | |
); | |
use Data::Dumper; | |
print Dumper($row); | |
$users = $user_model->lookup( users => 1 ); | |
print Dumper($users); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Users; | |
use base Data::Model; | |
use Data::Model::Schema; | |
base_driver(MyApp::DB->driver("default")); | |
install_model "users" => schema { | |
key "id"; | |
column "id" => "int" => { | |
auto_increment => 1, | |
required => 1, | |
unsigned => 1, | |
}; | |
columns qw(name email); | |
}; | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment