AtteanX-Store-DBI
view release on metacpan or search on metacpan
lib/AtteanX/Store/DBI.pm view on Meta::CPAN
}
=item C<< available_database_types >>
Returns the names of the database types for which the system has schemas
available to create and drop quadstore tables.
=cut
sub available_database_types {
my $self = shift;
my $dir = $ENV{ATTEAN_SHAREDIR} || eval { dist_dir('AtteanX-Store-DBI') } || 'share';
my $pat = File::Spec->catfile($dir, 'database-schema', '*-create.sql');
my @files = glob($pat);
my @types = map { /(\w+)-create.sql/ } @files;
return @types;
}
=item C<< dbi_connect_args ( $type, %args ) >>
=item C<< dbi_connect_args ( %args ) >>
Returns a quad C<< $dsn, $user, $password, \%connect_args >> suitable for
passing to C<< DBI->connect >> to obtain a database handle to be used in
constructing a C<< AtteanX::Store::DBI >> quadstore.
C<< %args >> must contain a value for the C<< database >> key. It may also
contain values for the optional keys: C<< user >>, C<< password >>,
C<< host >>, and C<< port >>.
If invoked as a class method, the C<< $type >> parameter is required, and must
be one of the database types returned by C<< available_database_types >>.
If invoked as an object method, the C<< $type >> parameter must not be
included; this information will be obtained directly from the
C<< AtteanX::Store::DBI >> object.
=cut
sub dbi_connect_args {
my $self = shift;
my $type = blessed($self) ? $self->database_type : shift;
my %args = @_;
my $database = $args{database};
my $user = $args{user};
my $password = $args{password};
my $host = $args{host};
my $port = $args{port};
my $dsn;
my %connect_args;
$connect_args{RaiseError} = 1;
if ($type eq 'mysql') {
$dsn = "DBI:mysql:database=${database}";
if (defined($host)) {
$dsn .= ";host=$host";
}
if (defined($port)) {
$dsn .= ";port=$port";
}
$connect_args{mysql_enable_utf8} = 1;
} elsif ($type eq 'postgresql') {
$dsn = "DBI:Pg:dbname=${database}";
if (defined($host)) {
$dsn .= ";host=$host";
}
if (defined($port)) {
$dsn .= ";port=$port";
}
} elsif ($type eq 'sqlite') {
$dsn = "DBI:SQLite:dbname=${database}";
$connect_args{sqlite_unicode} = 1;
}
return ($dsn, $user, $password, \%connect_args);
}
=item C<< plans_for_algebra( $algebra, $model, $active_graphs, $default_graphs ) >>
For BGP algebras, returns a DBI-specific L<Attean::API::Plan> object, otherwise
returns undef.
=cut
sub plans_for_algebra {
my $self = shift;
my $algebra = shift;
my $model = shift;
my $active_graphs = shift;
my $default_graphs = shift;
return unless ($algebra);
my %args = @_;
my $counter = $args{dbi_filter_counter}++;
if ($algebra->isa('Attean::Algebra::Filter')) {
my $e = $algebra->expression;
if ($e->isa('Attean::FunctionExpression')) {
if ($e->operator =~ m/IS(IRI|LITERAL|BLANK)/i) {
my $type = lc($1);
my ($operand) = @{ $e->children };
if ($operand->isa('Attean::ValueExpression') and $operand->value->does('Attean::API::Variable')) {
my $var = $operand->value;
if (my ($plan) = $self->plans_for_algebra($algebra->child, $model, $active_graphs, $default_graphs, %args)) {
if ($plan->isa('AtteanX::Store::DBI::Plan')) {
if (exists $plan->variables->{ $var->value }) {
my ($table, $col) = @{ $plan->variables->{ $var->value } };
my $ref = join('.', map { $self->dbh->quote_identifier($_) } ($table, $col));
my $typecol = $self->dbh->quote_identifier('type');
push(@{ $plan->where }, "$ref IN (SELECT term_id FROM term WHERE ${typecol} = ?)");
push(@{ $plan->bindings }, $type);
return $plan;
}
}
}
}
} elsif ($e->operator eq 'STRSTARTS' or $e->operator eq 'CONTAINS') {
my ($varexpr, $pat) = @{ $e->children };
if ($varexpr->isa('Attean::ValueExpression') and $varexpr->value->does('Attean::API::Variable') and $pat->isa('Attean::ValueExpression') and $pat->value->does('Attean::API::Literal')) {
if (my ($plan) = $self->plans_for_algebra($algebra->child, $model, $active_graphs, $default_graphs, %args)) {
if ($plan->isa('AtteanX::Store::DBI::Plan')) {
( run in 1.874 second using v1.01-cache-2.11-cpan-39bf76dae61 )