|
| 1 | +/* |
| 2 | + * pgeasy.c |
| 3 | + * |
| 4 | +*/ |
| 5 | + |
| 6 | +#include<stdio.h> |
| 7 | +#include<string.h> |
| 8 | +#include<stdarg.h> |
| 9 | + |
| 10 | +#include<libpq-fe.h> |
| 11 | +#include"halt.h" |
| 12 | +#include"libpgeasy.h" |
| 13 | + |
| 14 | +#defineNUL '\0' |
| 15 | + |
| 16 | +#ifndefTRUE |
| 17 | +#defineTRUE 1 |
| 18 | +#endif |
| 19 | + |
| 20 | +#ifndefFALSE |
| 21 | +#defineFALSE 0 |
| 22 | +#endif |
| 23 | + |
| 24 | +/* GLOBAL VARIABLES */ |
| 25 | +staticPGconn*conn; |
| 26 | +staticPGresult*res=NULL; |
| 27 | + |
| 28 | +#defineON_ERROR_STOP0 |
| 29 | +#defineON_ERROR_CONTINUE1 |
| 30 | + |
| 31 | +staticinton_error_state=ON_ERROR_STOP; |
| 32 | + |
| 33 | +staticin_result_block= FALSE; |
| 34 | +staticwas_get_unset_result= FALSE; |
| 35 | + |
| 36 | +/* LOCAL VARIABLES */ |
| 37 | +staticinttuple; |
| 38 | + |
| 39 | +/* |
| 40 | +** |
| 41 | +**connectdb - returns PGconn structure |
| 42 | +** |
| 43 | +*/ |
| 44 | +PGconn* |
| 45 | +connectdb(char*dbName, |
| 46 | +char*pghost, |
| 47 | +char*pgport, |
| 48 | +char*pgoptions, |
| 49 | +char*pgtty) |
| 50 | +{ |
| 51 | +/* make a connection to the database */ |
| 52 | +conn=PQsetdb(pghost,pgport,pgoptions,pgtty,dbName); |
| 53 | +if (PQstatus(conn)==CONNECTION_BAD) |
| 54 | +halt("Connection to database '%s' failed.\n%s\n",dbName, |
| 55 | +PQerrorMessage(conn)); |
| 56 | +returnconn; |
| 57 | +} |
| 58 | + |
| 59 | +/* |
| 60 | +** |
| 61 | +**disconnectdb |
| 62 | +** |
| 63 | +*/ |
| 64 | +void |
| 65 | +disconnectdb() |
| 66 | +{ |
| 67 | +PQfinish(conn); |
| 68 | +} |
| 69 | + |
| 70 | +/* |
| 71 | +** |
| 72 | +**doquery - returns PGresult structure |
| 73 | +** |
| 74 | +*/ |
| 75 | +PGresult* |
| 76 | +doquery(char*query) |
| 77 | +{ |
| 78 | +if (res!=NULL&&in_result_block== FALSE&&was_get_unset_result== FALSE) |
| 79 | +PQclear(res); |
| 80 | + |
| 81 | +was_get_unset_result= FALSE; |
| 82 | +res=PQexec(conn,query); |
| 83 | + |
| 84 | +if (on_error_state==ON_ERROR_STOP&& |
| 85 | +(res==NULL|| |
| 86 | +PQresultStatus(res)==PGRES_BAD_RESPONSE|| |
| 87 | +PQresultStatus(res)==PGRES_NONFATAL_ERROR|| |
| 88 | +PQresultStatus(res)==PGRES_FATAL_ERROR)) |
| 89 | +{ |
| 90 | +if (res!=NULL) |
| 91 | +fprintf(stderr,"query error: %s\n",PQcmdStatus(res)); |
| 92 | +else |
| 93 | +fprintf(stderr,"connection error: %s\n",PQerrorMessage(conn)); |
| 94 | +PQfinish(conn); |
| 95 | +halt("failed request: %s\n",query); |
| 96 | +} |
| 97 | +tuple=0; |
| 98 | +returnres; |
| 99 | +} |
| 100 | + |
| 101 | +/* |
| 102 | +** |
| 103 | +**fetch - returns tuple number (starts at 0), or the value END_OF_TUPLES |
| 104 | +**NULL pointers are skipped |
| 105 | +** |
| 106 | +*/ |
| 107 | +int |
| 108 | +fetch(void*param,...) |
| 109 | +{ |
| 110 | +va_listap; |
| 111 | +intarg, |
| 112 | +num_fields; |
| 113 | + |
| 114 | +num_fields=PQnfields(res); |
| 115 | + |
| 116 | +if (tuple >=PQntuples(res)) |
| 117 | +returnEND_OF_TUPLES; |
| 118 | + |
| 119 | +va_start(ap,param); |
| 120 | +for (arg=0;arg<num_fields;arg++) |
| 121 | +{ |
| 122 | +if (param!=NULL) |
| 123 | +{ |
| 124 | +if (PQfsize(res,arg)==-1) |
| 125 | +{ |
| 126 | +memcpy(param,PQgetvalue(res,tuple,arg),PQgetlength(res,tuple,arg)); |
| 127 | +((char*)param)[PQgetlength(res,tuple,arg)]=NUL; |
| 128 | +} |
| 129 | +else |
| 130 | +memcpy(param,PQgetvalue(res,tuple,arg),PQfsize(res,arg)); |
| 131 | +} |
| 132 | +param=va_arg(ap,char*); |
| 133 | +} |
| 134 | +va_end(ap); |
| 135 | +returntuple++; |
| 136 | +} |
| 137 | + |
| 138 | +/* |
| 139 | +** |
| 140 | +**fetchwithnulls - returns tuple number (starts at 0), |
| 141 | +**or the value END_OF_TUPLES |
| 142 | +**Returns TRUE or FALSE into null indicator variables |
| 143 | +**NULL pointers are skipped |
| 144 | +*/ |
| 145 | +int |
| 146 | +fetchwithnulls(void*param,...) |
| 147 | +{ |
| 148 | +va_listap; |
| 149 | +intarg, |
| 150 | +num_fields; |
| 151 | + |
| 152 | +num_fields=PQnfields(res); |
| 153 | + |
| 154 | +if (tuple >=PQntuples(res)) |
| 155 | +returnEND_OF_TUPLES; |
| 156 | + |
| 157 | +va_start(ap,param); |
| 158 | +for (arg=0;arg<num_fields;arg++) |
| 159 | +{ |
| 160 | +if (param!=NULL) |
| 161 | +{ |
| 162 | +if (PQfsize(res,arg)==-1) |
| 163 | +{ |
| 164 | +memcpy(param,PQgetvalue(res,tuple,arg),PQgetlength(res,tuple,arg)); |
| 165 | +((char*)param)[PQgetlength(res,tuple,arg)]=NUL; |
| 166 | +} |
| 167 | +else |
| 168 | +memcpy(param,PQgetvalue(res,tuple,arg),PQfsize(res,arg)); |
| 169 | +} |
| 170 | +param=va_arg(ap,char*); |
| 171 | +if (PQgetisnull(res,tuple,arg)!=0) |
| 172 | +*(int*)param=1; |
| 173 | +else |
| 174 | +*(int*)param=0; |
| 175 | +param=va_arg(ap,char*); |
| 176 | +} |
| 177 | +va_end(ap); |
| 178 | +returntuple++; |
| 179 | +} |
| 180 | + |
| 181 | +/* |
| 182 | +** |
| 183 | +**on_error_stop |
| 184 | +** |
| 185 | +*/ |
| 186 | +void |
| 187 | +on_error_stop() |
| 188 | +{ |
| 189 | +on_error_state=ON_ERROR_STOP; |
| 190 | +} |
| 191 | + |
| 192 | +/* |
| 193 | +** |
| 194 | +**on_error_continue |
| 195 | +** |
| 196 | +*/ |
| 197 | +void |
| 198 | +on_error_continue() |
| 199 | +{ |
| 200 | +on_error_state=ON_ERROR_CONTINUE; |
| 201 | +} |
| 202 | + |
| 203 | + |
| 204 | +/* |
| 205 | +** |
| 206 | +**get_result |
| 207 | +** |
| 208 | +*/ |
| 209 | +PGresult* |
| 210 | +get_result() |
| 211 | +{ |
| 212 | +char*cmdstatus=PQcmdStatus(res); |
| 213 | + |
| 214 | +was_get_unset_result= TRUE; |
| 215 | + |
| 216 | +/* we have to store the fetch location somewhere */ |
| 217 | +cmdstatus[0]=NUL; |
| 218 | +memcpy(&cmdstatus[1],&tuple,sizeof(tuple)); |
| 219 | + |
| 220 | +returnres; |
| 221 | +} |
| 222 | + |
| 223 | +/* |
| 224 | +** |
| 225 | +**set_result |
| 226 | +** |
| 227 | +*/ |
| 228 | +void |
| 229 | +set_result(PGresult*newres) |
| 230 | +{ |
| 231 | + |
| 232 | +char*cmdstatus=PQcmdStatus(res); |
| 233 | + |
| 234 | +if (newres==NULL) |
| 235 | +halt("set_result called with null result pointer\n"); |
| 236 | + |
| 237 | +if (res!=NULL&&was_get_unset_result== FALSE) |
| 238 | +if (in_result_block== FALSE) |
| 239 | +PQclear(res); |
| 240 | +else |
| 241 | +{ |
| 242 | +cmdstatus[0]=NUL; |
| 243 | +memcpy(&cmdstatus[1],&tuple,sizeof(tuple)); |
| 244 | +} |
| 245 | + |
| 246 | +in_result_block= TRUE; |
| 247 | +was_get_unset_result= FALSE; |
| 248 | + |
| 249 | +cmdstatus=PQcmdStatus(newres); |
| 250 | +memcpy(&tuple,&cmdstatus[1],sizeof(tuple)); |
| 251 | + |
| 252 | +res=newres; |
| 253 | +} |
| 254 | + |
| 255 | + |
| 256 | +/* |
| 257 | +** |
| 258 | +**unset_result |
| 259 | +** |
| 260 | +*/ |
| 261 | +void |
| 262 | +unset_result(PGresult*oldres) |
| 263 | +{ |
| 264 | +char*cmdstatus=PQcmdStatus(oldres); |
| 265 | + |
| 266 | +if (oldres==NULL) |
| 267 | +halt("unset_result called with null result pointer\n"); |
| 268 | + |
| 269 | +if (in_result_block== FALSE) |
| 270 | +halt("Unset of result without being set.\n"); |
| 271 | + |
| 272 | +was_get_unset_result= TRUE; |
| 273 | +cmdstatus[0]=NUL; |
| 274 | +memcpy(&cmdstatus[1],&tuple,sizeof(tuple)); |
| 275 | +in_result_block= FALSE; |
| 276 | +} |
| 277 | + |
| 278 | +/* |
| 279 | +** |
| 280 | +**reset_fetch |
| 281 | +** |
| 282 | +*/ |
| 283 | +void |
| 284 | +reset_fetch() |
| 285 | +{ |
| 286 | +tuple=0; |
| 287 | +} |