« blib/lib/App/RecordStream/Operation/xform.pm
↑ Summary
blib/lib/App/RecordStream/Operation/help/FromManual.pm »
| 1 | package App::RecordStream::DBHandle; | ||
| 2 | |||
| 3 | our $VERSION = "4.0.23"; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use App::RecordStream::OptionalRequire 'DBI'; | ||
| 9 | BEGIN { App::RecordStream::OptionalRequire::require_done() } | ||
| 10 | |||
| 11 | use Data::Dumper; | ||
| 12 | use Getopt::Long; | ||
| 13 | |||
| 14 | my $MODES = { | ||
| 15 | 'sqlite' => { | ||
| 16 | 'dbfile' => ['=s', 'testDb', 'Local file for database'], | ||
| 17 | }, | ||
| 18 | 'mysql' => { | ||
| 19 | 'host' => ['=s', undef, 'Mysql Host'], | ||
| 20 | 'dbname' => ['=s', undef, 'Database to connect to'], | ||
| 21 | }, | ||
| 22 | 'oracle' => { | ||
| 23 | 'db' => ['=s', undef, 'Database name (tnsname) to connect to'], | ||
| 24 | }, | ||
| 25 | 'pg' => { | ||
| 26 | 'host' => ['=s', undef, 'Hostname to connect to'], | ||
| 27 | 'db' => ['=s', undef, 'Database to connect to'], | ||
| 28 | }, | ||
| 29 | 'main' => { | ||
| 30 | 'type' => ['=s', 'sqlite', 'Type of database to connect to'], | ||
| 31 | 'user' => ['=s', '', 'User to connect as'], | ||
| 32 | 'password' => ['=s', '', 'Password to connect as'], | ||
| 33 | }, | ||
| 34 | }; | ||
| 35 | |||
| 36 | my $DESCRIPTIONS = { | ||
| 37 | 'sqlite' => 'A simple local file based db', | ||
| 38 | 'mysql' => 'Connect to a remote mysql database', | ||
| 39 | 'oracle' => 'Connect to a remote Oracle database', | ||
| 40 | 'pg' => 'Connect to a remote PostgreSQL database', | ||
| 41 | }; | ||
| 42 | |||
| 43 | my $DISPATCH_TABLE = { | ||
| 44 | 'sqlite' => \&sqlite_dbh, | ||
| 45 | 'mysql' => \&mysql_dbh, | ||
| 46 | 'oracle' => \&oracle_dbh, | ||
| 47 | 'pg' => \&pg_dbh, | ||
| 48 | }; | ||
| 49 | |||
| 50 | sub get_dbh { | ||
| 51 | my $args = shift; | ||
| 52 | my $options = {}; | ||
| 53 | |||
| 54 | parse_options($options, 'main', $args); | ||
| 55 | |||
| 56 | my $type = $options->{'type'}; | ||
| 57 | parse_options($options, $type, $args); | ||
| 58 | |||
| 59 | return $DISPATCH_TABLE->{$type}->($options); | ||
| 60 | } | ||
| 61 | |||
| 62 | sub parse_options { | ||
| 63 | my $options = shift; | ||
| 64 | my $mode = shift; | ||
| 65 | my $args = shift || \@ARGV; | ||
| 66 | |||
| 67 | my $spec = get_option_spec($mode, $options); | ||
| 68 | local @ARGV = @$args; | ||
| 69 | |||
| 70 | my $saved_settings = Getopt::Long::Configure(); | ||
| 71 | Getopt::Long::Configure("pass_through"); | ||
| 72 | GetOptions( %$spec ); | ||
| 73 | Getopt::Long::Configure($saved_settings); | ||
| 74 | |||
| 75 | set_defaults($mode, $options); | ||
| 76 | |||
| 77 | @$args = @ARGV; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub set_defaults { | ||
| 81 | my $mode = shift; | ||
| 82 | my $opts = shift; | ||
| 83 | |||
| 84 | my $options = $MODES->{$mode}; | ||
| 85 | foreach my $opt ( keys %$options ) { | ||
| 86 | my $default = @{$options->{$opt}}[1]; | ||
| 87 | |||
| 88 | if ( (not defined $default) && (!$opts->{$opt}) ) { | ||
| 89 | die "Must define $opt for type $mode"; | ||
| 90 | } | ||
| 91 | |||
| 92 | $opts->{$opt} = $default unless ( exists $opts->{$opt} ); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | sub get_option_spec { | ||
| 97 | my $mode = shift; | ||
| 98 | my $opts = shift; | ||
| 99 | |||
| 100 | my $options = $MODES->{$mode}; | ||
| 101 | |||
| 102 | my %spec; | ||
| 103 | foreach my $opt ( keys %$options ) { | ||
| 104 | my ($modifier) = @{$options->{$opt}}; | ||
| 105 | $spec{$opt . $modifier} = sub { add_opt($opts, @_) }; | ||
| 106 | } | ||
| 107 | |||
| 108 | return \%spec; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub mysql_dbh { | ||
| 112 | my $args = shift; | ||
| 113 | |||
| 114 | my $database = $args->{'dbname'}; | ||
| 115 | my $host = $args->{'host'}; | ||
| 116 | my $user = $args->{'user'}; | ||
| 117 | my $password = $args->{'password'}; | ||
| 118 | |||
| 119 | my $dbh = DBI->connect("DBI:mysql:database=$database;host=$host", | ||
| 120 | $user, | ||
| 121 | $password, | ||
| 122 | { RaiseError => 1, PrintError => 0 }); | ||
| 123 | |||
| 124 | return $dbh; | ||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | sub sqlite_dbh { | ||
| 129 | my $args = shift; | ||
| 130 | |||
| 131 | my $db_file = $args->{'dbfile'}; | ||
| 132 | my $user = $args->{'user'}; | ||
| 133 | my $password = $args->{'password'}; | ||
| 134 | |||
| 135 | my $dbh = DBI->connect("dbi:SQLite:dbname=$db_file", | ||
| 136 | $user, | ||
| 137 | $password, | ||
| 138 | { RaiseError => 1, PrintError => 0 }); | ||
| 139 | |||
| 140 | return $dbh; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub oracle_dbh { | ||
| 144 | my $args = shift; | ||
| 145 | |||
| 146 | my $user = $args->{'user'}; | ||
| 147 | my $password = $args->{'password'}; | ||
| 148 | my $database = $args->{'db'}; | ||
| 149 | |||
| 150 | |||
| 151 | my $dbh = DBI->connect("dbi:Oracle:$database", | ||
| 152 | $user, | ||
| 153 | $password, | ||
| 154 | { RaiseError => 1, PrintError => 1 }); | ||
| 155 | return $dbh; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub pg_dbh { | ||
| 159 | my $args = shift; | ||
| 160 | |||
| 161 | my $database = $args->{'db'}; | ||
| 162 | my $host = $args->{'host'}; | ||
| 163 | my $user = $args->{'user'}; | ||
| 164 | my $password = $args->{'password'}; | ||
| 165 | |||
| 166 | my $dbh = DBI->connect("DBI:Pg:database=$database;host=$host", | ||
| 167 | $user, | ||
| 168 | $password, | ||
| 169 | { RaiseError => 1, PrintError => 0 }); | ||
| 170 | |||
| 171 | return $dbh; | ||
| 172 | } | ||
| 173 | |||
| 174 | sub add_opt { | ||
| 175 | my $options = shift; | ||
| 176 | my $arg_name = shift; | ||
| 177 | my $value = shift; | ||
| 178 | |||
| 179 | $options->{$arg_name} = $value; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub usage { | ||
| 183 | my $usage = ''; | ||
| 184 | $usage .= "Database Options\n"; | ||
| 185 | |||
| 186 | $usage .= type_usage('main'); | ||
| 187 | |||
| 188 | $usage .= "Datbase types:\n"; | ||
| 189 | |||
| 190 | foreach my $type ( sort keys %$DESCRIPTIONS ) { | ||
| 191 | my $description = $DESCRIPTIONS->{$type}; | ||
| 192 | $usage .= " $type - $description\n"; | ||
| 193 | } | ||
| 194 | |||
| 195 | $usage .= "\n"; | ||
| 196 | |||
| 197 | foreach my $type ( sort keys %$MODES ) { | ||
| 198 | next if ( $type eq 'main' ); | ||
| 199 | $usage .= "Database Options for type: $type\n"; | ||
| 200 | $usage .= type_usage($type); | ||
| 201 | } | ||
| 202 | |||
| 203 | return $usage; | ||
| 204 | } | ||
| 205 | |||
| 206 | sub type_usage { | ||
| 207 | my $type = shift; | ||
| 208 | my $print_mode = shift; | ||
| 209 | |||
| 210 | my $usage = ''; | ||
| 211 | |||
| 212 | $usage .= " Usage for --type $type\n" if ( $print_mode ); | ||
| 213 | |||
| 214 | my $options = $MODES->{$type}; | ||
| 215 | |||
| 216 | foreach my $name ( sort keys %$options ) { | ||
| 217 | my $description = @{$options->{$name}}[2]; | ||
| 218 | my $default = @{$options->{$name}}[1]; | ||
| 219 | |||
| 220 | $usage .= " $name - $description"; | ||
| 221 | $usage .= " - Default: $default" if ( defined $default && $default ne '' ); | ||
| 222 | $usage .= "\n"; | ||
| 223 | } | ||
| 224 | |||
| 225 | $usage .= "\n"; | ||
| 226 | |||
| 227 | return $usage; | ||
| 228 | } | ||
| 229 | |||
| 230 | 1; |