Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # /usr/local/share/perl/5.10.1/Mojolicious/Plugin/DbixSimple.pm
- # copy of Mojolicious::Plugin::Database
- package Mojolicious::Plugin::DbixSimple;
- use Mojo::Base 'Mojolicious::Plugin';
- use version;
- use DBIx::Simple;
- our $VERSION = qv(0.01);
- has 'ds';
- sub register {
- my $self = shift;
- my $app = shift;
- my $conf = shift || {};
- die ref($self), ': missing dsn parameter', "\n" unless($conf->{dsn});
- $self->ds(DBIx::Simple->connect($conf->{dsn}, $conf->{username}, $conf->{password}, $conf->{options}));
- die ref($self), ': failed to connect to database: ', DBIx::Simple->errstr, "\n"
- unless($self->ds);
- my $helper_name = $conf->{helper} || 'ds';
- $app->helper($helper_name => sub { $self->ds } );
- }
- 1;
- # ---------------------------------------------------------------
- # Application
- # ---------------------------------------------------------------
- package OnlineOrder;
- use Mojo::Base 'Mojolicious';
- # This method will run once at server start
- sub startup {
- my $self = shift;
- my $config = $self->plugin('json_config');
- # DBIx::Simple plugin
- $self->plugin(
- 'dbix_simple',
- { dsn => "dbi:mysql:database=$config->{'dbname'}",
- username => $config->{'dbuser'},
- password => $config->{'dbpass'},
- options => {RaiseError => 1, AutoCommit => 1},
- helper => 'ds'
- }
- );
- # Routes
- my $r = $self->routes;
- # Normal route to controller
- $r->route('/add_data')->to('orders#add');
- }
- # ---------------------------------------------------------------
- # Controller
- # ---------------------------------------------------------------
- package OnlineOrder::Orders;
- use Mojo::Base 'Mojolicious::Controller';
- use encoding 'utf8';
- sub add {
- my $self = shift;
- # helper
- my $dbh = $self->ds;
- my $name = $self->param('name');
- my $phone = $self->param('phone');
- my $email = $self->param('email');
- my $desc = $self->param('desc');
- my $sql = qq{
- INSERT INTO orders
- (name,phone,email,desc)
- VALUES
- ('$name','$phone','$email','$desc')};
- $dbh->query($sql);
- }
- 1;
Add Comment
Please, Sign In to add comment