
from common.database.Database import database
import sys,time

# Simple function to do queries
def do_query(q):
    database.connect()
    c = database.cursor()
    c.execute(q)
    results = c.fetchall()
    c.close()
    return results

# Determine what query we want to test
if not len(sys.argv) == 2:
    print """Usage: "awe %s <number>" where number is
  1 for simple query of a SourceList
  2 for NeighBoursTest query
  3 for RadiusTest query""" % (sys.argv[0])
    sys.exit()

if sys.argv[1] == '2':
    # This query will fail the second time
    query = 'SELECT * FROM TABLE(AWOPER.AW_UTIL.NEIGHBOURSTEST(4067390, 9, 9))'
    
elif sys.argv[1] == '3':
    # This query will also fail
    query = 'SELECT * FROM TABLE(AWOPER.AW_UTIL.RADIUSTEST(15, 243.000000, 27.000000, 5.000000/3600.0))'
    
else:
    # This query will succeed
    query = 'SELECT "SLID","SID","HTM" FROM AWOPER."SOURCELIST*SOURCES" T WHERE T.SLID = 136111 AND T.SID = 10'


# Tell the user about the query
print "query:",query

# Do the query for the first time, will always work
data1 = do_query(query)
print "data1: %i rows" % (len(data1))

# The RPC Timeout will occur after about half an hour
print "sleeping for 35 minutes"
minutes = 35
for i in range(minutes):
        print "sleeping minute %i/%i" % (i,minutes)
        time.sleep(60)


# Try again, it will fail in query 2 and 3
data2 = do_query(query)
print "data2: %i rows" % (len(data2))

# cx_Oracle.DatabaseError: ORA-28576: lost RPC connection to external procedure agent




