Mercurial > hg > MonetDB-extend
changeset 36:f100f14c0d0a
update use of api and changed name of library/include file
author | Niels Nes <niels@cwi.nl> |
---|---|
date | Tue, 16 Jun 2020 10:11:57 +0200 (2020-06-16) |
parents | 2dfbe7d90da4 |
children | e5d2d0c9b7b3 |
files | CMakeLists.txt embedded/CMakeLists.txt embedded/example1.c monetdbe/CMakeLists.txt monetdbe/example1.c |
diffstat | 5 files changed, 90 insertions(+), 94 deletions(-) [+] |
line wrap: on
line diff
--- a/CMakeLists.txt Fri Jun 12 09:07:55 2020 +0200 +++ b/CMakeLists.txt Tue Jun 16 10:11:57 2020 +0200 @@ -29,7 +29,7 @@ add_subdirectory(regexp) add_subdirectory(reverse) -add_subdirectory(embedded) +add_subdirectory(monetdbe) add_custom_target(docs DEPENDS
--- a/embedded/CMakeLists.txt Fri Jun 12 09:07:55 2020 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,14 +0,0 @@ -#[[ -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. -# -# Copyright 1997 - July 2008 CWI, August 2008 - 2020 MonetDB B.V. -#]] - -add_executable(example1 example1.c) -target_link_libraries(example1 - PRIVATE - MonetDB::monetdb_config_header - MonetDB::embedded) -add_test(run_example1 example1)
--- a/embedded/example1.c Fri Jun 12 09:07:55 2020 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,79 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * Copyright 1997 - July 2008 CWI, August 2008 - 2020 MonetDB B.V. - */ - -#include "monetdb_embedded.h" -#include <stdlib.h> -#include <stdio.h> -#include <inttypes.h> - -#define error(msg) {fprintf(stderr, "Failure: %s\n", msg); return -1;} - -int -main(void) -{ - char* err = NULL; - monetdb_connection conn = NULL; - monetdb_result* result = NULL; - - // first argument is a string for the db directory or NULL for in-memory mode - if ((err = monetdb_startup(NULL, 0)) != NULL) - error(err) - if ((err = monetdb_connect(&conn)) != NULL) - error(err) - if ((err = monetdb_query(conn, "CREATE TABLE test (x integer, y string)", NULL, NULL, NULL)) != NULL) - error(err) - if ((err = monetdb_query(conn, "INSERT INTO test VALUES (42, 'Hello'), (NULL, 'World')", NULL, NULL, NULL)) != NULL) - error(err) - if ((err = monetdb_query(conn, "SELECT x, y FROM test; ", &result, NULL, NULL)) != NULL) - error(err) - - fprintf(stdout, "Query result with %zu cols and %"PRId64" rows\n", result->ncols, result->nrows); - for (int64_t r = 0; r < result->nrows; r++) { - for (size_t c = 0; c < result->ncols; c++) { - monetdb_column* rcol; - if ((err = monetdb_result_fetch(conn, result, &rcol, c)) != NULL) - error(err) - switch (rcol->type) { - case monetdb_int32_t: { - monetdb_column_int32_t * col = (monetdb_column_int32_t *) rcol; - if (col->data[r] == col->null_value) { - printf("NULL"); - } else { - printf("%d", col->data[r]); - } - break; - } - case monetdb_str: { - monetdb_column_str * col = (monetdb_column_str *) rcol; - if (col->is_null(col->data[r])) { - printf("NULL"); - } else { - printf("%s", (char*) col->data[r]); - } - break; - } - default: { - printf("UNKNOWN"); - } - } - - if (c + 1 < result->ncols) { - printf(", "); - } - } - printf("\n"); - } - - if ((err = monetdb_cleanup_result(conn, result)) != NULL) - error(err) - if ((err = monetdb_disconnect(conn)) != NULL) - error(err) - if ((err = monetdb_shutdown()) != NULL) - error(err) - return 0; -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/monetdbe/CMakeLists.txt Tue Jun 16 10:11:57 2020 +0200 @@ -0,0 +1,14 @@ +#[[ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# Copyright 1997 - July 2008 CWI, August 2008 - 2020 MonetDB B.V. +#]] + +add_executable(example1 example1.c) +target_link_libraries(example1 + PRIVATE + MonetDB::monetdb_config_header + MonetDB::monetdbe) +add_test(run_example1 example1)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/monetdbe/example1.c Tue Jun 16 10:11:57 2020 +0200 @@ -0,0 +1,75 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright 1997 - July 2008 CWI, August 2008 - 2020 MonetDB B.V. + */ + +#include "monetdbe.h" +#include <stdlib.h> +#include <stdio.h> +#include <inttypes.h> + +#define error(msg) {fprintf(stderr, "Failure: %s\n", msg); return -1;} + +int +main(void) +{ + char* err = NULL; + monetdbe_database mdbe = NULL; + monetdbe_result* result = NULL; + + // first argument is a string for the db directory or NULL for in-memory mode + if ((err = monetdbe_open(&mdbe, NULL, NULL)) != NULL) + error(err) + if ((err = monetdbe_query(mdbe, "CREATE TABLE test (x integer, y string)", NULL, NULL)) != NULL) + error(err) + if ((err = monetdbe_query(mdbe, "INSERT INTO test VALUES (42, 'Hello'), (NULL, 'World')", NULL, NULL)) != NULL) + error(err) + if ((err = monetdbe_query(mdbe, "SELECT x, y FROM test; ", &result, NULL)) != NULL) + error(err) + + fprintf(stdout, "Query result with %zu cols and %"PRId64" rows\n", result->ncols, result->nrows); + for (int64_t r = 0; r < result->nrows; r++) { + for (size_t c = 0; c < result->ncols; c++) { + monetdbe_column* rcol; + if ((err = monetdbe_result_fetch(result, &rcol, c)) != NULL) + error(err) + switch (rcol->type) { + case monetdbe_int32_t: { + monetdbe_column_int32_t * col = (monetdbe_column_int32_t *) rcol; + if (col->data[r] == col->null_value) { + printf("NULL"); + } else { + printf("%d", col->data[r]); + } + break; + } + case monetdbe_str: { + monetdbe_column_str * col = (monetdbe_column_str *) rcol; + if (col->is_null(col->data[r])) { + printf("NULL"); + } else { + printf("%s", (char*) col->data[r]); + } + break; + } + default: { + printf("UNKNOWN"); + } + } + + if (c + 1 < result->ncols) { + printf(", "); + } + } + printf("\n"); + } + + if ((err = monetdbe_cleanup_result(mdbe, result)) != NULL) + error(err) + if ((err = monetdbe_close(mdbe)) != NULL) + error(err) + return 0; +}