+sub ptab {
+ my @rows = ref($_[0]) eq 'ARRAY' ? @{ $_[0] } : @_;
+ return '<empty result set>' 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 '<no object>' 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);
+ }
+}
+
+sub part {
+ require SL::DB::Part;
+ SL::DB::Manager::Part->find_by(@_)
+}
+
+sub order {
+ require SL::DB::Order;
+ SL::DB::Manager::Order->find_by(@_)
+}
+
+sub invoice {
+ require SL::DB::Invoice;
+ SL::DB::Manager::Invoice->find_by(@_)
+}
+
+sub purchase_invoice {
+ require SL::DB::PurchaseInvoice;
+ SL::DB::Manager::PurchaseInvoice->find_by(@_)
+}
+
+sub customer {
+ require SL::DB::Customer;
+ SL::DB::Manager::Customer->find_by(@_)
+}
+
+sub vendor {
+ require SL::DB::Vendor;
+ SL::DB::Manager::Vendor->find_by(@_)
+}
+
+sub chart {
+ require SL::DB::Chart;
+ SL::DB::Manager::Chart->find_by(@_)
+}
+
+sub clock (&) {
+ my $s = [Time::HiRes::gettimeofday()];
+ $_[0]->();
+ Time::HiRes::tv_interval($s);
+}
+
+