LCOV - code coverage report
Current view: top level - clients/examples/C - sample4.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 29 91 31.9 %
Date: 2021-09-14 19:48:19 Functions: 1 1 100.0 %

          Line data    Source code
       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 - 2021 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           1 : main(int argc, char **argv)
      27             : {
      28             :         Mapi dbh;
      29             :         MapiHdl hdl = NULL;
      30             :         int64_t rows;
      31             : 
      32           1 :         if (argc != 4) {
      33           0 :                 fprintf(stderr, "usage:%s <host> <port> <language>\n", argv[0]);
      34           0 :                 exit(-1);
      35             :         }
      36             : 
      37           1 :         dbh = mapi_connect(argv[1], atoi(argv[2]), "monetdb", "monetdb", argv[3], NULL);
      38           1 :         if (dbh == NULL || mapi_error(dbh))
      39           0 :                 die(dbh, hdl);
      40             : 
      41           1 :         mapi_cache_limit(dbh, 2);
      42             :         /* mapi_trace_log(dbh, "/tmp/mapilog"); */
      43             :         /* mapi_trace(dbh, true); */
      44           1 :         if (strcmp(argv[3], "sql") == 0) {
      45             :                 /* switch of autocommit */
      46           1 :                 if (mapi_setAutocommit(dbh, false) != MOK || mapi_error(dbh))
      47           0 :                         die(dbh,NULL);
      48           1 :                 if ((hdl = mapi_query(dbh, "create table emp(name varchar(20), age int)")) == NULL || mapi_error(dbh))
      49           0 :                         die(dbh, hdl);
      50           1 :                 if (mapi_close_handle(hdl) != MOK)
      51           0 :                         die(dbh, hdl);
      52           1 :                 if ((hdl = mapi_query(dbh, "insert into emp values('John', 23)")) == NULL || mapi_error(dbh))
      53           0 :                         die(dbh, hdl);
      54           1 :                 if (mapi_close_handle(hdl) != MOK)
      55           0 :                         die(dbh, hdl);
      56           1 :                 if ((hdl = mapi_query(dbh, "insert into emp values('Mary', 22)")) == NULL || mapi_error(dbh))
      57           0 :                         die(dbh, hdl);
      58           1 :                 if (mapi_close_handle(hdl) != MOK)
      59           0 :                         die(dbh, hdl);
      60           1 :                 if ((hdl = mapi_query(dbh, "select * from emp")) == NULL || mapi_error(dbh))
      61           0 :                         die(dbh, hdl);
      62           1 :                 rows = mapi_fetch_all_rows(hdl);
      63           1 :                 if (mapi_error(dbh))
      64           0 :                         die(dbh, hdl);
      65           1 :                 if (rows != 2)
      66           0 :                         fprintf(stderr, "rows received %" PRId64 "\n", rows);
      67             :                 int n = 0;
      68           3 :                 while (mapi_fetch_row(hdl)) {
      69           2 :                         char *nme = mapi_fetch_field(hdl, 0);
      70           2 :                         char *age = mapi_fetch_field(hdl, 1);
      71             : 
      72           2 :                         switch (n) {
      73           1 :                         case 0:
      74           1 :                                 if (strcmp(nme, "John") != 0 || strcmp(age, "23") != 0)
      75           0 :                                         fprintf(stderr, "unexpected result: %s|%s\n", nme, age);
      76             :                                 break;
      77           1 :                         case 1:
      78           1 :                                 if (strcmp(nme, "Mary") != 0 || strcmp(age, "22") != 0)
      79           0 :                                         fprintf(stderr, "unexpected result: %s|%s\n", nme, age);
      80             :                                 break;
      81             : 
      82           0 :                         default:
      83           0 :                                 fprintf(stderr, "too many results: %s|%s\n", nme, age);
      84           0 :                                 break;
      85             :                         }
      86           2 :                         n++;
      87             :                 }
      88           0 :         } else if (strcmp(argv[3], "mal") == 0) {
      89           0 :                 if ((hdl = mapi_query(dbh, "emp := bat.new(:oid,:str);")) == NULL || mapi_error(dbh))
      90           0 :                         die(dbh, hdl);
      91           0 :                 if ((hdl = mapi_query(dbh, "age := bat.new(:oid,:int);")) == NULL || mapi_error(dbh))
      92           0 :                         die(dbh, hdl);
      93           0 :                 if (mapi_close_handle(hdl) != MOK)
      94           0 :                         die(dbh, hdl);
      95           0 :                 if ((hdl = mapi_query(dbh, "bat.append(emp, \"John\");")) == NULL || mapi_error(dbh))
      96           0 :                         die(dbh, hdl);
      97           0 :                 if ((hdl = mapi_query(dbh, "bat.append(age, 23);")) == NULL || mapi_error(dbh))
      98           0 :                         die(dbh, hdl);
      99           0 :                 if (mapi_close_handle(hdl) != MOK)
     100           0 :                         die(dbh, hdl);
     101           0 :                 if ((hdl = mapi_query(dbh, "bat.append(emp, \"Mary\");")) == NULL || mapi_error(dbh))
     102           0 :                         die(dbh, hdl);
     103           0 :                 if ((hdl = mapi_query(dbh, "bat.append(age, 22);")) == NULL || mapi_error(dbh))
     104           0 :                         die(dbh, hdl);
     105           0 :                 if (mapi_close_handle(hdl) != MOK)
     106           0 :                         die(dbh, hdl);
     107           0 :                 if ((hdl = mapi_query(dbh, "io.print(emp,age);")) == NULL || mapi_error(dbh))
     108           0 :                         die(dbh, hdl);
     109           0 :                 rows = mapi_fetch_all_rows(hdl);
     110           0 :                 if (mapi_error(dbh))
     111           0 :                         die(dbh, hdl);
     112           0 :                 if (rows != 2)
     113           0 :                         fprintf(stderr, "rows received %" PRId64 "\n", rows);
     114             :                 int n = 0;
     115           0 :                 while (mapi_fetch_row(hdl)) {
     116           0 :                         char *nme = mapi_fetch_field(hdl, 1);
     117           0 :                         char *age = mapi_fetch_field(hdl, 2);
     118             : 
     119           0 :                         switch (n) {
     120           0 :                         case 0:
     121           0 :                                 if (strcmp(nme, "John") != 0 || strcmp(age, "23") != 0)
     122           0 :                                         fprintf(stderr, "unexpected result: %s|%s\n", nme, age);
     123             :                                 break;
     124           0 :                         case 1:
     125           0 :                                 if (strcmp(nme, "Mary") != 0 || strcmp(age, "22") != 0)
     126           0 :                                         fprintf(stderr, "unexpected result: %s|%s\n", nme, age);
     127             :                                 break;
     128             : 
     129           0 :                         default:
     130           0 :                                 fprintf(stderr, "too many results: %s|%s\n", nme, age);
     131           0 :                                 break;
     132             :                         }
     133           0 :                         n++;
     134             :                 }
     135             :         } else {
     136           0 :                 fprintf(stderr, "%s: unknown language, only mal and sql supported\n", argv[0]);
     137           0 :                 exit(1);
     138             :         }
     139             : 
     140           1 :         if (mapi_error(dbh))
     141           0 :                 die(dbh, hdl);
     142             :         /* mapi_stat(dbh);
     143             :            printf("mapi_ping %d\n",mapi_ping(dbh)); */
     144           1 :         if (mapi_close_handle(hdl) != MOK)
     145           0 :                 die(dbh, hdl);
     146           1 :         mapi_destroy(dbh);
     147             : 
     148             :         return 0;
     149             : }

Generated by: LCOV version 1.14