Index: Mapi.mx =================================================================== RCS file: /cvsroot/monetdb/clients/src/mapilib/Mapi.mx,v retrieving revision 1.62 diff -u -r1.62 Mapi.mx --- Mapi.mx 12 Jun 2009 07:54:40 -0000 1.62 +++ Mapi.mx 12 Jun 2009 09:49:53 -0000 @@ -49,55 +49,69 @@ @verbatim #include #include +#include -#define die(dbh,hdl) (hdl?mapi_explain_query(hdl,stderr): \ - dbh?mapi_explain(dbh,stderr): \ - fprintf(stderr,"command failed\n"), \ - exit(-1)) +void die(Mapi dbh, MapiHdl hdl) +{ + if (hdl != NULL) { + mapi_explain_query(hdl, stderr); + do { + if (mapi_result_error(hdl) != NULL) + mapi_explain_result(hdl, stderr); + } while (mapi_next_result(hdl) == 1); + mapi_close_handle(hdl); + mapi_destroy(dbh); + } else if (dbh != NULL) { + mapi_explain(dbh, stderr); + mapi_destroy(dbh); + } else { + fprintf(stderr, "command failed\n"); + } + exit(-1); +} -int main(int argc, char **argv) +MapiHdl query(Mapi dbh, char *q) +{ + MapiHdl ret = NULL; + if ((ret = mapi_query(dbh, q)) == NULL || mapi_error(dbh) != MOK) + die(dbh, ret); + return(ret); +} + +void update(Mapi dbh, char *q) +{ + MapiHdl ret = query(dbh, q); + if (mapi_close_handle(ret) != MOK) + die(dbh, ret); +} + +int main(int argc, char *argv[]) { Mapi dbh; MapiHdl hdl = NULL; + char *name; + char *age; - dbh = mapi_connect("localhost", 50000, "monetdb", "monetdb", "sql", NULL); + dbh = mapi_connect("localhost", 50000, "monetdb", "monetdb", "sql", "demo"); if (mapi_error(dbh)) die(dbh, hdl); - if ((hdl = mapi_query(dbh, "create table emp(name varchar(20), age int)")) == NULL - || mapi_error(dbh) != MOK) - die(dbh, hdl); - if (mapi_close_handle(hdl) != MOK) - die(dbh, hdl); - if ((hdl = mapi_query(dbh, "insert into emp values('John', 23)")) == NULL - || mapi_error(dbh) != MOK) - die(dbh, hdl); - mapi_close_handle(hdl); - if (mapi_error(dbh) != MOK) - die(dbh, hdl); - if ((hdl = mapi_query(dbh, "insert into emp values('Mary', 22)")) == NULL - || mapi_error(dbh) != MOK) - die(dbh, hdl); - mapi_close_handle(hdl); - if (mapi_error(dbh) != MOK) - die(dbh, hdl); - if ((hdl = mapi_query(dbh, "select * from emp")) == NULL - || mapi_error(dbh) != MOK) - die(dbh, hdl); + update(dbh, "CREATE TABLE emp (name VARCHAR(20), age INT)"); + update(dbh, "INSERT INTO emp VALUES ('John', 23)"); + update(dbh, "INSERT INTO emp VALUES ('Mary', 22)"); + + hdl = query(dbh, "SELECT * FROM emp"); while (mapi_fetch_row(hdl)) { - char *nme = mapi_fetch_field(hdl, 0); - char *age = mapi_fetch_field(hdl, 1); - printf("%s is %s\n", nme, age); + name = mapi_fetch_field(hdl, 0); + age = mapi_fetch_field(hdl, 1); + printf("%s is %s\n", name, age); } - if (mapi_error(dbh) != MOK) - die(dbh, hdl); + mapi_close_handle(hdl); - if (mapi_error(dbh) != MOK) - die(dbh, hdl); mapi_destroy(dbh); - return 0; + return(0); } @end verbatim @end example