# NOT RUN {
# create a database
createdb("rpgtesting")
connect("rpgtesting")
begin()
# write data frame contents
data(mtcars)
write_table(mtcars)
# async processing on smallish result
# this wont be interesting if your machine is very fast
async_query("SELECT a.* FROM mtcars a, mtcars b")
repeat
{
status = async_status()
if ( status != "BUSY" ) break
cat("busy...\n")
Sys.sleep(1)
}
print(status)
head(fetch())
finish_async()
Sys.sleep(1)
# async processing on larger result
async_query("SELECT a.* FROM mtcars a, mtcars b, mtcars c")
count = 0
repeat
{
status = async_status()
if ( status == "BUSY" )
{
if ( count > 2 )
{
cat("calling cancel...\n")
cancel()
}
}
else break
cat("busy... \n")
Sys.sleep(1)
count = count + 1
}
print(status)
finish_async()
# you can run multiple queries with async_query
rollback(); begin()
write_table(mtcars)
sql1 = "SELECT mpg FROM mtcars LIMIT 3"
sql2 = "SELECT cyl FROM mtcars LIMIT 4"
async_query(paste(sql1, sql2, sep = "; "))
while ( async_status() == "BUSY" ) NULL
fetch()
while ( is_busy() ) NULL
async_status()
fetch()
finish_async()
# issue an async query and come back later
async_query(sql1)
push_conn()
connect("rpgtesting")
# fails because of transaction isolation
fetch(sql2)
pop_conn()
async_status()
# results from sql1
fetch()
# this is automatic if you issue new queries
finish_async()
# cleanup
rollback()
disconnect()
dropdb("rpgtesting")
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab