Initial JSON support for the dbicadmin script.
[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
2bbc85c9 9GetOptions(
10 'schema=s' => \my $schema_class,
11 'class=s' => \my $resultset_class,
12 'where=s' => \my $where,
13 'op=s' => \my $op,
14 'set=s' => \my $set,
15 'force' => \my $force,
16 'quiet' => \my $quiet,
17 'help' => \my $help,
18);
a94aa524 19
2bbc85c9 20pod2usage(1) if ($help);
21$ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} = 1 if (!$quiet);
a94aa524 22
2bbc85c9 23die('Invalid op') if ($op!~/^insert|update|delete$/s);
a94aa524 24
a94aa524 25eval("require $schema_class");
2bbc85c9 26die('Unable to load schema') if ($@);
a94aa524 27my $schema = $schema_class->connect();
28
2bbc85c9 29my $resultset = eval{ $schema->resultset($resultset_class) };
a94aa524 30die('Unable to load the class with the schema') if ($@);
31
2bbc85c9 32$where = jsonToObj( $where ) if ($where);
33$set = jsonToObj( $set ) if ($set);
a94aa524 34
35if ($op eq 'insert') {
2bbc85c9 36 die('The insert operator and the where option do not mix') if ($where);
a94aa524 37 my $obj = $resultset->create( $set );
2bbc85c9 38 print ''.ref($resultset).' ID: '.join(',',$obj->id())."\n";
a94aa524 39}
40elsif ($op eq 'update') {
41 $resultset = $resultset->search( $where );
42 my $count = $resultset->count();
2bbc85c9 43 print "This action will modify $count ".ref($resultset)." records.\n" if (!$quiet);
a94aa524 44 if ( $force || confirm() ) {
45 $resultset->update_all( $set );
46 }
47}
48elsif ($op eq 'delete') {
2bbc85c9 49 die('The delete operator and the set option do not mix') if ($set);
a94aa524 50 $resultset = $resultset->search( $where );
51 my $count = $resultset->count();
2bbc85c9 52 print "This action will delete $count ".ref($resultset)." records.\n" if (!$quiet);
a94aa524 53 if ( $force || confirm() ) {
54 $resultset->delete_all();
55 }
56}
57
58sub confirm {
59 print "Are you sure you want to do this? (type YES to confirm) ";
60 my $response = <STDIN>;
61 return 1 if ($response=~/^YES/);
62 return;
63}
64
65__END__
66
67=head1 NAME
68
69dbicadmin - Execute simple actions upon DBIx::Class objects.
70
71=head1 SYNOPSIS
72
73 dbicadmin insert My::Schema Class --set this=that
74 dbicadmin update My::Schema Class --set this=that --where those=these
75 dbicadmin delete My::Schema Class --where those=these
76
77=head1 DESCRIPTION
78
79This utility provides the ability to run INSERTs, UPDATEs, and
80DELETEs on any DBIx::Class object.
81
82=head1 ARGUMENTS
83
84Before any options are passed this script expects three arguments.
85
86=head2 operation
87
88The type of operation. Valid values are insert, update, and delete.
89
90=head2 schema
91
92The name of your schema class.
93
94=head2 class
95
96The name of the class, within your schema, that you want to run
97the operation on.
98
99=head1 OPTIONS
100
101=head2 help
102
103Display this help page.
104
105=head2 force
106
107Suppresses the confirmation dialogues that are usually displayed
108when someone runs a DELETE or UPDATE action.
109
2bbc85c9 110=head2 quiet
111
112Do not print status messages or SQL statements.
113
a94aa524 114=head2 where
115
116This option uses L<Getopt::Long>'s ability to specify a hash
117structure with command line options. Basically, for every
118clause that you want to include in the WHERE statement you
119have a --where option specifying the clause. So, if you wanted
120to specify two clauses you would do:
121
122 --where this=that --where those=these
123
124And that will become something like:
125
126 WHERE this="that" AND those="these"
127
128The insert does not suppor the where option and will croak if
129you try to use it.
130
131=head2 set
132
133This works just like the where option except that the insert
134operation does support it, but the delete operation does not.
135
136=head1 AUTHOR
137
138Aran Deltac <bluefeet@cpan.org>
139
140=head1 LICENSE
141
142You may distribute this code under the same terms as Perl itself.
143