summaryrefslogtreecommitdiffstats
path: root/lib/Chart/Clicker/SQL/REPL.pm
blob: dca39f91fda0cb1263ab8f49ad3c5950d44040a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package Chart::Clicker::SQL::REPL;
use Moose;
use 5.020;
use feature 'signatures', 'postderef';
no warnings 'experimental::signatures';
no warnings 'experimental::postderef';

use Browser::Open;
use File::HomeDir;
use File::Spec;
use File::Temp;
use Module::Runtime;
use Path::Class;
use Term::ReadLine;

use Chart::Clicker::SQL;

has sql => (
    is        => 'rw',
    isa       => 'Chart::Clicker::SQL',
    predicate => 'initialized',
);

has history_file => (
    is      => 'ro',
    isa     => 'Str',
    default => sub {
        my $filename = File::Spec->catfile(
            File::HomeDir->my_data,
            '.ccsql_history'
        );
        file($filename)->touch;
        return $filename;
    },
);

has rl => (
    is      => 'ro',
    isa     => 'Term::ReadLine',
    lazy    => 1,
    default => sub ($self) {
        my $rl = Term::ReadLine->new(__PACKAGE__);
        for my $line (file($self->history_file)->slurp(chomp => 1)) {
            $rl->addhistory($line);
        }
        return $rl;
    },
);

has chart_options => (
    is      => 'ro',
    isa     => 'HashRef',
    default => sub {
        {
            width        => 1000,
            height       => 600,
            set_renderer => Chart::Clicker::Renderer::Line->new,
        }
    },
);

has last_query => (
    is        => 'rw',
    isa       => 'Str',
    predicate => 'has_last_query',
);

sub dsn ($self, $dsn) {
    $self->sql(Chart::Clicker::SQL->new(dsn => $dsn));
}

sub select ($self, $query) {
    $query = "select $query";
    $self->last_query($query);
    $self->_draw;
}

sub _draw ($self) {
    if (!$self->initialized) {
        warn "not initialized";
        return;
    }
    if (!$self->has_last_query) {
        warn "no active query";
        return;
    }
    my $chart = $self->sql->render($self->last_query);
    $self->_configure_chart($chart);
    my ($fh, $filename) = File::Temp::tempfile(SUFFIX => '.png', UNLINK => 1);
    $chart->draw;
    $fh->write($chart->rendered_data);
    $fh->flush;
    Browser::Open::open_browser("file://$filename");
}

sub size ($self, $size) {
    my ($width, $height) = split ' ', $size;
    $self->chart_options->{width} = $width;
    $self->chart_options->{height} = $height;
}

sub title ($self, $title) {
    $self->chart_options->{title} = $title;
}

for my $renderer (qw(Line StackedLine Bar StackedBar Area StackedArea Point)) {
    __PACKAGE__->meta->add_method(lc($renderer) => sub ($self, $args) {
        my $renderer_class = "Chart::Clicker::Renderer::$renderer";
        Module::Runtime::require_module($renderer_class);
        $self->chart_options->{set_renderer} = $renderer_class->new;
        $self->_draw;
    })
}

sub _configure_chart ($self, $chart) {
    $chart->get_context('default')->range_axis->tick_division_type("LinearRounded");
    for my $opt (keys $self->chart_options->%*) {
        $chart->$opt($self->chart_options->{$opt})
            if $chart->can($opt);
    }
}

sub run ($self) {
    while (1) {
        if (defined(my $input = $self->rl->readline("> "))) {
            next unless length($input);
            chomp($input);
            last if $input eq 'exit';
            my ($command, $args) = split(' ', $input, 2);
            $self->$command($args);
        }
        else {
            print "\n";
            last;
        }
    }
}

sub DEMOLISH ($self, $igd) {
    $self->rl->WriteHistory($self->history_file);
}

__PACKAGE__->meta->make_immutable;
no Moose;

1;