📄 prog.c
字号:
/* * The contents of this file are subject to the MonetDB Public License * Version 1.1 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://monetdb.cwi.nl/Legal/MonetDBLicense-1.1.html * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the * License for the specific language governing rights and limitations * under the License. * * The Original Code is the MonetDB Database System. * * The Initial Developer of the Original Code is CWI. * Portions created by CWI are Copyright (C) 1997-2007 CWI. * All Rights Reserved. */#include "embeddedclient.h"#include <monet_options.h>#ifdef HAVE_STRING_H#include <string.h>#endif/* stolen piece */#ifdef HAVE_FTIME#include <sys/timeb.h>#endif#if TIME_WITH_SYS_TIME# include <sys/time.h># include <time.h>#else# if HAVE_SYS_TIME_H# include <sys/time.h># else# include <time.h># endif#endifstatic longgettime(void){#ifdef HAVE_GETTIMEOFDAY struct timeval tp; gettimeofday(&tp, NULL); return (long) tp.tv_sec * 1000000 + (long) tp.tv_usec;#else#ifdef HAVE_FTIME struct timeb tb; ftime(&tb); return (long) tb.time * 1000000 + (long) tb.millitm * 1000;#endif#endif}voidusage(char *prog){ fprintf(stderr, "Usage: %s [ options ] [ script+ ] \n", prog); fprintf(stderr, "Options are: \n"); fprintf(stderr, " -c <config_file> | --config=<config_file> \n"); fprintf(stderr, " -d<debug_level> | --debug=<debug_level> \n"); fprintf(stderr, " -t | --time \n"); fprintf(stderr, " --dbname=<database_name> \n"); fprintf(stderr, " --dbfarm=<database_directory>\n"); fprintf(stderr, " -s <option>=<value> | --set <option>=<value> \n"); fprintf(stderr, " -? | --help \n"); exit(-1);}intmain(int argc, char **av){ int curlen = 0, maxlen = BUFSIZ*8; char *prog = *av; opt *set = NULL; int setlen = 0, time = 0; long t0 = 0; Mapi mid; MapiHdl hdl; char *buf, *line; FILE *fp = NULL; static struct option long_options[] = { {"config", 1, 0, 'c'}, {"dbname", 1, 0, 0}, {"dbfarm", 1, 0, 0}, {"debug", 2, 0, 'd'}, {"time", 0, 0, 't'}, {"set", 1, 0, 's'}, {"help", 0, 0, '?'}, {0, 0, 0, 0} }; if (!(setlen = mo_builtin_settings(&set))) usage(prog); /* needed, to prevent the MonetDB/4 config file to be used */ setlen = mo_add_option(&set, setlen, opt_config, "prefix", MONETDBPREFIX); setlen = mo_add_option(&set, setlen, opt_config, "config", MONETDBCONFIG); setlen = mo_system_config(&set, setlen); for (;;) { int option_index = 0; int c = getopt_long(argc, av, "c:d::?s:t", long_options, &option_index); if (c == -1) break; switch (c) { case 0: if (strcmp((char*)long_options[option_index].name, "dbname") == 0) { setlen = mo_add_option(&set, setlen, opt_cmdline, "gdk_dbname", optarg); break; } if (strcmp((char*)long_options[option_index].name, "dbfarm") == 0) { setlen = mo_add_option(&set, setlen, opt_cmdline, "gdk_dbfarm", optarg); break; } usage(prog); break; case 't': time = 1; break; case 'c': setlen = mo_add_option(&set, setlen, opt_cmdline, "config", optarg); break; case 'd': if (optarg) { setlen = mo_add_option(&set, setlen, opt_cmdline, "gdk_debug", optarg); } break; case 's':{ /* should add option to a list */ char *tmp = strchr(optarg, '='); if (tmp) { *tmp = '\0'; setlen = mo_add_option(&set, setlen, opt_cmdline, optarg, tmp + 1); } else { fprintf(stderr, "!ERROR: wrong format %s\n", optarg); } } break; case '?': usage(prog); default: fprintf(stderr, "!ERROR: getopt returned character code 0%o ??\n", c); usage(prog); } } mid = embedded_mal(set, setlen); /* now for each file given on the command line (or stdin) read the query and execute it */ buf = GDKmalloc(maxlen); if (buf == NULL) { fprintf(stderr, "Cannot allocate memory for query buffer\n"); return -1; } if (optind == argc) fp = stdin; while (optind < argc || fp) { if (!fp && (fp=fopen(av[optind],"r")) == NULL){ fprintf(stderr,"could no open file %s\n", av[optind]); } while ((line = fgets(buf+curlen, 1024, fp)) != NULL) { int n = strlen(line); curlen += n; if (curlen+1024 > maxlen) { maxlen += 8*BUFSIZ; buf = GDKrealloc(buf, maxlen + 1); if (buf == NULL) { fprintf(stderr, "Cannot allocate memory for query buffer\n"); return -1; } } } if (fp != stdin) { fclose(fp); } fp = NULL; optind++; curlen = 0; if (time) t0 = gettime(); hdl = mapi_query(mid, buf); do { if (mapi_result_error(hdl) != NULL) mapi_explain_result(hdl, stderr); while ((line = mapi_fetch_line(hdl)) != NULL) printf("%s\n", line); } while (mapi_next_result(hdl) == 1); mapi_close_handle(hdl); if (time) printf("Timer: %ld (usec)\n", gettime()-t0); } GDKfree(buf); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -