#!/usr/bin/python
#
# I got a bug in PostgreSQL.
#
# Import the files:
#	 "szervizsor","javitvavissza","javitvafizetve","javitvacsere".sql
#
# and set the name of your database to DBNAME
#
# The following program produces the error on
# PostgreSQL 7.1.2 on i686-pc-linux-gnu, compiled by GCC egcs-2.91.66
# PostgreSQL 7.1 on i586-pc-linux-gnu, compiled by GCC 2.96
#
# Basically this runs "except all" from a large set of data.
# There are 2 records with rma=23034 in the original table (szervizsor).
# There is 1 record with rma=23034 in "javitvavissza"
# There are no records with rma=23034 in the other tables "javitvafizetve" and
#   "javitvacsere".
# If I except all "javitva*" tables from "szervizsor" based on rma, I should 
# have 1 record with rma=23034 in the result.
# When I put "where" to filter for rma=23034 in "szervizsor", I get correct
# result.
# When I return all records, rma=23034 disappears from the result.
# This is a big problem for me.
#
#
# Thanks, Gerzson Szinyei <gerzson@elender.hu>
#

import pg

DBNAME='set the name of your database'

conn=pg.connect(DBNAME)

print "Prerequisites:"
print
print "Counting records of rma=23034 in the tables:"
ts=("szervizsor","javitvavissza","javitvafizetve","javitvacsere")
for t in ts:
    qry="select count(*) from "+t+" where rma=23034;"
    data=conn.query(qry).getresult()
    val=data[0][0]
    print "table",t," has ",val," records"

print "(szervizsor except all javitvavissza except all javitvafizetve except all javitvacsere) should have 1 record of rma=23034."
print
print "Test 1:"
print 

# This query runs well
qry="select rma from szervizsor where rma=23034 except all select rma from javitvavissza except all select rma from javitvafizetve except all select rma from javitvacsere;"
print "query =",qry
print

r=conn.query(qry).dictresult()
print r
if len(r):
    print "One record found, CORRECT"

print
print "Test 2"
print

# This query fails
qry="select rma from szervizsor except all select rma from javitvavissza except all select rma from javitvafizetve except all select rma from javitvacsere;"
print "query =",qry
print
r=conn.query(qry).dictresult()

found=0
for i in r:
    if i['rma']==23034:
	print i
	found=found+1

if found==1:
    print "1 record found, CORRECT"
else:
    print found," record(s) found, BAD"

