The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.
=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>.