From: Justin Hunter Date: Tue, 16 Jun 2009 00:11:44 +0000 (-0700) Subject: some dbi parser magic X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b8657f04a19c7114a0f05b9acfc26fc9cd3589c5;p=dbsrgits%2FSQL-Translator-2.0-ish.git some dbi parser magic --- diff --git a/lib/SQL/Translator/Parser/DBI.pm b/lib/SQL/Translator/Parser/DBI.pm new file mode 100644 index 0000000..3c6adf0 --- /dev/null +++ b/lib/SQL/Translator/Parser/DBI.pm @@ -0,0 +1,74 @@ +package SQL::Translator::Parser::DBI; +use Class::MOP; +use Moose; +use MooseX::Types::Moose qw(Str); +use SQL::Translator::Types qw(DBIHandle); +use DBI::Const::GetInfoType; +extends 'SQL::Translator::Parser'; + +has 'dbh' => ( + is => 'rw', + isa => DBIHandle, + required => 1 +); + +has 'translator' => ( + is => 'rw', + does => 'SQL::Translator::Parser::DBI::Dialect', + handles => { + make_create_string => 'make_create_string', + make_update_string => 'make_update_string' + } +); + +has 'db_schema' => ( + is => 'rw', + isa => Str, + lazy => 1, + required => 1, + default => sub { shift->translator->db_schema } +); + +has 'quoter' => ( + is => 'rw', + isa => Str, + requried => 1, + default => q{"} +); + +has 'namesep' => ( + is => 'rw', + isa => Str, + required => 1, + default => '.' +); + +sub BUILD { + my $self = shift; + + local $self->dbh->{RaiseError} = 1; + local $self->dbh->{PrintError} = 0; + + my $dbtypename = $self->dbh->get_info( $GetInfoType{SQL_DBMS_NAME} ) || $self->dbh->{Driver}{Name}; + + my $class = 'SQL::Translator::Parser::DBI::' . $dbtypename; + Class::MOP::load_class( $class ); + my $translator = $class->new( dbh => $self->dbh ); + $self->translator($translator); + + $self->quoter( $self->dbh->get_info(29) || q{"} ); + $self->namesep( $self->dbh->get_info(41) || q{.} ); +} + +sub _tables_list { + my $self = shift; + + my $dbh = $self->dbh; + my @tables = $dbh->tables(undef, $self->db_schema, '%', '%'); + s/\Q$self->quoter\E//g for @tables; + s/^.*\Q$self->namesep\E// for @tables; + + return @tables; +} + +1; diff --git a/lib/SQL/Translator/Parser/DBI/Dialect.pm b/lib/SQL/Translator/Parser/DBI/Dialect.pm new file mode 100644 index 0000000..7bece8d --- /dev/null +++ b/lib/SQL/Translator/Parser/DBI/Dialect.pm @@ -0,0 +1,13 @@ +package SQL::Translator::Parser::DBI::Dialect; +use Moose::Role; + +requires 'make_create_string', + 'make_update_string'; + +sub do_common_stuff { + my ($self, @args) = @_; + print "COMMON STUFF!\n"; + # .... +} + +1; diff --git a/lib/SQL/Translator/Parser/DBI/MySQL.pm b/lib/SQL/Translator/Parser/DBI/MySQL.pm index 993fb6b..afb1405 100644 --- a/lib/SQL/Translator/Parser/DBI/MySQL.pm +++ b/lib/SQL/Translator/Parser/DBI/MySQL.pm @@ -1,5 +1,15 @@ package SQL::Translator::Parser::DBI::MySQL; use Moose; -extends 'SQL::Translator::Parser::DBI'; +with 'SQL::Translator::Parser::DBI::Dialect'; + +sub make_create_string { + print "MYSQL!\n"; + # ..... +} + +sub make_update_string { + print "mYSQL!\n"; +} + 1; diff --git a/lib/SQL/Translator/Parser/DBI/Oracle.pm b/lib/SQL/Translator/Parser/DBI/Oracle.pm index 94fa930..d555f49 100644 --- a/lib/SQL/Translator/Parser/DBI/Oracle.pm +++ b/lib/SQL/Translator/Parser/DBI/Oracle.pm @@ -1,5 +1,15 @@ package SQL::Translator::Parser::DBI::Oracle; use Moose; -extends 'SQL::Translator::Parser::DBI'; +with 'SQL::Translator::Parser::DBI::Dialect'; + +sub make_create_string { + print "Oracle!\n"; + # ..... +} + +sub make_update_string { + print "Oracle!\n"; +} + 1; diff --git a/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm b/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm index 68f68da..53d217d 100644 --- a/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm +++ b/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm @@ -1,5 +1,17 @@ package SQL::Translator::Parser::DBI::PostgreSQL; use Moose; -extends 'SQL::Translator::Parser::DBI'; +use MooseX::Types::Moose qw(Str); +with 'SQL::Translator::Parser::DBI::Dialect'; + +has 'db_schema' => (is => 'ro', isa => Str, default => 'public'); + +sub make_create_string { + print "HELLO WORLD\n"; + # ..... +} + +sub make_update_string { + print "WOOT\n"; +} 1; diff --git a/lib/SQL/Translator/Parser/DBI/SQLite.pm b/lib/SQL/Translator/Parser/DBI/SQLite.pm index 532599f..968acdb 100644 --- a/lib/SQL/Translator/Parser/DBI/SQLite.pm +++ b/lib/SQL/Translator/Parser/DBI/SQLite.pm @@ -1,5 +1,15 @@ package SQL::Translator::Parser::DBI::SQLite; use Moose; -extends 'SQL::Translator::Parser::DBI'; +with 'SQL::Translator::Parser::DBI::Dialect'; + +sub make_create_string { + print "SQLite\n"; + # ..... +} + +sub make_update_string { + print "SQLite\n"; +} + 1; diff --git a/lib/SQL/Translator/Parser/DBI/Sybase.pm b/lib/SQL/Translator/Parser/DBI/Sybase.pm index f8868c9..017047b 100644 --- a/lib/SQL/Translator/Parser/DBI/Sybase.pm +++ b/lib/SQL/Translator/Parser/DBI/Sybase.pm @@ -1,5 +1,15 @@ package SQL::Translator::Parser::DBI::Sybase; use Moose; -extends 'SQL::Translator::Parser::DBI'; +with 'SQL::Translator::Parser::DBI::Dialect'; + +sub make_create_string { + print "Sybase!\n"; + # ..... +} + +sub make_update_string { + print "Sybase!\n"; +} + 1;