From: Ken Youens-Clark Date: Fri, 3 Oct 2003 00:21:41 +0000 (+0000) Subject: Adding new DBI and experimental DBI::SQLite parsers. X-Git-Tag: v0.04~153 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6a9f7bae6c936d99cc57d57f2a0fb6202860f076;p=dbsrgits%2FSQL-Translator.git Adding new DBI and experimental DBI::SQLite parsers. --- diff --git a/lib/SQL/Translator/Parser/DBI.pm b/lib/SQL/Translator/Parser/DBI.pm new file mode 100644 index 0000000..20a62b4 --- /dev/null +++ b/lib/SQL/Translator/Parser/DBI.pm @@ -0,0 +1,150 @@ +package SQL::Translator::Parser::DBI; + +# ------------------------------------------------------------------- +# $Id: DBI.pm,v 1.1 2003-10-03 00:21:41 kycl4rk Exp $ +# ------------------------------------------------------------------- +# Copyright (C) 2003 Ken Y. Clark , +# darren chamberlain +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; version 2. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +# 02111-1307 USA +# ------------------------------------------------------------------- + +=head1 NAME + +SQL::Translator::Parser::DBI - "parser" for DBI handles + +=head1 SYNOPSIS + + use DBI; + use SQL::Translator; + + my $dbh = DBI->connect(...); + + my $translator = SQL::Translator->new( + parser => 'DBI', + dbh => $dbh, + ); + +Or: + + use SQL::Translator; + + my $translator = SQL::Translator->new( + parser => 'DBI', + dsn => 'dbi:mysql:FOO', + db_user => 'guest', + db_password => 'password', + ); + +=head1 DESCRIPTION + +This parser accepts an open database handle (or the arguments to create +one) and queries the database directly for the information. The correct +SQL::Translator::Parser::DBI class is determined automatically by +inspecting $dbh->{'Driver'}{'Name'}. + +The following are acceptable arguments: + +=over + +=item * dbh + +An open DBI database handle. + +=item * dsn + +The DSN to use for connecting to a database. + +=item * db_user + +The user name to use for connecting to a database. + +=item * db_password + +The password to use for connecting to a database. + +=back + +=cut + +# ------------------------------------------------------------------- + +use strict; +use DBI; +use vars qw($VERSION @EXPORT); +$VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/; + +use constant DRIVERS => { + sqlite => 'SQLite', +}; + +use Exporter; +use SQL::Translator::Utils qw(debug normalize_name); +use SQL::Translator::Parser::DBI::SQLite; + +use base qw(Exporter); +@EXPORT = qw(parse); + +# +# Passed a SQL::Translator instance and a string containing the data +# +sub parse { + my ( $tr, $data ) = @_; + + $tr->debug( "in DBI\n" ); + my $args = $tr->parser_args; + my $dbh = $args->{'dbh'}; + my $dsn = $args->{'dsn'}; + my $db_user = $args->{'db_user'}; + my $db_password = $args->{'db_password'}; + + unless ( $dbh ) { + die 'No DSN' unless $dsn; + $dbh = DBI->connect( $dsn, $db_user, $db_password, {RaiseError=>1} ); + } + + die 'No database handle' unless defined $dbh; + + my $db_type = $dbh->{'Driver'}{'Name'} or die 'Cannot determine DBI type'; + my $driver = DRIVERS->{ lc $db_type } or die "$db_type not supported"; + my $pkg = "SQL::Translator::Parser::DBI::$driver"; + my $sub = $pkg.'::parse'; + + $tr->debug( "Calling sub '$sub'\n" ); + +# $tr->load( $pkg ); + + { + no strict 'refs'; + &{ $sub }( $tr, $dbh ) or die "No result from $pkg"; + } + + return 1; +} + +1; + +# ------------------------------------------------------------------- +=pod + +=head1 AUTHOR + +Ken Y. Clark Ekclark@cpan.orgE. + +=head1 SEE ALSO + +DBI. + +=cut diff --git a/lib/SQL/Translator/Parser/DBI/SQLite.pm b/lib/SQL/Translator/Parser/DBI/SQLite.pm new file mode 100644 index 0000000..a4f28c5 --- /dev/null +++ b/lib/SQL/Translator/Parser/DBI/SQLite.pm @@ -0,0 +1,88 @@ +package SQL::Translator::Parser::DBI::SQLite; + +# ------------------------------------------------------------------- +# $Id: SQLite.pm,v 1.1 2003-10-03 00:21:41 kycl4rk Exp $ +# ------------------------------------------------------------------- +# Copyright (C) 2003 Ken Y. Clark . +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; version 2. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +# 02111-1307 USA +# ------------------------------------------------------------------- + +=head1 NAME + +SQL::Translator::Parser::DBI::SQLite - parser for DBD::SQLite + +=head1 SYNOPSIS + +See SQL::Translator::Parser::DBI. + +=head1 DESCRIPTION + +Queries the "sqlite_master" table for schema definition. + +=cut + +use strict; +use DBI; +use SQL::Translator::Parser::SQLite; +use Data::Dumper; + +use vars qw[ $DEBUG $VERSION @EXPORT_OK ]; +$VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/; +$DEBUG = 0 unless defined $DEBUG; + +# ------------------------------------------------------------------- +sub parse { + my ( $tr, $dbh ) = @_; + + my $data = $dbh->selectall_arrayref( + 'select * from sqlite_master', { Columns => {} } + ); + + $tr->debug( "sqlite_master =\n", Dumper( $data ) ); + + my $schema = $tr->schema; + + my $create; + for my $rec ( @$data ) { + my $sql = $rec->{'sql'} or next; + $create .= "$sql;\n"; + } + + $tr->debug( "create =\n$create\n" ); + + SQL::Translator::Parser::SQLite::parse( $tr, $create ); + return 1; +} + +1; + +# ------------------------------------------------------------------- +# Where man is not nature is barren. +# William Blake +# ------------------------------------------------------------------- + +=pod + +=head1 AUTHOR + +Ken Y. Clark Ekclark@cpan.orgE, +Chris Mungall Ecjm@fruitfly.orgE. + +=head1 SEE ALSO + +perl(1), Parse::RecDescent, SQL::Translator::Schema. + +=cut