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
|
=head1 NAME
Tangram::Dialect - support for SQL dialects and extensions
=head1 SYNOPSIS
package Tangram::Dialect::SomeDialect;
use Tangram::Dialect;
use base qw( Tangram::Dialect );
sub expr
{
...
}
=head1 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.
=head1 CLASS METHODS
=head2 new()
Returns a new Dialect object.
=head1 INSTANCE METHODS
=head2 expr($type, $expr, @remotes)
Returns a new Expr object. The object is obtained by calling method
expr() on $type. See L<Tangram::Expr> for a description of the
arguments.
=head1 EXAMPLE
The following code adds support for Sybase's C<datepart>
extension. See below how to actually I<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);
=head1 SEE ALSO
L<Tangram::Type>, L<Tangram::Expr>, L<Tangram::Storage>.
|