LCOV - code coverage report
Current view: top level - clients/examples/C - sample0.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 25 80 31.2 %
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 <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           1 : main(int argc, char **argv)
      26             : {
      27             :         Mapi dbh;
      28             :         MapiHdl hdl = NULL;
      29             : 
      30           1 :         if (argc != 4) {
      31           0 :                 fprintf(stderr, "usage:%s <host> <port> <language>\n", argv[0]);
      32           0 :                 exit(-1);
      33             :         }
      34             : 
      35           1 :         dbh = mapi_connect(argv[1], atoi(argv[2]), "monetdb", "monetdb", argv[3], NULL);
      36           1 :         if (dbh == NULL || mapi_error(dbh))
      37           0 :                 die(dbh, hdl);
      38             : 
      39             :         /* mapi_trace(dbh, true); */
      40           1 :         if (strcmp(argv[3], "sql") == 0) {
      41             :                 /* switch of autocommit */
      42           1 :                 if (mapi_setAutocommit(dbh, false) != MOK || mapi_error(dbh))
      43           0 :                         die(dbh,NULL);
      44           1 :                 if ((hdl = mapi_query(dbh, "create table emp(name varchar(20), age int)")) == NULL || mapi_error(dbh))
      45           0 :                         die(dbh, hdl);
      46           1 :                 if (mapi_close_handle(hdl) != MOK)
      47           0 :                         die(dbh, hdl);
      48           1 :                 if ((hdl = mapi_query(dbh, "insert into emp values('John', 23)")) == 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('Mary', 22)")) == 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, "select * from emp")) == NULL || mapi_error(dbh))
      57           0 :                         die(dbh, hdl);
      58             :                 int n = 0;
      59           3 :                 while (mapi_fetch_row(hdl)) {
      60           2 :                         char *nme = mapi_fetch_field(hdl, 0);
      61           2 :                         char *age = mapi_fetch_field(hdl, 1);
      62             : 
      63           2 :                         switch (n) {
      64           1 :                         case 0:
      65           1 :                                 if (strcmp(nme, "John") != 0 || strcmp(age, "23") != 0)
      66           0 :                                         fprintf(stderr, "unexpected result: %s|%s\n", nme, age);
      67             :                                 break;
      68           1 :                         case 1:
      69           1 :                                 if (strcmp(nme, "Mary") != 0 || strcmp(age, "22") != 0)
      70           0 :                                         fprintf(stderr, "unexpected result: %s|%s\n", nme, age);
      71             :                                 break;
      72             : 
      73           0 :                         default:
      74           0 :                                 fprintf(stderr, "too many results: %s|%s\n", nme, age);
      75           0 :                                 break;
      76             :                         }
      77           2 :                         n++;
      78             :                 }
      79           0 :         } else if (strcmp(argv[3], "mal") == 0) {
      80           0 :                 if ((hdl = mapi_query(dbh, "emp := bat.new(:oid,:str);")) == NULL || mapi_error(dbh))
      81           0 :                         die(dbh, hdl);
      82           0 :                 if ((hdl = mapi_query(dbh, "age := bat.new(:oid,:int);")) == NULL || mapi_error(dbh))
      83           0 :                         die(dbh, hdl);
      84           0 :                 if (mapi_close_handle(hdl) != MOK)
      85           0 :                         die(dbh, hdl);
      86           0 :                 if ((hdl = mapi_query(dbh, "bat.append(emp, \"John\");")) == NULL || mapi_error(dbh))
      87           0 :                         die(dbh, hdl);
      88           0 :                 if ((hdl = mapi_query(dbh, "bat.append(age, 23);")) == NULL || mapi_error(dbh))
      89           0 :                         die(dbh, hdl);
      90           0 :                 if (mapi_close_handle(hdl) != MOK)
      91           0 :                         die(dbh, hdl);
      92           0 :                 if ((hdl = mapi_query(dbh, "bat.append(emp, \"Mary\");")) == NULL || mapi_error(dbh))
      93           0 :                         die(dbh, hdl);
      94           0 :                 if ((hdl = mapi_query(dbh, "bat.append(age, 22);")) == NULL || mapi_error(dbh))
      95           0 :                         die(dbh, hdl);
      96           0 :                 if (mapi_close_handle(hdl) != MOK)
      97           0 :                         die(dbh, hdl);
      98           0 :                 if ((hdl = mapi_query(dbh, "io.print(emp,age);")) == NULL || mapi_error(dbh))
      99           0 :                         die(dbh, hdl);
     100             :                 int n = 0;
     101           0 :                 while (mapi_fetch_row(hdl)) {
     102           0 :                         char *nme = mapi_fetch_field(hdl, 1);
     103           0 :                         char *age = mapi_fetch_field(hdl, 2);
     104             : 
     105           0 :                         switch (n) {
     106           0 :                         case 0:
     107           0 :                                 if (strcmp(nme, "John") != 0 || strcmp(age, "23") != 0)
     108           0 :                                         fprintf(stderr, "unexpected result: %s|%s\n", nme, age);
     109             :                                 break;
     110           0 :                         case 1:
     111           0 :                                 if (strcmp(nme, "Mary") != 0 || strcmp(age, "22") != 0)
     112           0 :                                         fprintf(stderr, "unexpected result: %s|%s\n", nme, age);
     113             :                                 break;
     114             : 
     115           0 :                         default:
     116           0 :                                 fprintf(stderr, "too many results: %s|%s\n", nme, age);
     117           0 :                                 break;
     118             :                         }
     119           0 :                         n++;
     120             :                 }
     121             :         } else {
     122           0 :                 fprintf(stderr, "%s: unknown language, only mal and sql supported\n", argv[0]);
     123           0 :                 exit(1);
     124             :         }
     125             : 
     126           1 :         if (mapi_error(dbh))
     127           0 :                 die(dbh, hdl);
     128             :         /* mapi_stat(dbh);
     129             :            printf("mapi_ping %d\n",mapi_ping(dbh)); */
     130           1 :         if (mapi_close_handle(hdl) != MOK)
     131           0 :                 die(dbh, hdl);
     132           1 :         mapi_destroy(dbh);
     133             : 
     134             :         return 0;
     135             : }

Generated by: LCOV version 1.14