GetOptions(
'schema=s' => \my $schema_class,
'class=s' => \my $resultset_class,
- 'where=s' => \my $where,
'op=s' => \my $op,
+ 'where=s' => \my $where,
'set=s' => \my $set,
+ 'format=s' => \my $format,
'force' => \my $force,
'quiet' => \my $quiet,
'help' => \my $help,
pod2usage(1) if ($help);
$ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} = 1 if (!$quiet);
-die('Invalid op') if ($op!~/^insert|update|delete$/s);
+die('No op specified') if(!$op);
+die('Invalid op') if ($op!~/^insert|update|delete|select$/s);
+my $csv_class;
+if ($op eq 'select') {
+ $format ||= 'tsv';
+ die('Invalid format') if ($format!~/^tsv|csv$/s);
+ $csv_class = 'Text::CSV_XS';
+ eval{ require Text::CSV_XS };
+ if ($@) {
+ $csv_class = 'Text::CSV_PP';
+ eval{ require Text::CSV_PP };
+ die('The select op requires either the Text::CSV_XS or the Text::CSV_PP module') if ($@);
+ }
+}
+die('No schema specified') if(!$schema_class);
eval("require $schema_class");
die('Unable to load schema') if ($@);
my $schema = $schema_class->connect();
+die('No class specified') if(!$resultset_class);
my $resultset = eval{ $schema->resultset($resultset_class) };
die('Unable to load the class with the schema') if ($@);
$set = jsonToObj( $set ) if ($set);
if ($op eq 'insert') {
- die('The insert operator and the where option do not mix') if ($where);
+ die('Do not use the where option with the insert op') if ($where);
my $obj = $resultset->create( $set );
print ''.ref($resultset).' ID: '.join(',',$obj->id())."\n";
}
}
}
elsif ($op eq 'delete') {
- die('The delete operator and the set option do not mix') if ($set);
+ die('Do not use the set option with the delete op') if ($set);
$resultset = $resultset->search( $where );
my $count = $resultset->count();
print "This action will delete $count ".ref($resultset)." records.\n" if (!$quiet);
$resultset->delete_all();
}
}
+elsif ($op eq 'select') {
+ die('Do not use the set option with the select op') if ($set);
+ my $csv = $csv_class->new({
+ sep_char => ( $format eq 'tsv' ? "\t" : ',' ),
+ });
+ $resultset = $resultset->search( $where );
+ my @columns = $resultset->result_source->columns();
+ $csv->combine( @columns );
+ print $csv->string();
+ while (my $row = $resultset->next()) {
+ my @fields;
+ foreach my $column (@columns) {
+ push( @fields, $row->get_column($column) );
+ }
+ $csv->combine( @fields );
+ print $csv->string();
+ }
+}
sub confirm {
print "Are you sure you want to do this? (type YES to confirm) ";
=head1 SYNOPSIS
- dbicadmin insert My::Schema Class --set this=that
- dbicadmin update My::Schema Class --set this=that --where those=these
- dbicadmin delete My::Schema Class --where those=these
+ dbicadmin --op=insert --schema=My::Schema --class=Class --set=JSON
+ dbicadmin --op=update --schema=My::Schema --class=Class --set=JSON --where=JSON
+ dbicadmin --op=delete --schema=My::Schema --class=Class --where=JSON
+ dbicadmin --op=select --schema=My::Schema --class=Class --where=JSON --format=tsv
=head1 DESCRIPTION
-This utility provides the ability to run INSERTs, UPDATEs, and
-DELETEs on any DBIx::Class object.
+This utility provides the ability to run INSERTs, UPDATEs,
+DELETEs, and SELECTs on any DBIx::Class object.
-=head1 ARGUMENTS
-
-Before any options are passed this script expects three arguments.
+=head1 OPTIONS
-=head2 operation
+=head2 op
-The type of operation. Valid values are insert, update, and delete.
+The type of operation. Valid values are insert, update, delete,
+and select.
=head2 schema
The name of the class, within your schema, that you want to run
the operation on.
-=head1 OPTIONS
+=head2 where
+
+A valid JSON data string that is compatible with DBIC.
+
+=head2 set
+
+A valid JSON data stream that is compatible with DBIC.
=head2 help
Do not print status messages or SQL statements.
-=head2 where
-
-This option uses L<Getopt::Long>'s ability to specify a hash
-structure with command line options. Basically, for every
-clause that you want to include in the WHERE statement you
-have a --where option specifying the clause. So, if you wanted
-to specify two clauses you would do:
-
- --where this=that --where those=these
-
-And that will become something like:
-
- WHERE this="that" AND those="these"
-
-The insert does not suppor the where option and will croak if
-you try to use it.
-
-=head2 set
-
-This works just like the where option except that the insert
-operation does support it, but the delete operation does not.
-
=head1 AUTHOR
Aran Deltac <bluefeet@cpan.org>