query planner stuff

some gcc -Wall fixes
added suport for _ in identifier names in SQL
gmdb2 manual additions
This commit is contained in:
brianb
2003-01-20 16:04:24 +00:00
parent bafa51bd88
commit 092e2b79af
34 changed files with 1143 additions and 382 deletions

View File

@@ -1,7 +1,7 @@
/* A lexical scanner generated by flex */
/* Scanner skeleton version:
* $Header: /Users/brian/cvs/mdbtools/mdbtools/src/sql/Attic/lexer.c,v 1.5 2002/12/27 15:09:02 brianb Exp $
* $Header: /Users/brian/cvs/mdbtools/mdbtools/src/sql/Attic/lexer.c,v 1.6 2003/01/20 16:04:31 brianb Exp $
*/
#define FLEX_SCANNER
@@ -282,19 +282,21 @@ static void yy_fatal_error YY_PROTO(( yyconst char msg[] ));
*yy_cp = '\0'; \
yy_c_buf_p = yy_cp;
#define YY_NUM_RULES 22
#define YY_END_OF_BUFFER 23
static yyconst short int yy_accept[89] =
#define YY_NUM_RULES 24
#define YY_END_OF_BUFFER 25
static yyconst short int yy_accept[95] =
{ 0,
19, 19, 23, 21, 15, 22, 21, 21, 20, 21,
19, 21, 21, 17, 17, 17, 17, 17, 17, 17,
17, 17, 21, 0, 0, 18, 0, 20, 0, 20,
20, 19, 12, 13, 17, 17, 17, 17, 17, 17,
17, 17, 17, 5, 17, 0, 16, 0, 19, 19,
8, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 2, 14, 6, 17, 17, 17, 17,
17, 17, 17, 10, 7, 17, 17, 17, 1, 9,
3, 17, 17, 11, 17, 17, 4, 0
21, 21, 25, 23, 17, 24, 23, 23, 22, 23,
21, 23, 23, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 23, 0, 0, 20, 0, 22,
0, 22, 22, 21, 14, 15, 19, 19, 19, 19,
19, 19, 19, 19, 19, 12, 19, 19, 5, 19,
0, 18, 0, 21, 21, 11, 19, 19, 19, 19,
19, 19, 13, 19, 19, 19, 19, 19, 19, 2,
16, 6, 19, 19, 19, 19, 19, 19, 19, 9,
7, 19, 19, 19, 1, 8, 3, 19, 19, 10,
19, 19, 4, 0
} ;
static yyconst int yy_ec[256] =
@@ -305,14 +307,14 @@ static yyconst int yy_ec[256] =
1, 4, 1, 5, 1, 1, 1, 1, 6, 7,
7, 1, 8, 1, 9, 10, 11, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 1, 1, 13,
14, 15, 1, 1, 16, 16, 16, 16, 17, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
17, 16, 16, 16, 16, 16, 18, 19, 20, 21,
14, 15, 1, 1, 17, 18, 19, 20, 21, 22,
23, 24, 25, 23, 26, 27, 28, 29, 30, 23,
23, 31, 32, 33, 23, 23, 34, 23, 23, 23,
7, 1, 1, 1, 16, 1, 17, 18, 19, 20,
22, 23, 16, 24, 25, 16, 26, 27, 28, 29,
30, 16, 16, 31, 32, 33, 16, 16, 34, 16,
16, 16, 1, 1, 1, 35, 1, 1, 1, 1,
21, 22, 23, 24, 25, 23, 26, 27, 28, 29,
30, 23, 23, 31, 32, 33, 23, 23, 34, 23,
23, 23, 1, 1, 1, 35, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
@@ -332,119 +334,117 @@ static yyconst int yy_ec[256] =
static yyconst int yy_meta[36] =
{ 0,
1, 1, 2, 3, 3, 1, 4, 4, 1, 5,
5, 6, 1, 1, 1, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 1
5, 6, 1, 1, 1, 7, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 1
} ;
static yyconst short int yy_base[94] =
static yyconst short int yy_base[101] =
{ 0,
0, 0, 129, 295, 295, 295, 0, 116, 29, 31,
37, 104, 84, 42, 44, 46, 49, 51, 53, 55,
57, 59, 69, 85, 80, 74, 76, 79, 82, 87,
93, 0, 295, 295, 96, 98, 101, 103, 106, 110,
113, 115, 117, 121, 133, 67, 295, 46, 38, 92,
123, 136, 138, 140, 142, 146, 151, 153, 156, 161,
163, 166, 168, 170, 176, 178, 180, 183, 185, 191,
193, 198, 202, 204, 206, 209, 211, 214, 218, 221,
223, 227, 229, 234, 236, 242, 244, 295, 36, 275,
279, 282, 287
0, 0, 158, 285, 285, 285, 0, 149, 29, 31,
36, 132, 115, 41, 43, 45, 52, 51, 24, 39,
53, 46, 62, 64, 81, 113, 90, 75, 86, 87,
90, 95, 101, 0, 285, 285, 65, 0, 94, 91,
85, 100, 103, 105, 102, 113, 115, 123, 118, 124,
75, 285, 54, 53, 140, 126, 132, 135, 143, 138,
144, 146, 149, 151, 156, 157, 159, 164, 166, 165,
172, 173, 180, 179, 181, 188, 187, 189, 190, 195,
196, 201, 207, 208, 210, 215, 217, 218, 220, 223,
228, 225, 230, 285, 50, 258, 263, 267, 273, 276
} ;
static yyconst short int yy_def[94] =
static yyconst short int yy_def[101] =
{ 0,
88, 1, 88, 88, 88, 88, 89, 90, 91, 91,
91, 88, 88, 92, 92, 92, 92, 92, 92, 92,
92, 92, 11, 93, 90, 90, 88, 11, 11, 11,
11, 11, 88, 88, 92, 92, 92, 92, 92, 92,
92, 92, 92, 92, 92, 93, 88, 88, 88, 11,
92, 92, 92, 92, 92, 92, 92, 92, 92, 92,
92, 92, 92, 92, 92, 92, 92, 92, 92, 92,
92, 92, 92, 92, 92, 92, 92, 92, 92, 92,
92, 92, 92, 92, 92, 92, 92, 0, 88, 88,
88, 88, 88
94, 1, 94, 94, 94, 94, 95, 96, 97, 97,
97, 94, 94, 98, 98, 98, 98, 17, 17, 17,
17, 17, 17, 17, 11, 99, 96, 96, 94, 11,
11, 11, 11, 11, 94, 94, 17, 100, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
99, 94, 94, 94, 11, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 0, 94, 94, 94, 94, 94, 94
} ;
static yyconst short int yy_nxt[331] =
static yyconst short int yy_nxt[321] =
{ 0,
4, 5, 6, 5, 7, 8, 4, 4, 4, 9,
10, 11, 12, 4, 13, 14, 14, 15, 14, 16,
17, 14, 18, 14, 14, 14, 19, 14, 14, 14,
14, 20, 21, 22, 23, 27, 27, 88, 88, 29,
30, 88, 24, 88, 88, 30, 31, 29, 32, 49,
30, 28, 29, 28, 29, 28, 29, 49, 28, 29,
28, 29, 28, 29, 28, 29, 28, 29, 28, 29,
38, 47, 36, 39, 43, 37, 42, 41, 28, 26,
28, 40, 45, 48, 48, 26, 44, 49, 28, 47,
28, 28, 88, 28, 48, 48, 28, 34, 50, 27,
10, 11, 12, 4, 13, 4, 14, 15, 16, 17,
15, 18, 15, 15, 15, 15, 19, 15, 20, 21,
15, 22, 23, 24, 25, 29, 29, 94, 94, 31,
32, 94, 94, 94, 37, 33, 31, 34, 44, 32,
30, 31, 30, 31, 30, 31, 38, 26, 38, 37,
38, 30, 31, 37, 54, 54, 47, 38, 45, 39,
37, 37, 41, 37, 40, 37, 42, 37, 48, 52,
28, 43, 37, 46, 37, 37, 37, 50, 37, 37,
30, 49, 30, 53, 53, 28, 30, 54, 30, 30,
27, 28, 28, 50, 30, 28, 29, 28, 29, 30,
28, 29, 28, 29, 30, 28, 29, 33, 51, 28,
29, 26, 28, 29, 28, 29, 28, 29, 88, 52,
28, 29, 28, 29, 53, 59, 88, 54, 56, 55,
88, 58, 28, 29, 57, 28, 29, 28, 29, 28,
29, 28, 29, 88, 60, 28, 29, 62, 88, 63,
28, 29, 28, 29, 61, 28, 29, 65, 88, 64,
28, 29, 28, 29, 67, 28, 29, 28, 29, 28,
29, 88, 68, 66, 70, 28, 29, 28, 29, 28,
29, 69, 28, 29, 28, 29, 71, 72, 88, 73,
94, 30, 53, 53, 30, 37, 55, 29, 29, 37,
30, 37, 32, 56, 37, 37, 58, 52, 37, 57,
37, 32, 37, 37, 37, 37, 37, 37, 36, 37,
61, 59, 60, 37, 63, 37, 62, 37, 37, 37,
65, 64, 37, 37, 66, 35, 37, 37, 37, 30,
37, 55, 37, 68, 28, 37, 37, 94, 37, 37,
67, 69, 37, 37, 71, 70, 37, 37, 37, 37,
37, 73, 94, 37, 94, 37, 37, 37, 72, 76,
37, 37, 74, 37, 37, 37, 37, 75, 37, 37,
37, 94, 37, 37, 77, 78, 37, 37, 79, 80,
28, 29, 28, 29, 74, 88, 75, 28, 29, 88,
76, 28, 29, 28, 29, 28, 29, 77, 28, 29,
28, 29, 88, 28, 29, 88, 78, 28, 29, 82,
28, 29, 28, 29, 79, 80, 28, 29, 28, 29,
88, 81, 83, 28, 29, 28, 29, 88, 84, 88,
85, 28, 29, 28, 29, 86, 88, 88, 88, 88,
88, 88, 88, 88, 88, 88, 88, 88, 88, 88,
88, 88, 88, 88, 87, 25, 88, 25, 25, 25,
25, 25, 28, 28, 28, 28, 35, 35, 35, 46,
88, 88, 46, 46, 3, 88, 88, 88, 88, 88,
37, 81, 94, 37, 37, 37, 82, 37, 37, 37,
37, 83, 37, 37, 37, 37, 37, 84, 94, 37,
37, 37, 85, 94, 88, 37, 86, 37, 37, 94,
37, 37, 37, 87, 37, 37, 89, 37, 90, 37,
91, 37, 37, 37, 37, 37, 92, 37, 37, 37,
37, 94, 37, 94, 37, 94, 94, 93, 27, 94,
27, 27, 27, 27, 27, 27, 30, 30, 30, 94,
30, 37, 37, 37, 37, 51, 94, 94, 51, 51,
51, 38, 38, 38, 3, 94, 94, 94, 94, 94,
94, 94, 94, 94, 94, 94, 94, 94, 94, 94,
88, 88, 88, 88, 88, 88, 88, 88, 88, 88,
88, 88, 88, 88, 88, 88, 88, 88, 88, 88,
88, 88, 88, 88, 88, 88, 88, 88, 88, 88
94, 94, 94, 94, 94, 94, 94, 94, 94, 94,
94, 94, 94, 94, 94, 94, 94, 94, 94, 94
} ;
static yyconst short int yy_chk[331] =
static yyconst short int yy_chk[321] =
{ 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 9, 9, 10, 10, 9,
9, 10, 89, 11, 11, 9, 11, 11, 11, 49,
9, 14, 14, 15, 15, 16, 16, 48, 17, 17,
18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
17, 46, 15, 17, 21, 16, 20, 19, 23, 26,
23, 18, 22, 27, 27, 25, 21, 27, 28, 24,
28, 29, 29, 29, 30, 30, 30, 13, 30, 31,
9, 10, 11, 11, 19, 11, 11, 11, 19, 9,
14, 14, 15, 15, 16, 16, 14, 95, 15, 20,
16, 17, 17, 20, 54, 53, 22, 17, 20, 14,
22, 18, 17, 21, 16, 18, 17, 21, 23, 51,
28, 18, 23, 21, 24, 37, 23, 24, 24, 37,
25, 23, 25, 29, 29, 27, 30, 29, 30, 31,
31, 50, 31, 50, 31, 35, 35, 36, 36, 31,
37, 37, 38, 38, 31, 39, 39, 12, 36, 40,
40, 8, 41, 41, 42, 42, 43, 43, 3, 37,
44, 44, 51, 51, 38, 43, 0, 39, 41, 40,
0, 42, 45, 45, 41, 52, 52, 53, 53, 54,
54, 55, 55, 0, 45, 56, 56, 53, 0, 54,
57, 57, 58, 58, 52, 59, 59, 56, 0, 55,
60, 60, 61, 61, 58, 62, 62, 63, 63, 64,
64, 0, 59, 57, 61, 65, 65, 66, 66, 67,
67, 60, 68, 68, 69, 69, 62, 63, 0, 67,
31, 31, 32, 32, 32, 41, 32, 33, 33, 41,
33, 40, 33, 39, 39, 40, 41, 26, 39, 40,
42, 33, 45, 43, 42, 44, 45, 43, 13, 44,
44, 42, 43, 46, 45, 47, 44, 46, 49, 47,
48, 47, 49, 48, 50, 12, 56, 48, 50, 55,
56, 55, 57, 58, 8, 58, 57, 3, 60, 58,
57, 59, 60, 59, 61, 60, 62, 59, 61, 63,
62, 64, 0, 63, 0, 64, 65, 66, 62, 67,
65, 66, 65, 67, 68, 70, 69, 66, 68, 70,
69, 0, 71, 72, 68, 69, 71, 72, 73, 74,
70, 70, 71, 71, 68, 0, 69, 72, 72, 0,
70, 73, 73, 74, 74, 75, 75, 71, 76, 76,
77, 77, 0, 78, 78, 0, 72, 79, 79, 77,
80, 80, 81, 81, 73, 74, 82, 82, 83, 83,
0, 76, 78, 84, 84, 85, 85, 0, 82, 0,
83, 86, 86, 87, 87, 85, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 86, 90, 0, 90, 90, 90,
90, 90, 91, 91, 91, 91, 92, 92, 92, 93,
0, 0, 93, 93, 88, 88, 88, 88, 88, 88,
73, 75, 0, 74, 73, 75, 76, 77, 76, 78,
79, 77, 76, 78, 79, 80, 81, 78, 0, 80,
81, 82, 79, 0, 83, 82, 80, 83, 84, 0,
85, 83, 84, 82, 85, 86, 84, 87, 88, 86,
89, 87, 88, 90, 89, 92, 91, 90, 91, 92,
93, 0, 91, 0, 93, 0, 0, 92, 96, 0,
96, 96, 96, 96, 96, 96, 97, 97, 97, 0,
97, 98, 98, 98, 98, 99, 0, 0, 99, 99,
99, 100, 100, 100, 94, 94, 94, 94, 94, 94,
94, 94, 94, 94, 94, 94, 94, 94, 94, 94,
88, 88, 88, 88, 88, 88, 88, 88, 88, 88,
88, 88, 88, 88, 88, 88, 88, 88, 88, 88,
88, 88, 88, 88, 88, 88, 88, 88, 88, 88
94, 94, 94, 94, 94, 94, 94, 94, 94, 94,
94, 94, 94, 94, 94, 94, 94, 94, 94, 94
} ;
static yy_state_type yy_last_accepting_state;
@@ -687,13 +687,13 @@ yy_match:
while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
{
yy_current_state = (int) yy_def[yy_current_state];
if ( yy_current_state >= 89 )
if ( yy_current_state >= 95 )
yy_c = yy_meta[(unsigned int) yy_c];
}
yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
++yy_cp;
}
while ( yy_base[yy_current_state] != 295 );
while ( yy_base[yy_current_state] != 285 );
yy_find_action:
yy_act = yy_accept[yy_current_state];
@@ -757,85 +757,95 @@ YY_RULE_SETUP
case 8:
YY_RULE_SETUP
#line 34 "lexer.l"
{ return AND; }
{ return TABLES; }
YY_BREAK
case 9:
YY_RULE_SETUP
#line 35 "lexer.l"
{ return TABLES; }
{ return TABLE; }
YY_BREAK
case 10:
YY_RULE_SETUP
#line 36 "lexer.l"
{ return TABLE; }
{ return DESCRIBE; }
YY_BREAK
case 11:
YY_RULE_SETUP
#line 37 "lexer.l"
{ return DESCRIBE; }
{ return AND; }
YY_BREAK
case 12:
YY_RULE_SETUP
#line 38 "lexer.l"
{ return LTEQ; }
{ return OR; }
YY_BREAK
case 13:
YY_RULE_SETUP
#line 39 "lexer.l"
{ return GTEQ; }
{ return NOT; }
YY_BREAK
case 14:
YY_RULE_SETUP
#line 40 "lexer.l"
{ return LIKE; }
{ return LTEQ; }
YY_BREAK
case 15:
YY_RULE_SETUP
#line 41 "lexer.l"
;
{ return GTEQ; }
YY_BREAK
case 16:
YY_RULE_SETUP
#line 42 "lexer.l"
{ return LIKE; }
YY_BREAK
case 17:
YY_RULE_SETUP
#line 43 "lexer.l"
;
YY_BREAK
case 18:
YY_RULE_SETUP
#line 44 "lexer.l"
{
yylval.name = strdup(&yytext[1]);
yylval.name[strlen(yylval.name)-1]='\0';
return IDENT;
}
YY_BREAK
case 17:
YY_RULE_SETUP
#line 47 "lexer.l"
{ yylval.name = strdup(yytext); return NAME; }
YY_BREAK
case 18:
YY_RULE_SETUP
#line 49 "lexer.l"
{ yylval.name = strdup(yytext); return STRING; }
YY_BREAK
case 19:
YY_RULE_SETUP
#line 50 "lexer.l"
#line 49 "lexer.l"
{ yylval.name = strdup(yytext); return NAME; }
YY_BREAK
case 20:
YY_RULE_SETUP
#line 51 "lexer.l"
{ yylval.name = strdup(yytext); return STRING; }
YY_BREAK
case 21:
YY_RULE_SETUP
#line 52 "lexer.l"
{
yylval.name = strdup(yytext); return NUMBER;
}
YY_BREAK
case 20:
YY_RULE_SETUP
#line 53 "lexer.l"
{ yylval.name = strdup(yytext); return PATH; }
YY_BREAK
case 21:
YY_RULE_SETUP
#line 54 "lexer.l"
{ return yytext[0]; }
YY_BREAK
case 22:
YY_RULE_SETUP
#line 55 "lexer.l"
{ yylval.name = strdup(yytext); return PATH; }
YY_BREAK
case 23:
YY_RULE_SETUP
#line 56 "lexer.l"
{ return yytext[0]; }
YY_BREAK
case 24:
YY_RULE_SETUP
#line 57 "lexer.l"
ECHO;
YY_BREAK
#line 839 "lexer.c"
#line 849 "lexer.c"
case YY_STATE_EOF(INITIAL):
yyterminate();
@@ -1127,7 +1137,7 @@ static yy_state_type yy_get_previous_state()
while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
{
yy_current_state = (int) yy_def[yy_current_state];
if ( yy_current_state >= 89 )
if ( yy_current_state >= 95 )
yy_c = yy_meta[(unsigned int) yy_c];
}
yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
@@ -1162,11 +1172,11 @@ yy_state_type yy_current_state;
while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
{
yy_current_state = (int) yy_def[yy_current_state];
if ( yy_current_state >= 89 )
if ( yy_current_state >= 95 )
yy_c = yy_meta[(unsigned int) yy_c];
}
yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
yy_is_jam = (yy_current_state == 88);
yy_is_jam = (yy_current_state == 94);
return yy_is_jam ? 0 : yy_current_state;
}
@@ -1716,7 +1726,7 @@ int main()
return 0;
}
#endif
#line 55 "lexer.l"
#line 57 "lexer.l"
int yywrap()

View File

@@ -31,20 +31,22 @@ disconnect { return DISCONNECT; }
to { return TO; }
list { return LIST; }
where { return WHERE; }
and { return AND; }
tables { return TABLES; }
table { return TABLE; }
describe { return DESCRIBE; }
and { return AND; }
or { return OR; }
not { return NOT; }
(<=) { return LTEQ; }
(>=) { return GTEQ; }
like { return LIKE; }
[ \t\r] ;
\"[A-z][A-z0-9 ]*\" {
\"[A-z][A-z0-9 _]*\" {
yylval.name = strdup(&yytext[1]);
yylval.name[strlen(yylval.name)-1]='\0';
return IDENT;
}
[A-z][A-z0-9]* { yylval.name = strdup(yytext); return NAME; }
[A-z][A-z0-9_]* { yylval.name = strdup(yytext); return NAME; }
'.*' { yylval.name = strdup(yytext); return STRING; }
([0-9]+|([0-9]*\.[0-9+)([eE][-+]?[0-9]+)?) {

View File

@@ -27,6 +27,7 @@ void mdb_dump_results(MdbSQL *sql);
#include <wordexp.h>
#endif
void
mdb_sql_error(char *fmt, ...)
{
va_list ap;
@@ -58,6 +59,8 @@ MdbSQL *sql;
sql->columns = g_ptr_array_new();
sql->tables = g_ptr_array_new();
sql->sargs = g_ptr_array_new();
sql->sarg_tree = NULL;
sql->sarg_stack = NULL;
return sql;
}
@@ -88,7 +91,8 @@ MdbSQLTable *t;
return t;
}
MdbHandle *mdb_sql_close(MdbSQL *sql)
void
mdb_sql_close(MdbSQL *sql)
{
if (sql->mdb) {
mdb_close(sql->mdb);
@@ -137,14 +141,162 @@ wordexp_t words;
return sql->mdb;
}
int mdb_sql_add_sarg(MdbSQL *sql, char *col_name, int op, char *constant)
MdbSargNode *
mdb_sql_alloc_node()
{
MdbSQLSarg *sql_sarg;
int lastchar;
MdbSargNode *node;
node = g_malloc0(sizeof(MdbSargNode));
return node;
}
void
mdb_sql_free_tree(MdbSargNode *tree)
{
if (tree->left) mdb_sql_free_tree(tree->left);
if (tree->right) mdb_sql_free_tree(tree->right);
g_free(tree);
}
void
mdb_sql_push_node(MdbSQL *sql, MdbSargNode *node)
{
sql->sarg_stack = g_list_append(sql->sarg_stack, node);
/*
* Tree builds from bottom to top, so we should be left with
* the correct tree root when done
*/
sql->sarg_tree = node;
}
MdbSargNode *
mdb_sql_pop_node(MdbSQL *sql)
{
GList *glist;
MdbSargNode *node;
glist = g_list_last(sql->sarg_stack);
if (!glist) return NULL;
node = glist->data;
#if 0
if (node->op==MDB_EQUAL)
printf("popping %d\n", node->value.i);
else
printf("popping %s\n", node->op == MDB_OR ? "OR" : "AND");
#endif
sql->sarg_stack = g_list_remove(sql->sarg_stack, node);
return node;
}
void
mdb_sql_add_not(MdbSQL *sql)
{
MdbSargNode *node, *left;
left = mdb_sql_pop_node(sql);
if (!left) {
mdb_sql_error("parse error near 'NOT'");
mdb_sql_reset(sql);
return;
}
node = mdb_sql_alloc_node();
node->op = MDB_NOT;
node->left = left;
mdb_sql_push_node(sql, node);
}
void
mdb_sql_add_or(MdbSQL *sql)
{
MdbSargNode *node, *left, *right;
left = mdb_sql_pop_node(sql);
right = mdb_sql_pop_node(sql);
if (!left || !right) {
mdb_sql_error("parse error near 'OR'");
mdb_sql_reset(sql);
return;
}
node = mdb_sql_alloc_node();
node->op = MDB_OR;
node->left = left;
node->right = right;
mdb_sql_push_node(sql, node);
}
void
mdb_sql_add_and(MdbSQL *sql)
{
MdbSargNode *node, *left, *right;
left = mdb_sql_pop_node(sql);
right = mdb_sql_pop_node(sql);
if (!left || !right) {
mdb_sql_error("parse error near 'AND'");
mdb_sql_reset(sql);
return;
}
node = mdb_sql_alloc_node();
node->op = MDB_AND;
node->left = left;
node->right = right;
mdb_sql_push_node(sql, node);
}
void
mdb_sql_dump_node(MdbSargNode *node, int level)
{
int i;
int mylevel = level+1;
if (!level)
printf("root ");
for (i=0;i<mylevel;i++) printf("--->");
switch (node->op) {
case MDB_OR:
printf(" or\n");
break;
case MDB_AND:
printf(" and\n");
break;
case MDB_NOT:
printf(" not\n");
break;
case MDB_LT:
printf(" < %d\n", node->value.i);
break;
case MDB_GT:
printf(" < %d\n", node->value.i);
break;
case MDB_LIKE:
printf(" like %s\n", node->value.s);
break;
case MDB_EQUAL:
printf(" = %d\n", node->value.i);
break;
}
if (node->left) {
printf("left ");
mdb_sql_dump_node(node->left, mylevel);
}
if (node->right) {
printf("right ");
mdb_sql_dump_node(node->right, mylevel);
}
}
int
mdb_sql_add_sarg(MdbSQL *sql, char *col_name, int op, char *constant)
{
MdbSQLSarg *sql_sarg;
int lastchar;
MdbSargNode *node;
node = mdb_sql_alloc_node();
node->op = op;
/* stash the column name until we finish with the grammar */
node->parent = (void *) g_strdup(col_name);
sql_sarg = mdb_sql_alloc_sarg();
sql_sarg->col_name = g_strdup(col_name);
sql_sarg->sarg->op = op;
/* FIX ME -- we should probably just be storing the ascii value until the
** column definition can be checked for validity
*/
@@ -152,19 +304,27 @@ int lastchar;
lastchar = strlen(constant) > 256 ? 256 : strlen(constant);
strncpy(sql_sarg->sarg->value.s, &constant[1], lastchar - 2);
sql_sarg->sarg->value.s[lastchar - 1]='\0';
strncpy(node->value.s, &constant[1], lastchar - 2);;
node->value.s[lastchar - 1]='\0';
} else {
sql_sarg->sarg->value.i = atoi(constant);
node->value.i = atoi(constant);
}
g_ptr_array_add(sql->sargs, sql_sarg);
sql->num_sargs++;
mdb_sql_push_node(sql, node);
return 0;
}
int mdb_sql_all_columns(MdbSQL *sql)
void
mdb_sql_all_columns(MdbSQL *sql)
{
sql->all_columns=1;
}
int mdb_sql_add_column(MdbSQL *sql, char *column_name)
{
MdbSQLColumn *c, *cp;
MdbSQLColumn *c;
c = mdb_sql_alloc_column();
c->name = g_strdup(column_name);
@@ -218,6 +378,12 @@ MdbSQLSarg *sql_sarg;
if (sql_sarg->col_name) g_free(sql_sarg->col_name);
if (sql_sarg->sarg) g_free(sql_sarg->sarg);
}
if (sql->sarg_tree) {
mdb_sql_free_tree(sql->sarg_tree);
sql->sarg_tree = NULL;
}
g_list_free(sql->sarg_stack);
sql->sarg_stack = NULL;
g_ptr_array_free(sql->columns,TRUE);
g_ptr_array_free(sql->tables,TRUE);
g_ptr_array_free(sql->sargs,TRUE);
@@ -230,6 +396,7 @@ MdbSQLTable *t;
MdbSQLSarg *sql_sarg;
if (sql->cur_table) {
mdb_index_scan_free(sql->cur_table);
mdb_free_tabledef(sql->cur_table);
sql->cur_table = NULL;
}
@@ -246,6 +413,12 @@ MdbSQLSarg *sql_sarg;
if (sql_sarg->col_name) g_free(sql_sarg->col_name);
if (sql_sarg->sarg) g_free(sql_sarg->sarg);
}
if (sql->sarg_tree) {
mdb_sql_free_tree(sql);
sql->sarg_tree = NULL;
}
g_list_free(sql->sarg_stack);
sql->sarg_stack = NULL;
g_ptr_array_free(sql->columns,TRUE);
g_ptr_array_free(sql->tables,TRUE);
g_ptr_array_free(sql->sargs,TRUE);
@@ -382,7 +555,26 @@ char colsize[11];
/* the column and table names are no good now */
mdb_sql_reset(sql);
}
void mdb_sql_select(MdbSQL *sql)
int mdb_sql_find_sargcol(MdbSargNode *node, gpointer data)
{
MdbTableDef *table = data;
int i;
MdbColumn *col;
if (!mdb_is_relational_op(node->op)) return 0;
for (i=0;i<table->num_cols;i++) {
col=g_ptr_array_index(table->columns,i);
if (!strcmp(col->name, (char *)node->parent)) {
node->col = col;
break;
}
}
return 0;
}
void
mdb_sql_select(MdbSQL *sql)
{
int i,j;
MdbCatalogEntry *entry;
@@ -391,7 +583,6 @@ MdbTableDef *table = NULL;
MdbSQLTable *sql_tab;
MdbColumn *col;
MdbSQLColumn *sqlcol;
MdbSQLSarg *sql_sarg;
int found = 0;
if (!mdb) {
@@ -418,6 +609,7 @@ int found = 0;
return;
}
mdb_read_columns(table);
mdb_read_indices(table);
mdb_rewind_table(table);
if (sql->all_columns) {
@@ -448,14 +640,29 @@ int found = 0;
}
}
#if 0
/* now add back the sargs */
for (i=0;i<sql->num_sargs;i++) {
sql_sarg=g_ptr_array_index(sql->sargs,i);
mdb_add_sarg_by_name(table,sql_sarg->col_name, sql_sarg->sarg);
//mdb_add_sarg_by_name(table,sql_sarg->col_name, sql_sarg->sarg);
}
#endif
/*
* resolve column names to MdbColumn structs
*/
if (sql->sarg_tree) {
mdb_sql_walk_tree(sql->sarg_tree, mdb_sql_find_sargcol, table);
mdb_sql_walk_tree(sql->sarg_tree, mdb_find_indexable_sargs, NULL);
}
/*
* move the sarg_tree.
* XXX - this won't work when we implement joins
*/
table->sarg_tree = sql->sarg_tree;
sql->sarg_tree = NULL;
sql->cur_table = table;
mdb_index_scan_init(mdb, table);
}
void mdbsql_bind_column(MdbSQL *sql, int colnum, char *varaddr)
@@ -463,7 +670,7 @@ void mdbsql_bind_column(MdbSQL *sql, int colnum, char *varaddr)
MdbTableDef *table = sql->cur_table;
MdbSQLColumn *sqlcol;
MdbColumn *col;
int i, j;
int j;
/* sql columns are traditionally 1 based, so decrement colnum */
sqlcol = g_ptr_array_index(sql->columns,colnum - 1);
@@ -481,7 +688,7 @@ void mdbsql_bind_len(MdbSQL *sql, int colnum, int *len_ptr)
MdbTableDef *table = sql->cur_table;
MdbSQLColumn *sqlcol;
MdbColumn *col;
int i, j;
int j;
/* sql columns are traditionally 1 based, so decrement colnum */
sqlcol = g_ptr_array_index(sql->columns,colnum - 1);

View File

@@ -26,9 +26,8 @@ static MdbSQL *g_sql;
if (sql) {
g_sql = sql;
} else {
return g_sql;
}
return g_sql;
}
%}
@@ -42,7 +41,7 @@ static MdbSQL *g_sql;
%token <name> IDENT NAME PATH STRING NUMBER
%token SELECT FROM WHERE CONNECT DISCONNECT TO LIST TABLES WHERE AND
%token SELECT FROM WHERE CONNECT DISCONNECT TO LIST TABLES WHERE AND OR NOT
%token DESCRIBE TABLE
%token LTEQ GTEQ LIKE
@@ -52,6 +51,11 @@ static MdbSQL *g_sql;
%%
stmt:
query
| error { yyclearin; mdb_sql_reset(_mdb_sql(NULL)); }
;
query:
SELECT column_list FROM table where_clause {
mdb_sql_select(_mdb_sql(NULL));
@@ -76,8 +80,11 @@ where_clause:
;
sarg_list:
sarg
| sarg AND sarg_list
sarg
| '(' sarg_list ')'
| NOT sarg_list { mdb_sql_add_not(_mdb_sql(NULL)); }
| sarg_list OR sarg_list { mdb_sql_add_or(_mdb_sql(NULL)); }
| sarg_list AND sarg_list { mdb_sql_add_and(_mdb_sql(NULL)); }
;
sarg:
@@ -109,6 +116,7 @@ constant:
database:
PATH
| NAME
;
table:
NAME { mdb_sql_add_table(_mdb_sql(NULL), $1); free($1); }