"Fossies" - the Fresh Open Source Software Archive

Member "absence-v2.1/db/init-db.pl" (20 Oct 2013, 4198 Bytes) of package /linux/www/web-absence-2.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "init-db.pl" see the Fossies "Dox" file reference documentation.

    1 #!/usr/bin/perl
    2 
    3 #======================================================================
    4 #    This file is part of Absence.
    5 #
    6 #    Absence is free software: you can redistribute it and/or modify
    7 #    it under the terms of the GNU General Public License as published by
    8 #    the Free Software Foundation, either version 3 of the License, or
    9 #    (at your option) any later version.
   10 #
   11 #    Absence is distributed in the hope that it will be useful,
   12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 #    GNU General Public License for more details.
   15 #
   16 #    You should have received a copy of the GNU General Public License
   17 #    along with Absence.  If not, see <http://www.gnu.org/licenses/>.
   18 #======================================================================
   19 
   20 # copyright Robert Urban
   21 
   22 use DBI;
   23 
   24 my %initial_values = (
   25     c_group => [
   26         {
   27             name                => "'default'",
   28             description         => "'the default group'",
   29         },
   30         {
   31             name                => "'second'",
   32             description         => "'the second group'",
   33         },
   34         {
   35             name                => "'third'",
   36             description         => "'the 3rd group'",
   37         },
   38         {
   39             name                => "'fourth'",
   40             description         => "'the 4th group'",
   41         },
   42     ],
   43     c_object => [
   44         {
   45             name                => "'rob'",
   46             email               => "'urban@tru64.org'",
   47         },
   48         {
   49             name                => "'john'",
   50             email               => "'john@tru64.org'",
   51         },
   52         {
   53             name                => "'joe'",
   54             email               => "'joe@tru64.org'",
   55         },
   56         {
   57             name                => "'jane'",
   58             email               => "'jane@tru64.org'",
   59         },
   60     ],
   61     c_group_object => [
   62         {
   63             group_id            => 1,
   64             object_id           => 1,
   65         },
   66         {
   67             group_id            => 1,
   68             object_id           => 2,
   69         },
   70         {
   71             group_id            => 1,
   72             object_id           => 3,
   73         },
   74         {
   75             group_id            => 1,
   76             object_id           => 4,
   77         },
   78         {
   79             group_id            => 2,
   80             object_id           => 1,
   81         },
   82         {
   83             group_id            => 2,
   84             object_id           => 2,
   85         },
   86     ],
   87     c_reservation_type => [
   88         {
   89             name                => "'Vacation'",
   90             wide                => "'t'",
   91             position            => 1,
   92             overlap_group       => 1,
   93             skip_non_workdays   => "'t'",
   94             color               => 0xce1616,
   95             default_type        => "'f'",
   96             description         => "'dunno'",
   97         },
   98         {
   99             name                => "'Customer'",
  100             wide                => "'t'",
  101             position            => 1,
  102             overlap_group       => 1,
  103             skip_non_workdays   => "'t'",
  104             color               => 0x51ac4e,
  105             default_type        => "'t'",
  106             description         => "'dunno'",
  107         },
  108         {
  109             name                => "'Busy'",
  110             wide                => "'t'",
  111             position            => 1,
  112             overlap_group       => 1,
  113             skip_non_workdays   => "'t'",
  114             color               => 0x9090ff,
  115             default_type        => "'f'",
  116             description         => "'dunno'",
  117         },
  118         {
  119             name                => "'Other'",
  120             wide                => "'t'",
  121             position            => 1,
  122             overlap_group       => 1,
  123             skip_non_workdays   => "'t'",
  124             color               => 0xffa500,
  125             default_type        => "'f'",
  126             description         => "'dunno'",
  127         },
  128     ],
  129     d_group_mod_time => [
  130         {
  131             group_id            => 1,
  132             month               => 1,
  133             year                => 2008,
  134             mod_time            => 1207173602,
  135         },
  136         {
  137             group_id            => 2,
  138             month               => 1,
  139             year                => 2008,
  140             mod_time            => 1207173602,
  141         },
  142         {
  143             group_id            => 3,
  144             month               => 1,
  145             year                => 2008,
  146             mod_time            => 1207173602,
  147         },
  148         {
  149             group_id            => 2,
  150             month               => 2,
  151             year                => 2008,
  152             mod_time            => 1207173602,
  153         },
  154         {
  155             group_id            => 3,
  156             month               => 2,
  157             year                => 2008,
  158             mod_time            => 1207173602,
  159         },
  160         {
  161             group_id            => 4,
  162             month               => 2,
  163             year                => 2008,
  164             mod_time            => 1207173602,
  165         },
  166     ],
  167 );
  168 
  169 my @order = qw/c_group c_reservation_type c_object c_group_object d_group_mod_time/;
  170 
  171 
  172 my $dbname = 'absence';
  173 my $dbuser = 'absence';
  174 my $dbpass = 'absence';
  175 
  176 my $dbh = DBI->connect("dbi:Pg:dbname=$dbname", $dbuser, $dbpass, {AutoCommit => 1});
  177 defined($dbh) || die "connection failed";
  178 
  179 #------------------------------------------------------------------------
  180 # insert values
  181 #------------------------------------------------------------------------
  182 #foreach my $table (keys(%initial_values)) {
  183 foreach my $table (@order) {
  184     foreach my $href (@{ $initial_values{$table} }) {
  185         @keys = keys(%{ $href });
  186         $keys = join(',', @keys);
  187         @vals = @{$href}{@keys};
  188         $vals = join(',', @vals);
  189         $sql = qq{INSERT INTO $table ($keys) VALUES ($vals);};
  190         print "SQL> $sql\n";
  191         $rv = $dbh->do($sql);
  192         defined($rv) || print "statement failed\n";
  193     }
  194 }
  195 
  196 $dbh->disconnect;