Created
July 23, 2012 07:43
-
-
Save yongbin/3162453 to your computer and use it in GitHub Desktop.
silex-db-no-orm.pl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl | |
| use 5.010; | |
| use utf8; | |
| use strict; | |
| use warnings; | |
| use autodie; | |
| use Getopt::Long::Descriptive; | |
| use Carp qw/croak/; | |
| use Const::Fast; | |
| use Data::Dumper; | |
| use DBIx::Simple; | |
| use SQL::Abstract; | |
| use DBIx::Mysql::InformationSchema; | |
| binmode STDIN, ':utf8'; | |
| binmode STDOUT, ':utf8'; | |
| const my $DATABASE => 'mysql'; | |
| const my $USERNAME => 'root'; | |
| const my $PASSWORD => ''; | |
| const my $MYSQL_QUOTACHAR => q{`}; | |
| const my $MYSQL_NAMESEP => q{.}; | |
| my ( $opt, $usage ) = describe_options( | |
| "%c %o ...", | |
| [ | |
| 'database=s', | |
| sprintf( 'Database name (default: %s)', $DATABASE ), | |
| { default => $DATABASE } | |
| ], | |
| [ | |
| 'user=s', | |
| sprintf( 'Database username (default: %s)', $USERNAME ), | |
| { default => $USERNAME } | |
| ], | |
| [ | |
| 'password=s', | |
| sprintf( 'Database password (default: %s)', $PASSWORD ), | |
| { default => $PASSWORD } | |
| ], | |
| [ 'utf8!', 'Set mysql connection as utf8', { default => 1 } ], | |
| [ 'help|h', 'print usage message and exit' ], | |
| ); | |
| print( $usage->text ), exit if $opt->help; | |
| my $db = DBIx::Simple->connect( 'dbi:mysql:', $opt->user, $opt->password ) | |
| or croak; | |
| $db->abstract = SQL::Abstract->new( { quota_char => q{`}, name_sep => q{.} } ); | |
| query('set names utf8') if $opt->utf8; | |
| usedb( $opt->database ); | |
| my $meta = DBIx::Mysql::InformationSchema->new; | |
| $meta->connectdb( $opt->user, $opt->password ); | |
| sub usedb { query("use `$_[0]`"); } | |
| sub query { $db->query( $_[0] ) or croak; } | |
| #=================================================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment