Changeset: 7f382096f1d1 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=7f382096f1d1
Added Files:
ctest/tools/embedded/example_connections.c
Modified Files:
cmake/monetdb-versions.cmake
ctest/tools/embedded/CMakeLists.txt
ctest/tools/embedded/example2.c
tools/embedded/CMakeLists.txt
tools/embedded/monetdb_embedded.c
Branch: default
Log Message:
fetch result should work on empty results as well
diffs (284 lines):
diff --git a/cmake/monetdb-versions.cmake b/cmake/monetdb-versions.cmake
--- a/cmake/monetdb-versions.cmake
+++ b/cmake/monetdb-versions.cmake
@@ -30,6 +30,10 @@ set(MONETDB_VERSION_MAJOR "11")
set(MONETDB_VERSION_MINOR "35")
set(MONETDB_VERSION_PATCH "18")
+set(MONETDBE_VERSION_MAJOR "0")
+set(MONETDBE_VERSION_MINOR "1")
+set(MONETDBE_VERSION_PATCH "0")
+set(MONETDBE_VERSION
"${MONETDBE_VERSION_MAJOR}.${MONETDBE_VERSION_MINOR}.${MONETDBE_VERSION_PATCH}")
if(RELEASE_VERSION)
math(EXPR MONETDB_VERSION_PATCH "${MONETDB_VERSION_PATCH} + 1")
diff --git a/ctest/tools/embedded/CMakeLists.txt
b/ctest/tools/embedded/CMakeLists.txt
--- a/ctest/tools/embedded/CMakeLists.txt
+++ b/ctest/tools/embedded/CMakeLists.txt
@@ -57,3 +57,11 @@ target_link_libraries(backup
stream
matomic)
add_test(run_backup backup)
+
+add_executable(example_connections example_connections.c)
+target_link_libraries(example_connections
+ PRIVATE
+ monetdb_config_header
+ embedded)
+add_test(run_example_connections example_connections)
+
diff --git a/ctest/tools/embedded/example2.c b/ctest/tools/embedded/example2.c
--- a/ctest/tools/embedded/example2.c
+++ b/ctest/tools/embedded/example2.c
@@ -139,9 +139,24 @@ main(void)
}
printf("\n");
}
-
if ((err = monetdb_cleanup_result(conn, result)) != NULL)
error(err)
+
+ /* test empty results */
+ if ((err = monetdb_query(conn, "SELECT b, t, s, x, l, h, f, d, y FROM
test where t > 127; ", &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++) {
+ /* fetching the meta data should work */
+ for (size_t c = 0; c < result->ncols; c++) {
+ monetdb_column* rcol;
+ if ((err = monetdb_result_fetch(conn, result, &rcol,
c)) != NULL)
+ error(err)
+ }
+ }
+ if ((err = monetdb_cleanup_result(conn, result)) != NULL)
+ error(err)
+
if ((err = monetdb_disconnect(conn)) != NULL)
error(err)
if ((err = monetdb_shutdown()) != NULL)
diff --git a/ctest/tools/embedded/example_connections.c
b/ctest/tools/embedded/example_connections.c
new file mode 100644
--- /dev/null
+++ b/ctest/tools/embedded/example_connections.c
@@ -0,0 +1,83 @@
+/*
+ * 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 conn1 = NULL, conn2 = 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(&conn1)) != NULL)
+ error(err)
+ if ((err = monetdb_connect(&conn2)) != NULL)
+ error(err)
+ if ((err = monetdb_query(conn1, "CREATE TABLE test (x integer, y
string)", NULL, NULL, NULL)) != NULL)
+ error(err)
+ if ((err = monetdb_query(conn2, "INSERT INTO test VALUES (42, 'Hello'),
(NULL, 'World')", NULL, NULL, NULL)) != NULL)
+ error(err)
+ if ((err = monetdb_query(conn1, "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(conn1, 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(conn1, result)) != NULL)
+ error(err)
+ if ((err = monetdb_disconnect(conn2)) != NULL)
+ error(err)
+ if ((err = monetdb_disconnect(conn1)) != NULL)
+ error(err)
+ if ((err = monetdb_shutdown()) != NULL)
+ error(err)
+ return 0;
+}
diff --git a/tools/embedded/CMakeLists.txt b/tools/embedded/CMakeLists.txt
--- a/tools/embedded/CMakeLists.txt
+++ b/tools/embedded/CMakeLists.txt
@@ -32,6 +32,8 @@ target_compile_definitions(embedded
set_target_properties(embedded
PROPERTIES
+ VERSION ${MONETDBE_VERSION}
+ SOVERSION ${MONETDBE_VERSION_MAJOR}
PUBLIC_HEADER "${embedded_public_headers}")
target_link_libraries(embedded
diff --git a/tools/embedded/monetdb_embedded.c
b/tools/embedded/monetdb_embedded.c
--- a/tools/embedded/monetdb_embedded.c
+++ b/tools/embedded/monetdb_embedded.c
@@ -901,10 +901,12 @@ GENERATE_BASE_HEADERS(monetdb_data_times
GENERATE_BAT_INPUT_BASE(tpe_name);
\
bat_data->count = BATcount(b);
\
bat_data->null_value = mtype##_nil;
\
- bat_data->data = GDKzalloc(bat_data->count *
sizeof(bat_data->null_value)); \
- if (!bat_data->data) {
\
- msg = createException(MAL,
"embedded.monetdb_result_fetch", MAL_MALLOC_FAIL); \
- goto cleanup;
\
+ if (bat_data->count) {
\
+ bat_data->data = GDKzalloc(bat_data->count *
sizeof(bat_data->null_value)); \
+ if (!bat_data->data) {
\
+ msg = createException(MAL,
"embedded.monetdb_result_fetch", MAL_MALLOC_FAIL); \
+ goto cleanup;
\
+ }
\
}
\
size_t it = 0;
\
mtype* val = (mtype*)Tloc(b, 0);
\
@@ -989,11 +991,13 @@ monetdb_result_fetch(monetdb_connection
BUN p = 0, q = 0;
GENERATE_BAT_INPUT_BASE(str);
bat_data->count = BATcount(b);
- bat_data->data = GDKzalloc(sizeof(char *) * bat_data->count);
- bat_data->null_value = NULL;
- if (!bat_data->data) {
- msg = createException(MAL,
"embedded.monetdb_result_fetch", MAL_MALLOC_FAIL);
- goto cleanup;
+ if (bat_data->count) {
+ bat_data->data = GDKzalloc(sizeof(char *) *
bat_data->count);
+ bat_data->null_value = NULL;
+ if (!bat_data->data) {
+ msg = createException(MAL,
"embedded.monetdb_result_fetch", MAL_MALLOC_FAIL);
+ goto cleanup;
+ }
}
j = 0;
@@ -1015,10 +1019,12 @@ monetdb_result_fetch(monetdb_connection
date *baseptr;
GENERATE_BAT_INPUT_BASE(date);
bat_data->count = BATcount(b);
- bat_data->data = GDKmalloc(sizeof(bat_data->null_value) *
bat_data->count);
- if (!bat_data->data) {
- msg = createException(MAL,
"embedded.monetdb_result_fetch", MAL_MALLOC_FAIL);
- goto cleanup;
+ if (bat_data->count) {
+ bat_data->data = GDKmalloc(sizeof(bat_data->null_value)
* bat_data->count);
+ if (!bat_data->data) {
+ msg = createException(MAL,
"embedded.monetdb_result_fetch", MAL_MALLOC_FAIL);
+ goto cleanup;
+ }
}
baseptr = (date *)Tloc(b, 0);
@@ -1029,10 +1035,12 @@ monetdb_result_fetch(monetdb_connection
daytime *baseptr;
GENERATE_BAT_INPUT_BASE(time);
bat_data->count = BATcount(b);
- bat_data->data = GDKmalloc(sizeof(bat_data->null_value) *
bat_data->count);
- if (!bat_data->data) {
- msg = createException(MAL,
"embedded.monetdb_result_fetch", MAL_MALLOC_FAIL);
- goto cleanup;
+ if (bat_data->count) {
+ bat_data->data = GDKmalloc(sizeof(bat_data->null_value)
* bat_data->count);
+ if (!bat_data->data) {
+ msg = createException(MAL,
"embedded.monetdb_result_fetch", MAL_MALLOC_FAIL);
+ goto cleanup;
+ }
}
baseptr = (daytime *)Tloc(b, 0);
@@ -1043,10 +1051,12 @@ monetdb_result_fetch(monetdb_connection
timestamp *baseptr;
GENERATE_BAT_INPUT_BASE(timestamp);
bat_data->count = BATcount(b);
- bat_data->data = GDKmalloc(sizeof(bat_data->null_value) *
bat_data->count);
- if (!bat_data->data) {
- msg = createException(MAL,
"embedded.monetdb_result_fetch", MAL_MALLOC_FAIL);
- goto cleanup;
+ if (bat_data->count) {
+ bat_data->data = GDKmalloc(sizeof(bat_data->null_value)
* bat_data->count);
+ if (!bat_data->data) {
+ msg = createException(MAL,
"embedded.monetdb_result_fetch", MAL_MALLOC_FAIL);
+ goto cleanup;
+ }
}
baseptr = (timestamp *)Tloc(b, 0);
@@ -1058,10 +1068,12 @@ monetdb_result_fetch(monetdb_connection
BUN p = 0, q = 0;
GENERATE_BAT_INPUT_BASE(blob);
bat_data->count = BATcount(b);
- bat_data->data = GDKmalloc(sizeof(monetdb_data_blob) *
bat_data->count);
- if (!bat_data->data) {
- msg = createException(MAL,
"embedded.monetdb_result_fetch", MAL_MALLOC_FAIL);
- goto cleanup;
+ if (bat_data->count) {
+ bat_data->data = GDKmalloc(sizeof(monetdb_data_blob) *
bat_data->count);
+ if (!bat_data->data) {
+ msg = createException(MAL,
"embedded.monetdb_result_fetch", MAL_MALLOC_FAIL);
+ goto cleanup;
+ }
}
j = 0;
@@ -1091,11 +1103,13 @@ monetdb_result_fetch(monetdb_connection
BUN p = 0, q = 0;
GENERATE_BAT_INPUT_BASE(str);
bat_data->count = BATcount(b);
- bat_data->null_value = NULL;
- bat_data->data = GDKzalloc(sizeof(char *) * bat_data->count);
- if (!bat_data->data) {
- msg = createException(MAL,
"embedded.monetdb_result_fetch", MAL_MALLOC_FAIL);
- goto cleanup;
+ if (bat_data->count) {
+ bat_data->null_value = NULL;
+ bat_data->data = GDKzalloc(sizeof(char *) *
bat_data->count);
+ if (!bat_data->data) {
+ msg = createException(MAL,
"embedded.monetdb_result_fetch", MAL_MALLOC_FAIL);
+ goto cleanup;
+ }
}
j = 0;
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list