Guest User

Untitled

a guest
Oct 17th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. use Moose;
  2. use DBIx::DataModel;
  3. use DBI;
  4.  
  5. our $dbh;
  6. our $schema;
  7.  
  8. sub BUILD {
  9. $dbh = get_dbh();
  10. my %dbh_options;
  11. $dbh_options{last_insert_id} = \&get_last_insert_id;
  12. AppGen::Schema->dbh($dbh, %dbh_options);
  13. $schema = AppGen::Schema->singleton();
  14. }
  15.  
  16. sub get_dbh {
  17. my $dbi_dsn = 'dbi:ODBC:Driver={SQL Server Native Client 10.0};Server=localhost,1433;Database=app_gen;';
  18. my $user = 'app_gen';
  19. my $password = 'app_gen';
  20. my $dbh = DBI->connect($dbi_dsn, $user, $password);
  21. $dbh->{RaiseError} = 1; # Required for DBIx::DataModel
  22. return $dbh;
  23. }
  24.  
  25. sub get_last_insert_id() {
  26. my ($dbh, $table, $col) = @_;
  27. my $sql = 'SELECT @@IDENTITY';
  28. my @row_ary = $dbh->selectrow_array($sql);
  29. my $id = $row_ary[0];
  30. return $id;
  31. }
  32.  
  33. sub save($entity) {
  34. my $table_name = 'Foo';
  35. my $table = $schema->table($table_name);
  36. my $id = $table->insert({FOO => $entity->{FOO}, FOO_REF => $entity->{FOO_REF}});
  37. print "Inserted ID: $id\n";
  38. }
Add Comment
Please, Sign In to add comment