App-AltSQL

 view release on metacpan or  search on metacpan

lib/App/AltSQL/View.pm  view on Meta::CPAN

package App::AltSQL::View;

=head1 NAME

App::AltSQL::View

=head1 DESCRIPTION

This is an internal class used by L<App::AltSQL> to capture the output of a DBI statement handler and express it to the user somehow.  It does this mainly with L<Text::UnicodeBox::Table>, and is currently MySQL specific.

=cut

use Moose;
use Data::Dumper;
use Text::CharWidth qw(mbswidth);
use Time::HiRes qw(gettimeofday);
use Params::Validate;
use List::Util qw(sum max);

with 'App::AltSQL::Role';
with 'MooseX::Object::Pluggable';

has 'timing' => ( is => 'rw' );
has 'verb'   => ( is => 'rw' );

has 'buffer' => ( is => 'rw' );
has 'table_data' => ( is => 'rw' );
has 'footer' => ( is => 'rw' );

sub args_spec {
	return (
	);
}

around BUILDARGS => sub {
	my $orig = shift;
	my $class = shift;
	my %args = validate(@_, {
		app    => 1,
		timing => 1,
		verb   => 1,
		sth    => 1,
	});
	my $sth = delete $args{sth};

	if ($args{verb} eq 'use') {
		$args{buffer} = 'Database changed';
		return $class->$orig(\%args);
	}

	if (! $sth->{NUM_OF_FIELDS}) {
		$args{buffer} = sprintf "Query OK, %d row%s affected (%s)\n", $sth->rows, ($sth->rows > 1 ? 's' : ''), _describe_timing($args{timing});
		if ($args{verb} ne 'insert') {
			$args{buffer} .= sprintf "Records: %d  Warnings: %d\n", $sth->rows, $sth->{mysql_warning_count};
		}
		$args{buffer} .= "\n";
		return $class->$orig(\%args);
	}

	my %table_data = (
		columns => [],
		rows    => [],
	);
	$args{table_data} = \%table_data;

	# Populate table_data{columns}
	my %mysql_meta = (
		map { my $key = $_; $key =~ s/^mysql_//; +($key => $sth->{$_}) }
		qw(mysql_is_blob mysql_is_key mysql_is_num mysql_is_pri_key mysql_is_auto_increment mysql_length mysql_max_length)
	);
	foreach my $i (0..$sth->{NUM_OF_FIELDS} - 1) {
		push @{ $table_data{columns} }, {
			name      => $sth->{NAME}[$i],
			type      => $sth->{TYPE}[$i],



( run in 0.760 second using v1.01-cache-2.11-cpan-39bf76dae61 )