NAME
Tangram::Dialect - support for SQL dialects and extensions
SYNOPSIS
package Tangram::Dialect::SomeDialect;
use Tangram::Dialect;
use base qw( Tangram::Dialect );
sub expr
{
...
}
DESCRIPTION
Like Charles Moore (inventor of Forth) used to say, "standards are great, everybody should have one!".
Tangram can take advantage of extensions available in some SQL dialects. Each Storage has an associated Dialect object. The instantiation of some classes (currently Expr) is delegated to the Dialect object, which may return objects with extended functionality.
CLASS METHODS
new()
Returns a new Dialect object.
INSTANCE METHODS
expr($type, $expr, @remotes)
Returns a new Expr object. The object is obtained by calling method expr() on $type. See Tangram::Expr for a description of the arguments.
EXAMPLE
The following code adds support for Sybase's datepart
extension. See below how to actually use the extension.
use strict;
############################################
# derive a DateExpr class from existing Expr
package Tangram::Dialect::Sybase::DateExpr;
use base qw( Tangram::Expr );
############################
# add method datepart($part)
sub datepart
{
my ($self, $part) = @_; # $part is 'year', 'month', etc
my $expr = $self->expr(); # the SQL string for this Expr
##################################
# build a new Expr of Integer type
# pass this Expr's remote object list to the new Expr
return Tangram::Integer->expr("DATEPART($part, $expr)", $self->objects);
}
###########################
# subclass Tangram::Dialect
package Tangram::Dialect::Sybase;
use Tangram::Dialect;
use base qw( Tangram::Dialect );
############################################
# a hash that maps date-related Types to the
# DateExpr - the improved Expr class
my %improved =
(
'Tangram::RawDateTime' => 'Tangram::Dialect::Sybase::DateExpr',
'Tangram::RawDate' => 'Tangram::Dialect::Sybase::DateExpr',
);
######################################################
# Tangram calls this method to obtain new Expr objects
sub expr
{
my ($self, $type, $expr, @remotes) = @_;
###################################################
# is $type related to dates? if not, return default
my $improved = $improved{ref($type)} or return $type->expr(@_);
####################################
# $type is a Date; return a DateExpr
return $improved->new($type, $expr, @remotes);
}
1;
To take advantage of the new Dialect, we must pass it to the connect() method:
my $storage = Tangram::Storage->connect($schema,
$data_source, $user, $passwd,
{ dialect => 'Tangram::Dialect::Sybase' } );
Now we can filter on the various parts of a Date:
my $remote = $storage->remote('NaturalPerson');
my ($person) = $storage->select($remote,
$remote->{birth}->datepart('year') == 1963);