[Bio] / FigKernelPackages / DBrtns.pm Repository:
ViewVC logotype

Diff of /FigKernelPackages/DBrtns.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.5, Mon Nov 1 20:20:09 2004 UTC revision 1.15, Wed May 11 18:43:19 2005 UTC
# Line 59  Line 59 
59  }  }
60    
61  sub SQL {  sub SQL {
62      my($self,$sql,$verbose) = @_;      my($self, $sql, $verbose, @bind_values) = @_;
63      my($dbh,$sth,$rc,$tmp);      my($dbh,$sth,$rc,$tmp);
64    
65      if ($verbose)      if ($verbose)
# Line 69  Line 69 
69    
70      $dbh  = $self->{_dbh};      $dbh  = $self->{_dbh};
71    
72      if ($sql =~ /^select/i)      if ($sql =~ /^\s*select/i)
73      {      {
74          $tmp = $dbh->selectall_arrayref($sql);          eval {
75                $tmp = $dbh->selectall_arrayref($sql, undef, @bind_values);
76            };
77            if (!$tmp or $@)
78            {
79                confess "Try running 'load_links' and 'load_attributes' to fix this error:\n$@";
80                #print STDERR "DBrtns.pm error: SQL $sql failed\n";
81                #return;
82            }
83          return $tmp;          return $tmp;
84      }      }
85      else      else
86      {      {
87          return $dbh->do($sql);          my $rc;
88            my $ok = eval {
89                $rc = $dbh->do($sql, undef, @bind_values);
90            };
91    
92            if (!$ok)
93            {
94                confess "query failed: " . substr($sql, 0, 100) . ": $@";
95            }
96            return $rc;
97    
98    
99  #       $sth = $dbh->prepare($sql)  #       $sth = $dbh->prepare($sql)
100  #           or die "prepare failed: $DBI::errstr";  #           or die "prepare failed: $DBI::errstr";
101  #       $sth->execute()  #       $sth->execute()
# Line 119  Line 138 
138      {      {
139          $cmd = "DROP TABLE IF EXISTS $tbl;" ;          $cmd = "DROP TABLE IF EXISTS $tbl;" ;
140      }      }
141        elsif ($dbms eq "Pg")
142        {
143            if ($self->table_exists($tbl))
144            {
145                $cmd = "DROP TABLE $tbl CASCADE;" ;
146            }
147        }
148      else      else
149      {      {
150          if ($self->table_exists($tbl))          if ($self->table_exists($tbl))
# Line 139  Line 165 
165      my $flds = $arg{flds};      my $flds = $arg{flds};
166      my $dbh  = $self->{_dbh};      my $dbh  = $self->{_dbh};
167      my $dbms = $self->{_dbms};      my $dbms = $self->{_dbms};
168      $dbh->do("CREATE TABLE $tbl ( $flds );");  
169        my $type;
170    
171        if ($arg{type})
172        {
173            $type = " engine = $arg{type} ";
174        }
175        $dbh->do("CREATE TABLE $tbl ( $flds ) $type;");
176  }  }
177    
178  sub load_table {  sub load_table {
# Line 151  Line 184 
184      my $delim    = $arg{delim};      my $delim    = $arg{delim};
185      my $dbh  = $self->{_dbh};      my $dbh  = $self->{_dbh};
186      my $dbms = $self->{_dbms};      my $dbms = $self->{_dbms};
187        my $result;
188    
189      if ($file)      if ($file)
190      {      {
191          if ($dbms eq "mysql")          if ($dbms eq "mysql")
192          {          {
193              $dbh->do("LOAD DATA LOCAL INFILE '$file' REPLACE INTO TABLE $tbl;");              $result = $dbh->do("LOAD DATA LOCAL INFILE '$file' REPLACE INTO TABLE $tbl;");
194          }          }
195          elsif ($dbms eq "Pg")          elsif ($dbms eq "Pg")
196          {          {
197              $dbh->do("COPY $tbl FROM '$file' USING DELIMITERS '$delim';");              $result = $dbh->do("COPY $tbl FROM '$file' USING DELIMITERS '$delim';");
198          }          }
199      }      }
200        return $result;
201  }  }
202    
203  sub create_index {  sub create_index {
# Line 218  Line 253 
253      }      }
254  }  }
255    
256    =pod
257    
258    =head1 get_inserted_id
259    
260    Return the last ID of a row inserted into an autonumber/serial-containing table.
261    
262    =cut
263    
264    sub get_inserted_id
265    {
266        my($self, $table, $sth) = @_;
267        if ($self->{_dbms} eq "Pg")
268        {
269            my $oid = $sth->{pg_oid_status};
270            my $ret = $self->SQL("select id from $table where oid = ?", undef, $oid);
271            return $ret->[0]->[0];
272        }
273        elsif ($self->{_dbms} eq "mysql")
274        {
275            my $id = $self->{_dbh}->{mysql_insertid};
276            # print "mysql got $id\n";
277            return $id;
278        }
279    }
280    
281    
282  #  #
283  # Following are database administration routines. They create an instance of a ServerAdmin class  # Following are database administration routines. They create an instance of a ServerAdmin class
284  # for the appropriate server type (in order to eliminate the if mysql / if pg / etc stuff).  # for the appropriate server type (in order to eliminate the if mysql / if pg / etc stuff).
# Line 277  Line 338 
338    
339      my $rc = system($exe,      my $rc = system($exe,
340                      "--datadir=$db_dir",                      "--datadir=$db_dir",
341                        "--basedir=$FIG_Config::common_runtime",
342                      "--user=$FIG_Config::dbuser");                      "--user=$FIG_Config::dbuser");
343      if ($rc != 0)      if ($rc != 0)
344      {      {
# Line 340  Line 402 
402      push(@opts, "--basedir=$FIG_Config::common_runtime");      push(@opts, "--basedir=$FIG_Config::common_runtime");
403      push(@opts, "--datadir=$FIG_Config::db_datadir");      push(@opts, "--datadir=$FIG_Config::db_datadir");
404      push(@opts, "--ledir=$FIG_Config::common_runtime/libexec");      push(@opts, "--ledir=$FIG_Config::common_runtime/libexec");
405        push(@opts, "--old-password");
406        push(@opts, "--max-allowed-packet=128M");
407      #      #
408      # Oddly, this doesn't seem to work. need to set the environment variable.      # Oddly, this doesn't seem to work. need to set the environment variable.
409      #      #
410      #push(@opts, "--port=$FIG_Config::dbport");      #push(@opts, "--port=$FIG_Config::dbport");
411    
412        if (@FIG_Config::db_server_startup_options)
413        {
414            push(@opts, @FIG_Config::db_server_startup_options)
415        }
416    
417      #      #
418      # We are going to assume that if mysql has shipped with this release, we'll use it. Otherwise      # We are going to assume that if mysql has shipped with this release, we'll use it. Otherwise
419      # try to use a system one.      # try to use a system one.
# Line 356  Line 425 
425          $exe = "mysqld_safe";          $exe = "mysqld_safe";
426      }      }
427    
428        print "Would run $exe @opts\n";
429    
430      my $pid = fork;      my $pid = fork;
431    
432      if ($pid == 0)      if ($pid == 0)

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.15

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3