Hi all,
Triggered by a request from a user I changed the mapilib example
somewhat. In particular, I tried to make the example more "school
style", e.g. don't use a define for something which could have been a
function. More important, I added code to walk multi-resultset handles
in case there is an error, such that it eventually gets printed. I also
closed the handle in the die function as proper cleanup example. I
removed some mapi_error checks that I thought were redundant, but I'm
not sure. Hence, please review the attached diff and comment before I
commit this.
The example compiles without warnings for me now, and also works for as
far as I can see.
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 <mapilib/Mapi.h>
#include <stdio.h>
+#include <stdlib.h>
-#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
------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
Monetdb-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/monetdb-developers