/usr/share/perl5/DBIx/DR/PlPlaceHolders.pm is in libdbix-dr-perl 0.26-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | use utf8;
use strict;
use warnings;
package DBIx::DR::PlPlaceHolders;
use Mouse;
extends 'DBIx::DR::PerlishTemplate';
use DBIx::DR::ByteStream;
use Carp ();
use File::Spec ();
use Digest::MD5 ();
use Encode qw(encode);
has sql_dir => (is => 'bare', isa => 'Str');
has file_suffix => (is => 'rw', isa => 'Str', default => '.sql.ep');
has helpers => (is => 'ro', isa => 'HashRef', default => sub {{}});
sub sql_dir {
my ($self, $dir) = @_;
if (defined $dir) {
Carp::croak "Diectory $dir is not found or a dir" unless -d $dir;
$self->{sql_dir} = File::Spec->rel2abs($dir);
} elsif(@_ >= 2) {
$self->{sql_dir} = undef;
}
return $self->{sql_dir};
}
sub BUILD {
my ($self) = @_;
# add default helpers
$self->set_helper(
include => sub {
my ($tpl, $file, @args) = @_;
my $res = ref($self)->new(
pretokens => $self->prepretokens,
prepretokens => $self->prepretokens,
helpers => $self->helpers,
sql_dir => $self->sql_dir,
file_suffix => $self->file_suffix,
)->sql_transform(
-f => $file,
@args
);
$tpl->immediate($res->sql);
$tpl->add_bind_value($res->bind_values);
return DBIx::DR::ByteStream->new('');
},
list => sub {
my ($tpl, @args) = @_;
$tpl->immediate(join ',' => map '?', @args);
$tpl->add_bind_value(@args);
return DBIx::DR::ByteStream->new('');
},
hlist => sub {
my ($tpl, @args) = @_;
if ('ARRAY' eq ref $args[0]) {
my $filter = shift @args;
$tpl->immediate(
join ',' => (
'(' .
join(',' => ('?')x @$filter) .
')'
)x @args
);
for my $a (@args) {
$tpl->add_bind_value( map { $a->{$_} } @$filter );
}
return DBIx::DR::ByteStream->new('');
}
$tpl->immediate(
join ',' => map {
'(' .
join(',' => ('?') x keys %$_) .
')'
} @args
);
$tpl->add_bind_value(map { values %$_ } @args);
return DBIx::DR::ByteStream->new('');
},
);
$self;
}
sub sql_transform {
my $self = shift;
my ($sql, %opts);
my $pt;
if (@_ % 2) {
($sql, %opts) = @_;
delete $opts{-f};
} else {
%opts = @_;
Carp::croak $self->usage unless $opts{-f};
my $file = $opts{-f};
$file = File::Spec->catfile($self->sql_dir, $file)
if $self->sql_dir and $file !~ m{^/};
my $resuffix = quotemeta $self->file_suffix;
$file .= $self->file_suffix
if $self->file_suffix and $file !~ /$resuffix$/;
my @fstat = stat $file;
Carp::croak "Can't find file $file" unless @fstat;
$opts{-f} = $file;
}
my $namespace = $opts{-f} || $sql;
$namespace = encode utf8 => $namespace if utf8::is_utf8($namespace);
$namespace = Digest::MD5::md5_hex($namespace);
$self->{namespace} = __PACKAGE__ . '::Sandbox::t' . $namespace;
$self -> clean_prepends
-> clean_preprepends
;
for my $name (keys %{ $self->helpers }) {
$self->preprepend(
'BEGIN{ ' .
"*" . $name . '= sub {' .
'$_PTPL->call_helper(q{' . $name . '}, @_)' .
'} ' .
'}'
);
}
my @args;
for (keys %opts) {
next unless /^\w/;
$self->prepend("my \$$_ = shift");
push @args, $opts{$_};
}
if ($sql) {
$self->render($sql, @args);
} else {
$self->render_file($opts{-f}, @args);
}
my $res =
DBIx::DR::PlPlaceHolders::TransformResult->new(rtemplate => $self);
# clean memory
$self->{sql} = '';
$self->{variables} = [];
$res;
}
sub call_helper {
my ($self, $name, @args) = @_;
Carp::croak "Helper '$name' is not found or has already been removed"
unless exists $self->helpers->{ $name };
$self->helpers->{ $name }->($self, @args);
}
sub set_helper {
my ($self, %opts) = @_;
Carp::croak $self->usage unless %opts;
while (my ($n, $s) = each %opts) {
Carp::croak $self->usage unless 'CODE' eq ref $s and $n =~ /^\w/;
$self->helpers->{ $n } = $s ;
}
$self;
}
sub usage {
my ($self) = @_;
my @caller = caller 1;
return 'Usage: $ph->sql_transform($sql | -f => $sql_file, ...)'
if $caller[3] =~ /sql_transform$/;
return 'Usage: $ph->set_helper($name => sub { ... })'
if $caller[3] =~ /set_helper$/;
return $caller[3];
}
package DBIx::DR::PlPlaceHolders::TransformResult;
use Mouse;
has rtemplate => (is => 'ro', isa => 'Object', weak_ref => 1);
has sql => (is => 'ro', isa => 'Str');
sub BUILD {
my ($self) = @_;
$self->{sql} = $self->rtemplate->sql;
$self->{bind_values} = $self->rtemplate->variables;
}
sub bind_values {
my ($self) = @_;
return @{ $self->{bind_values} } if wantarray;
return $self->{bind_values} || [];
}
1;
=head1 NAME
DBIx::DR::PlPlaceHolders - template converter for L<DBIx::DR>.
=head1 COPYRIGHT
Copyright (C) 2011 Dmitry E. Oboukhov <unera@debian.org>
Copyright (C) 2011 Roman V. Nikolaev <rshadow@rambler.ru>
This program is free software, you can redistribute it and/or
modify it under the terms of the Artistic License.
=cut
|