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 <mapi.h> |
13 | |
14 | #define die(dbh,hdl) do { \ |
15 | if (hdl) \ |
16 | mapi_explain_result(hdl,stderr); \ |
17 | else if (dbh) \ |
18 | mapi_explain(dbh,stderr); \ |
19 | else \ |
20 | fprintf(stderr,"command failed\n"); \ |
21 | exit(-1); \ |
22 | } while (0) |
23 | |
24 | int |
25 | main(int argc, char **argv) |
26 | { |
27 | Mapi dbh; |
28 | MapiHdl hdl = NULL; |
29 | |
30 | if (argc != 4) { |
31 | printf("usage:%s <host> <port> <language>\n" , argv[0]); |
32 | exit(-1); |
33 | } |
34 | |
35 | dbh = mapi_connect(argv[1], atoi(argv[2]), "monetdb" , "monetdb" , argv[3], NULL); |
36 | if (dbh == NULL || mapi_error(dbh)) |
37 | die(dbh, hdl); |
38 | |
39 | /* mapi_trace(dbh, true); */ |
40 | mapi_cache_limit(dbh, 2); |
41 | if (strcmp(argv[3], "sql" ) == 0) { |
42 | /* switch of autocommit */ |
43 | if (mapi_setAutocommit(dbh, false) != MOK || mapi_error(dbh)) |
44 | die(dbh,NULL); |
45 | if ((hdl = mapi_query(dbh, "create table emp(name varchar(20), age int)" )) == NULL || mapi_error(dbh)) |
46 | die(dbh, hdl); |
47 | if (mapi_close_handle(hdl) != MOK) |
48 | die(dbh, hdl); |
49 | if ((hdl = mapi_query(dbh, "insert into emp values('John', 23)" )) == NULL || mapi_error(dbh)) |
50 | die(dbh, hdl); |
51 | if (mapi_close_handle(hdl) != MOK) |
52 | die(dbh, hdl); |
53 | if ((hdl = mapi_query(dbh, "insert into emp values('Mary', 22)" )) == NULL || mapi_error(dbh)) |
54 | die(dbh, hdl); |
55 | if (mapi_close_handle(hdl) != MOK) |
56 | die(dbh, hdl); |
57 | if ((hdl = mapi_query(dbh, "select * from emp" )) == NULL || mapi_error(dbh)) |
58 | die(dbh, hdl); |
59 | while (mapi_fetch_row(hdl)) { |
60 | char *nme = mapi_fetch_field(hdl, 0); |
61 | char *age = mapi_fetch_field(hdl, 1); |
62 | |
63 | printf("%s is %s\n" , nme, age); |
64 | } |
65 | } else if (strcmp(argv[3], "mal" ) == 0) { |
66 | if ((hdl = mapi_query(dbh, "emp := bat.new(:oid,:str);" )) == NULL || mapi_error(dbh)) |
67 | die(dbh, hdl); |
68 | if ((hdl = mapi_query(dbh, "age := bat.new(:oid,:int);" )) == NULL || mapi_error(dbh)) |
69 | die(dbh, hdl); |
70 | if (mapi_close_handle(hdl) != MOK) |
71 | die(dbh, hdl); |
72 | if ((hdl = mapi_query(dbh, "bat.append(emp, \"John\");" )) == NULL || mapi_error(dbh)) |
73 | die(dbh, hdl); |
74 | if ((hdl = mapi_query(dbh, "bat.append(age, 23);" )) == NULL || mapi_error(dbh)) |
75 | die(dbh, hdl); |
76 | if (mapi_close_handle(hdl) != MOK) |
77 | die(dbh, hdl); |
78 | if ((hdl = mapi_query(dbh, "bat.append(emp, \"Mary\");" )) == NULL || mapi_error(dbh)) |
79 | die(dbh, hdl); |
80 | if ((hdl = mapi_query(dbh, "bat.append(age, 22);" )) == NULL || mapi_error(dbh)) |
81 | die(dbh, hdl); |
82 | if (mapi_close_handle(hdl) != MOK) |
83 | die(dbh, hdl); |
84 | if ((hdl = mapi_query(dbh, "io.print(emp,age);" )) == NULL || mapi_error(dbh)) |
85 | die(dbh, hdl); |
86 | while (mapi_fetch_row(hdl)) { |
87 | char *nme = mapi_fetch_field(hdl, 1); |
88 | char *age = mapi_fetch_field(hdl, 2); |
89 | printf("%s is %s\n" , nme, age); |
90 | } |
91 | } else { |
92 | fprintf(stderr, "%s: unknown language, only mal and sql supported\n" , argv[0]); |
93 | exit(1); |
94 | } |
95 | |
96 | if (mapi_error(dbh)) |
97 | die(dbh, hdl); |
98 | /* mapi_stat(dbh); |
99 | printf("mapi_ping %d\n",mapi_ping(dbh)); */ |
100 | if (mapi_close_handle(hdl) != MOK) |
101 | die(dbh, hdl); |
102 | mapi_destroy(dbh); |
103 | |
104 | return 0; |
105 | } |
106 | |