File: yytchk.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 (195 lines) | stat: -rw-r--r-- 3,689 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
/**
    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>
#include	<yyerr.h>

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

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

			switch((nd->value).iattr)
			{
				case en_lines:
				case en_article_num:
				case en_sql_num:
				case en_sql_count:
					return en_nt_num;

				case en_date:
				case en_sql_date:
					return en_nt_date;

				default:
					break;
			}
			return en_nt_qstr;

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

			switch(par->type)
			{
				case en_nt_null:
				case en_nt_num:
				case en_nt_qstr:
				case en_nt_date:
					break;

				default:
					return -1;
			}
			return par->type;

		case en_nt_num:
		case en_nt_qstr:
		case en_nt_date:
		case en_nt_null:
			return nd->type;

		default:
			return -1;
	}
}

static int	cmp_tchk(yystmt_t* yystmt, node_t* a, node_t* b)
{
	int	ta, tb;

	ta = getleaftype( yystmt, a );
	tb = getleaftype( yystmt, b );

	if( ta == -1 || tb == -1 )
		return -1;

	if( ta == en_nt_date && tb == en_nt_qstr )
		return 0;

	if( ta != tb && ta != en_nt_null && tb != en_nt_null )
		return -1;

	return 0;
}

static	int	evl_like_tchk(yystmt_t* yystmt, node_t* a, node_t* b)
{
	int	ta, tb;

	ta = getleaftype(yystmt, a);
	tb = getleaftype(yystmt, b);

	if( tb == en_nt_null
	 || tb == en_nt_null )
		return 0;

	if( ta != en_nt_qstr
	 || tb != en_nt_qstr )
		return -1;

	return 0;
}

static int srchtree_tchk(yystmt_t* yystmt, node_t* node)
/* return -1: err or syserr, 0: ok */
{
	int	r, s, flag = 0;
	node_t* ptr;

	if( ! node )
		return 0;

	switch( node->type )
	{
		case en_nt_cmpop:
			return cmp_tchk(yystmt, node->left, node->right);

		case en_nt_logop:
			switch( (node->value).logop )
			{
				case en_logop_not:
					return srchtree_tchk(yystmt, node->right);

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

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

					return 0;

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

		case en_nt_isnull:
			return 0;

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

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

			return 0;

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

				if( r == -1 )
					return -1;
			}
			return 0;

		case en_nt_caselike:
		case en_nt_like:
			return evl_like_tchk( yystmt, node->left, node->right);

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

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

int	nnsql_srchtree_tchk(void* hstmt)
{
	yystmt_t*	yystmt = hstmt;
	int		r;

	r = srchtree_tchk(yystmt, yystmt->srchtree);

	if( r )
		yystmt->errcode = TYPE_VIOLATION;

	return r;
}