Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use Moose;
- use DBIx::DataModel;
- use DBI;
- our $dbh;
- our $schema;
- sub BUILD {
- $dbh = get_dbh();
- my %dbh_options;
- $dbh_options{last_insert_id} = \&get_last_insert_id;
- AppGen::Schema->dbh($dbh, %dbh_options);
- $schema = AppGen::Schema->singleton();
- }
- sub get_dbh {
- my $dbi_dsn = 'dbi:ODBC:Driver={SQL Server Native Client 10.0};Server=localhost,1433;Database=app_gen;';
- my $user = 'app_gen';
- my $password = 'app_gen';
- my $dbh = DBI->connect($dbi_dsn, $user, $password);
- $dbh->{RaiseError} = 1; # Required for DBIx::DataModel
- return $dbh;
- }
- sub get_last_insert_id() {
- my ($dbh, $table, $col) = @_;
- my $sql = 'SELECT @@IDENTITY';
- my @row_ary = $dbh->selectrow_array($sql);
- my $id = $row_ary[0];
- return $id;
- }
- sub save($entity) {
- my $table_name = 'Foo';
- my $table = $schema->table($table_name);
- my $id = $table->insert({FOO => $entity->{FOO}, FOO_REF => $entity->{FOO_REF}});
- print "Inserted ID: $id\n";
- }
Add Comment
Please, Sign In to add comment