Guest User

Untitled

a guest
Jan 26th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. # /usr/local/share/perl/5.10.1/Mojolicious/Plugin/DbixSimple.pm
  2. # copy of Mojolicious::Plugin::Database
  3.  
  4. package Mojolicious::Plugin::DbixSimple;
  5.  
  6. use Mojo::Base 'Mojolicious::Plugin';
  7. use version;
  8. use DBIx::Simple;
  9.  
  10. our $VERSION = qv(0.01);
  11.  
  12. has 'ds';
  13.  
  14. sub register {
  15. my $self = shift;
  16. my $app = shift;
  17. my $conf = shift || {};
  18.  
  19. die ref($self), ': missing dsn parameter', "\n" unless($conf->{dsn});
  20.  
  21. $self->ds(DBIx::Simple->connect($conf->{dsn}, $conf->{username}, $conf->{password}, $conf->{options}));
  22.  
  23. die ref($self), ': failed to connect to database: ', DBIx::Simple->errstr, "\n"
  24. unless($self->ds);
  25.  
  26. my $helper_name = $conf->{helper} || 'ds';
  27.  
  28. $app->helper($helper_name => sub { $self->ds } );
  29. }
  30.  
  31. 1;
  32.  
  33. # ---------------------------------------------------------------
  34. # Application
  35. # ---------------------------------------------------------------
  36. package OnlineOrder;
  37. use Mojo::Base 'Mojolicious';
  38.  
  39. # This method will run once at server start
  40. sub startup {
  41. my $self = shift;
  42.  
  43. my $config = $self->plugin('json_config');
  44.  
  45. # DBIx::Simple plugin
  46. $self->plugin(
  47. 'dbix_simple',
  48. { dsn => "dbi:mysql:database=$config->{'dbname'}",
  49. username => $config->{'dbuser'},
  50. password => $config->{'dbpass'},
  51. options => {RaiseError => 1, AutoCommit => 1},
  52. helper => 'ds'
  53. }
  54. );
  55.  
  56. # Routes
  57. my $r = $self->routes;
  58.  
  59. # Normal route to controller
  60. $r->route('/add_data')->to('orders#add');
  61.  
  62. }
  63.  
  64. # ---------------------------------------------------------------
  65. # Controller
  66. # ---------------------------------------------------------------
  67. package OnlineOrder::Orders;
  68. use Mojo::Base 'Mojolicious::Controller';
  69. use encoding 'utf8';
  70.  
  71. sub add {
  72. my $self = shift;
  73.  
  74. # helper
  75. my $dbh = $self->ds;
  76.  
  77. my $name = $self->param('name');
  78. my $phone = $self->param('phone');
  79. my $email = $self->param('email');
  80. my $desc = $self->param('desc');
  81.  
  82. my $sql = qq{
  83. INSERT INTO orders
  84. (name,phone,email,desc)
  85. VALUES
  86. ('$name','$phone','$email','$desc')};
  87.  
  88. $dbh->query($sql);
  89.  
  90. }
  91.  
  92. 1;
Add Comment
Please, Sign In to add comment