1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2005, 2013 Oracle and/or its affiliates. All rights reserved.
*
* $Id$
*/
#include "csv.h"
#include "csv_local.h"
#include "csv_extern.h"
static int query_by_field(char *);
static int query_fieldlist(char *);
static int query_help(char *);
static int query_usage(void);
typedef struct _cmdtab {
char *cmd; /* Command name */
int (*f)(char *); /* Underlying function. */
char *help; /* Help message. */
} CMDTAB;
static CMDTAB cmdtab[] = {
{ "?",
query_help,
"?\t\tDisplay help screen" },
{ "exit",
NULL,
"exit\t\tExit program" },
{ "fields",
query_fieldlist,
"fields\t\tDisplay list of field names" },
{ "help",
query_help,
"help\t\tDisplay help screen" },
{ "quit",
NULL,
"quit\t\tExit program" },
{ NULL,
query_by_field,
"field[op]value\tDisplay fields by value (=, !=, <, <=, >, >=, ~, !~)" },
{ NULL, NULL, NULL }
};
/*
* query_interactive --
* Allow the user to interactively query the database.
*/
int
query_interactive()
{
int done;
char *p, input[256];
for (;;) {
printf("Query: ");
(void)fflush(stdout);
if (fgets(input, sizeof(input), stdin) == NULL) {
printf("\n");
if (ferror(stdin)) {
dbenv->err(dbenv, errno,
"error occurred reading from stdin");
return (1);
}
break;
}
if ((p = strchr(input, '\n')) == NULL) {
dbenv->errx(dbenv, "input buffer too small");
return (1);
}
*p = '\0';
if (query(input, &done) != 0)
return (1);
if (done != 0)
break;
}
return (0);
}
/*
* query --
* Process a query.
*/
int
query(char *cmd, int *donep)
{
CMDTAB *p;
if (donep != NULL)
*donep = 0;
for (p = cmdtab; p->cmd != NULL; ++p)
if (p->cmd != NULL &&
strncasecmp(cmd, p->cmd, strlen(p->cmd)) == 0)
break;
if (p->cmd == NULL)
return (query_by_field(cmd));
if (p->f == NULL) {
if (donep != NULL)
*donep = 1;
return (0);
}
return (p->f(cmd));
}
/*
* query_by_field --
* Query the primary database by field.
*/
static int
query_by_field(char *input)
{
OPERATOR operator;
size_t len;
char *field, *op, *value;
/*
* We expect to see "field [op] value" -- figure it out.
*
* Skip leading whitespace.
*/
while (isspace(*input))
++input;
/*
* Find an operator, and it better not start the string.
*/
if ((len = strcspn(field = input, "<>!=~")) == 0)
return (query_usage());
op = field + len;
/* Figure out the operator, and find the start of the value. */
switch (op[0]) {
case '~':
operator = WC;
value = op + 1;
break;
case '!':
if (op[1] == '=') {
operator = NEQ;
value = op + 2;
break;
}
if (op[1] == '~') {
operator = NWC;
value = op + 2;
break;
}
return (query_usage());
case '<':
if (op[1] == '=') {
operator = LTEQ;
value = op + 2;
} else {
operator = LT;
value = op + 1;
}
break;
case '=':
operator = EQ;
if (op[1] == '=')
value = op + 2;
else
value = op + 1;
break;
case '>':
if (op[1] == '=') {
operator = GTEQ;
value = op + 2;
} else {
operator = GT;
value = op + 1;
}
break;
default:
return (query_usage());
}
/* Terminate the field name, and there better be a field name. */
while (--op > input && isspace(*op))
;
if (op == input)
return (query_usage());
op[1] = '\0';
/* Make sure there is a value field. */
while (isspace(*value))
++value;
if (*value == '\0')
return (query_usage());
return (DbRecord_search_field_name(field, value, operator));
}
/*
* query_fieldlist --
* Display list of field names.
*/
static int
query_fieldlist(char *input)
{
DbField *f;
input = input; /* Quiet compiler. */
for (f = fieldlist; f->name != NULL; ++f)
printf("field %3d: %s\n", f->fieldno, f->name);
return (0);
}
/*
* query_help --
* Query command list.
*/
static int
query_help(char *input)
{
CMDTAB *p;
input = input; /* Quiet compiler. */
printf("Query commands:\n");
for (p = cmdtab; p->help != NULL; ++p)
printf("\t%s\n", p->help);
return (0);
}
/*
* query_usage --
* Query usage message.
*/
static int
query_usage(void)
{
fprintf(stderr, "%s: query syntax error\n", progname);
return (query_help(NULL));
}
|