1package Gantry::Utils::CDBI; 2use strict; use warnings; 3 4use Gantry::Utils::ModelHelper qw( 5 db_Main 6 retrieve_all_for_main_listing 7 get_listing 8 get_form_selections 9); 10 11use POSIX qw( strftime ); 12 13use base 'Class::DBI::Sweet'; 14 15my $db_options = { __PACKAGE__->_default_attributes, AutoCommit => 0 }; 16 17__PACKAGE__->_remember_handle('Main'); 18 19sub get_db_options { 20 return $db_options; 21} 22 23#------------------------------------------------- 24# db_Main 25#------------------------------------------------- 26# This method is exported by Gantry::Utils::ModelHelper 27 28#------------------------------------------------- 29# $class->get_form_selctions 30#------------------------------------------------- 31# This method is exported by Gantry::Utils::ModelHelper 32 33#------------------------------------------------- 34# $class->get_listing 35#------------------------------------------------- 36# This method is exported by Gantry::Utils::ModelHelper 37 38#------------------------------------------------- 39# $class->retrieve_all_for_main_listing 40#------------------------------------------------- 41# This deprecated method is exported by Gantry::Utils::ModelHelper 42 43#------------------------------------------------- 44# $class->pretty_date( $strftime_format, $sql_date ) 45#------------------------------------------------- 46sub pretty_date { 47 my ( $class, $fmt, $input_date ) = @_; 48 49 return unless defined $input_date and $input_date; 50 51 my ( $date, $time ) = split /\s+/, $input_date; 52 my ( $year, $mon, $day ) = split /-/, $date; 53 my ( $trim_time, $useless ) = split /\./, $time; 54 my ( $hour, $min, $sec ) = split /:/, $trim_time; 55 56 my $output_time = strftime( 57 $fmt, 58 $sec, $min, $hour, 59 $day, $mon - 1, $year - 1900 60 ); 61 62 return $output_time; 63} 64 651; 66 67=head1 NAME 68 69Gantry::Utils::CDBI - Class::DBI base class for Gantry applications 70 71=head1 SYNOPSIS 72 73This module expects to retrieve the database connection, 74username, and password from one of two places. 75 76=head2 In mod_perl 77 78If it lives in mod_perl, it expects these to come 79from the apache conf file. You might supply them like this: 80 81 <Location / > 82 PerlSetVar dbconn 'dbi:Pg:dbname=your_db_name' 83 PerlSetVar dbuser 'your_user' 84 PerlSetVar dbpass 'your_password' 85 </Location> 86 87It then retrieves them roughly like this (the mod_perl version affects this): 88 89 $r = Apache->request(); 90 91 $r->dir_config( 'dbconn' ), 92 $r->dir_config( 'dbuser' ), 93 $r->dir_config( 'dbpass' ), 94 95The handle is cached using pnotes to avoid recreating it. 96 97=head2 In scripts 98 99On the other hand, if the module does not live in mod_perl, it 100needs to directly use Gantry::Utils::DBConnHelper::Script like this: 101 102 use Gantry::Utils::DBConnHelper::Script { 103 dbconn => 'dbi:Pg:dbname=your_db_name', 104 dbuser => 'your_user', 105 dbuser => 'your_pass', 106 }; 107 108If you can't put the connection info into the use statement (say because 109you take it from the command line) do the above in two steps: 110 111 use Gantry::Utils::DBConnHelper::Script; 112 113 # figure out your connection info 114 115 Gantry::Utils::DBConnHelper::Script->set_conn_info( 116 dbconn => $dsn, 117 dbuser => $dbuser, 118 dbuser => $dbpass, 119 ); 120 121The database handle is cached by the helper. To get hold of it say: 122 123 my $dbh = Gantry::Utils::DBConnHelper::Script->get_dbh(); 124 125=head1 DESCRIPTION 126 127This module provides the base methods for Class:DBI, including the db conection 128method within a mod_perl environment. 129 130=head1 METHODS 131 132=over 4 133 134=item get_db_options 135 136Default database attributes usually supplied by Class::DBI's 137_default_attributes method. 138 139=item pretty_date 140 141A failed attempt at date format beautification. Probably should be removed. 142 143=back 144 145Note that these other methods are mixed in from Gantry::Utils::ModelHelper: 146 147 db_Main 148 retrieve_all_for_main_listing 149 get_form_selections 150 151See its docs for details. 152 153=head1 AUTHOR 154 155Tim Keefer <tkeefer@gmail.com> 156 157=head1 COPYRIGHT and LICENSE 158 159Copyright (c) 2005-6, Tim Keefer. 160 161This library is free software; you can redistribute it and/or modify 162it under the same terms as Perl itself, either Perl version 5.8.6 or, 163at your option, any later version of Perl 5 you may have available. 164 165=cut 166