Changeset 19


Ignore:
Timestamp:
Jun 6, 2004, 8:12:46 PM (17 years ago)
Author:
kake
Message:

Added support for passing in dbh instead of connection params.

Location:
cgi-wiki/trunk
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • cgi-wiki/trunk/Changes

    r17 r19  
     10.51_02 6 June 2004
     2        Added support for passing in a database handle instead of
     3          connection parameters (Chris Winters).
     4
    150.51_01 24 February 2004
    26        Refactoring of search classes in preparation for new search
  • cgi-wiki/trunk/lib/CGI/Wiki.pm

    r17 r19  
    44
    55use vars qw( $VERSION );
    6 $VERSION = '0.51_01';
     6$VERSION = '0.51_02';
    77
    88use Carp qw(croak carp);
  • cgi-wiki/trunk/lib/CGI/Wiki/Setup/DBIxFTSMySQL.pm

    r2 r19  
    2424
    2525Set up DBIx::FullTextSearch indexes for use with CGI::Wiki. Has only
    26 one function, C<setup>, which takes as arguments the database name,
    27 the username and the password. The username must be able to create and
    28 drop tables in the database.
     26one function, C<setup>, which takes as arguments B<either> the
     27database name, the username and the password B<or> a database handle
     28. The username must be able to create and drop tables in the database.
    2929
    3030The $dbhost argument is optional -- omit it if the database is local.
     
    3636=cut
    3737
    38 sub setup
    39 {
    40   my ($dbname, $dbuser, $dbpass, $dbhost) = (@_);
    41 
    42     my $dsn = "dbi:mysql:$dbname";
    43     $dsn .= ";host=$dbhost" if $dbhost;
    44     my $dbh = DBI->connect($dsn, $dbuser, $dbpass,
    45                            { PrintError => 1, RaiseError => 1,
    46                              AutoCommit => 1 } )
    47       or croak DBI::errstr;
     38sub setup {
     39  my $dbh = _get_dbh( @_ );
    4840
    4941  # Drop FTS indexes if they already exist.
     
    8072}
    8173
     74sub _get_dbh {
     75    return $_[0] if ( ref $_[0] and ref $_[0] eq 'DBI::db' );
     76    my ($dbname, $dbuser, $dbpass, $dbhost) = @_;
     77    my $dsn = "dbi:mysql:$dbname";
     78    $dsn .= ";host=$dbhost" if $dbhost;
     79    my $dbh = DBI->connect($dsn, $dbuser, $dbpass,
     80                           { PrintError => 1, RaiseError => 1,
     81                             AutoCommit => 1 } )
     82      or croak DBI::errstr;
     83    return $dbh;
     84}
     85
    8286=head1 AUTHOR
    8387
     
    8690=head1 COPYRIGHT
    8791
    88      Copyright (C) 2002 Kake Pugh.  All Rights Reserved.
     92     Copyright (C) 2002-2004 Kake Pugh.  All Rights Reserved.
    8993
    9094This module is free software; you can redistribute it and/or modify it
  • cgi-wiki/trunk/lib/CGI/Wiki/Setup/MySQL.pm

    r18 r19  
    44
    55use vars qw( $VERSION );
    6 $VERSION = '0.06';
     6$VERSION = '0.07';
    77
    88use DBI;
     
    7373  CGI::Wiki::Setup::MySQL::setup($dbname, $dbuser, $dbpass, $dbhost);
    7474
    75 Takes three mandatory arguments -- the database name, the username and the
    76 password. The username must be able to create and drop tables in the
    77 database.
     75or
     76
     77  CGI::Wiki::Setup::Mysql::setup( $dbh );
     78
     79You can either provide an active database handle C<$dbh> or connection
     80parameters.                                                                   
     81
     82If you provide connection parameters the following arguments are
     83mandatory -- the database name, the username and the password. The
     84username must be able to create and drop tables in the database.
    7885
    7986The $dbhost argument is optional -- omit it if the database is local.
     
    8895
    8996sub setup {
    90     my ($dbname, $dbuser, $dbpass, $dbhost) = _get_args(@_);
    91 
    92     my $dsn = "dbi:mysql:$dbname";
    93     $dsn .= ";host=$dbhost" if $dbhost;
    94     my $dbh = DBI->connect($dsn, $dbuser, $dbpass,
    95                            { PrintError => 1, RaiseError => 1,
    96                              AutoCommit => 1 } )
    97       or croak DBI::errstr;
     97    my @args = @_;
     98    my $dbh = _get_dbh( @args );
     99    my $disconnect_required = _disconnect_required( @args );
    98100
    99101    # Check whether tables exist, set them up if not.
     
    116118    }
    117119
    118     # Clean up.
    119     $dbh->disconnect;
     120    # Clean up if we made our own dbh.
     121    $dbh->disconnect if $disconnect_required;
    120122}
    121123
     
    124126  use CGI::Wiki::Setup::MySQL;
    125127
    126   # Clear out the old database completely, then set up tables afresh.
     128  # Clear out all CGI::Wiki tables from the database.
    127129  CGI::Wiki::Setup::MySQL::cleardb($dbname, $dbuser, $dbpass, $dbhost);
    128   CGI::Wiki::Setup::MySQL::setup($dbname, $dbuser, $dbpass, $dbhost);
    129 
    130 Takes three mandatory arguments -- the database name, the username and the
    131 password. The username must be able to drop tables in the database.
     130
     131or
     132
     133  CGI::Wiki::Setup::Mysql::cleardb( $dbh );
     134
     135You can either provide an active database handle C<$dbh> or connection
     136parameters.                                                                   
     137
     138If you provide connection parameters the following arguments are
     139mandatory -- the database name, the username and the password. The
     140username must be able to drop tables in the database.
    132141
    133142The $dbhost argument is optional -- omit if the database is local.
     
    144153
    145154sub cleardb {
    146     my ($dbname, $dbuser, $dbpass, $dbhost) = _get_args(@_);
    147 
    148     my $dsn = "dbi:mysql:$dbname";
    149     $dsn .= ";host=$dbhost" if $dbhost;
    150     my $dbh = DBI->connect($dsn, $dbuser, $dbpass,
    151                            { PrintError => 1, RaiseError => 1,
    152                              AutoCommit => 1 } )
    153       or croak DBI::errstr;
     155    my @args = @_;
     156    my $dbh = _get_dbh( @args );
     157    my $disconnect_required = _disconnect_required( @args );
    154158
    155159    print "Dropping tables... ";
     
    158162    print "done\n";
    159163
    160     # Clean up.
    161     $dbh->disconnect;
    162 }
    163 
    164 sub _get_args {
     164    # Clean up if we made our own dbh.
     165    $dbh->disconnect if $disconnect_required;
     166}
     167
     168sub _get_dbh {
     169    # Database handle passed in.
     170    if ( ref $_[0] and ref $_[0] eq 'DBI::db' ) {
     171        return $_[0];
     172    }
     173
     174    # Args passed as hashref.
    165175    if ( ref $_[0] and ref $_[0] eq 'HASH' ) {
    166         my %hash = %{$_[0]};
    167         return @hash{ qw( dbname dbuser dbpass dbhost ) };
    168     } else {
    169         return @_;
    170     }
     176        my %args = %{$_[0]};
     177        if ( $args{dbh} ) {
     178            return $args{dbh};
     179        } else {
     180            return _make_dbh( %args );
     181        }
     182    }
     183
     184    # Args passed as list of connection details.
     185    return _make_dbh(
     186                      dbname => $_[0],
     187                      dbuser => $_[1],
     188                      dbpass => $_[2],
     189                      dbhost => $_[3],
     190                    );
     191}
     192
     193sub _disconnect_required {
     194    # Database handle passed in.
     195    if ( ref $_[0] and ref $_[0] eq 'DBI::db' ) {
     196        return 0;
     197    }
     198
     199    # Args passed as hashref.
     200    if ( ref $_[0] and ref $_[0] eq 'HASH' ) {
     201        my %args = %{$_[0]};
     202        if ( $args{dbh} ) {
     203            return 0;
     204        } else {
     205            return 1;
     206        }
     207    }
     208
     209    # Args passed as list of connection details.
     210    return 1;
     211}
     212
     213sub _make_dbh {
     214    my %args = @_;
     215    my $dsn = "dbi:mysql:$args{dbname}";
     216    $dsn .= ";host=$args{dbhost}" if $args{dbhost};
     217    my $dbh = DBI->connect($dsn, $args{dbuser}, $args{dbpass},
     218                           { PrintError => 1, RaiseError => 1,
     219                             AutoCommit => 1 } )
     220      or croak DBI::errstr;
     221    return $dbh;
    171222}
    172223
     
    188239  )
    189240
     241or indeed as
     242
     243  ( { dbh => $dbh } )
     244
    190245Note that's a hashref, not a hash.
    191246
     
    196251=head1 COPYRIGHT
    197252
    198      Copyright (C) 2002-2003 Kake Pugh.  All Rights Reserved.
     253     Copyright (C) 2002-2004 Kake Pugh.  All Rights Reserved.
    199254
    200255This module is free software; you can redistribute it and/or modify it
  • cgi-wiki/trunk/lib/CGI/Wiki/Setup/Pg.pm

    r18 r19  
    44
    55use vars qw( $VERSION );
    6 $VERSION = '0.07';
     6$VERSION = '0.08';
    77
    88use DBI;
     
    7979  CGI::Wiki::Setup::Pg::setup($dbname, $dbuser, $dbpass, $dbhost);
    8080
    81 Takes three mandatory arguments -- the database name, the username and the
    82 password. The username must be able to create and drop tables in the
    83 database.
     81or
     82
     83  CGI::Wiki::Setup::Pg::setup( $dbh );
     84
     85You can either provide an active database handle C<$dbh> or connection
     86parameters.                                                                   
     87
     88If you provide connection parameters the following arguments are
     89mandatory -- the database name, the username and the password. The
     90username must be able to create and drop tables in the database.
    8491
    8592The $dbhost argument is optional -- omit it if the database is local.
     
    94101
    95102sub setup {
    96     my ($dbname, $dbuser, $dbpass, $dbhost) = _get_args(@_);
    97 
    98     my $dsn = "dbi:Pg:dbname=$dbname";
    99     $dsn .= ";host=$dbhost" if $dbhost;
    100     my $dbh = DBI->connect($dsn, $dbuser, $dbpass,
    101                            { PrintError => 1, RaiseError => 1,
    102                              AutoCommit => 1 } )
    103       or croak DBI::errstr;
     103    my @args = @_;
     104    my $dbh = _get_dbh( @args );
     105    my $disconnect_required = _disconnect_required( @args );
    104106
    105107    # Check whether tables exist, set them up if not.
     
    125127    }
    126128
    127     # Clean up.
    128     $dbh->disconnect;
     129    # Clean up if we made our own dbh.
     130    $dbh->disconnect if $disconnect_required;
    129131}
    130132
     
    133135  use CGI::Wiki::Setup::Pg;
    134136
    135   # Clear out the old database completely, then set up tables afresh.
     137  # Clear out all CGI::Wiki tables from the database.
    136138  CGI::Wiki::Setup::Pg::cleardb($dbname, $dbuser, $dbpass, $dbhost);
    137   CGI::Wiki::Setup::Pg::setup($dbname, $dbuser, $dbpass, $dbhost);
    138 
    139 Takes three mandatory arguments -- the database name, the username and the
    140 password. The username must be able to drop tables in the database.
     139
     140or
     141
     142  CGI::Wiki::Setup::Pg::cleardb( $dbh );
     143
     144You can either provide an active database handle C<$dbh> or connection
     145parameters.                                                                   
     146
     147If you provide connection parameters the following arguments are
     148mandatory -- the database name, the username and the password. The
     149username must be able to drop tables in the database.
    141150
    142151The $dbhost argument is optional -- omit it if the database is local.
     
    153162
    154163sub cleardb {
    155     my ($dbname, $dbuser, $dbpass, $dbhost) = _get_args(@_);
    156 
    157     my $dsn = "dbi:Pg:dbname=$dbname";
    158     $dsn .= ";host=$dbhost" if $dbhost;
    159     my $dbh = DBI->connect($dsn, $dbuser, $dbpass,
    160                            { PrintError => 1, RaiseError => 1,
    161                              AutoCommit => 1 } )
    162       or croak DBI::errstr;
     164    my @args = @_;
     165    my $dbh = _get_dbh( @args );
     166    my $disconnect_required = _disconnect_required( @args );
    163167
    164168    print "Dropping tables... ";
     
    171175    print "done\n";
    172176
    173     # Clean up.
    174     $dbh->disconnect;
    175 }
    176 
    177 sub _get_args {
     177    # Clean up if we made our own dbh.
     178    $dbh->disconnect if $disconnect_required;
     179}
     180
     181sub _get_dbh {
     182    # Database handle passed in.
     183    if ( ref $_[0] and ref $_[0] eq 'DBI::db' ) {
     184        return $_[0];
     185    }
     186
     187    # Args passed as hashref.
    178188    if ( ref $_[0] and ref $_[0] eq 'HASH' ) {
    179         my %hash = %{$_[0]};
    180         return @hash{ qw( dbname dbuser dbpass dbhost ) };
    181     } else {
    182         return @_;
    183     }
     189        my %args = %{$_[0]};
     190        if ( $args{dbh} ) {
     191            return $args{dbh};
     192        } else {
     193            return _make_dbh( %args );
     194        }
     195    }
     196
     197    # Args passed as list of connection details.
     198    return _make_dbh(
     199                      dbname => $_[0],
     200                      dbuser => $_[1],
     201                      dbpass => $_[2],
     202                      dbhost => $_[3],
     203                    );
     204}
     205
     206sub _disconnect_required {
     207    # Database handle passed in.
     208    if ( ref $_[0] and ref $_[0] eq 'DBI::db' ) {
     209        return 0;
     210    }
     211
     212    # Args passed as hashref.
     213    if ( ref $_[0] and ref $_[0] eq 'HASH' ) {
     214        my %args = %{$_[0]};
     215        if ( $args{dbh} ) {
     216            return 0;
     217        } else {
     218            return 1;
     219        }
     220    }
     221
     222    # Args passed as list of connection details.
     223    return 1;
     224}
     225
     226sub _make_dbh {
     227    my %args = @_;
     228    my $dsn = "dbi:Pg:dbname=$args{dbname}";
     229    $dsn .= ";host=$args{dbhost}" if $args{dbhost};
     230    my $dbh = DBI->connect($dsn, $args{dbuser}, $args{dbpass},
     231                           { PrintError => 1, RaiseError => 1,
     232                             AutoCommit => 1 } )
     233      or croak DBI::errstr;
     234    return $dbh;
    184235}
    185236
     
    201252  )
    202253
     254or indeed as
     255
     256  ( { dbh => $dbh } )
     257
    203258Note that's a hashref, not a hash.
    204259
     
    209264=head1 COPYRIGHT
    210265
    211      Copyright (C) 2002-2003 Kake Pugh.  All Rights Reserved.
     266     Copyright (C) 2002-2004 Kake Pugh.  All Rights Reserved.
    212267
    213268This module is free software; you can redistribute it and/or modify it
  • cgi-wiki/trunk/lib/CGI/Wiki/Setup/SQLite.pm

    r18 r19  
    44
    55use vars qw( $VERSION );
    6 $VERSION = '0.05';
     6$VERSION = '0.06';
    77
    88use DBI;
     
    6666
    6767  use CGI::Wiki::Setup::SQLite;
    68   CGI::Wiki::Setup::SQLite::setup($dbfile);
    69 
    70 Takes one argument - the name of the file that the SQLite database is
    71 stored in.
     68
     69  CGI::Wiki::Setup::SQLite::setup( $filename );
     70
     71or
     72
     73  CGI::Wiki::Setup::SQLite::setup( $dbh );
     74
     75Takes one argument - B<either> the name of the file that the SQLite
     76database is stored in B<or> an active database handle.
    7277
    7378B<NOTE:> If a table that the module wants to create already exists,
     
    8085
    8186sub setup {
    82     my $dbfile = _get_args(@_);
    83 
    84     my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", "", "",
    85                            { PrintError => 1, RaiseError => 1,
    86                              AutoCommit => 1 } )
    87       or croak DBI::errstr;
     87    my @args = @_;
     88    my $dbh = _get_dbh( @args );
     89    my $disconnect_required = _disconnect_required( @args );
    8890
    8991    # Check whether tables exist, set them up if not.
     
    107109    }
    108110
    109     # Clean up.
    110     $dbh->disconnect;
     111    # Clean up if we made our own dbh.
     112    $dbh->disconnect if $disconnect_required;
    111113}
    112114
     
    115117  use CGI::Wiki::Setup::SQLite;
    116118
    117   # Clear out the old database completely, then set up tables afresh.
    118   CGI::Wiki::Setup::SQLite::cleardb($dbfile);
    119   CGI::Wiki::Setup::SQLite::setup($dbfile);
    120 
    121 Takes one argument - the name of the file that the SQLite database is
    122 stored in.
     119  # Clear out all CGI::Wiki tables from the database.
     120  CGI::Wiki::Setup::SQLite::cleardb( $filename );
     121
     122or
     123
     124  CGI::Wiki::Setup::SQLite::cleardb( $dbh );
     125
     126Takes one argument - B<either> the name of the file that the SQLite
     127database is stored in B<or> an active database handle.
    123128
    124129Clears out all L<CGI::Wiki> store tables from the database. B<NOTE>
     
    133138
    134139sub cleardb {
    135     my $dbfile = _get_args(@_);
    136 
    137     my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", "", "",
    138                            { PrintError => 1, RaiseError => 1,
    139                              AutoCommit => 1 } )
    140       or croak DBI::errstr;
     140    my @args = @_;
     141    my $dbh = _get_dbh( @args );
     142    my $disconnect_required = _disconnect_required( @args );
    141143
    142144    print "Dropping tables... ";
     
    149151    print "done\n";
    150152
    151     # Clean up.
    152     $dbh->disconnect;
    153 }
    154 
    155 sub _get_args {
    156     my @args;
     153    # Clean up if we made our own dbh.
     154    $dbh->disconnect if $disconnect_required;
     155}
     156
     157sub _get_dbh {
     158    # Database handle passed in.
     159    if ( ref $_[0] and ref $_[0] eq 'DBI::db' ) {
     160        return $_[0];
     161    }
     162
     163    # Args passed as hashref.
    157164    if ( ref $_[0] and ref $_[0] eq 'HASH' ) {
    158         my %hash = %{$_[0]};
    159         @args = @hash{ qw( dbname ) };
    160     } else {
    161         @args = @_;
    162     }
    163     return $args[0];
     165        my %args = %{$_[0]};
     166        if ( $args{dbh} ) {
     167            return $args{dbh};
     168        } else {
     169            return _make_dbh( %args );
     170        }
     171    }
     172
     173    # Args passed as list of connection details.
     174    return _make_dbh( dbname => $_[0] );
     175}
     176
     177sub _disconnect_required {
     178    # Database handle passed in.
     179    if ( ref $_[0] and ref $_[0] eq 'DBI::db' ) {
     180        return 0;
     181    }
     182
     183    # Args passed as hashref.
     184    if ( ref $_[0] and ref $_[0] eq 'HASH' ) {
     185        my %args = %{$_[0]};
     186        if ( $args{dbh} ) {
     187            return 0;
     188        } else {
     189            return 1;
     190        }
     191    }
     192
     193    # Args passed as list of connection details.
     194    return 1;
     195}
     196
     197sub _make_dbh {
     198    my %args = @_;
     199    my $dbh = DBI->connect("dbi:SQLite:dbname=$args{dbname}", "", "",
     200                           { PrintError => 1, RaiseError => 1,
     201                             AutoCommit => 1 } )
     202      or croak DBI::errstr;
     203    return $dbh;
    164204}
    165205
     
    170210As requested by Podmaster.  Instead of passing arguments to the methods as
    171211
    172   ($dbfile)
     212  ($filename)
    173213
    174214you can pass them as
    175215
    176   ( { dbname => $dbfile
    177     }
    178   )
     216  ( { dbname => $filename } )
     217
     218or indeed
     219
     220  ( { dbh => $dbh } )
    179221
    180222Note that's a hashref, not a hash.
     
    186228=head1 COPYRIGHT
    187229
    188      Copyright (C) 2002 Kake Pugh.  All Rights Reserved.
     230     Copyright (C) 2002-2004 Kake Pugh.  All Rights Reserved.
    189231
    190232This module is free software; you can redistribute it and/or modify it
  • cgi-wiki/trunk/lib/CGI/Wiki/Store/Database.pm

    r2 r19  
    1212use Digest::MD5 qw( md5_hex );
    1313
    14 $VERSION = '0.20';
     14$VERSION = '0.21';
    1515
    1616=head1 NAME
     
    3737                                            dbhost => "db.example.com" );
    3838
    39 C<dbname> is mandatory. C<dbpass>, C<dbuser> and C<dbhost> are optional, but
    40 you'll want to supply them unless your database's authentication
    41 method doesn't require it.
     39or
     40
     41  my $store = CGI::Wiki::Store::MySQL->new( dbh => $dbh );
     42
     43If you do not provide an active database handle in C<dbh>, then
     44C<dbname> is mandatory. C<dbpass>, C<dbuser> and C<dbhost> are
     45optional, but you'll want to supply them unless your database's
     46authentication method doesn't require it.
     47
     48If you do provide C<database> then it must have the following
     49parameters set, otherwise you should just provide the connection
     50information and let us create our own handle:
     51
     52=over 4
     53
     54=item *
     55
     56C<RaiseError> = 1
     57
     58=item *
     59
     60C<PrintError> = 0
     61
     62=item *
     63
     64C<AutoCommit> = 1
     65
     66=back
    4267
    4368=cut
     
    5378    my ($self, %args) = @_;
    5479
    55     # Store parameters.
    56     foreach ( qw(dbname) ) {
    57         die "Must supply a value for $_" unless defined $args{$_};
    58         $self->{"_$_"} = $args{$_};
    59     }
    60     $self->{_dbuser} = $args{dbuser} || "";
    61     $self->{_dbpass} = $args{dbpass} || "";
    62     $self->{_dbhost} = $args{dbhost} || "";
    63 
    64     # Connect to database and store the database handle.
    65     my ($dbname, $dbuser, $dbpass, $dbhost) =
     80    if ( $args{dbh} ) {
     81        $self->{_dbh} = $args{dbh};
     82        $self->{_external_dbh} = 1; # don't disconnect at DESTROY time
     83    } else {
     84        die "Must supply a dbname" unless defined $args{dbname};
     85        $self->{_dbname} = $args{dbname};
     86        $self->{_dbuser} = $args{dbuser} || "";
     87        $self->{_dbpass} = $args{dbpass} || "";
     88        $self->{_dbhost} = $args{dbhost} || "";
     89
     90        # Connect to database and store the database handle.
     91        my ($dbname, $dbuser, $dbpass, $dbhost) =
    6692                               @$self{qw(_dbname _dbuser _dbpass _dbhost)};
    67     my $dsn = $self->_dsn($dbname, $dbhost)
    68        or croak "No data source string provided by class";
    69     $self->{_dbh} = DBI->connect($dsn, $dbuser, $dbpass,
    70                                  { PrintError => 0, RaiseError => 1,
    71                                    AutoCommit => 1 } )
    72        or croak "Can't connect to database $dbname using $dsn: " . DBI->errstr;
     93        my $dsn = $self->_dsn($dbname, $dbhost)
     94            or croak "No data source string provided by class";
     95        $self->{_dbh} = DBI->connect( $dsn, $dbuser, $dbpass,
     96                                      { PrintError => 0, RaiseError => 1,
     97                                        AutoCommit => 1 } )
     98          or croak "Can't connect to database $dbname using $dsn: "
     99                   . DBI->errstr;
     100    }
    73101
    74102    return $self;
     
    872900sub DESTROY {
    873901    my $self = shift;
     902    return if $self->{_external_dbh};
    874903    my $dbh = $self->dbh;
    875904    $dbh->disconnect if $dbh;
  • cgi-wiki/trunk/lib/CGI/Wiki/Store/Pg.pm

    r2 r19  
    6666        $dbh->rollback;
    6767        $dbh->{AutoCommit} = 1;
    68         if ($error =~ /Can't serialize access due to concurrent update/) {
     68        if ( $error =~ /can't serialize access due to concurrent update/i
     69            or $error =~ /could not serialize access due to concurrent update/i
     70           ) {
    6971            return 0;
    7072        } else {
  • cgi-wiki/trunk/t/002_datastore_setup.t

    r2 r19  
    11use strict;
    2 use Test::More tests => 15;
     2use Test::More tests => 27;
    33use CGI::Wiki;
    44use CGI::Wiki::TestConfig;
     5use DBI;
    56
    67foreach my $dbtype (qw( MySQL Pg SQLite )) {
    78
    89    SKIP: {
    9         skip "$dbtype backend not configured", 4
     10        skip "$dbtype backend not configured", 8
    1011            unless $CGI::Wiki::TestConfig::config{$dbtype}->{dbname};
    1112
     
    1314        my $setup_class = "CGI::Wiki::Setup::$dbtype";
    1415        eval "require $setup_class";
     16        my $store_class = "CGI::Wiki::Store::$dbtype";
     17        eval "require $store_class";
    1518        {
    1619            no strict 'refs';
     20
     21            my $dsn = $store_class->_dsn( $config{dbname} );
    1722
    1823            foreach my $method ( qw( cleardb setup ) ) {
     
    2328                };
    2429                is( $@, "",
    25                   "${setup_class}::$method doesn't die when called with list");
     30                  "${setup_class}::$method doesn't die when called with connection details list");
    2631
    2732                eval {
     
    2934                };
    3035                is( $@, "",
    31                "${setup_class}::$method doesn't die when called with hashref");
     36               "${setup_class}::$method doesn't die when called with connection details hashref");
     37
     38                eval {
     39                    my $dbh = DBI->connect($dsn, @config{ qw( dbuser dbpass )},
     40                                           { PrintError => 0, RaiseError => 1,
     41                                             AutoCommit => 1 } )
     42                      or die DBI->errstr;
     43                    &{$setup_class . "::" . $method}( $dbh );
     44                    $dbh->disconnect;
     45                };
     46                is( $@, "",
     47                  "${setup_class}::$method doesn't die when called with dbh");
     48
     49                eval {
     50                    my $dbh = DBI->connect($dsn, @config{ qw( dbuser dbpass )},
     51                                           { PrintError => 0, RaiseError => 1,
     52                                             AutoCommit => 1 } )
     53                      or die DBI->errstr;
     54                    &{$setup_class . "::" . $method}( { dbh => $dbh } );
     55                    $dbh->disconnect;
     56                };
     57                is( $@, "",
     58                  "${setup_class}::$method doesn't die when called with dbh in hashref");
    3259            }
    3360        }
  • cgi-wiki/trunk/t/050_mysql_store.t

    r2 r19  
    22
    33use strict;
    4 use Test::More tests => 10;
     4use Test::More tests => 13;
    55use CGI::Wiki::TestConfig;
    66
     
    1919
    2020SKIP: {
    21     skip "No MySQL database configured for testing", 8 unless $dbname;
     21    skip "No MySQL database configured for testing", 11 unless $dbname;
    2222
    2323    my $store = eval { $class->new( dbname => $dbname,
     
    2626                                    dbhost => $dbhost );
    2727                     };
    28     is( $@, "", "Creation succeeds" );
     28    is( $@, "", "Creation succeeds with connection parameters" );
    2929    isa_ok( $store, $class );
    3030    ok( $store->dbh, "...and has set up a database handle" );
    3131
     32    my $dsn = "dbi:mysql:$dbname";
     33    $dsn .= ";host=$dbhost" if $dbhost;
     34    my $dbh = DBI->connect( $dsn, $dbuser, $dbpass );
     35    my $evil_store = eval { $class->new( dbh => $dbh ); };
     36    is( $@, "", "Creation succeeds with dbh" );
     37    isa_ok( $evil_store, $class );
     38    ok( $evil_store->dbh, "...and we can retrieve the database handle" );
     39
    3240    # White box test - do internal locking functions work the way we expect?
    33     my $evil_store = $class->new( dbname => $dbname,
    34                                   dbuser => $dbuser,
    35                                   dbpass => $dbpass,
    36                                   dbhost => $dbhost  );
    37 
    3841    ok( $store->_lock_node("Home"), "Can lock a node" );
    3942    ok( ! $evil_store->_lock_node("Home"),
  • cgi-wiki/trunk/t/051_pg_store.t

    r2 r19  
    44use CGI::Wiki::Setup::Pg;
    55use CGI::Wiki::TestConfig;
    6 use Test::More tests => 6;
     6use Test::More tests => 9;
    77
    88my $class = "CGI::Wiki::Store::Pg";
     
    1515
    1616SKIP: {
    17     skip "No Postgres database configured for testing", 5 unless $dbname;
     17    skip "No Postgres database configured for testing", 8 unless $dbname;
    1818
    19     my $store = eval { $class->new( dbname => $dbname,
    20                                     dbuser => $dbuser,
    21                                     dbpass => $dbpass,
    22                                     dbhost => $dbhost );
    23                      };
    24     is( $@, "", "Creation doesn't die" );
    25     isa_ok( $store, $class );
    26     ok( $store->dbh, "...and has set up a database handle" );
     19    {
     20      my $store = eval { $class->new( dbname => $dbname,
     21                                      dbuser => $dbuser,
     22                                      dbpass => $dbpass,
     23                                      dbhost => $dbhost );
     24                       };
     25      is( $@, "", "Creation doesn't die when given connection params" );
     26      isa_ok( $store, $class );
     27      ok( $store->dbh, "...and has set up a database handle" );
     28    }
     29
     30    {
     31      my $dsn = "dbi:Pg:dbname=$dbname";
     32      $dsn .= ";host=$dbhost" if $dbhost;
     33      my $dbh = DBI->connect( $dsn, $dbuser, $dbpass );
     34      my $store = eval { $class->new( dbh => $dbh ); };
     35      is( $@, "", "Creation doesn't die when given a dbh" );
     36      isa_ok( $store, $class );
     37      ok( $store->dbh, "...and we can retrieve the database handle" );
     38      $dbh->disconnect;
     39    }
    2740
    2841    CGI::Wiki::Setup::Pg::cleardb({ dbname => $dbname,
     
    3144                                    dbhost => $dbhost });
    3245    CGI::Wiki::Setup::Pg::setup({ dbname => $dbname,
    33                                     dbuser => $dbuser,
    34                                     dbpass => $dbpass,
    35                                     dbhost => $dbhost });
     46                                  dbuser => $dbuser,
     47                                  dbpass => $dbpass,
     48                                  dbhost => $dbhost });
    3649
    3750    SKIP: {
     
    3952        skip "either Hook::LexWrap or Test::MockObject not installed", 2 if $@;
    4053
     54        my $store = $class->new( dbname => $dbname,
     55                                 dbuser => $dbuser,
     56                                 dbpass => $dbpass,
     57                                 dbhost => $dbhost );
    4158        my $wiki = CGI::Wiki->new( store => $store );
    4259
  • cgi-wiki/trunk/t/052_sqlite_store.t

    r2 r19  
    44use CGI::Wiki::Store::SQLite;
    55use CGI::Wiki::TestConfig;
    6 use Test::More tests => 6;
     6use Test::More tests => 9;
    77
    88my $class = "CGI::Wiki::Store::SQLite";
     
    1414
    1515SKIP: {
    16     skip "No SQLite database configured for testing", 5 unless $dbname;
     16    skip "No SQLite database configured for testing", 8 unless $dbname;
    1717
    1818    CGI::Wiki::Setup::SQLite::cleardb( $dbname );
     
    2020
    2121    my $store = eval { $class->new( dbname => $dbname ) };
    22     is( $@, "", "Creation doesn't die" );
     22    is( $@, "", "Creation doesn't die when given connection parameters" );
    2323    isa_ok( $store, $class );
    2424    ok( $store->dbh, "...and has set up a database handle" );
     25
     26    my $dbh = DBI->connect( "dbi:SQLite:dbname=$dbname" );
     27    my $store2 = eval { $class->new( dbh => $dbh ) };
     28    is( $@, "", "Creation doesn't die when given dbh" );
     29    isa_ok( $store2, $class );
     30    ok( $store2->dbh, "...and we can access the database handle" );
    2531
    2632    SKIP: {
Note: See TracChangeset for help on using the changeset viewer.