« blib/lib/DBIx/Class/Schema/Loader/Table/Sybase.pm
↑ Summary
blib/lib/DBIx/Class/Schema/Loader/Column.pm »
| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | dbicdump - Dump a schema using DBIx::Class::Schema::Loader | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | dbicdump <configuration_file> | ||||||||||||||||||||||||||||
| 12 | dbicdump [-I <lib-path>] [-o <loader_option>=<value> ] \ | ||||||||||||||||||||||||||||
| 13 | <schema_class> <connect_info> | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Examples: | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | $ dbicdump schema.conf | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | $ dbicdump -o dump_directory=./lib \ | ||||||||||||||||||||||||||||
| 20 | -o components='["InflateColumn::DateTime"]' \ | ||||||||||||||||||||||||||||
| 21 | MyApp::Schema dbi:SQLite:./foo.db | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | $ dbicdump -o dump_directory=./lib \ | ||||||||||||||||||||||||||||
| 24 | -o components='["InflateColumn::DateTime"]' \ | ||||||||||||||||||||||||||||
| 25 | MyApp::Schema dbi:SQLite:./foo.db '{ quote_char => "\"" }' | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | $ dbicdump -Ilib -o dump_directory=./lib \ | ||||||||||||||||||||||||||||
| 28 | -o components='["InflateColumn::DateTime"]' \ | ||||||||||||||||||||||||||||
| 29 | -o preserve_case=1 \ | ||||||||||||||||||||||||||||
| 30 | MyApp::Schema dbi:mysql:database=foo user pass \ | ||||||||||||||||||||||||||||
| 31 | '{ quote_char => "`" }' | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | $ dbicdump -o dump_directory=./lib \ | ||||||||||||||||||||||||||||
| 34 | -o components='["InflateColumn::DateTime"]' \ | ||||||||||||||||||||||||||||
| 35 | MyApp::Schema 'dbi:mysql:database=foo;host=domain.tld;port=3306' \ | ||||||||||||||||||||||||||||
| 36 | user pass | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | On Windows that would be: | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | $ dbicdump -o dump_directory=.\lib ^ | ||||||||||||||||||||||||||||
| 41 | -o components="[q{InflateColumn::DateTime}]" ^ | ||||||||||||||||||||||||||||
| 42 | -o preserve_case=1 ^ | ||||||||||||||||||||||||||||
| 43 | MyApp::Schema dbi:mysql:database=foo user pass ^ | ||||||||||||||||||||||||||||
| 44 | "{ quote_char => q{`} }" | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | Configuration files must have schema_class and connect_info sections, | ||||||||||||||||||||||||||||
| 47 | an example of a general config file is as follows: | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | schema_class MyApp::Schema | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | lib /extra/perl/libs | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # connection string | ||||||||||||||||||||||||||||
| 54 | <connect_info> | ||||||||||||||||||||||||||||
| 55 | dsn dbi:mysql:example | ||||||||||||||||||||||||||||
| 56 | user root | ||||||||||||||||||||||||||||
| 57 | pass secret | ||||||||||||||||||||||||||||
| 58 | </connect_info> | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # dbic loader options | ||||||||||||||||||||||||||||
| 61 | <loader_options> | ||||||||||||||||||||||||||||
| 62 | dump_directory ./lib | ||||||||||||||||||||||||||||
| 63 | components InflateColumn::DateTime | ||||||||||||||||||||||||||||
| 64 | components TimeStamp | ||||||||||||||||||||||||||||
| 65 | </loader_options> | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | Using a config file requires L<Config::Any> installed. | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | The optional C<lib> key is equivalent to the C<-I> option. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | Dbicdump generates a L<DBIx::Class> schema using | ||||||||||||||||||||||||||||
| 74 | L<DBIx::Class::Schema::Loader/make_schema_at> and dumps it to disk. | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | You can pass any L<DBIx::Class::Schema::Loader::Base> constructor option using | ||||||||||||||||||||||||||||
| 77 | C<< -o <option>=<value> >>. For convenience, option names will have C<-> | ||||||||||||||||||||||||||||
| 78 | replaced with C<_> and values that look like references or quote-like | ||||||||||||||||||||||||||||
| 79 | operators will be C<eval>-ed before being passed to the constructor. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | The C<dump_directory> option defaults to the current directory if not | ||||||||||||||||||||||||||||
| 82 | specified. | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class>. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | See L<DBIx::Class::Schema::Loader/AUTHORS>. | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 95 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 34 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 100 | 34 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 101 | 34 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 102 | 34 | ▶ | use Pod::Usage; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 103 | 34 | ▶ | use DBIx::Class::Schema::Loader 'make_schema_at'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 104 | 34 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 105 | 34 | ▶ | use DBIx::Class::Schema::Loader::Base (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 106 | 34 | ▶ | use DBIx::Class::Schema::Loader::Optional::Dependencies (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 107 | 34 | require lib; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 34 | my $loader_options; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 34 | Getopt::Long::Configure('gnu_getopt'); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | GetOptions( | ||||||||||||||||||||||||||||
| 114 | 4 | ▶ | 'I=s' => sub { shift; lib->import(shift) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 115 | 34 | 'loader-option|o=s%' => \&handle_option, | |||||||||||||||||||||||||||
| 116 | ); | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 34 | ▶ | $loader_options->{dump_directory} ||= '.'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$loader_options->{"dump_directory"} ||= "."
MC/DC0%0/1
$loader_options->{"dump_directory"} ||= "."
$loader_options->{"dump_directory"}
Truth table50%1/2
A
A= $loader_options->{"dump_directory"}
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 34 | ▶ | if (@ARGV == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if (not DBIx::Class::Schema::Loader::Optional::Dependencies->req_ok_for('dbicdump_config')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | die sprintf "You must install the following CPAN modules to use a config file with dbicdump: %s.\n", | |||||||||||||||||||||||||||
| 123 | DBIx::Class::Schema::Loader::Optional::Dependencies->req_missing_for('dbicdump_config'); | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | my $configuration_file = shift @ARGV; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | my $configurations = Config::Any->load_files({ | |||||||||||||||||||||||||||
| 129 | use_ext => 1, | ||||||||||||||||||||||||||||
| 130 | flatten_to_hash => 1, | ||||||||||||||||||||||||||||
| 131 | files => [$configuration_file] | ||||||||||||||||||||||||||||
| 132 | }); | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | my $c = (values %$configurations)[0]; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | unless (keys %{$c->{connect_info}} && $c->{schema_class}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
keys %{$$c{"connect_info"};} and $c->{"schema_class"}
MC/DC0%0/2
keys %{$$c{"connect_info"};} and $c->{"schema_class"}
keys %{$$c{"connect_info"};}
$c->{"schema_class"}
Truth table0%0/3
A and B
A= keys %{$$c{"connect_info"};}B= $c->{"schema_class"}
| |||||||||||||||||||||||||||||
| 137 | 0 | pod2usage(1); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | my @libs; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | if ($c->{lib}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | if (ref $c->{lib}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | @libs = @{ $c->{lib} }; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | @libs = ($c->{lib}); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | lib->import($_) for @libs; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | my ($dsn, $user, $pass, $options) = | ||||||||||||||||||||||||||||
| 153 | 0 | map { $c->{connect_info}->{$_} } qw/dsn user pass options/; | |||||||||||||||||||||||||||
| 154 | 0 | ▶ | $options ||= {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$options ||= {}
MC/DC0%0/1
$options ||= {}
$options
Truth table0%0/2
A
A= $options
| |||||||||||||||||||||||||||||
| 155 | $c->{loader_options}->{dump_directory} ||= | ||||||||||||||||||||||||||||
| 156 | 0 | ▶ | $loader_options->{dump_directory}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$c->{"loader_options"}{"dump_directory"} ||= $loader_options->{"dump_directory"}
MC/DC0%0/2
$c->{"loader_options"}{"dump_directory"} ||= $loader_options->{"dump_directory"}
$c->{"loader_options"}{"dump_directory"}
$loader_options->{"dump_directory"}
Truth table0%0/3
A ||= B
A= $c->{"loader_options"}{"dump_directory"}B= $loader_options->{"dump_directory"}
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | make_schema_at( | ||||||||||||||||||||||||||||
| 159 | $c->{schema_class}, | ||||||||||||||||||||||||||||
| 160 | $c->{loader_options} || {}, | ||||||||||||||||||||||||||||
| 161 | 0 | ▶ | [ $dsn, $user, $pass, $options ], | ||||||||||||||||||||||||||
|
Condition0%0/2
$c->{'loader_options'} || {}
MC/DC0%0/1
$c->{'loader_options'} || {}
$c->{'loader_options'}
Truth table0%0/2
A
A= $c->{'loader_options'}
| |||||||||||||||||||||||||||||
| 162 | ); | ||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | else { | ||||||||||||||||||||||||||||
| 165 | 34 | ▶ | my ($schema_class, @loader_connect_info) = @ARGV | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | or pod2usage(1); | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 34 | my $dsn = shift @loader_connect_info; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 34 | ▶ | my ($user, $pass) = $dsn =~ /sqlite/i ? ('', '') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | : splice @loader_connect_info, 0, 2; | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 34 | my @extra_connect_info_opts = map parse_value($_), @loader_connect_info; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 34 | make_schema_at( | |||||||||||||||||||||||||||
| 176 | $schema_class, | ||||||||||||||||||||||||||||
| 177 | $loader_options, | ||||||||||||||||||||||||||||
| 178 | [ $dsn, $user, $pass, @extra_connect_info_opts ], | ||||||||||||||||||||||||||||
| 179 | ); | ||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 32 | exit 0; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 157 | ▶ | sub parse_value { | ||||||||||||||||||||||||||
|
Subroutine
parse_value: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 185 | 157 | my $value = shift; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 157 | ▶ | $value = eval $value if $value =~ /^\s*(?:sub\s*\{|q\w?\s*[^\w\s]|[[{])/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 157 | return $value; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 154 | ▶ | sub handle_option { | ||||||||||||||||||||||||||
|
Subroutine
handle_option: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 193 | 154 | my ($self, $key, $value) = @_; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 154 | $key =~ tr/-/_/; | |||||||||||||||||||||||||||
| 196 | 154 | ▶ | die "Unknown option: $key\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | unless DBIx::Class::Schema::Loader::Base->can($key); | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 154 | $value = parse_value $value; | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 154 | $loader_options->{$key} = $value; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 1; | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | __END__ | ||||||||||||||||||||||||||||