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

Generated by: LCOV version 1.14