1package SPOPS::Import::DBI::TableTransform; 2 3# $Id: TableTransform.pm,v 3.7 2004/06/02 00:48:23 lachoy Exp $ 4 5use strict; 6use base qw( Class::Factory ); 7use SPOPS::Exception; 8 9$SPOPS::Import::DBI::TableTransform::VERSION = sprintf("%d.%02d", q$Revision: 3.7 $ =~ /(\d+)\.(\d+)/); 10 11sub transform { 12 my ( $self, $sql ) = @_; 13 $self->increment( $sql ); 14 $self->increment_type( $sql ); 15 $self->datetime( $sql ); 16} 17 18my %TYPES = ( 19 mysql => 'SPOPS::Import::DBI::TableTransform::MySQL', 20 MySQL => 'SPOPS::Import::DBI::TableTransform::MySQL', 21 oracle => 'SPOPS::Import::DBI::TableTransform::Oracle', 22 Oracle => 'SPOPS::Import::DBI::TableTransform::Oracle', 23 pg => 'SPOPS::Import::DBI::TableTransform::Pg' , 24 Pg => 'SPOPS::Import::DBI::TableTransform::Pg' , 25 postgres => 'SPOPS::Import::DBI::TableTransform::Pg', 26 asany => 'SPOPS::Import::DBI::TableTransform::Sybase', 27 ASAny => 'SPOPS::Import::DBI::TableTransform::Sybase', 28 MSSQL => 'SPOPS::Import::DBI::TableTransform::Sybase', 29 mssql => 'SPOPS::Import::DBI::TableTransform::Sybase', 30 sybase => 'SPOPS::Import::DBI::TableTransform::Sybase', 31 Sybase => 'SPOPS::Import::DBI::TableTransform::Sybase', 32 sqlite => 'SPOPS::Import::DBI::TableTransform::SQLite', 33 SQLite => 'SPOPS::Import::DBI::TableTransform::SQLite', 34 interbase => 'SPOPS::Import::DBI::TableTransform::InterBase', 35 InterBase => 'SPOPS::Import::DBI::TableTransform::InterBase', 36 Firebird => 'SPOPS::Import::DBI::TableTransform::InterBase', 37); 38 39sub class_initialize { 40 while ( my ( $type, $class ) = each %TYPES ) { 41 __PACKAGE__->register_factory_type( $type, $class ); 42 } 43} 44 45class_initialize(); 46 471; 48 49__END__ 50 51=head1 NAME 52 53SPOPS::Import::DBI::TableTransform - Factory class for database-specific transformations 54 55=head1 SYNOPSIS 56 57 my $table = qq/ CREATE TABLE blah ( id %%INCREMENT%% primary key, 58 name varchar(50) ) /; 59 my $transformer = SPOPS::Import::DBI::TableTransform->new( 'sybase' ); 60 $transformer->increment( \$table ); 61 print $table; 62 63=head1 DESCRIPTION 64 65This class is a factory class for database-specific 66transformations. This means that 67L<SPOPS::Import::DBI::Table|SPOPS::Import::DBI::Table> supports 68certain keys that can be replaced by database-specific values. This 69class is a factory for objects that take SQL data and do the 70replacements. 71 72=head1 METHODS 73 74B<new( $database_type )> 75 76Create a new transformer using the database type C<$database_type>. 77 78Available database types are: 79 80=over 4 81 82=item asany: Sybase Adaptive Server Anywhere 83 84=item interbase: InterBase family (also: 'firebird') 85 86=item mssql: Microsoft SQL Server 87 88=item mysql: MySQL 89 90=item oracle: Oracle 91 92=item postgres: PostgreSQL (also: 'pg') 93 94=item sybase: Sybase SQL Server/ASE 95 96=back 97 98B<register_factory_type( $database_type, $transform_class )> 99 100Registers a new database type for a transformation class. You will 101need to run this every time you run the program. 102 103If you develop a transformation class for a database not represented 104here, please email the author so it can be included with future 105distributions. 106 107=head1 CREATING A TRANSFORMATION CLASS 108 109Creating a new subclass is extremely easy. You just need to subclass 110this class, then create a subroutine for each of the built-in 111transformations specified in 112L<SPOPS::Import::DBI::Table|SPOPS::Import::DBI::Table>. 113 114Each transformation takes two arguments: C<$self> and a scalar 115reference to the SQL to be transformed. For example, here is a 116subclass for a made-up database: 117 118 package SPOPS::Import::DBI::TableTransform::SavMor; 119 120 use strict; 121 use base qw( SPOPS::Import::DBI::TableTransform ); 122 123 sub increment { 124 my ( $self, $sql ) = @_; 125 $$sql =~ s/%%INCREMENT%%/UNIQUE_VALUE/g; 126 } 127 128 sub increment_type { 129 my ( $self, $sql ) = @_; 130 $$sql =~ s/%%INCREMENT_TYPE%%/INT/g; 131 } 132 133 sub datetime { 134 my ( $self, $sql ) = @_; 135 $$sql =~ s/%%DATETIME%%/timestamp/g; 136 } 137 138 1; 139 140And then we could register the transformation agent with every run: 141 142 SPOPS::Import::DBI::TableTransform->register_factory_type( 143 'savmor', 'SPOPS::Import::DBI::TableTransform::SavMor' ); 144 my $transformer = SPOPS::Import::DBI::TableTransform->new( 'savmor' ); 145 my $sql = qq/ CREATE TABLE ( id %%INCREMENT%% primary key ) /; 146 $transformer->increment( \$sql ); 147 print $sql; 148 149Output: 150 151 CREATE TABLE ( id UNIQUE_VALUE primary key ) 152 153=head1 BUGS 154 155None known. 156 157=head1 TO DO 158 159Nothing known. 160 161=head1 SEE ALSO 162 163L<SPOPS::Import::DBI::Table|SPOPS::Import::DBI::Table> 164 165=head1 COPYRIGHT 166 167Copyright (c) 2001-2004 intes.net, inc.. All rights reserved. 168 169This library is free software; you can redistribute it and/or modify 170it under the same terms as Perl itself. 171 172=head1 AUTHORS 173 174Chris Winters E<lt>chris@cwinters.comE<gt> 175