| RDF-Query documentation | view source | Contained in the RDF-Query distribution. |
new ( $parse_tree )compile ()emit_selectlimit_clauseorder_by_clausevariable_columns ( $var )add_variable_values_joinspatterns2sql ( \@triples, \$level, %args )expr2sql ( $expression, \$level, %args )_mysql_hash ( $data )_mysql_node_hash ( $node )qualify_uri ( $uri )add_function ( $uri, $function )get_function ( $uri )
RDF::Query::Compiler::SQL - Compile a SPARQL query directly to SQL.
This document describes RDF::Query::Compiler::SQL version 2.907.
new ( $parse_tree )Returns a new compiler object.
compile ()Returns a SQL query string for the specified parse tree.
emit_selectReturns a SQL query string representing the query.
limit_clauseReturns a SQL LIMIT clause, or an empty string if the query does not need limiting.
order_by_clauseReturns a SQL ORDER BY clause, or an empty string if the query does not use ordering.
variable_columns ( $var )Given a variable name, returns the set of column aliases that store the values for the column (values for Literals, URIs, and Blank Nodes).
add_variable_values_joinsModifies the query by adding LEFT JOINs to the tables in the database that contain the node values (for literals, resources, and blank nodes).
patterns2sql ( \@triples, \$level, %args )Builds the SQL query in instance data from the supplied @triples.
$level is used as a unique identifier for recursive calls.
%args may contain callback closures for the following keys:
'where_hook' 'from_hook'
When present, these closures are used to add SQL FROM and WHERE clauses to the query instead of adding them directly to the object's instance data.
expr2sql ( $expression, \$level, %args )Returns a SQL expression for the supplied query $expression.
$level is used as a unique identifier for recursive calls.
%args may contain callback closures for the following keys:
'where_hook' 'from_hook'
When present, these closures are used to add necessary SQL FROM and WHERE clauses to the query.
_mysql_hash ( $data )Returns a hash value for the supplied $data string. This value is computed
using the same algorithm that Redland's mysql storage backend uses.
_mysql_node_hash ( $node )Returns a hash value (computed by _mysql_hash for the supplied $node.
The hash value is based on the string value of the node and the node type.
qualify_uri ( $uri )Returns a fully qualified URI from the supplied $uri. $uri may already
be a qualified URI, or a parse tree for a qualified URI or QName. If $uri is
a QName, the namespaces defined in the query parse tree are used to fully qualify.
add_function ( $uri, $function )Associates the custom function $function (a CODE reference) with the
specified URI, allowing the function to be called by query FILTERs.
get_function ( $uri )If $uri is associated with a query function, returns a CODE reference
to the function. Otherwise returns undef.
=cut
sub get_function { my $self = shift; my $uri = shift;
our %functions;
my $func = $self->{'functions'}{$uri} || $functions{ $uri };
return $func;
}
our %functions; BEGIN { $functions{ 'sparql:regex' } = sub { my $self = shift; my $parsed_vars = shift; my $level = shift || \do{ my $a = 0 }; my @args = @_; my (@from, @where);
my (@regex, @literal, @pattern);
if (blessed($args[0]) and $args[0]->isa('RDF::Query::Node::Variable')) {
my $name = $args[0]->name;
push(@literal, "${name}_Value");
push(@literal, "${name}_URI");
push(@literal, "${name}_Name");
} else {
push(@literal, $self->expr2sql( $args[0], $level ));
}
if ($args[1][0] eq 'VAR') {
my $name = $args[0][1];
push(@pattern, "${name}_Value");
push(@pattern, "${name}_URI");
push(@pattern, "${name}_Name");
} else {
push(@pattern, $self->expr2sql( $args[1], $level ));
}
foreach my $literal (@literal) {
foreach my $pattern (@pattern) {
push(@regex, sprintf(qq(%s REGEXP %s), $literal, $pattern));
}
}
push(@where, '(' . join(' OR ', @regex) . ')');
return ({}, \@from, \@where);
};
$functions{ 'sparql:bound' } = sub {
my $self = shift;
my $parsed_vars = shift;
my $level = shift || \do{ my $a = 0 };
my @args = @_;
my (@from, @where);
my $literal = $self->expr2sql( $args[0], $level );
push(@where, sprintf(qq(%s IS NOT NULL), $literal));
return ({}, \@from, \@where);
};
$functions{ 'rdfquery:isNotBound' } = sub {
my $self = shift;
my $parsed_vars = shift;
my $level = shift || \do{ my $a = 0 };
my @args = @_;
my (@from, @where);
my $literal = $self->expr2sql( $args[0], $level );
push(@where, sprintf(qq(%s IS NULL), $literal));
return ({}, \@from, \@where);
};
$functions{ 'http://www.w3.org/2001/XMLSchema#integer' } = sub {
my $self = shift;
my $parsed_vars = shift;
my $level = shift || \do{ my $a = 0 };
my @args = @_;
my (@from, @where);
my $literal = $self->expr2sql( $args[0], $level );
push(@where, sprintf(qq((0 + %s)), $literal));
return ({}, \@from, \@where);
};
$functions{ 'http://www.w3.org/2001/XMLSchema#double' } =
$functions{ 'http://www.w3.org/2001/XMLSchema#decimal' } = sub {
my $self = shift;
my $parsed_vars = shift;
my $level = shift || \do{ my $a = 0 };
my @args = @_;
my (@from, @where);
if ($args[0] eq 'FUNCTION') {
Carp::confess;
}
my $literal = $self->expr2sql( $args[0], $level );
push(@where, sprintf(qq((0.0 + %s)), $literal));
return ({}, \@from, \@where);
};
}
1;
__END__
Gregory Williams <gwilliams@cpan.org>
| RDF-Query documentation | view source | Contained in the RDF-Query distribution. |