summaryrefslogtreecommitdiff
path: root/util/db_sql_codegen/hint_comment.c
blob: 7b365a905ddb81365bc75409b74afe857954202e (plain)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1996, 2015 Oracle and/or its affiliates.  All rights reserved.
 *
 */

/*
 * These are functions related to parsing and handling hint comments
 * embedded in the input SQL DDL source.  Hint comments convey BDB
 * configuration information that cannot be represented in SQL DDL.
 */

#include <ctype.h>
#include "db_sql_codegen.h"

static void
hc_warn(char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	fprintf(stderr, "Warning: ");
	vfprintf(stderr, fmt, ap);
	fprintf(stderr, ", near line %d\n", line_number);

	va_end(ap);
}

/*
 * Return a static copy of the given string, with the given length, in
 * which all whitespace has been removed
 */
static char *
static_copy_minus_whitespace(in, len)
	const char *in;
	int len;
{
#define	smw_bufsiz 10240
	static char out[smw_bufsiz];

	int in_i;
	int out_i;
	int in_quote;

	in_quote = 0;
	for (in_i = out_i = 0; in_i < len && in[in_i] != '\0'; in_i++) {
		if (in[in_i] == '"') {
			if (in_quote) in_quote = 0;
			else in_quote = 1;
		}

		if (in_quote || ! isspace(in[in_i])) {
			out[out_i++] = in[in_i];
			assert(out_i < smw_bufsiz);
		}
	}

	out[out_i] = '\0';

	return out;
}

/*
 * Extract a string from the given token.  The returned copy is static
 * and has had all whitespace removed.
 */
static char *
hint_comment_from_token(t)
	Token *t;
{
	int len;
	char *p;

	len = 0;
	p = NULL;
	if (t == NULL)
		return NULL;

	/* The token should be a whole comment; verify that */

	if (t->z[0] == '/') {
		assert(t->n >= 4 &&
		       t->z[1] == '*' &&
		       t->z[t->n - 2] == '*' &&
		       t->z[t->n - 1] == '/');
		p = ((char *)t->z) + 2;
		len = t->n - 4;
	} else if (t->z[0] == '-') {
		assert(t->n >= 3 &&
		       t->z[1] == '-');
		p = ((char *)t->z) + 2;
		len = t->n - 2;
	}

	assert(p != NULL);

	if (*p != '+')              /* the hint comment indicator */
		return NULL;

	return static_copy_minus_whitespace(p+1, len-1);
}

/*
 * Break a string into two parts at the delimiting char.  The left
 * token is returned, while the right token, if any, is placed in
 * *rest.  If found, the delimiting char in the input string is
 * replaced with a null char, to terminate the left token string.
 */
static char *
split(in, delimiter, rest)
	char *in;
	char delimiter;
	char **rest;
{
	char *p;

	*rest = NULL;

	for (p = in; ! (*p == delimiter || *p == '\0'); p++)
		;

	if (*p != '\0') {
		*rest = p + 1;
		*p = '\0';
	}

	return in;
}

/*
 * This is basically strtoul with multipliers for suffixes such as k,
 * m, g for kilobytes, megabytes, and gigabytes
 */
static
unsigned long int parse_integer(s)
	char *s;
{
	unsigned long int x;
	char *t;

	x = strtoul(s, &t, 0);
	if (s == t)
		hc_warn("cannot parse integer string %s", s);

	switch (*t) {
	case '\0':
		break;
	case 'k':
	case 'K':
		x = x * KILO;
		t++;
		break;
	case 'm':
	case 'M':
		x = x * MEGA;
		t++;
		break;
	case 'g':
	case 'G':
		x = x * GIGA;
		t++;
		break;
	}

	if (*t != '\0')
		hc_warn("unrecognized characters in integer string %s", s);

	return x;
}

static void
apply_environment_property(key, value)
	char *key;
	char *value;
{
	if (strcasecmp(key, "CACHESIZE") == 0) {
		the_schema.environment.cache_size = parse_integer(value);
	} else if (strcasecmp(key, "MODE") == 0) {
		if (strcasecmp(value, "TRANSACTIONAL") == 0) {
			the_schema.environment.transactional = 1;
		} else if (strcasecmp(value, "NONTRANSACTIONAL") == 0) {
			the_schema.environment.transactional = txnflag;
		} else {
			hc_warn("Unrecognized environment value %s", value);
		}
	} else {
		hc_warn("Unrecognized environment property %s", key);
	}
}

static void
set_dbtype(entity, value)
	ENTITY *entity;
	char *value;
{
	if (strcasecmp(value, "btree") == 0) {
		entity->dbtype = "DB_BTREE";
	} else if (strcasecmp(value, "hash") == 0) {
		entity->dbtype = "DB_HASH";
	} else {
		hc_warn(
"unknown DBTYPE %s for antecedent %s, using default of DB_BTREE",
			value, entity->name);
		entity->dbtype = "DB_BTREE";
	}
}

static void
set_idx_dbtype(idx, value)
	DB_INDEX *idx;
	char *value;
{
	if (strcasecmp(value, "btree") == 0) {
		idx->dbtype = "DB_BTREE";
	} else if (strcasecmp(value, "hash") == 0) {
		idx->dbtype = "DB_HASH";
	} else {
		hc_warn(
"unknown DBTYPE %s for antecedent %s, using default of DB_BTREE",
			value, idx->name);
		idx->dbtype = "DB_BTREE";
	}
}

static void
set_transactional(entity, value)
	ENTITY *entity;
	char *value;
{
	if (strcasecmp(value, "TRANSACTIONAL") == 0) {
		if (the_schema.environment.transactional) {
			entity->transactional = 1;
		} else {
			hc_warn("Wrong entity value %s", value);
		}
	} else if (strcasecmp(value, "NONTRANSACTIONAL") == 0) {
		entity->transactional = txnflag;
	} else {
		hc_warn("Unrecognized entity value %s", value);
	}
}

static void
apply_entity_property(key, value, entity)
	char *key;
	char *value;
	ENTITY *entity;
{
	if (strcasecmp(key, "DBTYPE") == 0) {
		set_dbtype(entity, value);
	} else if (strcasecmp(key, "MODE") == 0) {
		set_transactional(entity, value);
	} else {
		hc_warn("Unrecognized entity property %s", key);
	}
}

static void
apply_index_property(key, value, idx)
	char *key;
	char *value;
	DB_INDEX *idx;
{
	if (strcasecmp(key, "DBTYPE") == 0) {
		set_idx_dbtype(idx, value);
	} else {
		hc_warn("Unrecognized index property %s", key);
	}
}

/*
 * Apply a configuration keyword and parameter.
 */

static void
apply_configuration_property(key, value)
	char * key;
	char *value;
{
	switch (the_parse_progress.last_event) {
	case PE_NONE:
		hc_warn(
		      "Property setting (%s) with no antecedent SQL statement",
		      key);
		break;
	case PE_ENVIRONMENT:
		apply_environment_property(key, value);
		break;
	case PE_ENTITY:
	case PE_ATTRIBUTE:  /* no per-attribute properties yet */
		apply_entity_property(key, value,
				      the_parse_progress.last_entity);
		break;
	case PE_INDEX:
		apply_index_property(key, value, the_parse_progress.last_index);
	}
}

/*
 * Extract property assignments from a SQL comment, if it is marked
 * as a hint comment.
 */
void parse_hint_comment(t)
	Token *t;
{
	char *assignment, *key, *value;
	char *comment;

	comment = hint_comment_from_token(t);

	if (comment == NULL)
		return;

	while (! (comment == NULL || *comment == '\0')) {
		assignment = split(comment, ',', &comment);

		/*
		 * Split the assignment into key, value tokens on the
		 * equals sign.  Verify that there is only one equals
		 * sign.
		 */
		key = split(assignment, '=', &value);

		if (value == NULL) {
			hc_warn("No value specified for property %s\n",
				key);
			break;
		}

		apply_configuration_property(key, value);

		key = split(key, '=', &value);
		if (value != NULL)
			hc_warn(
		      "Warning: incorrect hint comment syntax with property %s",
				key);
	}
}