X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=scripts%2Fconsole;h=d264a8941f05e16c3f83fd0b5702dc4b969eda98;hb=75603a224c1bc962d18e9398972fa3bf2ff77f87;hp=e183ac83a516b5fb2bf85b7aa881e1ac6c03e83b;hpb=81fed28305f28aff317639f9a190cdf83a678ed4;p=kivitendo-erp.git diff --git a/scripts/console b/scripts/console index e183ac83a..d264a8941 100755 --- a/scripts/console +++ b/scripts/console @@ -48,6 +48,7 @@ use SL::InstanceConfiguration; use SL::Locale; use SL::LXDebug; use Data::Dumper; +use List::Util qw(max); # this is a cleaned up version of am.pl # it lacks redirection, some html setup and most of the authentication process. @@ -106,14 +107,14 @@ sub quit { sub help { print <' unless @rows; + + my @columns = sort keys %{ $rows[0] }; + my @widths = map { max @{ $_ } } map { my $column = $_; [ length($column), map { length("" . ($_->{$column} // '')) } @rows ] } @columns; + my @output = (join ' | ', map { my $width = $widths[$_]; sprintf "\%-${width}s", $columns[$_] } (0..@columns - 1)); + push @output, join('-+-', map { '-' x $_ } @widths); + push @output, map { my $row = $_; join(' | ', map { my $width = $widths[$_]; sprintf "\%-${width}s", $row->{ $columns[$_] } // '' } (0..@columns - 1) ) } @rows; + + return join("\n", @output); +} + +sub pobj { + my ($obj) = @_; + return '' unless $obj; + + my $ref = ref $obj; + $ref =~ s/^SL::DB:://; + my %primaries = map { ($_ => 1) } $obj->meta->primary_key; + my @columns = map { "${_}:" . ($obj->$_ // 'UNDEF') } sort $obj->meta->primary_key; + push @columns, map { "${_}:" . ($obj->$_ // 'UNDEF') } grep { !$primaries{$_} } sort map { $_->{name} } $obj->meta->columns; + + return "<${ref} " . join(' ', @columns) . '>'; +} + +sub sql { + my $dbh = ref($_[0]) ? shift : $::form->get_standard_dbh; + my ($query, @args) = @_; + + if ($query =~ m/^\s*select/i) { + ptab($dbh->selectall_arrayref($query, { Slice => {} }, @args)); + } else { + $dbh->do($query, { Slice => {} }, @args); + } +} + 1; __END__ =head1 NAME -scripts/console - Lx-Office console +scripts/console - kivitendo console =head1 SYNOPSIS @@ -165,6 +203,47 @@ Currently C will set the Data::Dumper depth to 2, so if you need a different depth, you'll have to change that. A nice feature would be to configure that, or at least to be able to change it at runtime. +=head2 ptab C<@data> + +Returns a tabular representation of C<@data>. C<@data> must be an +array or array reference containing hash references. Column widths are +calculated automatically. + +Undefined values are represented by an empty column. + +Example usage: + + ptab($dbh->selectall_arrayref("SELECT * FROM employee", { Slice => {} })); + +=head2 pobj C<$obj> + +Returns a textual representation of the L instance +C<$obj>. This includes the class name, then the primary key columns as +name/value pairs and then all other columns as name/value pairs. + +Undefined values are represented by C. + +Example usage: + + pobj(SL::DB::Manager::Employee->find_by(login => 'demo')); + +=head2 sql C<[ $dbh, ] $query, @bind_values> + +Executes an SQL query using the optional bind values. If the first +parameter is a database handle then that database handle is used; +otherwise the handle returned by L is used. + +If the query is a C