this code never runs anyway
[dbsrgits/DBIx-Class.git] / script / dbicadmin
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Getopt::Long::Descriptive;
7 use DBIx::Class::Admin;
8
9 my ($opts, $usage) = describe_options(
10   "%c: %o",
11   (
12     ['Actions'],
13     ["action" => hidden => { one_of => [
14       ['create|c' => 'Create version diffs needs preversion',],
15       ['upgrade|u' => 'Upgrade the database to the current schema '],
16       ['install|i' => 'Install the schema to the database',],
17       ['deploy|d' => 'Deploy the schema to the database',],
18       ['select|s'   => 'Select data from the schema', ],
19       ['insert|i'   => 'Insert data into the schema', ],
20       ['update|u'   => 'Update data in the schema', ], 
21       ['delete|D'   => 'Delete data from the schema',],
22       ['op:s' => 'compatiblity option all of the above can be suppied as --op=<action>'],
23       ['help|h' => 'display this help'],
24     ], required=> 1 }],
25     ['Options'],
26     ['schema-class|schema|C:s' => 'The class of the schema to load', { required => 1 } ],
27     ['resultset|resultset_class|class|r:s' => 'The resultset to operate on for data manipulation' ],
28     ['config-stanza|S:s' => 'Where in the config to find the connection_info, supply in form MyApp::Model::DB',],
29     ['config|f:s' => 'Supply the config file for parsing by Config::Any', { depends => 'config_stanza'} ],
30     ['connect-info|n:s%' => 'Supply the connect info as additonal options ie -I dsn=<dsn> user=<user> password=<pass> '],
31     ['connect:s' => 'Supply the connect info as a json string' ],
32     ['sql-dir|q:s' => 'The directory where sql diffs will be created'],
33     ['sql-type|t:s' => 'The RDBMs flavour you wish to use'],
34     ['version|v:i' => 'Supply a version install'],
35     ['preversion|p:s' => 'The previous version to diff against',],
36     ['set:s' => 'JSON data used to perform data operations' ],
37     ['lib|I:s' => 'Additonal library path to search in'], 
38     ['attrs:s' => 'JSON string to be used for the second argument for search'],
39     ['where:s' => 'JSON string to be used for the where clause of search'],
40     ['force' => 'Be forceful with some operations'],
41     ['trace' => 'Turn on DBIx::Class trace output'],
42     ['quiet' => 'Be less verbose'],
43   )
44 );
45
46
47 die "please only use one of --config or --connect-info" if ($opts->{config} and $opts->{connect_info});
48
49 # option compatability mangle
50 if($opts->{connect}) {
51   $opts->{connect_info} = delete $opts->{connect};
52 }
53
54 my $admin = DBIx::Class::Admin->new( %$opts );
55
56
57 my $action = $opts->{action};
58
59 $action = $opts->{op} if ($action eq 'op');
60 my $res = $admin->$action();
61
62 print "going to perform action $action\n";
63 if ($action eq 'select') {
64
65   my $csv_class;
66   my $format = $opts->{format} || 'tsv';
67   die('Invalid format') if ($format!~/^tsv|csv$/s);
68   $csv_class = 'Text::CSV_XS';
69   eval{ require Text::CSV_XS };
70   if ($@) {
71     $csv_class = 'Text::CSV_PP';
72     eval{ require Text::CSV_PP };
73     die('The select op requires either the Text::CSV_XS or the Text::CSV_PP module') if ($@);
74   }
75
76   my $csv = $csv_class->new({
77       sep_char => ( $format eq 'tsv' ? "\t" : ',' ),
78     });
79   foreach my $row (@$res) {
80     $csv->combine( @$row );
81     print $csv->string()."\n";
82   }
83 }
84
85
86
87 =head1 AUTHOR
88
89 Aran Deltac <bluefeet@cpan.org>
90
91 refactored by
92 Gordon Irving <goraxe@cpan.org>
93
94 =head1 LICENSE
95
96 You may distribute this code under the same terms as Perl itself.