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