LCOV - code coverage report
Current view: top level - clients/examples/C - sample1.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 29 84 34.5 %
Date: 2024-04-25 20:03:45 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  * SPDX-License-Identifier: MPL-2.0
       3             :  *
       4             :  * This Source Code Form is subject to the terms of the Mozilla Public
       5             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       6             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       7             :  *
       8             :  * Copyright 2024 MonetDB Foundation;
       9             :  * Copyright August 2008 - 2023 MonetDB B.V.;
      10             :  * Copyright 1997 - July 2008 CWI.
      11             :  */
      12             : 
      13             : #include <stdlib.h>
      14             : #include <stdio.h>
      15             : #include <string.h>
      16             : #include <mapi.h>
      17             : 
      18             : #define die(dbh,hdl)    do {                                            \
      19             :                                 if (hdl)                                \
      20             :                                         mapi_explain_result(hdl,stderr); \
      21             :                                 else if (dbh)                           \
      22             :                                         mapi_explain(dbh,stderr);       \
      23             :                                 else                                    \
      24             :                                         fprintf(stderr,"command failed\n"); \
      25             :                                 exit(-1);                               \
      26             :                         } while (0)
      27             : 
      28             : int
      29           1 : main(int argc, char **argv)
      30             : {
      31           1 :         Mapi dbh;
      32           1 :         MapiHdl hdl = NULL;
      33             : 
      34           1 :         if (argc != 4) {
      35           0 :                 fprintf(stderr, "usage:%s <host> <port> <language>\n", argv[0]);
      36           0 :                 exit(-1);
      37             :         }
      38             : 
      39           1 :         dbh = mapi_connect(argv[1], atoi(argv[2]), "monetdb", "monetdb", argv[3], NULL);
      40           1 :         if (dbh == NULL || mapi_error(dbh))
      41           0 :                 die(dbh, hdl);
      42             : 
      43             :         /* mapi_trace(dbh, true); */
      44           1 :         mapi_cache_limit(dbh, 2);
      45           1 :         if (strcmp(argv[3], "sql") == 0) {
      46             :                 /* switch of autocommit */
      47           1 :                 if (mapi_setAutocommit(dbh, false) != MOK || mapi_error(dbh))
      48           0 :                         die(dbh,NULL);
      49           1 :                 if ((hdl = mapi_query(dbh, "create table emp(name varchar(20), age int)")) == NULL || mapi_error(dbh))
      50           0 :                         die(dbh, hdl);
      51           1 :                 if (mapi_close_handle(hdl) != MOK)
      52           0 :                         die(dbh, hdl);
      53           1 :                 if ((hdl = mapi_query(dbh, "insert into emp values('John', 23)")) == NULL || mapi_error(dbh))
      54           0 :                         die(dbh, hdl);
      55           1 :                 if (mapi_close_handle(hdl) != MOK)
      56           0 :                         die(dbh, hdl);
      57           1 :                 if ((hdl = mapi_query(dbh, "insert into emp values('Mary', 22)")) == NULL || mapi_error(dbh))
      58           0 :                         die(dbh, hdl);
      59           1 :                 if (mapi_close_handle(hdl) != MOK)
      60           0 :                         die(dbh, hdl);
      61           1 :                 if ((hdl = mapi_query(dbh, "select * from emp")) == NULL || mapi_error(dbh))
      62           0 :                         die(dbh, hdl);
      63             :                 int n = 0;
      64           3 :                 while (mapi_fetch_row(hdl)) {
      65           2 :                         char *nme = mapi_fetch_field(hdl, 0);
      66           2 :                         char *age = mapi_fetch_field(hdl, 1);
      67             : 
      68           2 :                         switch (n) {
      69           1 :                         case 0:
      70           1 :                                 if (strcmp(nme, "John") != 0 || strcmp(age, "23") != 0)
      71           0 :                                         fprintf(stderr, "unexpected result: %s|%s\n", nme, age);
      72             :                                 break;
      73           1 :                         case 1:
      74           1 :                                 if (strcmp(nme, "Mary") != 0 || strcmp(age, "22") != 0)
      75           0 :                                         fprintf(stderr, "unexpected result: %s|%s\n", nme, age);
      76             :                                 break;
      77             : 
      78           0 :                         default:
      79           0 :                                 fprintf(stderr, "too many results: %s|%s\n", nme, age);
      80           0 :                                 break;
      81             :                         }
      82           2 :                         n++;
      83             :                 }
      84           0 :         } else if (strcmp(argv[3], "mal") == 0) {
      85           0 :                 if ((hdl = mapi_query(dbh, "emp := bat.new(:oid,:str);")) == NULL || mapi_error(dbh))
      86           0 :                         die(dbh, hdl);
      87           0 :                 if ((hdl = mapi_query(dbh, "age := bat.new(:oid,:int);")) == NULL || mapi_error(dbh))
      88           0 :                         die(dbh, hdl);
      89           0 :                 if (mapi_close_handle(hdl) != MOK)
      90           0 :                         die(dbh, hdl);
      91           0 :                 if ((hdl = mapi_query(dbh, "bat.append(emp, \"John\");")) == NULL || mapi_error(dbh))
      92           0 :                         die(dbh, hdl);
      93           0 :                 if ((hdl = mapi_query(dbh, "bat.append(age, 23);")) == NULL || mapi_error(dbh))
      94           0 :                         die(dbh, hdl);
      95           0 :                 if (mapi_close_handle(hdl) != MOK)
      96           0 :                         die(dbh, hdl);
      97           0 :                 if ((hdl = mapi_query(dbh, "bat.append(emp, \"Mary\");")) == NULL || mapi_error(dbh))
      98           0 :                         die(dbh, hdl);
      99           0 :                 if ((hdl = mapi_query(dbh, "bat.append(age, 22);")) == NULL || mapi_error(dbh))
     100           0 :                         die(dbh, hdl);
     101           0 :                 if (mapi_close_handle(hdl) != MOK)
     102           0 :                         die(dbh, hdl);
     103           0 :                 if ((hdl = mapi_query(dbh, "io.print(emp,age);")) == NULL || mapi_error(dbh))
     104           0 :                         die(dbh, hdl);
     105             :                 int n = 0;
     106           0 :                 while (mapi_fetch_row(hdl)) {
     107           0 :                         char *nme = mapi_fetch_field(hdl, 1);
     108           0 :                         char *age = mapi_fetch_field(hdl, 2);
     109             : 
     110           0 :                         switch (n) {
     111           0 :                         case 0:
     112           0 :                                 if (strcmp(nme, "John") != 0 || strcmp(age, "23") != 0)
     113           0 :                                         fprintf(stderr, "unexpected result: %s|%s\n", nme, age);
     114             :                                 break;
     115           0 :                         case 1:
     116           0 :                                 if (strcmp(nme, "Mary") != 0 || strcmp(age, "22") != 0)
     117           0 :                                         fprintf(stderr, "unexpected result: %s|%s\n", nme, age);
     118             :                                 break;
     119             : 
     120           0 :                         default:
     121           0 :                                 fprintf(stderr, "too many results: %s|%s\n", nme, age);
     122           0 :                                 break;
     123             :                         }
     124           0 :                         n++;
     125             :                 }
     126             :         } else {
     127           0 :                 fprintf(stderr, "%s: unknown language, only mal and sql supported\n", argv[0]);
     128           0 :                 exit(1);
     129             :         }
     130             : 
     131           1 :         if (mapi_error(dbh))
     132           0 :                 die(dbh, hdl);
     133             :         /* mapi_stat(dbh);
     134             :            printf("mapi_ping %d\n",mapi_ping(dbh)); */
     135           1 :         if (mapi_close_handle(hdl) != MOK)
     136           0 :                 die(dbh, hdl);
     137           1 :         mapi_destroy(dbh);
     138             : 
     139           1 :         return 0;
     140             : }

Generated by: LCOV version 1.14