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(
30 my $translator = SQL::Translator->new(
33 dsn => 'dbi:mysql:FOO',
35 db_password => 'password',
41 This parser accepts an open database handle (or the arguments to create
42 one) and queries the database directly for the information.
44 The following are acceptable arguments:
50 An open DBI database handle. NB: Be sure to create the database with the
51 "FetchHashKeyName => 'NAME_lc'" option as all the DBI parsers expect
52 lowercased column names.
56 The DSN to use for connecting to a database.
60 The user name to use for connecting to a database.
64 The password to use for connecting to a database.
68 There is no need to specify which type of database you are querying as
69 this is determined automatically by inspecting $dbh->{'Driver'}{'Name'}.
70 If a parser exists for your database, it will be used automatically;
71 if not, the code will fail automatically (and you can write the parser
72 and contribute it to the project!).
74 Currently parsers exist for the following databases:
84 =item * PostgreSQL (still experimental)
88 Most of these parsers are able to query the database directly for the
89 structure rather than parsing a text file. For large schemas, this is
90 probably orders of magnitude faster than traditional parsing (which
91 uses Parse::RecDescent, an amazing module but really quite slow).
93 Though no Oracle parser currently exists, it would be fairly easy to
94 query an Oracle database directly by using DDL::Oracle to generate a
95 DDL for the schema and then using the normal Oracle parser on this.
96 Perhaps future versions of SQL::Translator will include the ability to
97 query Oracle directly and skip the parsing of a text file, too.
105 our $VERSION = '1.60';
107 use constant DRIVERS => {
119 use SQL::Translator::Utils qw(debug);
121 use base qw(Exporter);
125 # Passed a SQL::Translator instance and a string containing the data
128 my ( $tr, $data ) = @_;
130 my $args = $tr->parser_args;
131 my $dbh = $args->{'dbh'};
132 my $dsn = $args->{'dsn'};
133 my $db_user = $args->{'db_user'};
134 my $db_password = $args->{'db_password'};
138 die 'No DSN' unless $dsn;
139 $dbh = DBI->connect( $dsn, $db_user, $db_password,
141 FetchHashKeyName => 'NAME_lc',
150 die 'No database handle' unless defined $dbh;
152 my $db_type = $dbh->{'Driver'}{'Name'} or die 'Cannot determine DBI type';
153 my $driver = DRIVERS->{ lc $db_type } or die "$db_type not supported";
154 my $pkg = "SQL::Translator::Parser::DBI::$driver";
155 my $sub = $pkg.'::parse';
157 SQL::Translator::load( $pkg );
161 &{ $sub }( $tr, $dbh ) or die "No result from $pkg";
165 eval { $dbh->disconnect } if (defined $dbh and $dbh_is_local);
178 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
182 DBI, SQL::Translator.