Changeset: 746e70a943ea for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=746e70a943ea
Added Files:
        ctest/tools/monetdbe/CMakeLists.txt
        ctest/tools/monetdbe/backup.c
        ctest/tools/monetdbe/example1.c
        ctest/tools/monetdbe/example2.c
        ctest/tools/monetdbe/example_append.c
        ctest/tools/monetdbe/example_blob.c
        ctest/tools/monetdbe/example_connections.c
        ctest/tools/monetdbe/example_decimals.c
        ctest/tools/monetdbe/example_temporal.c
        ctest/tools/monetdbe/mapi.c
        ctest/tools/monetdbe/mapi.h
        tools/monetdbe/CMakeLists.txt
        tools/monetdbe/monetdbe.c
        tools/monetdbe/monetdbe.h
Modified Files:
        ctest/tools/CMakeLists.txt
        tools/CMakeLists.txt
Branch: default
Log Message:

first step in moving to monetdbe


diffs (truncated from 2920 to 300 lines):

diff --git a/ctest/tools/CMakeLists.txt b/ctest/tools/CMakeLists.txt
--- a/ctest/tools/CMakeLists.txt
+++ b/ctest/tools/CMakeLists.txt
@@ -6,4 +6,5 @@
 # Copyright 1997 - July 2008 CWI, August 2008 - 2020 MonetDB B.V.
 #]]
 
-add_subdirectory(embedded)
+#add_subdirectory(embedded)
+add_subdirectory(monetdbe)
diff --git a/ctest/tools/monetdbe/CMakeLists.txt 
b/ctest/tools/monetdbe/CMakeLists.txt
new file mode 100644
--- /dev/null
+++ b/ctest/tools/monetdbe/CMakeLists.txt
@@ -0,0 +1,73 @@
+#[[
+# 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_config_header
+    monetdbe)
+add_test(run_example1 example1)
+
+add_executable(example2 example2.c)
+target_link_libraries(example2
+  PRIVATE
+    monetdb_config_header
+    monetdbe)
+add_test(run_example2 example2)
+
+add_executable(example_temporal example_temporal.c)
+target_link_libraries(example_temporal
+  PRIVATE
+    monetdb_config_header
+    monetdbe)
+add_test(run_example_temporal example_temporal)
+
+add_executable(example_decimals example_decimals.c)
+target_link_libraries(example_decimals
+  PRIVATE
+    monetdb_config_header
+    monetdbe)
+add_test(run_example_decimals example_decimals)
+
+add_executable(example_blob example_blob.c)
+target_link_libraries(example_blob
+  PRIVATE
+    monetdb_config_header
+    monetdbe)
+add_test(run_example_blob example_blob)
+
+add_executable(example_append example_append.c)
+target_link_libraries(example_append
+  PRIVATE
+    monetdb_config_header
+    monetdbe)
+add_test(run_example_append example_append)
+
+add_executable(backup
+  backup.c
+  mapi.h
+  mapi.c
+  ${CMAKE_SOURCE_DIR}/clients/mapiclient/dump.c)
+target_include_directories(backup
+  PRIVATE
+  ${CMAKE_SOURCE_DIR}/clients/mapilib)
+target_link_libraries(backup
+  PRIVATE
+    monetdb_config_header
+    monetdbe
+    stream
+    matomic)
+add_test(run_backup backup)
+
+add_executable(example_connections example_connections.c)
+target_link_libraries(example_connections
+  PRIVATE
+    monetdb_config_header
+    monetdbe)
+add_test(run_example_connections example_connections)
+
diff --git a/ctest/tools/monetdbe/backup.c b/ctest/tools/monetdbe/backup.c
new file mode 100644
--- /dev/null
+++ b/ctest/tools/monetdbe/backup.c
@@ -0,0 +1,47 @@
+
+#include "monetdb_config.h"
+#include "stream.h"
+#include "mstring.h"
+#include <unistd.h>
+#include <string.h>
+#include <ctype.h>
+#include "mapi.h"
+#include <monetdbe.h>
+
+extern int dump_database(Mapi mid, stream *toConsole, bool describe, bool 
useInserts);
+
+#define error(msg) {fprintf(stderr, "Failure: %s\n", msg); return -1;}
+
+int
+main(void) 
+{
+       char* err = NULL;
+       Mapi mid = (Mapi)malloc(sizeof(struct MapiStruct));
+
+       if ((mid->msg = monetdb_open(&mid->mdbe, NULL)) != NULL)
+               error(mid->msg);
+
+       if ((err = monetdb_query(mid->mdbe, "CREATE TABLE test (b bool, t 
tinyint, s smallint, x integer, l bigint, "
+#ifdef HAVE_HGE
+               "h hugeint, "
+#else
+               "h bigint, "
+#endif
+               "f float, d double, y string)", NULL, NULL)) != NULL)
+               error(err)
+       if ((err = monetdb_query(mid->mdbe, "INSERT INTO test VALUES (TRUE, 42, 
42, 42, 42, 42, 42.42, 42.42, 'Hello'), (NULL, NULL, NULL, NULL, NULL, NULL, 
NULL, NULL, 'World')", NULL, NULL)) != NULL)
+               error(err)
+
+       /* open file stream */
+       stream *fd = open_wastream("/tmp/backup");
+
+       if (dump_database(mid, fd, 0, 0)) {
+               if (mid->msg)
+                       error(mid->msg)
+               fprintf(stderr, "database backup failed\n");
+       }
+       close_stream(fd);
+
+       if ((mid->msg = monetdb_close(mid->mdbe)) != NULL)
+               error(mid->msg);
+}
diff --git a/ctest/tools/monetdbe/example1.c b/ctest/tools/monetdbe/example1.c
new file mode 100644
--- /dev/null
+++ b/ctest/tools/monetdbe/example1.c
@@ -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;
+       monetdb_database mdbe = NULL;
+       monetdb_result* result = NULL;
+
+       // second argument is a string for the db directory or NULL for 
in-memory mode
+       if ((err = monetdb_open(&mdbe, NULL)) != NULL)
+               error(err)
+       if ((err = monetdb_query(mdbe, "CREATE TABLE test (x integer, y 
string)", NULL, NULL)) != NULL)
+               error(err)
+       if ((err = monetdb_query(mdbe, "INSERT INTO test VALUES (42, 'Hello'), 
(NULL, 'World')", NULL, NULL)) != NULL)
+               error(err)
+       if ((err = monetdb_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++) {
+                       monetdb_column* rcol;
+                       if ((err = monetdb_result_fetch(mdbe, 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(mdbe, result)) != NULL)
+               error(err)
+       if ((err = monetdb_close(mdbe)) != NULL)
+               error(err)
+       return 0;
+}
diff --git a/ctest/tools/monetdbe/example2.c b/ctest/tools/monetdbe/example2.c
new file mode 100644
--- /dev/null
+++ b/ctest/tools/monetdbe/example2.c
@@ -0,0 +1,175 @@
+/*
+ * 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;
+       monetdb_database mdbe = NULL;
+       monetdb_result* result = NULL;
+
+       // second argument is a string for the db directory or NULL for 
in-memory mode
+       if ((err = monetdb_open(&mdbe, NULL)) != NULL)
+               error(err)
+       if ((err = monetdb_query(mdbe, "CREATE TABLE test (b bool, t tinyint, s 
smallint, x integer, l bigint, "
+#ifdef HAVE_HGE
+               "h hugeint, "
+#else
+               "h bigint, "
+#endif
+               "f float, d double, y string)", NULL, NULL)) != NULL)
+               error(err)
+       if ((err = monetdb_query(mdbe, "INSERT INTO test VALUES (TRUE, 42, 42, 
42, 42, 42, 42.42, 42.42, 'Hello'), (NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
NULL, 'World')", NULL, NULL)) != NULL)
+               error(err)
+       if ((err = monetdb_query(mdbe, "SELECT b, t, s, x, l, h, f, d, 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++) {
+                       monetdb_column* rcol;
+                       if ((err = monetdb_result_fetch(mdbe, result, &rcol, 
c)) != NULL)
+                               error(err)
+                       switch (rcol->type) {
+                               case monetdb_bool: {
+                                       monetdb_column_bool * col = 
(monetdb_column_bool *) rcol;
+                                       if (col->data[r] == col->null_value) {
+                                               printf("NULL");
+                                       } else {
+                                               printf("%c", 
col->data[r]?'T':'F');
+                                       }
+                                       break;
+                               }
+                               case monetdb_int8_t: {
+                                       monetdb_column_int8_t * col = 
(monetdb_column_int8_t *) rcol;
+                                       if (col->data[r] == col->null_value) {
+                                               printf("NULL");
+                                       } else {
+                                               printf("%d", col->data[r]);
+                                       }
+                                       break;
+                               }
+                               case monetdb_int16_t: {
+                                       monetdb_column_int16_t * col = 
(monetdb_column_int16_t *) rcol;
+                                       if (col->data[r] == col->null_value) {
+                                               printf("NULL");
+                                       } else {
+                                               printf("%d", col->data[r]);
+                                       }
+                                       break;
+                               }
+                               case monetdb_int32_t: {
+                                       monetdb_column_int32_t * col = 
(monetdb_column_int32_t *) rcol;
+                                       if (col->data[r] == col->null_value) {
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to