LCOV - code coverage report
Current view: top level - clients/examples/C - sample0.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 28 83 33.7 %
Date: 2024-11-15 19:37: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 :         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             :                 int n = 0;
      63           3 :                 while (mapi_fetch_row(hdl)) {
      64           2 :                         char *nme = mapi_fetch_field(hdl, 0);
      65           2 :                         char *age = mapi_fetch_field(hdl, 1);
      66             : 
      67           2 :                         switch (n) {
      68           1 :                         case 0:
      69           1 :                                 if (strcmp(nme, "John") != 0 || strcmp(age, "23") != 0)
      70           0 :                                         fprintf(stderr, "unexpected result: %s|%s\n", nme, age);
      71             :                                 break;
      72           1 :                         case 1:
      73           1 :                                 if (strcmp(nme, "Mary") != 0 || strcmp(age, "22") != 0)
      74           0 :                                         fprintf(stderr, "unexpected result: %s|%s\n", nme, age);
      75             :                                 break;
      76             : 
      77           0 :                         default:
      78           0 :                                 fprintf(stderr, "too many results: %s|%s\n", nme, age);
      79           0 :                                 break;
      80             :                         }
      81           2 :                         n++;
      82             :                 }
      83           0 :         } else if (strcmp(argv[3], "mal") == 0) {
      84           0 :                 if ((hdl = mapi_query(dbh, "emp := bat.new(:oid,:str);")) == NULL || mapi_error(dbh))
      85           0 :                         die(dbh, hdl);
      86           0 :                 if ((hdl = mapi_query(dbh, "age := bat.new(:oid,:int);")) == NULL || mapi_error(dbh))
      87           0 :                         die(dbh, hdl);
      88           0 :                 if (mapi_close_handle(hdl) != MOK)
      89           0 :                         die(dbh, hdl);
      90           0 :                 if ((hdl = mapi_query(dbh, "bat.append(emp, \"John\");")) == NULL || mapi_error(dbh))
      91           0 :                         die(dbh, hdl);
      92           0 :                 if ((hdl = mapi_query(dbh, "bat.append(age, 23);")) == NULL || mapi_error(dbh))
      93           0 :                         die(dbh, hdl);
      94           0 :                 if (mapi_close_handle(hdl) != MOK)
      95           0 :                         die(dbh, hdl);
      96           0 :                 if ((hdl = mapi_query(dbh, "bat.append(emp, \"Mary\");")) == NULL || mapi_error(dbh))
      97           0 :                         die(dbh, hdl);
      98           0 :                 if ((hdl = mapi_query(dbh, "bat.append(age, 22);")) == NULL || mapi_error(dbh))
      99           0 :                         die(dbh, hdl);
     100           0 :                 if (mapi_close_handle(hdl) != MOK)
     101           0 :                         die(dbh, hdl);
     102           0 :                 if ((hdl = mapi_query(dbh, "io.print(emp,age);")) == NULL || mapi_error(dbh))
     103           0 :                         die(dbh, hdl);
     104             :                 int n = 0;
     105           0 :                 while (mapi_fetch_row(hdl)) {
     106           0 :                         char *nme = mapi_fetch_field(hdl, 1);
     107           0 :                         char *age = mapi_fetch_field(hdl, 2);
     108             : 
     109           0 :                         switch (n) {
     110           0 :                         case 0:
     111           0 :                                 if (strcmp(nme, "John") != 0 || strcmp(age, "23") != 0)
     112           0 :                                         fprintf(stderr, "unexpected result: %s|%s\n", nme, age);
     113             :                                 break;
     114           0 :                         case 1:
     115           0 :                                 if (strcmp(nme, "Mary") != 0 || strcmp(age, "22") != 0)
     116           0 :                                         fprintf(stderr, "unexpected result: %s|%s\n", nme, age);
     117             :                                 break;
     118             : 
     119           0 :                         default:
     120           0 :                                 fprintf(stderr, "too many results: %s|%s\n", nme, age);
     121           0 :                                 break;
     122             :                         }
     123           0 :                         n++;
     124             :                 }
     125             :         } else {
     126           0 :                 fprintf(stderr, "%s: unknown language, only mal and sql supported\n", argv[0]);
     127           0 :                 exit(1);
     128             :         }
     129             : 
     130           1 :         if (mapi_error(dbh))
     131           0 :                 die(dbh, hdl);
     132             :         /* mapi_stat(dbh);
     133             :            printf("mapi_ping %d\n",mapi_ping(dbh)); */
     134           1 :         if (mapi_close_handle(hdl) != MOK)
     135           0 :                 die(dbh, hdl);
     136           1 :         mapi_destroy(dbh);
     137             : 
     138           1 :         return 0;
     139             : }

Generated by: LCOV version 1.14