Newlines after each csv lines. Add trace option.
[dbsrgits/DBIx-Class-Historic.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
2bbc85c9 9GetOptions(
10 'schema=s' => \my $schema_class,
11 'class=s' => \my $resultset_class,
2bbc85c9 12 'op=s' => \my $op,
b04e5d3e 13 'where=s' => \my $where,
2bbc85c9 14 'set=s' => \my $set,
b04e5d3e 15 'format=s' => \my $format,
2bbc85c9 16 'force' => \my $force,
5deea98c 17 'trace' => \my $trace,
2bbc85c9 18 'quiet' => \my $quiet,
19 'help' => \my $help,
20);
a94aa524 21
2bbc85c9 22pod2usage(1) if ($help);
5deea98c 23$ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} = 1 if ($trace);
a94aa524 24
b04e5d3e 25die('No op specified') if(!$op);
26die('Invalid op') if ($op!~/^insert|update|delete|select$/s);
27my $csv_class;
28if ($op eq 'select') {
29 $format ||= 'tsv';
30 die('Invalid format') if ($format!~/^tsv|csv$/s);
31 $csv_class = 'Text::CSV_XS';
32 eval{ require Text::CSV_XS };
33 if ($@) {
34 $csv_class = 'Text::CSV_PP';
35 eval{ require Text::CSV_PP };
36 die('The select op requires either the Text::CSV_XS or the Text::CSV_PP module') if ($@);
37 }
38}
a94aa524 39
b04e5d3e 40die('No schema specified') if(!$schema_class);
a94aa524 41eval("require $schema_class");
2bbc85c9 42die('Unable to load schema') if ($@);
a94aa524 43my $schema = $schema_class->connect();
44
b04e5d3e 45die('No class specified') if(!$resultset_class);
2bbc85c9 46my $resultset = eval{ $schema->resultset($resultset_class) };
a94aa524 47die('Unable to load the class with the schema') if ($@);
48
2bbc85c9 49$where = jsonToObj( $where ) if ($where);
50$set = jsonToObj( $set ) if ($set);
a94aa524 51
52if ($op eq 'insert') {
b04e5d3e 53 die('Do not use the where option with the insert op') if ($where);
a94aa524 54 my $obj = $resultset->create( $set );
2bbc85c9 55 print ''.ref($resultset).' ID: '.join(',',$obj->id())."\n";
a94aa524 56}
57elsif ($op eq 'update') {
58 $resultset = $resultset->search( $where );
59 my $count = $resultset->count();
2bbc85c9 60 print "This action will modify $count ".ref($resultset)." records.\n" if (!$quiet);
a94aa524 61 if ( $force || confirm() ) {
62 $resultset->update_all( $set );
63 }
64}
65elsif ($op eq 'delete') {
b04e5d3e 66 die('Do not use the set option with the delete op') if ($set);
a94aa524 67 $resultset = $resultset->search( $where );
68 my $count = $resultset->count();
2bbc85c9 69 print "This action will delete $count ".ref($resultset)." records.\n" if (!$quiet);
a94aa524 70 if ( $force || confirm() ) {
71 $resultset->delete_all();
72 }
73}
b04e5d3e 74elsif ($op eq 'select') {
75 die('Do not use the set option with the select op') if ($set);
76 my $csv = $csv_class->new({
77 sep_char => ( $format eq 'tsv' ? "\t" : ',' ),
78 });
79 $resultset = $resultset->search( $where );
80 my @columns = $resultset->result_source->columns();
81 $csv->combine( @columns );
5deea98c 82 print $csv->string()."\n";
b04e5d3e 83 while (my $row = $resultset->next()) {
84 my @fields;
85 foreach my $column (@columns) {
86 push( @fields, $row->get_column($column) );
87 }
88 $csv->combine( @fields );
5deea98c 89 print $csv->string()."\n";
b04e5d3e 90 }
91}
a94aa524 92
93sub confirm {
94 print "Are you sure you want to do this? (type YES to confirm) ";
95 my $response = <STDIN>;
96 return 1 if ($response=~/^YES/);
97 return;
98}
99
100__END__
101
102=head1 NAME
103
5deea98c 104dbicadmin - Execute operations upon DBIx::Class objects.
a94aa524 105
106=head1 SYNOPSIS
107
b04e5d3e 108 dbicadmin --op=insert --schema=My::Schema --class=Class --set=JSON
109 dbicadmin --op=update --schema=My::Schema --class=Class --set=JSON --where=JSON
110 dbicadmin --op=delete --schema=My::Schema --class=Class --where=JSON
111 dbicadmin --op=select --schema=My::Schema --class=Class --where=JSON --format=tsv
a94aa524 112
113=head1 DESCRIPTION
114
b04e5d3e 115This utility provides the ability to run INSERTs, UPDATEs,
116DELETEs, and SELECTs on any DBIx::Class object.
a94aa524 117
b04e5d3e 118=head1 OPTIONS
a94aa524 119
b04e5d3e 120=head2 op
a94aa524 121
b04e5d3e 122The type of operation. Valid values are insert, update, delete,
123and select.
a94aa524 124
125=head2 schema
126
127The name of your schema class.
128
129=head2 class
130
131The name of the class, within your schema, that you want to run
132the operation on.
133
b04e5d3e 134=head2 where
135
136A valid JSON data string that is compatible with DBIC.
137
138=head2 set
139
140A valid JSON data stream that is compatible with DBIC.
a94aa524 141
142=head2 help
143
144Display this help page.
145
146=head2 force
147
148Suppresses the confirmation dialogues that are usually displayed
149when someone runs a DELETE or UPDATE action.
150
2bbc85c9 151=head2 quiet
152
5deea98c 153Do not display status messages.
154
155=head2 trace
156
157Turns on tracing on the DBI storage, thus printing SQL as it is
158executed.
2bbc85c9 159
a94aa524 160=head1 AUTHOR
161
162Aran Deltac <bluefeet@cpan.org>
163
164=head1 LICENSE
165
166You may distribute this code under the same terms as Perl itself.
167