LCOV - code coverage report
Current view: top level - ctest/tools/monetdbe - example_proxy.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 27 35 77.1 %
Date: 2021-10-13 02:24:04 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 "monetdbe.h"
      10             : #include <stdlib.h>
      11             : #include <stdio.h>
      12             : #include <inttypes.h>
      13             : 
      14             : #define expected_error(msg) {fprintf(stderr, "Failure: %s\n", msg); return 0;}
      15             : #define error(msg) {fprintf(stderr, "Failure: %s\n", msg); return -1;}
      16             : 
      17             : int
      18           1 : main(int argc, char** argv)
      19             : {
      20             :         (void) argc;
      21             :         char* err = NULL;
      22           1 :         monetdbe_database mdbe = NULL;
      23           1 :         monetdbe_result* result = NULL;
      24           1 :         assert(argc==3);
      25           1 :         const int port = strtol(argv[1], NULL, 10);
      26           1 :         const char* database = argv[2];
      27           1 :         monetdbe_remote remote = {.host="localhost", .port=port, .database=database, .username="monetdb", .password="monetdb"};
      28           1 :         monetdbe_options opts = {.remote = &remote};
      29             : 
      30           1 :         if (monetdbe_open(&mdbe, NULL, &opts))
      31           0 :                 expected_error("Failed to open database")
      32           1 :         if ((err = monetdbe_query(mdbe, "SELECT x, y FROM test ORDER BY y ASC; ", &result, NULL)) != NULL)
      33           0 :                 error(err)
      34             : 
      35           1 :         fprintf(stdout, "Query result with %zu cols and %"PRId64" rows\n", result->ncols, result->nrows);
      36           3 :         for (int64_t r = 0; r < result->nrows; r++) {
      37           6 :                 for (size_t c = 0; c < result->ncols; c++) {
      38             :                         monetdbe_column* rcol;
      39           4 :                         if ((err = monetdbe_result_fetch(result, &rcol, c)) != NULL)
      40           0 :                                 error(err)
      41           4 :                         switch (rcol->type.type) {
      42           2 :                                 case monetdbe_int32_t: {
      43             :                                         monetdbe_column_int32_t * col = (monetdbe_column_int32_t *) rcol;
      44           2 :                                         if (col->data[r] == col->null_value) {
      45           1 :                                                 printf("NULL");
      46             :                                         } else {
      47           1 :                                                 printf("%d", col->data[r]);
      48             :                                         }
      49             :                                         break;
      50             :                                 }
      51           2 :                                 case monetdbe_str: {
      52             :                                         monetdbe_column_str * col = (monetdbe_column_str *) rcol;
      53           2 :                                         if (col->is_null(col->data+r)) {
      54           0 :                                                 printf("NULL");
      55             :                                         } else {
      56           2 :                                                 printf("%s", (char*) col->data[r]);
      57             :                                         }
      58             :                                         break;
      59             :                                 }
      60           0 :                                 default: {
      61           0 :                                         printf("UNKNOWN");
      62             :                                 }
      63             :                         }
      64             : 
      65           4 :                         if (c + 1 < result->ncols) {
      66           2 :                                 printf(", ");
      67             :                         }
      68             :                 }
      69           2 :                 printf("\n");
      70             :         }
      71             : 
      72           1 :         if ((err = monetdbe_cleanup_result(mdbe, result)) != NULL)
      73           0 :                 error(err)
      74           1 :         if (monetdbe_close(mdbe))
      75           0 :                 error("Failed to close database")
      76             :         return 0;
      77             : }

Generated by: LCOV version 1.14