Changeset: 65dcdc347a47 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=65dcdc347a47
Added Files:
ctest/tools/embedded/backup.c
ctest/tools/embedded/mapi.c
ctest/tools/embedded/mapi.h
Modified Files:
ctest/tools/embedded/CMakeLists.txt
Branch: mbedded
Log Message:
initial example of backup
diffs (282 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
@@ -77,3 +77,13 @@ target_link_libraries(example_append
#sql
)
add_test(run_example_append example_append)
+
+add_executable(backup backup.c mapi.h mapi.c
../../../clients/mapiclient/dump.c)
+target_include_directories(backup PUBLIC .)
+target_link_libraries(backup
+ PRIVATE
+ monetdb_config_header
+ embedded
+ sqlinclude
+ gdk)
+add_test(run_backup backup)
diff --git a/ctest/tools/embedded/backup.c b/ctest/tools/embedded/backup.c
new file mode 100644
--- /dev/null
+++ b/ctest/tools/embedded/backup.c
@@ -0,0 +1,38 @@
+
+#include "monetdb_config.h"
+#include "stream.h"
+#include "mstring.h"
+#include <unistd.h>
+#include <string.h>
+#include <ctype.h>
+#include "mapi.h"
+#include <monetdb_embedded.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()
+{
+ Mapi mid = (Mapi)malloc(sizeof(struct MapiStruct));
+
+ if ((mid->msg = monetdb_startup(NULL, 0)) != NULL)
+ error(mid->msg);
+ if ((mid->msg = monetdb_connect(&mid->conn)) != NULL)
+ error(mid->msg);
+ /* 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_disconnect(mid->conn)) != NULL)
+ error(mid->msg);
+ if ((mid->msg = monetdb_shutdown()) != NULL)
+ error(mid->msg);
+}
diff --git a/ctest/tools/embedded/mapi.c b/ctest/tools/embedded/mapi.c
new file mode 100644
--- /dev/null
+++ b/ctest/tools/embedded/mapi.c
@@ -0,0 +1,159 @@
+
+#include "monetdb_config.h"
+#include "stream.h"
+#include "mapi.h"
+
+#define MAPIalloc(sz) malloc(sz)
+#define MAPIfree(p) free(p)
+
+char *
+mapi_error(Mapi mid)
+{
+ return mid->msg;
+}
+
+MapiHdl
+mapi_query(Mapi mid, const char *query)
+{
+ MapiHdl mh = (MapiHdl)MAPIalloc(sizeof(struct MapiStatement));
+
+ mh->mid = mid;
+ mh->query = (char*)query;
+ mh->msg = monetdb_query(mh->mid->conn, mh->query, &mh->result,
&mh->affected_rows, &mh->prepare_id);
+ mh->current_row = 0;
+ return mh;
+}
+
+MapiMsg
+mapi_close_handle(MapiHdl hdl)
+{
+ if (hdl) {
+ char *msg = monetdb_cleanup_result(hdl->mid->conn, hdl->result);
+ Mapi mid = hdl->mid;
+
+ MAPIfree(hdl);
+ if (msg)
+ hdl->mid->msg = msg;
+ }
+ return MOK;
+}
+
+int
+mapi_fetch_row(MapiHdl hdl)
+{
+ int n = 0;
+ if (hdl && hdl->current_row < hdl->result->nrows) {
+ n = ++hdl->current_row;
+ }
+ return n;
+}
+
+char *
+mapi_fetch_field(MapiHdl hdl, int fnr)
+{
+ /* should only be used for string columns !!! */
+ if (hdl && fnr < hdl->result->ncols && hdl->current_row > 0 &&
hdl->current_row <= hdl->result->nrows) {
+ monetdb_column *rcol = NULL;
+ if (monetdb_result_fetch(hdl->mid->conn, hdl->result, &rcol,
fnr) == NULL) {
+ monetdb_column_str *icol = (monetdb_column_str*)rcol;
+ return icol->data[hdl->current_row-1];
+ }
+ }
+ return NULL;
+}
+
+char *
+mapi_get_type(MapiHdl hdl, int fnr)
+{
+ if (hdl && fnr < hdl->result->ncols) {
+ monetdb_column *rcol = NULL;
+ if (monetdb_result_fetch(hdl->mid->conn, hdl->result, &rcol,
fnr) != NULL) {
+ switch(rcol->type) {
+ case monetdb_int8_t:
+ case monetdb_int16_t:
+ case monetdb_int32_t:
+ case monetdb_int64_t:
+ return "integer";
+ case monetdb_float:
+ case monetdb_double:
+ return "float";
+ case monetdb_str:
+ return "string";
+ default:
+ return "unknown";
+ }
+ }
+ }
+ return NULL;
+}
+
+MapiMsg
+mapi_seek_row(MapiHdl hdl, int64_t rowne, int whence)
+{
+ if (hdl && rowne == 0 && whence == MAPI_SEEK_SET) {
+ hdl->current_row = 0;
+ }
+ return MOK;
+}
+
+int64_t
+mapi_get_row_count(MapiHdl hdl)
+{
+ if (hdl) {
+ return hdl->result->nrows;
+ }
+ return 0;
+}
+
+int64_t
+mapi_rows_affected(MapiHdl hdl)
+{
+ if (hdl) {
+ return hdl->affected_rows;
+ }
+ return 0;
+}
+
+int
+mapi_get_field_count(MapiHdl hdl)
+{
+ if (hdl) {
+ return hdl->result->ncols;
+ }
+ return 0;
+}
+
+const char *
+mapi_result_error(MapiHdl hdl)
+{
+ if (hdl) {
+ return hdl->msg;
+ }
+ return NULL;
+}
+
+int mapi_get_len(MapiHdl hdl, int fnr)
+{
+ (void)hdl;
+ (void)fnr;
+ return 0;
+}
+
+void mapi_explain(Mapi mid, FILE *fd)
+{
+ (void)mid;
+ (void)fd;
+}
+
+void mapi_explain_query(MapiHdl hdl, FILE *fd)
+{
+ (void)hdl;
+ (void)fd;
+}
+
+void mapi_explain_result(MapiHdl hdl, FILE *fd)
+{
+ (void)hdl;
+ (void)fd;
+}
+
diff --git a/ctest/tools/embedded/mapi.h b/ctest/tools/embedded/mapi.h
new file mode 100644
--- /dev/null
+++ b/ctest/tools/embedded/mapi.h
@@ -0,0 +1,53 @@
+
+#ifndef MAPI_H_
+#define MAPI_H_
+
+#include "monetdb_config.h"
+#include "stream.h"
+#include "mstring.h"
+#include <unistd.h>
+#include <string.h>
+#include <ctype.h>
+#include <monetdb_embedded.h>
+
+typedef struct MapiStruct {
+ monetdb_connection conn;
+ char *msg;
+} *Mapi;
+
+typedef struct MapiStatement {
+ Mapi mid;
+ char *query;
+ monetdb_result *result;
+ monetdb_cnt current_row;
+ monetdb_cnt affected_rows;
+ int prepare_id;
+ char *msg;
+} *MapiHdl;
+
+typedef int MapiMsg;
+
+#define MAPI_SEEK_SET 0
+#define MAPI_SEEK_CUR 1
+#define MAPI_SEEK_END 2
+
+#define MOK 0
+
+extern char *mapi_error(Mapi mid);
+extern MapiHdl mapi_query(Mapi mid, const char *query);
+extern MapiMsg mapi_close_handle(MapiHdl hdl);
+extern int mapi_fetch_row(MapiHdl hdl);
+extern char *mapi_fetch_field(MapiHdl hdl, int fnr);
+extern char *mapi_get_type(MapiHdl hdl, int fnr);
+extern MapiMsg mapi_seek_row(MapiHdl hdl, int64_t rowne, int whence);
+extern int64_t mapi_get_row_count(MapiHdl hdl);
+extern int64_t mapi_rows_affected(MapiHdl hdl);
+extern int mapi_get_field_count(MapiHdl hdl);
+extern const char *mapi_result_error(MapiHdl hdl);
+extern int mapi_get_len(MapiHdl hdl, int fnr);
+
+extern void mapi_explain(Mapi mid, FILE *fd);
+extern void mapi_explain_query(MapiHdl hdl, FILE *fd);
+extern void mapi_explain_result(MapiHdl hdl, FILE *fd);
+
+#endif // MAPI_H_
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list