DBIx-DBStag
view release on metacpan or search on metacpan
DBIx/DBStag.pm view on Meta::CPAN
chomp;
next if /^\#/;
s/^\!//;
my @parts =split(' ', $_);
next unless (@parts >= 3);
my ($name, $type, $loc, $tagstr) =@parts;
my %tagh = ();
if ($tagstr) {
my @parts = split(/;\s*/, $tagstr);
foreach (@parts) {
my ($t, $v) = split(/\s*=\s*/, $_);
$tagh{$t} = $v;
}
}
$rh->{$name} =
{
%tagh,
name=>$name,
type=>$type,
loc=>$loc,
tagstr=>$tagstr,
};
}
close(F) || $self->throw("Cannot close $mapf");
} else {
$self->throw("$mapf does not exist");
}
}
return $rh;
}
sub resources_list {
my $self = shift;
my $rh =
$self->resources_hash;
my $rl;
if ($rh) {
$rl =
[map {$_} values %$rh];
}
return $rl;
}
sub find_template {
my $self = shift;
my $tname = shift;
my $path = $ENV{DBSTAG_TEMPLATE_DIRS} || '';
my $tl = $self->template_list;
my ($template, @rest) = grep {$tname eq $_->name} @$tl;
if (!$template) {
print STDERR "\n\nI could not find the Stag SQL template called \"$tname\".\n";
if (!$path) {
print STDERR <<EOM1
In order to do use this or any other template, you need to set the environment
variable DBSTAG_TEMPLATE_DIRS to the directory or a set of directories
containing SQL templates. For example
setenv DBSTAG_TEMPLATE_DIRS=".:\$HOME/my-sql-templates:/usr/share/system-sql-templates"
EOM1
;
}
else {
print STDERR <<EOM2
I am looking in the following directories:
$path
Check the contents of the directory to see if the stag sql template
you require is there, and is readable by you. Stag SQL templates
should end with the suffix ".stg"
If you wish to search other directories, set the environment variable
DBSTAG_TEMPLATE_DIRS, like this:
setenv DBSTAG_TEMPLATE_DIRS=".:\$HOME/my-sql-templates:$path"
EOM2
;
}
$self->throw("Could not find template \"$tname\" in: $path");
}
return $template;
}
sub find_templates_by_schema {
my $self = shift;
my $schema = shift;
my $tl = $self->template_list;
my @templates = grep {$_->stag_props->tmatch('schema', $schema)} @$tl;
return \@templates;
}
sub find_templates_by_dbname {
my $self = shift;
my $dbname = shift;
my $res = $self->resources_hash->{$dbname};
my $templates;
if ($res) {
my $schema = $res->{schema} || '';
if ($schema) {
$templates = $self->find_templates_by_schema($schema);
}
else {
# unknown schema - show all templates
# $templates = $self->template_list;
}
}
else {
$self->throw("unknown db: $dbname");
}
return $templates;
}
sub template_list {
my $self = shift;
my %already_got = ();
if (!$self->{_template_list}) {
my $path = $ENV{DBSTAG_TEMPLATE_DIRS} || '.';
my @dirs = split(/:/, $path);
my @templates = ();
foreach my $dir (@dirs) {
foreach my $fn (glob("$dir/*.stg")) {
if (-f $fn) {
require "DBIx/DBStag/SQLTemplate.pm";
my $template = DBIx::DBStag::SQLTemplate->new;
$template->parse($fn);
push(@templates, $template) unless $already_got{$template->name};
$already_got{$template->name} = 1;
}
}
}
$self->{_template_list} = \@templates;
( run in 1.044 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )