CAMLprim value mapi_fetch_field_stub(value hdl, value index) { char *res = mapi_fetch_field((MapiHdl) hdl, Int_val(index)); if (res) { return Val_some(caml_copy_string(res)); } else { return Val_int(0); } }
int main(int argc, char **argv) { Mapi dbh; MapiHdl hdl = NULL; mapi_int64 rows; if (argc != 4) { printf("usage:%s <host> <port> <language>\n", argv[0]); exit(-1); } dbh = mapi_connect(argv[1], atoi(argv[2]), "monetdb", "monetdb", argv[3], NULL); if (dbh == NULL || mapi_error(dbh)) die(dbh, hdl); mapi_cache_limit(dbh, 2); /* mapi_trace_log(dbh, "/tmp/mapilog"); */ /* mapi_trace(dbh, 1); */ if (strcmp(argv[3], "sql") == 0) { /* switch of autocommit */ if (mapi_setAutocommit(dbh, 0) != MOK || mapi_error(dbh)) die(dbh,NULL); if ((hdl = mapi_query(dbh, "create table emp(name varchar(20), age int)")) == NULL || mapi_error(dbh)) 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)) die(dbh, hdl); if (mapi_close_handle(hdl) != MOK) die(dbh, hdl); if ((hdl = mapi_query(dbh, "insert into emp values('Mary', 22)")) == NULL || mapi_error(dbh)) die(dbh, hdl); if (mapi_close_handle(hdl) != MOK) die(dbh, hdl); if ((hdl = mapi_query(dbh, "select * from emp")) == NULL || mapi_error(dbh)) die(dbh, hdl); rows = mapi_fetch_all_rows(hdl); if (mapi_error(dbh)) die(dbh, hdl); printf("rows received " LLFMT "\n", rows); 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); } } else if (strcmp(argv[3], "mal") == 0) { if ((hdl = mapi_query(dbh, "emp := bat.new(:oid,:str);")) == NULL || mapi_error(dbh)) die(dbh, hdl); if ((hdl = mapi_query(dbh, "age := bat.new(:oid,:int);")) == NULL || mapi_error(dbh)) die(dbh, hdl); if (mapi_close_handle(hdl) != MOK) die(dbh, hdl); if ((hdl = mapi_query(dbh, "bat.append(emp, \"John\");")) == NULL || mapi_error(dbh)) die(dbh, hdl); if ((hdl = mapi_query(dbh, "bat.append(age, 23);")) == NULL || mapi_error(dbh)) die(dbh, hdl); if (mapi_close_handle(hdl) != MOK) die(dbh, hdl); if ((hdl = mapi_query(dbh, "bat.append(emp, \"Mary\");")) == NULL || mapi_error(dbh)) die(dbh, hdl); if ((hdl = mapi_query(dbh, "bat.append(age, 22);")) == NULL || mapi_error(dbh)) die(dbh, hdl); if (mapi_close_handle(hdl) != MOK) die(dbh, hdl); if ((hdl = mapi_query(dbh, "io.print(emp,age);")) == NULL || mapi_error(dbh)) die(dbh, hdl); rows = mapi_fetch_all_rows(hdl); if (mapi_error(dbh)) die(dbh, hdl); printf("rows received " LLFMT "\n", rows); while (mapi_fetch_row(hdl)) { char *nme = mapi_fetch_field(hdl, 1); char *age = mapi_fetch_field(hdl, 2); printf("%s is %s\n", nme, age); } } else { fprintf(stderr, "%s: unknown language, only mal and sql supported\n", argv[0]); exit(1); } if (mapi_error(dbh)) die(dbh, hdl); /* mapi_stat(dbh); printf("mapi_ping %d\n",mapi_ping(dbh)); */ if (mapi_close_handle(hdl) != MOK) die(dbh, hdl); mapi_destroy(dbh); return 0; }
int main(int argc, char **argv) { Mapi dbh; MapiHdl hdl = NULL; mapi_int64 rows, i; char *parm[] = { "peter", 0 }; char *parm2[] = { "25", 0 }; int j; if (argc != 4) { printf("usage:%s <host> <port> <language>\n", argv[0]); exit(-1); } dbh = mapi_connect(argv[1], atoi(argv[2]), "monetdb", "monetdb", argv[3], NULL); if (dbh == NULL || mapi_error(dbh)) die(dbh, hdl); /* mapi_trace(dbh, 1); */ if (strcmp(argv[3], "sql") == 0) { /* switch of autocommit */ if (mapi_setAutocommit(dbh, 0) != MOK || mapi_error(dbh)) die(dbh,NULL); if ((hdl = mapi_query(dbh, "create table emp(name varchar(20), age int)")) == NULL || mapi_error(dbh)) 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)) die(dbh, hdl); if (mapi_close_handle(hdl) != MOK) die(dbh, hdl); if ((hdl = mapi_query(dbh, "insert into emp values('Mary', 22)")) == NULL || mapi_error(dbh)) die(dbh, hdl); if (mapi_close_handle(hdl) != MOK) die(dbh, hdl); if ((hdl = mapi_query(dbh, "select * from emp")) == NULL || mapi_error(dbh)) die(dbh, hdl); } else if (strcmp(argv[3], "mal") == 0) { if ((hdl = mapi_query(dbh, "emp := bat.new(:oid,:str);")) == NULL || mapi_error(dbh)) die(dbh, hdl); if ((hdl = mapi_query(dbh, "age := bat.new(:oid,:int);")) == NULL || mapi_error(dbh)) die(dbh, hdl); if (mapi_close_handle(hdl) != MOK) die(dbh, hdl); if ((hdl = mapi_query_array(dbh, "bat.append(emp,\"?\");", parm)) == NULL || mapi_error(dbh)) die(dbh, hdl); if ((hdl = mapi_query_array(dbh, "bat.append(age,?);", parm2)) == NULL || mapi_error(dbh)) die(dbh, hdl); if (mapi_close_handle(hdl) != MOK) die(dbh, hdl); if ((hdl = mapi_query(dbh, "io.print(emp,age);")) == NULL || mapi_error(dbh)) die(dbh, hdl); } else { fprintf(stderr, "%s: unknown language, only mal and sql supported\n", argv[0]); exit(1); } /* Retrieve all tuples in the client cache first */ rows = mapi_fetch_all_rows(hdl); if (mapi_error(dbh)) die(dbh, hdl); printf("rows received " LLFMT " with %d fields\n", rows, mapi_get_field_count(hdl)); /* Interpret the cache as a two-dimensional array */ for (i = 0; i < rows; i++) { if (mapi_seek_row(hdl, i, MAPI_SEEK_SET) || mapi_fetch_row(hdl) == 0) break; for (j = 0; j < mapi_get_field_count(hdl); j++) { printf("%s=%s ", mapi_get_name(hdl, j), mapi_fetch_field(hdl, j)); } printf("\n"); } if (mapi_error(dbh)) die(dbh, hdl); if (mapi_close_handle(hdl) != MOK) die(dbh, hdl); mapi_destroy(dbh); return 0; }