Tangram::Dialect(3pm)

Tangram::Dialect3pm

Tangram::Dialect - support for SQL dialects and extensions

   package Tangram::Dialect::SomeDialect;
   use Tangram::Dialect;
   use base qw( Tangram::Dialect );
   sub expr
   {
      ...
   }

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.
new() Returns a new Dialect object.
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. 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);

Tangram::Type, Tangram::Expr, Tangram::Storage.

Releases

2 releases since Debian 3.0 (Woody); the text changed 1 time.

  1. 3.0
  2. 3.1

From libtangram-perl 2.07-1 in Debian 3.1 (Sarge). Parsed by carta from the page's mdoc source.