Created
April 20, 2011 17:06
-
-
Save yongbin/931962 to your computer and use it in GitHub Desktop.
DB를 사용하지만 ORM을 사용하지 않는 간단한 스크립트 작성에 사용하는 skel 입니다.
This file contains 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 Readonly; | |
use Data::Dumper; | |
use DBIx::Simple; | |
use SQL::Abstract; | |
use DBIx::Mysql::InformationSchema | |
binmode STDIN, ':utf8'; | |
binmode STDOUT, ':utf8'; | |
Readonly::Scalar my $DATABASE => 'mysql'; | |
Readonly::Scalar my $USERNAME => 'root'; | |
Readonly::Scalar my $PASSWORD => ''; | |
Readonly::Scalar my $MYSQL_QUOTACHAR => q{`}; | |
Readonly::Scalar 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