| 1 | /* |
| 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | * |
| 6 | * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V. |
| 7 | */ |
| 8 | |
| 9 | #include <stdlib.h> |
| 10 | #include <stdio.h> |
| 11 | #include <string.h> |
| 12 | #include <inttypes.h> |
| 13 | #include <mapi.h> |
| 14 | |
| 15 | #define die(dbh,hdl) do { \ |
| 16 | if (hdl) \ |
| 17 | mapi_explain_result(hdl,stderr); \ |
| 18 | else if (dbh) \ |
| 19 | mapi_explain(dbh,stderr); \ |
| 20 | else \ |
| 21 | fprintf(stderr,"command failed\n"); \ |
| 22 | exit(-1); \ |
| 23 | } while (0) |
| 24 | |
| 25 | int |
| 26 | main(int argc, char **argv) |
| 27 | { |
| 28 | Mapi dbh; |
| 29 | MapiHdl hdl = NULL; |
| 30 | int64_t rows; |
| 31 | |
| 32 | if (argc != 4) { |
| 33 | printf("usage:%s <host> <port> <language>\n" , argv[0]); |
| 34 | exit(-1); |
| 35 | } |
| 36 | |
| 37 | dbh = mapi_connect(argv[1], atoi(argv[2]), "monetdb" , "monetdb" , argv[3], NULL); |
| 38 | if (dbh == NULL || mapi_error(dbh)) |
| 39 | die(dbh, hdl); |
| 40 | |
| 41 | mapi_cache_limit(dbh, 2); |
| 42 | /* mapi_trace_log(dbh, "/tmp/mapilog"); */ |
| 43 | /* mapi_trace(dbh, true); */ |
| 44 | if (strcmp(argv[3], "sql" ) == 0) { |
| 45 | /* switch of autocommit */ |
| 46 | if (mapi_setAutocommit(dbh, false) != MOK || mapi_error(dbh)) |
| 47 | die(dbh,NULL); |
| 48 | if ((hdl = mapi_query(dbh, "create table emp(name varchar(20), age int)" )) == NULL || mapi_error(dbh)) |
| 49 | die(dbh, hdl); |
| 50 | if (mapi_close_handle(hdl) != MOK) |
| 51 | die(dbh, hdl); |
| 52 | if ((hdl = mapi_query(dbh, "insert into emp values('John', 23)" )) == NULL || mapi_error(dbh)) |
| 53 | die(dbh, hdl); |
| 54 | if (mapi_close_handle(hdl) != MOK) |
| 55 | die(dbh, hdl); |
| 56 | if ((hdl = mapi_query(dbh, "insert into emp values('Mary', 22)" )) == NULL || mapi_error(dbh)) |
| 57 | die(dbh, hdl); |
| 58 | if (mapi_close_handle(hdl) != MOK) |
| 59 | die(dbh, hdl); |
| 60 | if ((hdl = mapi_query(dbh, "select * from emp" )) == NULL || mapi_error(dbh)) |
| 61 | die(dbh, hdl); |
| 62 | rows = mapi_fetch_all_rows(hdl); |
| 63 | if (mapi_error(dbh)) |
| 64 | die(dbh, hdl); |
| 65 | printf("rows received %" PRId64 "\n" , rows); |
| 66 | while (mapi_fetch_row(hdl)) { |
| 67 | char *nme = mapi_fetch_field(hdl, 0); |
| 68 | char *age = mapi_fetch_field(hdl, 1); |
| 69 | |
| 70 | printf("%s is %s\n" , nme, age); |
| 71 | } |
| 72 | } else if (strcmp(argv[3], "mal" ) == 0) { |
| 73 | if ((hdl = mapi_query(dbh, "emp := bat.new(:oid,:str);" )) == NULL || mapi_error(dbh)) |
| 74 | die(dbh, hdl); |
| 75 | if ((hdl = mapi_query(dbh, "age := bat.new(:oid,:int);" )) == NULL || mapi_error(dbh)) |
| 76 | die(dbh, hdl); |
| 77 | if (mapi_close_handle(hdl) != MOK) |
| 78 | die(dbh, hdl); |
| 79 | if ((hdl = mapi_query(dbh, "bat.append(emp, \"John\");" )) == NULL || mapi_error(dbh)) |
| 80 | die(dbh, hdl); |
| 81 | if ((hdl = mapi_query(dbh, "bat.append(age, 23);" )) == NULL || mapi_error(dbh)) |
| 82 | die(dbh, hdl); |
| 83 | if (mapi_close_handle(hdl) != MOK) |
| 84 | die(dbh, hdl); |
| 85 | if ((hdl = mapi_query(dbh, "bat.append(emp, \"Mary\");" )) == NULL || mapi_error(dbh)) |
| 86 | die(dbh, hdl); |
| 87 | if ((hdl = mapi_query(dbh, "bat.append(age, 22);" )) == NULL || mapi_error(dbh)) |
| 88 | die(dbh, hdl); |
| 89 | if (mapi_close_handle(hdl) != MOK) |
| 90 | die(dbh, hdl); |
| 91 | if ((hdl = mapi_query(dbh, "io.print(emp,age);" )) == NULL || mapi_error(dbh)) |
| 92 | die(dbh, hdl); |
| 93 | rows = mapi_fetch_all_rows(hdl); |
| 94 | if (mapi_error(dbh)) |
| 95 | die(dbh, hdl); |
| 96 | printf("rows received %" PRId64 "\n" , rows); |
| 97 | while (mapi_fetch_row(hdl)) { |
| 98 | char *nme = mapi_fetch_field(hdl, 1); |
| 99 | char *age = mapi_fetch_field(hdl, 2); |
| 100 | |
| 101 | printf("%s is %s\n" , nme, age); |
| 102 | } |
| 103 | } else { |
| 104 | fprintf(stderr, "%s: unknown language, only mal and sql supported\n" , argv[0]); |
| 105 | exit(1); |
| 106 | } |
| 107 | |
| 108 | if (mapi_error(dbh)) |
| 109 | die(dbh, hdl); |
| 110 | /* mapi_stat(dbh); |
| 111 | printf("mapi_ping %d\n",mapi_ping(dbh)); */ |
| 112 | if (mapi_close_handle(hdl) != MOK) |
| 113 | die(dbh, hdl); |
| 114 | mapi_destroy(dbh); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |