Skip to content

Instantly share code, notes, and snippets.

@yongbin
Created April 20, 2011 17:06
Show Gist options
  • Save yongbin/931962 to your computer and use it in GitHub Desktop.
Save yongbin/931962 to your computer and use it in GitHub Desktop.
DB를 사용하지만 ORM을 사용하지 않는 간단한 스크립트 작성에 사용하는 skel 입니다.
#!/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