SPOPS::Import::DBI::TableTransform - Factory class for database-specific transformations
my $table = qq/ CREATE TABLE blah ( id %%INCREMENT%% primary key, name varchar(50) ) /; my $transformer = SPOPS::Import::DBI::TableTransform->new( 'sybase' ); $transformer->increment( \$table ); print $table;
This class is a factory class for database-specific transformations. This means that SPOPS::Import::DBI::Table supports certain keys that can be replaced by database-specific values. This class is a factory for objects that take SQL data and do the replacements.
new( $database_type )
Create a new transformer using the database type $database_type.
$database_type
Available database types are:
register_factory_type( $database_type, $transform_class )
Registers a new database type for a transformation class. You will need to run this every time you run the program.
If you develop a transformation class for a database not represented here, please email the author so it can be included with future distributions.
Creating a new subclass is extremely easy. You just need to subclass this class, then create a subroutine for each of the built-in transformations specified in SPOPS::Import::DBI::Table.
Each transformation takes two arguments: $self and a scalar reference to the SQL to be transformed. For example, here is a subclass for a made-up database:
$self
package SPOPS::Import::DBI::TableTransform::SavMor; use strict; use base qw( SPOPS::Import::DBI::TableTransform ); sub increment { my ( $self, $sql ) = @_; $$sql =~ s/%%INCREMENT%%/UNIQUE_VALUE/g; } sub increment_type { my ( $self, $sql ) = @_; $$sql =~ s/%%INCREMENT_TYPE%%/INT/g; } sub datetime { my ( $self, $sql ) = @_; $$sql =~ s/%%DATETIME%%/timestamp/g; } 1;
And then we could register the transformation agent with every run:
SPOPS::Import::DBI::TableTransform->register_factory_type( 'savmor', 'SPOPS::Import::DBI::TableTransform::SavMor' ); my $transformer = SPOPS::Import::DBI::TableTransform->new( 'savmor' ); my $sql = qq/ CREATE TABLE ( id %%INCREMENT%% primary key ) /; $transformer->increment( \$sql ); print $sql;
Output:
CREATE TABLE ( id UNIQUE_VALUE primary key )
None known.
Nothing known.
SPOPS::Import::DBI::Table
Copyright (c) 2001-2004 intes.net, inc.. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Chris Winters <chris@cwinters.com>
To install SPOPS, copy and paste the appropriate command in to your terminal.
cpanm
cpanm SPOPS
CPAN shell
perl -MCPAN -e shell install SPOPS
For more information on module installation, please visit the detailed CPAN module installation guide.