import asyncio
import pyngres.asyncio as py

async def spin(connected):
    '''display a spinning rotor until connected'''

    rotor = '-\\|/'
    i = 0
    while not connected.is_set():
        i = (i+1) % 4
        output = rotor[i] + ' ' + '\b\b' 
        print(output, end='', flush=True)
        await asyncio.sleep(0.10)
    print('connected')

async def connect(connected):
    '''connect to an Actian database'''

    print('connecting...')
    inp = py.IIAPI_INITPARM()
    inp.in_timeout = -1
    inp.in_version = py.IIAPI_VERSION
    py.IIapi_initialize(inp)
    envHandle = inp.in_envHandle

    dbname = 'loathing::sandbox'
    target = dbname.encode()
    
    cop = py.IIAPI_CONNPARM()
    cop.co_target = target
    cop.co_connHandle = envHandle
    cop.co_type = py.IIAPI_CT_SQL
    cop.co_timeout = -1

    await py.IIapi_connect(cop)

    connected.set()

async def main():
    '''start the tasks to run concurrently'''

    connected = asyncio.Event()
    spinner = spin(connected)
    connector = connect(connected)
    await asyncio.gather(spinner,connector)

asyncio.run(main())
quit()
