Created
July 15, 2009 05:44
-
-
Save shelling/147479 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
#!/usr/bin/env perl | |
package User; | |
use Jifty::DBI::Schema; | |
use Jifty::DBI::Record schema { | |
column name => | |
type is "text", | |
label is "Name", | |
render as "text"; | |
column email => | |
type is "text", | |
label is "Email", | |
render as "text"; | |
}; | |
package UserCollection; | |
use base qw(Jifty::DBI::Collection); | |
package main; | |
use Data::Dumper; | |
use Jifty::DBI::Handle; | |
use Jifty::DBI::SchemaGenerator; | |
my $user; | |
my $handle; | |
sub setup { | |
$handle = Jifty::DBI::Handle->new(); | |
$handle->connect( driver => "SQLite", | |
database => "simple-jifty.sqlite3" ); | |
$user = User->new( handle => $handle ); | |
} | |
sub init_db { | |
my $gen = Jifty::DBI::SchemaGenerator->new($handle); | |
$gen->add_model($user); | |
my @statements = $gen->create_table_sql_statements; | |
$handle->begin_transaction; | |
for (@statements) { | |
my $ret = $handle->simple_query($_); | |
$ret or die "error creating a table: " . $ret->error_message; | |
} | |
$handle->commit; | |
} | |
if ( -e "simple-jifty.sqlite3" ) { | |
setup; | |
} else { | |
setup; | |
init_db; | |
} | |
$user->create( name => "shelling", | |
email => 'shelling@cpan.org' ); | |
my $user_collection = UserCollection->new( handle => $handle ); | |
$user_collection->limit( column => "id", value => "1" ); | |
while ( my $record = $user_collection->next ) { | |
print $record->id . " " . $record->name . " " . $record->email, "\n"; | |
} | |
__END__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment