Changeset: ff8e3560ca3e for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=ff8e3560ca3e
Added Files:
        ctest/tools/monetdbe/example_copy.c
Modified Files:
        ctest/tools/monetdbe/CMakeLists.txt
Branch: default
Log Message:

Added a test for COPY INTO from file


diffs (103 lines):

diff --git a/ctest/tools/monetdbe/CMakeLists.txt 
b/ctest/tools/monetdbe/CMakeLists.txt
--- a/ctest/tools/monetdbe/CMakeLists.txt
+++ b/ctest/tools/monetdbe/CMakeLists.txt
@@ -64,6 +64,15 @@ target_link_libraries(example_backup
 add_test(run_example_backup example_backup)
 endif()
 
+if(NOT WIN32)
+add_executable(example_copy example_copy.c)
+target_link_libraries(example_copy
+  PRIVATE
+    monetdb_config_header
+    monetdbe)
+add_test(run_example_copy example_copy)
+endif()
+
 add_executable(example_connections example_connections.c)
 target_link_libraries(example_connections
   PRIVATE
diff --git a/ctest/tools/monetdbe/example_copy.c 
b/ctest/tools/monetdbe/example_copy.c
new file mode 100644
--- /dev/null
+++ b/ctest/tools/monetdbe/example_copy.c
@@ -0,0 +1,79 @@
+/*
+ * 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 - 2021 MonetDB B.V.
+ */
+
+#include "monetdb_config.h"
+#include <monetdbe.h>
+#include <unistd.h>
+#include <limits.h>
+
+#define error(msg) {fprintf(stderr, "Failure: %s\n", msg); return -1;}
+#define delete_file(fpath) {  int del = remove(fpath); if (del) { error("Could 
not remove the file"); } }
+
+int
+main(void)
+{
+       char sql[1000];
+       char csv_path[PATH_MAX];
+       char* err = NULL;
+       monetdbe_database mdbe;
+       monetdbe_result* result = NULL;
+
+       if (monetdbe_open(&mdbe, NULL, NULL))
+               error("Failed to open database");
+
+       if ((err = monetdbe_query(mdbe, "CREATE TABLE test (b bool, x integer, 
f float, y string)", NULL, NULL)) != NULL)
+               error(err)
+
+       if ((err = monetdbe_query(mdbe, "INSERT INTO test VALUES (TRUE, 42, 
42.42, 'Hello'), (NULL, NULL, NULL, 'World')", NULL, NULL)) != NULL)
+               error(err)
+
+       // Get working directory and construct the CSV path
+       if (getcwd(csv_path, sizeof(csv_path)) == NULL) {
+               error("Could not get the current working directory");
+       }
+       strcat(csv_path, "/test.csv");
+
+    strcpy(sql, "COPY SELECT * FROM test INTO '");
+    strcat(sql, csv_path);
+    strcat(sql, "' USING DELIMITERS ',' NULL AS ''");
+
+       if ((err = monetdbe_query(mdbe, sql, NULL, NULL)) != NULL)
+               error(err)
+       
+       if ((err = monetdbe_query(mdbe, "CREATE TABLE test_copy (b bool, x 
integer, f float, y string)", NULL, NULL)) != NULL) {
+               delete_file(csv_path)
+               error(err)
+       }
+
+       memset(sql, 0, 1000);
+       strcpy(sql, "COPY INTO test_copy FROM '");
+    strcat(sql, csv_path);
+    strcat(sql, "' DELIMITERS ','");
+
+       if ((err = monetdbe_query(mdbe, sql, NULL, NULL)) != NULL) {
+               delete_file(csv_path)
+               error(err)
+       }
+
+       if ((err = monetdbe_query(mdbe, "SELECT * FROM test_copy; ", &result, 
NULL)) != NULL) {
+               delete_file(csv_path)
+               error(err)
+       }
+
+       if (result->nrows == 0) {
+               delete_file(csv_path)
+               error("Copy failed, database is empty")
+       }
+
+       delete_file(csv_path)
+
+       if (monetdbe_close(mdbe))
+               error("Failed to close database");
+
+       return 0;
+}
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to