dbcore_pgsql.pm 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #####################################################
  2. # dbcore_pgsql.pm
  3. #
  4. # These are the "core" routines for using a
  5. # PostgreSQL database.
  6. #
  7. # See COPYING for licensing information.
  8. #
  9. #####################################################
  10. # Perl modules
  11. use strict;
  12. use DBD::Pg;
  13. # global variables
  14. use vars qw($conf $dbh $dbconnected);
  15. $dbconnected = '';
  16. #############################################################
  17. # connect to database
  18. #############################################################
  19. sub dbconnect {
  20. # allow persistance?
  21. return
  22. if $dbh and
  23. $$conf{'persistance'} and
  24. $$conf{'persistance'} eq 'YES';
  25. # Name of GnuDIP (mySQL) database
  26. my $gnudipdatabase = $$conf{'gnudipdatabase'};
  27. # User name to connect to the database
  28. my $gnudipuser = $$conf{'gnudipuser'};
  29. # Password to connect to the database
  30. my $gnudippass = $$conf{'gnudippassword'};
  31. # Host where the database server resides
  32. my $gnudipserver = $$conf{'gnudipserver'};
  33. $gnudipdatabase = '' if !defined($gnudipdatabase);
  34. $gnudipuser = '' if !defined($gnudipuser);
  35. $gnudippass = '' if !defined($gnudippass);
  36. $gnudipserver = '' if !defined($gnudipserver);
  37. # connect to database
  38. my $gnudipconn = "dbi:Pg:dbname=$gnudipdatabase";
  39. $gnudipconn = "dbi:Pg:dbname=$gnudipdatabase;host=$gnudipserver"
  40. if $gnudipserver;
  41. $dbh = DBI->connect(
  42. $gnudipconn,
  43. $gnudipuser,
  44. $gnudippass
  45. );
  46. if (!$dbh) {
  47. my $str = $DBI::errstr;
  48. writelog('Could not connect to PostgreSQL database:');
  49. writelog(" message: $str");
  50. dberror();
  51. }
  52. }
  53. #############################################################
  54. # execute an SQL statement and catch errors
  55. #############################################################
  56. sub dbexecute {
  57. my $statement = shift;
  58. if (! $dbconnected) {
  59. dbconnect();
  60. $dbconnected = 1;
  61. }
  62. my $sth = $dbh->prepare($statement);
  63. if (!$sth) {
  64. my $str = $DBI::errstr;
  65. writelog('Could not prepare SQL statement:');
  66. writelog(" statement: $statement");
  67. writelog(" message: $str");
  68. dberror();
  69. }
  70. my $rc = $sth->execute;
  71. if (!$rc) {
  72. my $str = $DBI::errstr;
  73. writelog('Could not execute SQL statement:');
  74. writelog(" statement: $statement");
  75. writelog(" message: $str");
  76. dberror();
  77. }
  78. return $sth;
  79. }
  80. #####################################################
  81. # must return 1
  82. #####################################################
  83. 1;