I am trying to create an array of global functions from the postgres
library mainly to ease passing parameters amongst other things. I have snipped some repetitive code out. I am sure that there are a few errors in the way I am using imc so any corrections or pointers would be much appreciated, I am quite new to it.
I have been using the docs to try and pick this up and I am having a hard time........... imcc.faq version December 2001 hint hint.
1 .pcc_sub _MAIN prototyped
2 .param pmc argv
3 .include "/home/parrot/parrot/library/postgres.pasm"
4 .include "/home/parrot/lib/postgreslib.imc"
5 .include "/home/parrot/lib/Commandlib.imc"
6
7
8
9 dbstring = "host=lhost dbname=Forum user=user password=password"
10
11 print "Entering Connect\n"
12 .pcc_begin prototyped
13 .arg Command
14 .arg dbstring
15 .pcc_call connect
16 retconnect:
17 .result CONN
18 .result int_answer
19 .result message
20 .pcc_end
21 .PRINT("Connection Message = ", message, " \n")
22 .PRINT("Connection state = ", int_answer, " \n")
23 eq -1, int_answer, fail
24 eq 1, int_answer, go
25 fail:
26 .PRINT("\n", message, "\n")
27 end
28 go:
29
30 query = "select * from parrot"
31 print "Entering Send Query \n"
32 .pcc_begin prototyped
33 .arg Command
34 .arg CONN
35 .arg query
36 .pcc_call pqsendquery
37 ret:
38 .result message
39 .pcc_end
postgreslib.imc contains sub definitions as follows
47 .sym Sub pqsendquery 48 newsub pqsendquery, .Sub, _pqsendquery 49 .local pmc PQSENDQUERY 50 PQSENDQUERY = global "PostgreSQL::PQsendQuery" 51 52 53 54 .sym Sub PQconnectStart 55 newsub PQconnectStart, .Sub, p_PQconnectStart_t 56 .local pmc PQCONNECTSTART 57 58 PQCONNECTSTART = global "PostgreSQL::PQconnectStart" 59 60 61 .sym Sub PQconnectPoll 62 newsub PQconnectPoll, .Sub, i_PQconnectPoll_p 63 .local pmc PQCONNECTPOLL 64 65 PQCONNECTPOLL = global "PostgreSQL::PQconnectPoll"
Commanlib.imc is where I will build an array to contain all the subs to call.
1 .local PerlArray Command 2 Command = new PerlArray 3 4 Command[0] = PQCONNECTSTART 5 Command[1] = PQCONNECTPOLL 6 Command[18] = PQSTATUS 7 Command[31] = PQSENDQUERY 8 9 #Command[2] = PQCONNECTDB 10 #Command[3] = PQSETDBLOGIN 11 #Command[4] = PQFINISH 12 #Command[5] = PQCONNDEFAULTS 13 #Command[6] = PQCONNINFOFREE 14 #Command[7] = PQRESETSTART 15 #Command[8] = PQRESETPOLL 16 #Command[9] = PQRESET 17 #Command[10] = PQREQUESTCANCEL
The first function call to "connect" works and the array "Command" gets passed into the funtion as follows. You will notice below that to get it to work I had to take a local copy of the passed in array or it got clobbered???????
241 .pcc_sub _connect prototyped 242 .param PerlArray Command 243 .param PerlString s 244 .local PerlString message 245 message = new PerlString 246 247 .local PerlArray C 248 C = new PerlArray 249 C = Command 250 251 .local pmc CONN 252 .local int int_answer 253 print "About to Connect\n" 254 P0 = C[0] 255 S5 = s 256 invoke 257 CONN = P5
Various magic things happen in this function and we get a connection to postgres and pass the CONN back out.
When I call the next funtion on line 32 above the call works but the Array has not been passed correctly. You can see in the following function that I am taking a local copy again.
192 .pcc_sub _pqsendquery prototyped 193 .param PerlArray Command 194 .param pmc CONN 195 .param PerlString query 196 .local PerlArray C 197 C = new PerlArray 198 C = Command 199 .local int answer 200 .local PerlString message 201 .PRINT("Sending query now\n", query, "\n\n") 202 P0 = C[31] 203 P5 = CONN 204 S5 = query 205 invoke 206 answer = I5 207 eq 0, answer, error 208 eq 1, answer, good 209 error: 210 .PQerrorMessage(CONN, message) 211 branch finish 212 good: 213 message = "Successful\n" 214 finish: 215 .pcc_begin_return 216 .return message 217 .pcc_end_return 218 end 219 .end
Running this fails with the followng error.
set_string_native() not implemented in class 'PerlArray'
which suggests that I am trying to set an Array to a string which is obviously wrong.
I am pretty sure that I am butchering imc syntax throughout this but it has got me a bit stumped. I have managed to get as far as getting data out of the database when I don't use the array so I am not 100% if I am butchering the syntax that much. Unfortunately I have been trying to RTFM but most of the FM's are out of date ;-)
Harry