File: test2.rb

package info (click to toggle)
libpgsql-ruby 0.7.1-10
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 192 kB
  • ctags: 231
  • sloc: ruby: 1,359; ansic: 1,260; makefile: 55; sh: 9
file content (44 lines) | stat: -rw-r--r-- 1,042 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#! /usr/bin/env ruby
#
# original file src/test/examples/testlibpq2.c
#                       Test of the asynchronous notification interface
# CREATE TABLE TBL1 (i int4);
# CREATE TABLE TBL2 (i int4);
# CREATE RULE r1 AS ON INSERT TO TBL1 DO (INSERT INTO TBL2 values (new.i); \
#                                         NOTIFY TBL2);
# Then start up this program
# After the program has begun, do
# INSERT INTO TBL1 values (10);


require 'postgres'

def main
  pghost = nil
  pgport = nil
  pgoptions = nil
  pgtty = nil
  dbname = ENV['USER'] 
  begin
    conn = PGconn.connect(pghost,pgport,pgoptions,pgtty,dbname)
  rescue PGError
    printf(STDERR, "Connection to database '%s' failed.\n",dbname)
    exit(2)
  end
  begin
    res = conn.exec("LISTEN TBL2")
  rescue PGError
    printf(STDERR, "LISTEN command failed\n")
    exit(2)
  end
  res.clear
  while 1
    notify = conn.get_notify
    if (notify)
      printf(STDERR,"ASYNC NOTIFY '%s' from backend pid '%d' received\n",notify[0],notify[1])
      break
    end
  end
end

main