Changeset: 20206593665d for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/20206593665d
Modified Files:
        clients/ChangeLog.Jun2023
        clients/mapiclient/mclient.c
Branch: Jun2023
Log Message:

When using ON CLIENT, also look at location relative to file.


diffs (108 lines):

diff --git a/clients/ChangeLog.Jun2023 b/clients/ChangeLog.Jun2023
--- a/clients/ChangeLog.Jun2023
+++ b/clients/ChangeLog.Jun2023
@@ -1,3 +1,10 @@
 # ChangeLog file for clients
 # This file is updated with Maddlog
 
+* Tue Jun 20 2023 Sjoerd Mullender <[email protected]>
+- The COPY INTO from file ON CLIENT was extended to also look for a
+  relative path name relative to the file from which the query was read.
+  This is only possible if the name of the query file is known, so when
+  it is specified on the command line or read using the interactive
+  \< command.
+
diff --git a/clients/mapiclient/mclient.c b/clients/mapiclient/mclient.c
--- a/clients/mapiclient/mclient.c
+++ b/clients/mapiclient/mclient.c
@@ -78,6 +78,7 @@ static char *encoding;
 #endif
 static bool errseen = false;
 static bool allow_remote = false;
+static const char *curfile = NULL;
 
 #define setPrompt() snprintf(promptbuf, sizeof(promptbuf), "%.*s>", (int) 
sizeof(promptbuf) - 2, language)
 #define debugMode() (strncmp(promptbuf, "mdb", 3) == 0)
@@ -2744,8 +2745,14 @@ doFile(Mapi mid, stream *fp, bool useins
                                                if (s)
                                                        close_stream(s);
                                                mnstr_printf(stderr_stream, 
"Cannot open %s: %s\n", line, mnstr_peek_error(NULL));
-                                       } else
+                                       } else {
+                                               const char *oldfile = curfile;
+                                               char *newfile = strdup(line);
+                                               curfile = newfile;
                                                doFile(mid, s, 0, 0, 0);
+                                               curfile = oldfile;
+                                               free(newfile);
+                                       }
                                        continue;
                                }
                                case '>':
@@ -3033,8 +3040,25 @@ getfile(void *data, const char *filename
                        }
 #endif
                }
-               if (f == NULL)
-                       return (char*) mnstr_peek_error(NULL);
+               if (f == NULL) {
+                       if (curfile != NULL) {
+                               char *p = strrchr(curfile, '/');
+#ifdef _MSC_VER
+                               char *q = strrchr(curfile, '\\');
+                               if (p == NULL || (q != NULL && q > p))
+                                       p = q;
+#endif
+                               if (p != NULL) {
+                                       size_t x = (size_t) (p - curfile) + 
strlen(filename) + 2;
+                                       char *b = malloc(x);
+                                       snprintf(b, x, "%.*s/%s", (int) (p - 
curfile), curfile, filename);
+                                       f = binary ? open_rstream(b) : 
open_rastream(b);
+                                       free(b);
+                               }
+                       }
+                       if (f == NULL)
+                               return (char*) mnstr_peek_error(NULL);
+               }
                while (offset > 1) {
                        s = mnstr_readline(f, buf, READSIZE);
                        if (s < 0) {
@@ -3528,7 +3552,7 @@ main(int argc, char **argv)
        c = 0;
        has_fileargs = optind != argc;
 
-       if (dbname == NULL && has_fileargs) {
+       if (dbname == NULL && has_fileargs && strcmp(argv[optind], "-") != 0) {
                s = open_rastream(argv[optind]);
                if (s == NULL || !isfile(getFile(s))) {
                        mnstr_close(s);
@@ -3538,6 +3562,8 @@ main(int argc, char **argv)
                        dbname = strdup(argv[optind]);
                        optind++;
                        has_fileargs = optind != argc;
+               } else {
+                       curfile = argv[optind];
                }
        }
 
@@ -3710,15 +3736,18 @@ main(int argc, char **argv)
                        const char *arg = argv[optind];
 
                        if (s == NULL) {
-                               if (strcmp(arg, "-") == 0)
+                               if (strcmp(arg, "-") == 0) {
                                        s = stdin_rastream();
-                               else
+                               } else {
                                        s = open_rastream(arg);
+                                       curfile = arg;
+                               }
                        }
                        if (s == NULL) {
                                mnstr_printf(stderr_stream, "%s: cannot open: 
%s\n", arg, mnstr_peek_error(NULL));
                                c |= 1;
                                optind++;
+                               curfile = NULL;
                                continue;
                        }
                        // doFile closes 's'.
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to