File: yyevl.c

package info (click to toggle)
unixodbc 2.1.1-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 12,668 kB
  • ctags: 12,486
  • sloc: ansic: 107,685; cpp: 33,663; sh: 13,300; makefile: 2,926; yacc: 499; lex: 241; sed: 93; sql: 1
file content (649 lines) | stat: -rw-r--r-- 11,528 bytes parent folder | download | duplicates (6)
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/**
    Copyright (C) 1995, 1996 by Ke Jin <kejin@visigenic.com>
	Enhanced for unixODBC (1999) by Peter Harvey <pharvey@codebydesign.com>
	
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
**/
#include	<config.h>
#include	<yystmt.h>
#include	<yylex.h>
#include	<nncol.h>
#include	<nndate.h>

typedef struct {
	int	type;	/* can only be en_nt_qstr, en_nt_num and en_nt_null */

	union {
		char*	qstr;
		long	num;
		date_t	date;
	} value;
} leaf_t;

static int	getleaf(yystmt_t* yystmt, node_t* nd, leaf_t* lf)
{
	yypar_t*	par;
	yyattr_t*	attr;

	switch( nd->type )
	{
		case en_nt_attr:
			attr = yystmt->pattr + (nd->value).iattr;

			if( (nd->value).iattr == en_lines
			 || (nd->value).iattr == en_article_num )
			{
				lf->type = en_nt_num;
				(lf->value).num = (attr->value).number;
				break;
			}

			if( (nd->value).iattr == en_date )
			{
				if( (attr->value).date.day )
				{
					lf->type = en_nt_date;
					(lf->value).date =
						(attr->value).date;
				}
				else
					lf->type = en_nt_null;
				break;
			}

			if( (attr->value).location )
			{
				lf->type = en_nt_qstr;
				(lf->value).qstr =
					(attr->value).location;
			}
			else
				lf->type = en_nt_null;
			break;

		case en_nt_param:
			par = yystmt->ppar + (nd->value).ipar - 1;

			if( par->type == en_nt_null )
			{
				lf->type = en_nt_null;
				break;
			}

			if( par->type == en_nt_num )
			{
				lf->type = en_nt_num;
				(lf->value).num = (par->value).number;
				break;
			}

			if( par->type == en_nt_qstr)
			{
				if( (par->value).location )
				{
					lf->type = en_nt_qstr;

					(lf->value).qstr =
						(par->value).location;
					break;
				}

				lf->type = en_nt_null;
				break;
			}

			if( par->type == en_nt_date)
			{
				if( (par->value).date.day )
				{
					lf->type = en_nt_date;

					(lf->value).date =
						(par->value).date;
					break;
				}

				lf->type = en_nt_null;
			}

			return -1;

		case en_nt_num:
			lf->type = en_nt_num;
			(lf->value).num = (nd->value).num;
			break;

		case en_nt_qstr:
			lf->type = en_nt_qstr;

			if( (nd->value).qstr )
			{
				lf->type = en_nt_qstr;
				(lf->value).qstr = (nd->value).qstr;
				break;
			}

			lf->type = en_nt_null;
			break;

		case en_nt_date:
			lf->type = en_nt_date;

			(lf->value).date = (nd->value).date;
			break;

		case en_nt_null:
			lf->type = en_nt_null;
			break;

		default:
			return -1;
	}

	return 0;
}

#ifndef MAX_SIGNED_LONG
# define	MAX_SIGNED_LONG ((-1UL) >> 1)
#endif

# define	AREA		( MAX_SIGNED_LONG + 1UL )
# define	RANGE_ALL	{ 0, 1UL, MAX_SIGNED_LONG }
# define	RANGE_EMPTY	{ 1,  0UL, 0UL }
# define	ALL_RANGE(r)	{ r.flag = 0; \
				  r.min = 1UL; \
				  r.max = MAX_SIGNED_LONG; }
# define	EMPTY_RANGE(r)	{ r.flag = 1;\
				  r.min = r.max = 0UL; }
# define	IS_EMPTY_RANGE(r) \
				(((r.min == 0UL) && (r.max==0UL))? 1:0)
# define	RANGE_MIN(a, b) ((((unsigned long)(a)) < ((unsigned long)(b)))? a:b)
# define	RANGE_MAX(a, b) ((((unsigned long)(a)) > ((unsigned long)(b)))? a:b)

typedef struct {
	int		flag;
	unsigned long	min;
	unsigned long	max;
} range_t;

static	range_t range_and(range_t r1, range_t r2)
{
	range_t r = RANGE_EMPTY;

	if( !(r.flag = r1.flag || r2.flag) )
		return r;

	if( r1.min > r2.max
	 || r1.max < r2.min )
		return r;

	r.min = (RANGE_MAX(r1.min, r2.min))%AREA;
	r.max = (RANGE_MIN(r1.max, r2.max))%AREA;

	return r;
}

static	range_t range_or(range_t r1, range_t r2)
{
	range_t r = RANGE_ALL;

	if( !(r.flag = r1.flag || r2.flag) )
		return r;

	if( !(r1.max/AREA) || !(r2.max/AREA) )
	/* at least one of the rangion does not cross cell */
	{
		unsigned long	c1, c2;

		c1 = (r1.max/2) + (r1.min/2);	/* central of rangion 1 */
		c2 = (r2.max/2) + (r2.min/2);	/* central of rangion 2 */

		if( c1 > c2 && (c1 - c2) > (AREA/2) )
		{
			/* shift it to the second cell */
			r2.min += AREA;
			r2.max += AREA;
		}
		else
		if( c2 > c1 && (c2 - c1) > (AREA/2) )
		{
			/* shift it to the second cell */
			r1.min += AREA;
			r1.max += AREA;
		}
	}

	r.min = (RANGE_MIN(r1.min, r2.min))%AREA;
	r.max = (RANGE_MAX(r1.max, r2.max))%AREA;

	return r;
}

static	range_t getrange(yystmt_t* yystmt, node_t* pnd)
{
	range_t r = RANGE_ALL, r1, r2;
	node_t* tpnd = 0;
	leaf_t	a, b;
	int	flag = 0;

	if( !pnd )
		return r;

	if( pnd->left
	 && pnd->left->type == en_nt_attr
	 && (pnd->left->value).iattr == en_article_num )
	{
		r.flag = 1;

		switch( pnd->type )
		{
			case en_nt_between:
				getleaf(yystmt, pnd->right->left,  &a);
				getleaf(yystmt, pnd->right->right, &b);

				if( a.type == en_nt_null
				 || b.type == en_nt_null )
					break;

				r.min = RANGE_MIN(a.value.num, b.value.num);
				r.max = RANGE_MAX(a.value.num, b.value.num);
				break;

			case en_nt_in:
				EMPTY_RANGE(r);

				for(tpnd = pnd->right; tpnd; tpnd=tpnd->right)
				{
					getleaf(yystmt, tpnd, &a);

					if( a.type == en_nt_null )
						continue;

					if( IS_EMPTY_RANGE(r) )
						r.min = r.max = a.value.num;
					else
					{
						r.min = RANGE_MIN(r.min, a.value.num);
						r.max = RANGE_MAX(r.max, a.value.num);
					}
				}
				break;

			case en_nt_cmpop:
				getleaf(yystmt, pnd->right, &a);

				if( a.type == en_nt_null )
					break;

				switch((pnd->value).cmpop)
				{
					case en_cmpop_eq:
						r.min = r.max = a.value.num;
						break;

					case en_cmpop_ne:
						r.min = (a.value.num + 1UL )%AREA;
						r.max = (a.value.num - 1UL + AREA)%AREA;
						break;

					case en_cmpop_gt:
						r.min = (a.value.num + 1UL)%AREA;
						break;

					case en_cmpop_lt:
						r.max = (a.value.num - 1UL + AREA)%AREA;
						r.min = (r.max)? 1UL:0UL;
						break;

					case en_cmpop_ge:
						r.min = a.value.num;
						break;

					case en_cmpop_le:
						r.max = a.value.num;
						r.min = (r.max)? 1UL:0UL;
						break;

					default:
						EMPTY_RANGE(r);
						break;
				}
				break;

			default:
				break;
		}

		return r;
	}

	if( pnd->type != en_nt_logop )
		return r;

	switch( (pnd->value).logop )
	{
		case en_logop_not:
			r1 = getrange(yystmt, pnd->right);
			if( r1.flag )
			{
				r.min = (r1.max + 1UL)%AREA;
				r.max = (r1.min - 1UL + AREA)%AREA;
			}
			break;

		case en_logop_or:
			flag = 1;
		case en_logop_and:
			r1 = getrange(yystmt, pnd->left);
			r2 = getrange(yystmt, pnd->right);

			if( !(r1.flag || r2.flag ) )
				break;

			if( r1.min > r1.max )
				r1.max = r1.max + AREA;

			if( r2.min > r2.max )
				r2.max = r2.max + AREA;

			if( flag )
				r = range_or ( r1, r2 );
			else
				r = range_and( r1, r2 );
			break;

		default:
			EMPTY_RANGE(r);
			break;
	}

	return r;
}

void	nnsql_getrange(void* hstmt, long* pmin, long* pmax)
{
	yystmt_t*	yystmt = hstmt;
	range_t 	r;

	r = getrange(hstmt, yystmt->srchtree);

	if( !r.flag )
	{
		*pmin = 1UL;
		*pmax = MAX_SIGNED_LONG;
	}
	else
	{
		*pmin = r.min;
		*pmax = r.max;
	}
}

static int	is_sql_null(yystmt_t* yystmt, node_t* a)
{
	leaf_t	lf;

	if( getleaf(yystmt, a, &lf) )
		return -1;

	return (lf.type == en_nt_null);
}

static int	compare(yystmt_t* yystmt, node_t* a, node_t* b, int op)
{
	leaf_t	la, lb;
	int	diff, r;

	if( getleaf( yystmt, a, &la )
	 || getleaf( yystmt, b, &lb ) )
		return -1;

	if( la.type == en_nt_date && lb.type == en_nt_qstr )
	{
		lb.type = en_nt_date;

		if( nnsql_odbcdatestr2date(lb.value.qstr, &(lb.value.date)) )
			return -1;
	}

	if( la.type != lb.type
	 || la.type == en_nt_null
	 || lb.type == en_nt_null )
		return 0;

	switch( la.type )
	{
		case en_nt_qstr:
			diff = STRCMP(la.value.qstr, lb.value.qstr);
			break;

		case en_nt_num:
			diff = la.value.num - lb.value.num;
			break;

		case en_nt_date:
			diff = nnsql_datecmp(&(la.value.date), &(lb.value.date));
			break;

		default:
			abort();
			return -1;
	}

	switch(op)
	{
		case en_cmpop_eq:
			r = !diff;
			break;

		case en_cmpop_ne:
			r = !!diff;
			break;

		case en_cmpop_gt:
			r = (diff>0)?  1:0;
			break;

		case en_cmpop_ge:
			r = (diff>=0)? 1:0;
			break;

		case en_cmpop_lt:
			r = (diff<0)?  1:0;
			break;

		case en_cmpop_le:
			r = (diff<=0)? 1:0;
			break;

		default:
			r = -1;
			break;
	}

	return r;
}

static	int	ch_case_cmp(char a, char b)
{
	if( a >= 'a' && a <= 'z' )
		a += ('A' - 'a');

	if( b >= 'a' && b <= 'z' )
		b += ('A' - 'a');

	return (a - b);
}

static int strlike(
		char* str,
		char* pattern,
		char esc,
		int flag )	/* flag = 0: case sensitive */
{
	char	c, cp;

	for(;;str++, pattern++)
	{
		c = *str;
		cp= *pattern;

		if( esc && cp == esc )
		{
			cp = *pattern++;

			if( (!flag && (c - cp) )
			 || ( flag && ch_case_cmp(c, cp) ) )
				return 0;

			if( !c )
				return 1;

			continue;
		}

		switch(cp)
		{
			case 0:
				return !c;

			case '_':
				if(!c)
					return 0;
				break;

			case '%':
				if(! *(pattern + 1) )
					return 1;

				for(;*str;str++)
				{
					if(strlike(str,
						pattern + 1, esc, flag))
						return 1;
				}
				return 0;

			default:
				if( (!flag && (c - cp) )
				 || ( flag && ch_case_cmp(c, cp) ) )
					return 0;
				break;
		}
	}
}

int	nnsql_strlike(char* str, char* pattern, char esc, int flag)
{
	return strlike(str, pattern, esc, flag);
}

static	int	evl_like(yystmt_t* yystmt, node_t* a, node_t* b, char esc, int flag)
{
	leaf_t	la, lb;

	if( getleaf(yystmt, a, &la )
	 || getleaf(yystmt, b, &lb ) )
		return -1;

	if( la.type != en_nt_qstr
	 || lb.type != en_nt_qstr )
		return 0;

	return strlike(la.value.qstr, lb.value.qstr, esc, flag);
}

static int srchtree_evl(yystmt_t* yystmt, node_t* node)
/* return -1: syserr, 0: fail, 1: true */
{
	int	r, s, flag = 0;
	node_t* ptr;

	if( ! node )
		return 1;

	switch( node->type )
	{
		case en_nt_cmpop:
			return compare(yystmt, node->left, node->right,
					(node->value).cmpop);

		case en_nt_logop:
			switch( (node->value).logop )
			{
				case en_logop_not:
					r = srchtree_evl(yystmt, node->right);
					if( r == -1 )
						return -1;
					return ( ! r );

				case en_logop_and:
					flag = 1;
				case en_logop_or:
					r = srchtree_evl(yystmt, node->left);
					s = srchtree_evl(yystmt, node->right);

					if( r == -1 || s == -1 )
						return -1;

					if( flag )
						return ( r && s );
					else
						return ( r || s );

				default:
					abort();
					break;	/* just  for turn off the warning */
			}
			break;	/* just for turn off the warning */

		case en_nt_isnull:
			return is_sql_null(yystmt, node->left);

		case en_nt_between:
			r = compare(yystmt, node->left, node->right->left,  en_cmpop_ge);
			s = compare(yystmt, node->left, node->right->right, en_cmpop_le);

			if( r == -1 || s == -1 )
				return -1;

			return ( r && s );

		case en_nt_in:
			for(ptr=node->right; ptr; ptr=ptr->right)
			{
				r = compare(yystmt, node->left, ptr, en_cmpop_eq);

				if( r ) /* -1 or 1 */
					return r;
			}
			return 0;

		case en_nt_caselike:
			flag = 1;
		case en_nt_like:
			return evl_like( yystmt, node->left, node->right,
				(node->value).esc, flag );

		default:
			abort();
			break;	/* turn off the warning */
	}

	return -1;	/* just for turn off the warning */
}

int	nnsql_srchtree_evl(void* hstmt)
{
	yystmt_t*	yystmt = hstmt;

	return srchtree_evl(yystmt, yystmt->srchtree);
}