| 1 | package AutoSQL::EasyArgv; | ||
| 2 | use strict; | ||
| 3 | use Exporter (); | ||
| 4 | our @ISA = qw(Exporter); | ||
| 5 | our @EXPORT = qw(get_autosql_db_from_argv); | ||
| 6 | use AutoSQL::DBSQL::DBContext; | ||
| 7 | use Getopt::Long; | ||
| 8 | |||
| 9 | sub get_autosql_db_from_argv { | ||
| 10 | my ($db_file, $host, $user, $pass, $dbname, $driver)= | ||
| 11 | (undef, 'localhost', 'root', undef, undef, 'mysql'); | ||
| 12 | Getopt::Long::config('pass_through'); | ||
| 13 | &GetOptions( | ||
| 14 | 'db_file=s' => \$db_file, | ||
| 15 | 'host|dbhost=s' => \$host, | ||
| 16 | 'user|dbuser=s' => \$user, | ||
| 17 | 'pass|dbpass=s' => \$pass, | ||
| 18 | 'driver|dbdriver=s' => \$driver, | ||
| 19 | 'dbname=s' => \$dbname | ||
| 20 | ); | ||
| 21 | my $db; | ||
| 22 | if(defined $db_file){ | ||
| 23 | -e $db_file or die "$db_file is defined but does not exist\n"; | ||
| 24 | eval{$db=do($db_file)}; | ||
| 25 | $@ and die"$db_file is not a perlobj file\n"; | ||
| 26 | }elsif(defined $host and defined $user and defined $dbname){ | ||
| 27 | $db = AutoSQL::DBSQL::DBContext->new( | ||
| 28 | -host => $host, | ||
| 29 | -user => $user, | ||
| 30 | -pass => $pass, | ||
| 31 | -driver => $driver, | ||
| 32 | -dbname => $dbname | ||
| 33 | ); | ||
| 34 | }else{ | ||
| 35 | die "Cannot get the db, due to the insufficient information\n"; | ||
| 36 | } | ||
| 37 | return $db; | ||
| 38 | } | ||
| 39 | 1; | ||
| 40 |