home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / contrib / pgperl / testlibpq.pl < prev    next >
Encoding:
Perl Script  |  1992-08-27  |  3.5 KB  |  148 lines

  1. #!./pgperl
  2. # An example of how to use Postgres 2.0 from perl.
  3. # This example is modelled after the example in the libpq reference manual.
  4. # $Id: testlibpq.pl,v 1.3 1992/02/15 20:33:20 mer Exp $
  5. # $Log: testlibpq.pl,v $
  6. % Revision 1.3  1992/02/15  20:33:20  mer
  7. % updated by George Hartzell of the Stanford Genome project
  8. %
  9. #% Revision 1.2  92/02/07  16:33:47  hartzell
  10. #% *** empty log message ***
  11. #% 
  12. #% Revision 1.1  92/01/29  15:10:42  hartzell
  13. #% Initial revision
  14. #% 
  15. #% Revision 1.2  91/03/08  13:22:41  kemnitz
  16. #% added RCS header.
  17. #% 
  18. #
  19. # $Header: /genome/src/postgres/src/contrib/pgperl/RCS/testlibpq.pl,v 1.2 92/0
  20. 2/07 16:33:47 hartzell Exp $
  21. #
  22. #% Revision 1.1  90/10/24  20:31:22  cimarron
  23. #% Initial revision
  24. #% 
  25.  
  26. &init_handler();
  27.  
  28. # specify the database to access
  29. &PQsetdb ("cimarron");
  30.  
  31. printf("creating relation person:\n\n");
  32. &test_create();
  33.  
  34. printf("Relation person before appends:\n\n");
  35. &test_functions();
  36. &test_append();
  37.  
  38. printf("Relation person after appends:\n\n");
  39. &test_functions();
  40. &PQreset (); # why do I have to reset the line ?? :-(
  41. &test_remove();
  42.  
  43. printf("Relation person after removes:\n\n");
  44. &test_functions();
  45. &test_vars();
  46.  
  47. # finish execution
  48. &PQfinish ();
  49.  
  50. exit(0);
  51.  
  52. sub test_create {
  53.     local($query);
  54.  
  55.     $query = "create person (name = char16, age = int4, location = point)";
  56.     printf("query = %s\n", $query);
  57.     &PQexec($query);
  58. }
  59.  
  60. sub test_append {
  61.     local($i, $query);
  62.  
  63.     &PQexec("begin"); # transaction
  64.     for ($i=50; $i <= 150; $i = $i + 10) {
  65.     $query = "append person (name = \"fred\", age = $i, location = \"($i,10
  66. )\"::point)";
  67.     printf("query = %s\n", $query);
  68.     &PQexec($query);
  69.     }
  70.     &PQexec("end"); # transaction
  71. }
  72.  
  73. sub test_remove {
  74.     local($i, $query);
  75.     for ($i=50; $i <= 150; $i = $i + 10) {
  76.     $query = "delete person where person.age = $i ";
  77.     printf("query = %s\n", $query);
  78.     &PQexec($query);
  79.     }
  80. }
  81.  
  82. sub test_functions {
  83.     local($p, $g, $t, $n, $m, $k, $i, $j);
  84.  
  85.     # fetch tuples from the person table
  86.     &PQexec ("begin");
  87.     &PQexec ("retrieve portal eportal (person.all)");
  88.     &PQexec ("fetch all in eportal");
  89.     
  90.     # examine all the tuples fetched
  91.     $p = &PQparray ("eportal");    # remember: $p is a pointer !
  92.     $g = &PQngroups ($p);
  93.     $t = 0;
  94.  
  95.     for ($k=0; $k < $g; $k++) {
  96.     printf("\nNew tuple group:\n");
  97.     $n = &PQntuplesGroup($p, $k);
  98.     $m = &PQnfieldsGroup($p, $k);
  99.     
  100.     # print out the attribute names
  101.     for ($i=0; $i < $m; $i++) {
  102.         printf("%-15s", &PQfnameGroup($p, $k, $i));
  103.     }
  104.     printf("\n");
  105.     
  106.     # print out the tuples
  107.     for ($i=0; $i < $n; $i++) {
  108.         for ($j=0; $j < $m; $j++) {
  109.         printf("%-15s", &PQgetvalue($p, $t + $i, $j));
  110.         }
  111.         printf("\n");
  112.     }
  113.     $t = $t + $n;
  114.     }
  115.     
  116.     # close the portal
  117.     &PQexec ("close eportal");
  118.     &PQexec ("end");
  119.  
  120.     # try some other functions
  121.     printf("\nNumber of portals open: %d\n", &PQnportals(0));
  122. }
  123.  
  124. sub test_vars {
  125.     printf("PQhost = %s\n",     $PQhost);
  126.     printf("PQport = %s\n",      $PQport);
  127.     printf("PQtty = %s\n",      $PQtty);
  128.     printf("PQoption = %s\n",      $PQoption);
  129.     printf("PQdatabase = %s\n", $PQdatabase);
  130.     printf("PQportset = %d\n",     $PQportset);
  131.     printf("PQxactid = %d\n",      $PQxactid);
  132.     printf("PQinitstr = %s\n",     $PQinitstr);
  133.     printf("PQtracep = %d\n",      $PQtracep);
  134. }
  135.  
  136. sub init_handler {
  137.     $SIG{'HUP'} = 'handler';
  138.     $SIG{'INT'} = 'handler';
  139.     $SIG{'QUIT'} = 'handler';
  140. }
  141.  
  142. sub handler {  # 1st argument is signal name
  143.     local($sig) = @_;
  144.     print "Caught a SIG$sig--shutting down connection to Postgres.\n";
  145.     &PQfinish();
  146.     exit(0);
  147. }
  148.