Skip to content

Instantly share code, notes, and snippets.

@hoehrmann
Created May 9, 2019 22:33
Show Gist options
  • Save hoehrmann/e3ed3f810c635003e0be2bf3a77a85a8 to your computer and use it in GitHub Desktop.
Save hoehrmann/e3ed3f810c635003e0be2bf3a77a85a8 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use JSON;
use YAML::XS;
my $dbh = DBI->connect('dbi:SQLite:dbname=:memory:');
our $Arg;
$dbh->sqlite_create_function('param', 0, sub {
return $Arg;
});
$dbh->sqlite_create_function('eval', 2, sub {
my ($sql, $arg) = @_;
local $Arg = $arg;
my $result = $dbh->selectall_arrayref($sql, { Slice => {} });
return JSON->new->encode($result);
});
$dbh->do(q{
CREATE VIEW view_with_param AS
SELECT 'first row' AS name, param() AS param
UNION ALL
SELECT 'second row' AS name, param() AS param
});
print Dump (
$dbh->selectall_arrayref(q{
SELECT * FROM (SELECT eval(
'SELECT * FROM view_with_param',
'[1,2,3]'
))
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment