Changeset: cf33fead690f for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=cf33fead690f
Added Files:
        ctest/tools/embedded/example2.c
Modified Files:
        ctest/tools/embedded/CMakeLists.txt
        tools/embedded/monetdb_embedded.c
        tools/embedded/monetdb_embedded.h
Branch: mbedded
Log Message:

add bool type
add simple example of base types bool, tinyint, smallint, int, bigint, float, 
double and varchar


diffs (213 lines):

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
@@ -17,3 +17,15 @@ target_link_libraries(example1
     #sql
     )
 add_test(run_example1 example1)
+
+add_executable(example2 example2.c)
+target_link_libraries(example2
+  PRIVATE
+    monetdb_config_header
+    embedded
+    sqlinclude
+    gdk
+    mapi
+    #sql
+    )
+add_test(run_example2 example2)
diff --git a/ctest/tools/embedded/example2.c b/ctest/tools/embedded/example2.c
new file mode 100644
--- /dev/null
+++ b/ctest/tools/embedded/example2.c
@@ -0,0 +1,133 @@
+/*
+ * 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 (b bool, t tinyint, s 
smallint, x integer, l bigint, f float, d double, y string)", NULL, NULL, 
NULL)) != NULL)
+               error(err)
+       if ((err = monetdb_query(conn, "INSERT INTO test VALUES (TRUE, 42, 42, 
42, 42, 42.42, 42.42, 'Hello'), (NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
'World')", NULL, NULL, NULL)) != NULL)
+               error(err)
+       if ((err = monetdb_query(conn, "SELECT b, t, s, x, l, f, d, 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, &rcol, result, 
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) {
+                                               printf("NULL");
+                                       } else {
+                                               printf("%d", col->data[r]);
+                                       }
+                                       break;
+                               }
+                               case monetdb_int64_t: {
+                                       monetdb_column_int64_t * col = 
(monetdb_column_int64_t *) rcol;
+                                       if (col->data[r] == col->null_value) {
+                                               printf("NULL");
+                                       } else {
+                                               printf("%ld", col->data[r]);
+                                       }
+                                       break;
+                               }
+                               case monetdb_float: {
+                                       monetdb_column_float * col = 
(monetdb_column_float *) rcol;
+                                       if (col->data[r] == col->null_value) {
+                                               printf("NULL");
+                                       } else {
+                                               printf("%f", col->data[r]);
+                                       }
+                                       break;
+                               }
+                               case monetdb_double: {
+                                       monetdb_column_double * col = 
(monetdb_column_double *) rcol;
+                                       if (col->data[r] == col->null_value) {
+                                               printf("NULL");
+                                       } else {
+                                               printf("%f", 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;
+}
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
@@ -792,6 +792,7 @@ monetdb_shutdown(void)
        GENERATE_BASE_HEADERS(tpe, tpename); \
        static int tpename##_is_null(tpe value) { return value == mname##_nil; }
 
+GENERATE_BASE_FUNCTIONS(int8_t, bool, bit)
 GENERATE_BASE_FUNCTIONS(int8_t, int8_t, bte)
 GENERATE_BASE_FUNCTIONS(int16_t, int16_t, sht)
 GENERATE_BASE_FUNCTIONS(int32_t, int32_t, int)
@@ -887,7 +888,10 @@ monetdb_result_fetch(monetdb_connection 
        bat_type = b->ttype;
        sqltpe = &result->monetdb_resultset->cols[column_index].type;
 
-       if (bat_type == TYPE_bit || bat_type == TYPE_bte) {
+       if (bat_type == TYPE_bit) {
+               GENERATE_BAT_INPUT(b, int8_t, bit);
+               column_result->type = monetdb_bool;
+       } else if (bat_type == TYPE_bte) {
                GENERATE_BAT_INPUT(b, int8_t, bte);
        } else if (bat_type == TYPE_sht) {
                GENERATE_BAT_INPUT(b, int16_t, sht);
diff --git a/tools/embedded/monetdb_embedded.h 
b/tools/embedded/monetdb_embedded.h
--- a/tools/embedded/monetdb_embedded.h
+++ b/tools/embedded/monetdb_embedded.h
@@ -77,7 +77,7 @@ typedef struct {
 } monetdb_data_blob;
 
 typedef enum  {
-       monetdb_int8_t, monetdb_int16_t, monetdb_int32_t, monetdb_int64_t, 
monetdb_size_t,
+       monetdb_bool, monetdb_int8_t, monetdb_int16_t, monetdb_int32_t, 
monetdb_int64_t, monetdb_size_t,
        monetdb_float, monetdb_double,
        monetdb_str, monetdb_blob,
        monetdb_date, monetdb_time, monetdb_timestamp
@@ -111,10 +111,12 @@ typedef void* monetdb_connection;
                int (*is_null)(ctype value);               \
        } monetdb_column_##typename
 
+DEFAULT_STRUCT_DEFINITION(int8_t, bool);
 DEFAULT_STRUCT_DEFINITION(int8_t, int8_t);
 DEFAULT_STRUCT_DEFINITION(int16_t, int16_t);
 DEFAULT_STRUCT_DEFINITION(int32_t, int32_t);
 DEFAULT_STRUCT_DEFINITION(int64_t, int64_t);
+// HUGE INT ?
 DEFAULT_STRUCT_DEFINITION(size_t, size_t);
 
 DEFAULT_STRUCT_DEFINITION(float, float);
@@ -126,6 +128,7 @@ DEFAULT_STRUCT_DEFINITION(monetdb_data_b
 DEFAULT_STRUCT_DEFINITION(monetdb_data_date, date);
 DEFAULT_STRUCT_DEFINITION(monetdb_data_time, time);
 DEFAULT_STRUCT_DEFINITION(monetdb_data_timestamp, timestamp);
+// UUID, INET, XML ?
 
 embedded_export char* monetdb_connect(monetdb_connection *conn);
 embedded_export char* monetdb_disconnect(monetdb_connection conn);
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to