| 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 | #ifdef _MSC_VER | 
|---|
| 10 | /* suppress deprecation warning for snprintf */ | 
|---|
| 11 | #define _CRT_SECURE_NO_WARNINGS | 
|---|
| 12 | #endif | 
|---|
| 13 |  | 
|---|
| 14 | #include <stdlib.h> | 
|---|
| 15 | #include <stdio.h> | 
|---|
| 16 | #include <string.h> | 
|---|
| 17 | #include <mapi.h> | 
|---|
| 18 |  | 
|---|
| 19 | #ifdef _MSC_VER | 
|---|
| 20 | #define snprintf _snprintf | 
|---|
| 21 | #endif | 
|---|
| 22 |  | 
|---|
| 23 | #define die(dbh,hdl)	do {						\ | 
|---|
| 24 | if (hdl)				\ | 
|---|
| 25 | mapi_explain_result(hdl,stderr); \ | 
|---|
| 26 | else if (dbh)				\ | 
|---|
| 27 | mapi_explain(dbh,stderr);	\ | 
|---|
| 28 | else					\ | 
|---|
| 29 | fprintf(stderr,"command failed\n"); \ | 
|---|
| 30 | exit(-1);				\ | 
|---|
| 31 | } while (0) | 
|---|
| 32 |  | 
|---|
| 33 | int | 
|---|
| 34 | main(int argc, char **argv) | 
|---|
| 35 | { | 
|---|
| 36 | Mapi dbh; | 
|---|
| 37 | MapiHdl hdl = NULL; | 
|---|
| 38 | int i; | 
|---|
| 39 | char buf[40], *line; | 
|---|
| 40 | int port; | 
|---|
| 41 | int lang = 1; | 
|---|
| 42 | char *l = "sql"; | 
|---|
| 43 |  | 
|---|
| 44 | if (argc != 2 && argc != 3) { | 
|---|
| 45 | printf( "usage: smack01 <port> [<language>]\n"); | 
|---|
| 46 | exit(-1); | 
|---|
| 47 | } | 
|---|
| 48 | if (argc == 3) { | 
|---|
| 49 | l = argv[2]; | 
|---|
| 50 | if (strcmp(argv[2], "sql") == 0) | 
|---|
| 51 | lang = 1; | 
|---|
| 52 | else if (strcmp(argv[2], "mal") == 0) | 
|---|
| 53 | lang = 3; | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 |  | 
|---|
| 57 | port = atol(argv[1]); | 
|---|
| 58 | dbh = mapi_connect( "localhost", port, "monetdb", "monetdb", l, NULL); | 
|---|
| 59 | if (dbh == NULL || mapi_error(dbh)) | 
|---|
| 60 | die(dbh, hdl); | 
|---|
| 61 |  | 
|---|
| 62 | for (i = 0; i < 1000; i++) { | 
|---|
| 63 | /* printf("setup connection %d\n", i); */ | 
|---|
| 64 | mapi_reconnect(dbh); | 
|---|
| 65 | if (mapi_error(dbh)) | 
|---|
| 66 | die(dbh, hdl); | 
|---|
| 67 |  | 
|---|
| 68 | /* switch of autocommit */ | 
|---|
| 69 | if (lang==1 && (mapi_setAutocommit(dbh, false) != MOK || mapi_error(dbh))) | 
|---|
| 70 | die(dbh,NULL); | 
|---|
| 71 |  | 
|---|
| 72 | if (lang==1) | 
|---|
| 73 | snprintf(buf, 40, "select %d;", i); | 
|---|
| 74 | else | 
|---|
| 75 | snprintf(buf, 40, "io.print(%d);", i); | 
|---|
| 76 | if ((hdl = mapi_query(dbh, buf)) == NULL || mapi_error(dbh)) | 
|---|
| 77 | die(dbh, hdl); | 
|---|
| 78 | while ((line = mapi_fetch_line(hdl))) { | 
|---|
| 79 | printf( "%s \n", line); | 
|---|
| 80 | } | 
|---|
| 81 | if (mapi_error(dbh)) | 
|---|
| 82 | die(dbh, hdl); | 
|---|
| 83 | if (mapi_close_handle(hdl) != MOK) | 
|---|
| 84 | die(dbh, hdl); | 
|---|
| 85 | mapi_disconnect(dbh); | 
|---|
| 86 | /* printf("close connection %d\n", i); */ | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | mapi_destroy(dbh); | 
|---|
| 90 |  | 
|---|
| 91 | return 0; | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|