Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.34 KB | None | 0 0
  1. /root_dir/lib/Mojo_App.pm
  2. package Mojo_App;
  3. use Mojo::Base 'Mojolicious';
  4.  
  5. # This method will run once at server start
  6. sub startup {
  7.     my $self = shift;
  8.  
  9.     # Load configuration from hash returned by "my_app.conf"
  10.     my $config = $self->plugin('Config');
  11.  
  12.     # Router
  13.     my $r = $self->routes;
  14.     # Normal route to controller
  15.     $r->get('/')->to('main#index');
  16.  
  17.     #db conf
  18.  
  19.     my $db_conf= {
  20.         db1 => {
  21.             host => 'dbi:Sybase:server=ip01;database=db01',
  22.             user => 'user01',
  23.             pass => 'pass01'
  24.         },
  25.         db2 => {
  26.             host => 'dbi:Sybase:server=ip02;database=db02',
  27.             user => 'user02',
  28.             pass => 'pass02'
  29.         },
  30.         db3 => {
  31.             host => 'dbi:Sybase:server=ip03;database=db03',
  32.             user => 'user03',
  33.             pass => 'pass03'
  34.         }
  35.     };
  36.  
  37.     #db helper
  38.  
  39.     my $db;
  40.     $self->helper(db => sub {
  41.         use DBIx::Connector;
  42.         my $self = shift;
  43.         return 0 unless $self;
  44.         if (exists($db_conf->{$self})) {
  45.             my $db = DBIx::Connector->new($db_conf->{$self}->{host}, $db_conf->{$self}->{user}, $db_conf->{$self}->{pass}, {
  46.                 RaiseError => 1,
  47.                 AutoCommit => 1
  48.             });
  49.             return $db;
  50.         } else {
  51.             return "Connection not in config. \n" ;
  52.         }
  53.     });
  54. }
  55.  
  56. 1;
  57.  
  58. /root_dir/lib/Controller/Main.pm
  59.  
  60. sub index {
  61.     my $self = shift;
  62.     my $dbh = $self->db->('db1')->dbh();
  63.     my $test = $dbh->selectall_arrayref('select * from test1', { Slice => {} });
  64.    
  65.     $self->render();
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement