7 use JSON qw( jsonToObj );
10 'schema=s' => \my $schema_class,
11 'class=s' => \my $resultset_class,
12 'where=s' => \my $where,
15 'force' => \my $force,
16 'quiet' => \my $quiet,
20 pod2usage(1) if ($help);
21 $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} = 1 if (!$quiet);
23 die('Invalid op') if ($op!~/^insert|update|delete$/s);
25 eval("require $schema_class");
26 die('Unable to load schema') if ($@);
27 my $schema = $schema_class->connect();
29 my $resultset = eval{ $schema->resultset($resultset_class) };
30 die('Unable to load the class with the schema') if ($@);
32 $where = jsonToObj( $where ) if ($where);
33 $set = jsonToObj( $set ) if ($set);
35 if ($op eq 'insert') {
36 die('The insert operator and the where option do not mix') if ($where);
37 my $obj = $resultset->create( $set );
38 print ''.ref($resultset).' ID: '.join(',',$obj->id())."\n";
40 elsif ($op eq 'update') {
41 $resultset = $resultset->search( $where );
42 my $count = $resultset->count();
43 print "This action will modify $count ".ref($resultset)." records.\n" if (!$quiet);
44 if ( $force || confirm() ) {
45 $resultset->update_all( $set );
48 elsif ($op eq 'delete') {
49 die('The delete operator and the set option do not mix') if ($set);
50 $resultset = $resultset->search( $where );
51 my $count = $resultset->count();
52 print "This action will delete $count ".ref($resultset)." records.\n" if (!$quiet);
53 if ( $force || confirm() ) {
54 $resultset->delete_all();
59 print "Are you sure you want to do this? (type YES to confirm) ";
60 my $response = <STDIN>;
61 return 1 if ($response=~/^YES/);
69 dbicadmin - Execute simple actions upon DBIx::Class objects.
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
79 This utility provides the ability to run INSERTs, UPDATEs, and
80 DELETEs on any DBIx::Class object.
84 Before any options are passed this script expects three arguments.
88 The type of operation. Valid values are insert, update, and delete.
92 The name of your schema class.
96 The name of the class, within your schema, that you want to run
103 Display this help page.
107 Suppresses the confirmation dialogues that are usually displayed
108 when someone runs a DELETE or UPDATE action.
112 Do not print status messages or SQL statements.
116 This option uses L<Getopt::Long>'s ability to specify a hash
117 structure with command line options. Basically, for every
118 clause that you want to include in the WHERE statement you
119 have a --where option specifying the clause. So, if you wanted
120 to specify two clauses you would do:
122 --where this=that --where those=these
124 And that will become something like:
126 WHERE this="that" AND those="these"
128 The insert does not suppor the where option and will croak if
133 This works just like the where option except that the insert
134 operation does support it, but the delete operation does not.
138 Aran Deltac <bluefeet@cpan.org>
142 You may distribute this code under the same terms as Perl itself.