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 | if (strcmp(argv[3], "sql" ) == 0) { |
41 | /* switch of autocommit */ |
42 | if (mapi_setAutocommit(dbh, false) != MOK || mapi_error(dbh)) |
43 | die(dbh,NULL); |
44 | if ((hdl = mapi_query(dbh, "create table emp(name varchar(20), age int)" )) == NULL || mapi_error(dbh)) |
45 | die(dbh, hdl); |
46 | if (mapi_close_handle(hdl) != MOK) |
47 | die(dbh, hdl); |
48 | if ((hdl = mapi_query(dbh, "insert into emp values('John', 23)" )) == 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('Mary', 22)" )) == 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, "select * from emp" )) == NULL || mapi_error(dbh)) |
57 | die(dbh, hdl); |
58 | while (mapi_fetch_row(hdl)) { |
59 | char *nme = mapi_fetch_field(hdl, 0); |
60 | char *age = mapi_fetch_field(hdl, 1); |
61 | |
62 | printf("%s is %s\n" , nme, age); |
63 | } |
64 | } else if (strcmp(argv[3], "mal" ) == 0) { |
65 | if ((hdl = mapi_query(dbh, "emp := bat.new(:oid,:str);" )) == NULL || mapi_error(dbh)) |
66 | die(dbh, hdl); |
67 | if ((hdl = mapi_query(dbh, "age := bat.new(:oid,:int);" )) == NULL || mapi_error(dbh)) |
68 | die(dbh, hdl); |
69 | if (mapi_close_handle(hdl) != MOK) |
70 | die(dbh, hdl); |
71 | if ((hdl = mapi_query(dbh, "bat.append(emp, \"John\");" )) == NULL || mapi_error(dbh)) |
72 | die(dbh, hdl); |
73 | if ((hdl = mapi_query(dbh, "bat.append(age, 23);" )) == NULL || mapi_error(dbh)) |
74 | die(dbh, hdl); |
75 | if (mapi_close_handle(hdl) != MOK) |
76 | die(dbh, hdl); |
77 | if ((hdl = mapi_query(dbh, "bat.append(emp, \"Mary\");" )) == NULL || mapi_error(dbh)) |
78 | die(dbh, hdl); |
79 | if ((hdl = mapi_query(dbh, "bat.append(age, 22);" )) == NULL || mapi_error(dbh)) |
80 | die(dbh, hdl); |
81 | if (mapi_close_handle(hdl) != MOK) |
82 | die(dbh, hdl); |
83 | if ((hdl = mapi_query(dbh, "io.print(emp,age);" )) == NULL || mapi_error(dbh)) |
84 | die(dbh, hdl); |
85 | while (mapi_fetch_row(hdl)) { |
86 | char *nme = mapi_fetch_field(hdl, 1); |
87 | char *age = mapi_fetch_field(hdl, 2); |
88 | printf("%s is %s\n" , nme, age); |
89 | } |
90 | } else { |
91 | fprintf(stderr, "%s: unknown language, only mal and sql supported\n" , argv[0]); |
92 | exit(1); |
93 | } |
94 | |
95 | if (mapi_error(dbh)) |
96 | die(dbh, hdl); |
97 | /* mapi_stat(dbh); |
98 | printf("mapi_ping %d\n",mapi_ping(dbh)); */ |
99 | if (mapi_close_handle(hdl) != MOK) |
100 | die(dbh, hdl); |
101 | mapi_destroy(dbh); |
102 | |
103 | return 0; |
104 | } |
105 | |