Docced JSON usage and added support for the attrs option.
[dbsrgits/DBIx-Class.git] / maint / dbicadmin
CommitLineData
a94aa524 1#!/usr/bin/perl
2use strict;
3use warnings;
4
5use Getopt::Long;
6use Pod::Usage;
2bbc85c9 7use JSON qw( jsonToObj );
a94aa524 8
6717e3a8 9$JSON::BareKey = 1;
10$JSON::QuotApos = 1;
11
2bbc85c9 12GetOptions(
13 'schema=s' => \my $schema_class,
14 'class=s' => \my $resultset_class,
2bbc85c9 15 'op=s' => \my $op,
16 'set=s' => \my $set,
6717e3a8 17 'where=s' => \my $where,
18 'attrs=s' => \my $attrs,
b04e5d3e 19 'format=s' => \my $format,
2bbc85c9 20 'force' => \my $force,
5deea98c 21 'trace' => \my $trace,
2bbc85c9 22 'quiet' => \my $quiet,
23 'help' => \my $help,
24);
a94aa524 25
2bbc85c9 26pod2usage(1) if ($help);
5deea98c 27$ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} = 1 if ($trace);
a94aa524 28
b04e5d3e 29die('No op specified') if(!$op);
30die('Invalid op') if ($op!~/^insert|update|delete|select$/s);
31my $csv_class;
32if ($op eq 'select') {
33 $format ||= 'tsv';
34 die('Invalid format') if ($format!~/^tsv|csv$/s);
35 $csv_class = 'Text::CSV_XS';
36 eval{ require Text::CSV_XS };
37 if ($@) {
38 $csv_class = 'Text::CSV_PP';
39 eval{ require Text::CSV_PP };
40 die('The select op requires either the Text::CSV_XS or the Text::CSV_PP module') if ($@);
41 }
42}
a94aa524 43
b04e5d3e 44die('No schema specified') if(!$schema_class);
a94aa524 45eval("require $schema_class");
2bbc85c9 46die('Unable to load schema') if ($@);
a94aa524 47my $schema = $schema_class->connect();
48
b04e5d3e 49die('No class specified') if(!$resultset_class);
2bbc85c9 50my $resultset = eval{ $schema->resultset($resultset_class) };
a94aa524 51die('Unable to load the class with the schema') if ($@);
52
2bbc85c9 53$set = jsonToObj( $set ) if ($set);
6717e3a8 54$where = jsonToObj( $where ) if ($where);
55$attrs = jsonToObj( $attrs ) if ($attrs);
a94aa524 56
57if ($op eq 'insert') {
b04e5d3e 58 die('Do not use the where option with the insert op') if ($where);
6717e3a8 59 die('Do not use the attrs option with the insert op') if ($attrs);
a94aa524 60 my $obj = $resultset->create( $set );
2bbc85c9 61 print ''.ref($resultset).' ID: '.join(',',$obj->id())."\n";
a94aa524 62}
63elsif ($op eq 'update') {
64 $resultset = $resultset->search( $where );
65 my $count = $resultset->count();
2bbc85c9 66 print "This action will modify $count ".ref($resultset)." records.\n" if (!$quiet);
a94aa524 67 if ( $force || confirm() ) {
68 $resultset->update_all( $set );
69 }
70}
71elsif ($op eq 'delete') {
b04e5d3e 72 die('Do not use the set option with the delete op') if ($set);
6717e3a8 73 $resultset = $resultset->search( $where, $attrs );
a94aa524 74 my $count = $resultset->count();
2bbc85c9 75 print "This action will delete $count ".ref($resultset)." records.\n" if (!$quiet);
a94aa524 76 if ( $force || confirm() ) {
77 $resultset->delete_all();
78 }
79}
b04e5d3e 80elsif ($op eq 'select') {
81 die('Do not use the set option with the select op') if ($set);
82 my $csv = $csv_class->new({
83 sep_char => ( $format eq 'tsv' ? "\t" : ',' ),
84 });
6717e3a8 85 $resultset = $resultset->search( $where, $attrs );
b04e5d3e 86 my @columns = $resultset->result_source->columns();
87 $csv->combine( @columns );
5deea98c 88 print $csv->string()."\n";
b04e5d3e 89 while (my $row = $resultset->next()) {
90 my @fields;
91 foreach my $column (@columns) {
92 push( @fields, $row->get_column($column) );
93 }
94 $csv->combine( @fields );
5deea98c 95 print $csv->string()."\n";
b04e5d3e 96 }
97}
a94aa524 98
99sub confirm {
100 print "Are you sure you want to do this? (type YES to confirm) ";
101 my $response = <STDIN>;
102 return 1 if ($response=~/^YES/);
103 return;
104}
105
106__END__
107
108=head1 NAME
109
5deea98c 110dbicadmin - Execute operations upon DBIx::Class objects.
a94aa524 111
112=head1 SYNOPSIS
113
b04e5d3e 114 dbicadmin --op=insert --schema=My::Schema --class=Class --set=JSON
115 dbicadmin --op=update --schema=My::Schema --class=Class --set=JSON --where=JSON
116 dbicadmin --op=delete --schema=My::Schema --class=Class --where=JSON
117 dbicadmin --op=select --schema=My::Schema --class=Class --where=JSON --format=tsv
a94aa524 118
119=head1 DESCRIPTION
120
b04e5d3e 121This utility provides the ability to run INSERTs, UPDATEs,
122DELETEs, and SELECTs on any DBIx::Class object.
a94aa524 123
b04e5d3e 124=head1 OPTIONS
a94aa524 125
b04e5d3e 126=head2 op
a94aa524 127
b04e5d3e 128The type of operation. Valid values are insert, update, delete,
129and select.
a94aa524 130
131=head2 schema
132
133The name of your schema class.
134
135=head2 class
136
137The name of the class, within your schema, that you want to run
138the operation on.
139
6717e3a8 140=head2 set
141
142This option must be valid JSON data string and is passed in to
143the DBIC update() method. Use this option with the update
144and insert ops.
145
b04e5d3e 146=head2 where
147
6717e3a8 148This option must be valid JSON data string and is passed in as
149the first argument to the DBIC search() method. Use this
150option with the update, delete, and select ops.
b04e5d3e 151
6717e3a8 152=head2 attrs
b04e5d3e 153
6717e3a8 154This option must be valid JSON data string and is passed in as
155the second argument to the DBIC search() method. Use this
156option with the update, delete, and select ops.
a94aa524 157
158=head2 help
159
160Display this help page.
161
162=head2 force
163
164Suppresses the confirmation dialogues that are usually displayed
165when someone runs a DELETE or UPDATE action.
166
2bbc85c9 167=head2 quiet
168
5deea98c 169Do not display status messages.
170
171=head2 trace
172
173Turns on tracing on the DBI storage, thus printing SQL as it is
174executed.
2bbc85c9 175
6717e3a8 176=head1 JSON
177
178JSON is a lightweight data-interchange format. It allows you
179to express complex data structures for use in the where and
180set options.
181
182This module turns on L<JSON>'s BareKey and QuotApos options so
183that your data can look a bit more readable.
184
185 --where={"this":"that"} # generic JSON
186 --where={this:'that'} # with BareKey and QuoteApos
187
188Consider wrapping your JSON in outer quotes so that you don't
189have to escape your inner quotes.
190
191 --where={this:\"that\"} # no outer quote
192 --where='{this:"that"}' # outer quoted
193
a94aa524 194=head1 AUTHOR
195
196Aran Deltac <bluefeet@cpan.org>
197
198=head1 LICENSE
199
200You may distribute this code under the same terms as Perl itself.
201