Skip to content

Instantly share code, notes, and snippets.

@hercynium
Created August 28, 2012 19:29
Show Gist options
  • Save hercynium/3503052 to your computer and use it in GitHub Desktop.
Save hercynium/3503052 to your computer and use it in GitHub Desktop.
I am a terrible, horrible person
use feature ':5.10.0';
package Foo;
use Text::Template;
# needs to be in scope for Text::Template's processing
my %data;
sub get_metadata {
my ($tmpl) = @_;
return Bar::get_metadata($tmpl);
}
sub fill {
my ($tmpl, $user_input) = @_;
my $meta = get_metadata($tmpl);
for my $var_name (keys %{ $meta->{vars} || {} } ) {
my $default = $meta->{vars}{default};
$data{vars}{$var_name} = $user_input->{vars}{$var_name} // $default;
}
$data{select} = $user_input->{select};
$data{where} = $user_input->{where};
$data{where_mode} = $user_input->{where_mode};
$data{limit} = $user_input->{limit};
my $tt = Text::Template->new(TYPE => 'STRING', SOURCE => $tmpl );
# strip out blank lines
(my $new_hql = $tt->fill_in) =~ s/^\s*\n//gms;
return $new_hql;
}
sub insert_var {
my ($name, %opt) = @_;
return $data{vars}{$name};
}
sub insert_select {
my (%opt) = @_;
return unless $data{select};
}
sub insert_where {
my (%opt) = @_;
return unless $data{where};
my $mode = $data{where_mode} || 'AND';
my $where_clause = join " $mode ", map { join ' ', @$_ } @{$data{where}};
return unless $where_clause;
return "WHERE $where_clause";
}
sub insert_limit {
my (%opt) = @_;
return unless $data{limit};
return "LIMIT $data{limit}";
}
sub append_group_by {
my (%opt) = @_;
return unless $data{group_by};
my $clause = ", " . join ", ", @{$data{group_by}};
return $clause;
}
package Bar;
# same deal as %data, above
my %metadata;
sub get_metadata {
my ($tmpl) = @_;
my $tt = Text::Template->new(TYPE => 'STRING', SOURCE => $tmpl );
$tt->fill_in;
return \%metadata;
}
sub insert_var {
my ($name, %opt) = @_;
$metadata{vars}{$name} ||= \%opt;
return;
}
sub insert_select {
my (%opt) = @_;
return;
}
sub insert_where {
my (%opt) = @_;
$metadata{where_columns} ||= $opt{columns};
return;
}
sub insert_limit {
my (%opt) = @_;
$metadata{limit} ||= \%opt;
return;
}
sub append_group_by {
my (%opt) = @_;
$metadata{group_columns} ||= $opt{columns};
return;
}
1 && q{ I clearly have no shame }; # truth
SELECT g_vw.ds,
g_vw.locale,
round(((SUM(
CASE
WHEN g_vw.au_first_pageview_ts >= pg_vw.last_pageview_ts THEN 1
ELSE 0
END
) / COUNT(*)) * 100.0), 3) as bounce_rate
FROM vw_discoverau_general g_vw
JOIN (
SELECT fp.ds,
fp.locale,
fp.session_id,
MAX( fp.request_finished ) as last_pageview_ts
FROM f_pageviews_hist fp
WHERE fp.ds ='{{ insert_var('view_date', default => '2012-08-22') }}'
GROUP BY fp.ds, fp.locale, fp.session_id
) pg_vw
ON (
g_vw.ds = '{{ insert_var('view_date') }}' AND g_vw.ds = pg_vw.ds AND
g_vw.locale = pg_vw.locale AND g_vw.session_id = pg_vw.session_id )
{{
insert_where( columns => [qw/g_vw.locale pg_vw.session_id/] );
}}
GROUP BY g_vw.ds, g_vw.locale
{{ append_group_by(columns => [qw/fp.session_id/]) }}
{{ insert_limit() }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment