added typemap foo and default values
[DBIx-DataSource.git] / DataSource.pm
1 package DBIx::DataSource;
2
3 use strict;
4 use vars qw($VERSION @ISA @EXPORT_OK $errstr);
5 use Exporter;
6
7 @ISA = qw(Exporter);
8 @EXPORT_OK = qw( create_database drop_database );
9
10 $VERSION = '0.03';
11
12 =head1 NAME
13
14 DBIx::DataSource - Database-independant create and drop functions
15
16 =head1 SYNOPSIS
17
18   use DBIx::DataSource qw( create_database drop_database );
19
20   create_database( $data_source, $username, $password )
21     or warn $DBIx::DataSource::errstr;
22
23   drop_database( $data_source, $username, $password )
24     or warn $DBIx::DataSource::errstr;
25
26 =head1 DESCRIPTION
27
28 This module implements create_database and drop_database functions for
29 databases.  It aims to provide a common interface to database creation and
30 deletion regardless of the actual database being used.
31
32 Currently supported databases are MySQL and PostgreSQL.  Assistance adding
33 support for other databases is welcomed and relatively simple - see
34 L<DBIx::DataSource::Driver>.
35
36 =head1 FUNCTIONS
37
38 =over 4
39
40 =item create_database DATA_SOURCE USERNAME PASSWORD
41
42 Create the database specified by the given DBI data source.
43
44 =cut
45
46 sub create_database {
47   my( $dsn, $user, $pass ) = @_;
48   my $driver = _load_driver($dsn);
49   eval "DBIx::DataSource::$driver->create_database( \$dsn, \$user, \$pass )"
50     or do { $errstr=$@ if $@; ''; };
51 }
52
53 =item drop_database DATA_SOURCE
54
55 Drop the database specified by the given DBI data source.
56
57 =cut
58
59 sub drop_database {
60   my( $dsn, $user, $pass ) = @_;
61   my $driver = _load_driver($dsn);
62   eval "DBIx::DataSource::$driver->drop_database( \$dsn, \$user, \$pass )"
63     or do { $errstr=$@ if $@; ''; };
64 }
65
66 sub _load_driver {
67   my $datasrc = shift;
68   $datasrc =~ s/^dbi:(\w*?)(?:\((.*?)\))?://i #nicked from DBI->connect
69                         or '' =~ /()/; # ensure $1 etc are empty if match fails
70   my $driver = $1 or die "can't parse data source: $datasrc";
71   require "DBIx/DataSource/$driver.pm";
72   $driver;
73 }
74
75 =back
76
77 =head1 AUTHOR
78
79 Ivan Kohler <ivan-dbix-datasource@420.am>
80
81 =head1 COPYRIGHT
82
83 Copyright (c) 2000 Ivan Kohler
84 Copyright (c) 2000 Mail Abuse Prevention System LLC
85 All rights reserved.
86 This program is free software; you can redistribute it and/or modify it under
87 the same terms as Perl itself.
88
89 =head1 BUGS
90
91 If DBI data sources were objects, these functions would be methods.
92
93 =head1 SEE ALSO
94
95 L<DBIx::DataSource::Driver>, L<DBIx::DataSource::mysql>, L<DBIx::DataSource::Pg>,
96 L<DBI>
97
98 =cut
99
100 1;