From: Aran Deltac Date: Sat, 29 Apr 2006 02:22:45 +0000 (+0000) Subject: Fixes to dbicadmin as well as the ability to support SELECTs. X-Git-Tag: v0.07002~75^2~227 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b04e5d3e02eb7f256757fdb5201f875aabf9e58a;p=dbsrgits%2FDBIx-Class.git Fixes to dbicadmin as well as the ability to support SELECTs. --- diff --git a/maint/dbicadmin b/maint/dbicadmin index 0e927be..99928c6 100755 --- a/maint/dbicadmin +++ b/maint/dbicadmin @@ -9,9 +9,10 @@ use JSON qw( jsonToObj ); 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, @@ -20,12 +21,27 @@ GetOptions( 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 ($@); @@ -33,7 +49,7 @@ $where = jsonToObj( $where ) if ($where); $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"; } @@ -46,7 +62,7 @@ elsif ($op eq 'update') { } } 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); @@ -54,6 +70,24 @@ elsif ($op eq 'delete') { $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) "; @@ -70,22 +104,22 @@ dbicadmin - Execute simple actions upon DBIx::Class objects. =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 @@ -96,7 +130,13 @@ The name of your schema class. 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 @@ -111,28 +151,6 @@ when someone runs a DELETE or UPDATE action. Do not print status messages or SQL statements. -=head2 where - -This option uses L'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