1 package SQL::Translator::Parser::DBI;
5 SQL::Translator::Parser::DBI - "parser" for DBI handles
12 my $dbh = DBI->connect('dsn', 'user', 'pass',
15 FetchHashKeyName => 'NAME_lc',
19 my $translator = SQL::Translator->new(
28 my $translator = SQL::Translator->new(
31 dsn => 'dbi:mysql:FOO',
33 db_password => 'password',
39 This parser accepts an open database handle (or the arguments to create
40 one) and queries the database directly for the information.
42 The following are acceptable arguments:
48 An open DBI database handle. NB: Be sure to create the database with the
49 "FetchHashKeyName => 'NAME_lc'" option as all the DBI parsers expect
50 lowercased column names.
54 The DSN to use for connecting to a database.
58 The user name to use for connecting to a database.
62 The password to use for connecting to a database.
66 There is no need to specify which type of database you are querying as
67 this is determined automatically by inspecting $dbh->{'Driver'}{'Name'}.
68 If a parser exists for your database, it will be used automatically;
69 if not, the code will fail automatically (and you can write the parser
70 and contribute it to the project!).
72 Currently parsers exist for the following databases:
82 =item * PostgreSQL (still experimental)
86 Most of these parsers are able to query the database directly for the
87 structure rather than parsing a text file. For large schemas, this is
88 probably orders of magnitude faster than traditional parsing (which
89 uses Parse::RecDescent, an amazing module but really quite slow).
91 Though no Oracle parser currently exists, it would be fairly easy to
92 query an Oracle database directly by using DDL::Oracle to generate a
93 DDL for the schema and then using the normal Oracle parser on this.
94 Perhaps future versions of SQL::Translator will include the ability to
95 query Oracle directly and skip the parsing of a text file, too.
101 use vars qw($VERSION @EXPORT);
104 use constant DRIVERS => {
117 use SQL::Translator::Utils qw(debug);
119 use base qw(Exporter);
123 # Passed a SQL::Translator instance and a string containing the data
126 my ( $tr, $data ) = @_;
128 my $args = $tr->parser_args;
129 my $dbh = $args->{'dbh'};
130 my $dsn = $args->{'dsn'};
131 my $db_user = $args->{'db_user'};
132 my $db_password = $args->{'db_password'};
135 die 'No DSN' unless $dsn;
136 $dbh = DBI->connect( $dsn, $db_user, $db_password,
138 FetchHashKeyName => 'NAME_lc',
146 die 'No database handle' unless defined $dbh;
148 my $db_type = $dbh->{'Driver'}{'Name'} or die 'Cannot determine DBI type';
149 my $driver = DRIVERS->{ lc $db_type } or die "$db_type not supported";
150 my $pkg = "SQL::Translator::Parser::DBI::$driver";
151 my $sub = $pkg.'::parse';
153 SQL::Translator::load( $pkg );
157 &{ $sub }( $tr, $dbh ) or die "No result from $pkg";
160 $dbh->disconnect if defined $dbh;
173 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
177 DBI, SQL::Translator.