2001-09-29 00:16:16 +00:00
|
|
|
/* MDB Tools - A library for reading MS Access database files
|
2001-05-16 00:21:17 +00:00
|
|
|
* Copyright (C) 2000 Brian Bruns
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library 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
|
|
|
|
|
* Library General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
2011-08-28 19:53:29 -04:00
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2001-05-16 00:21:17 +00:00
|
|
|
*/
|
2020-08-18 12:09:11 +10:00
|
|
|
|
|
|
|
|
%{
|
2001-04-08 01:32:43 +00:00
|
|
|
#include "mdbsql.h"
|
|
|
|
|
|
2020-08-18 12:09:11 +10:00
|
|
|
struct sql_context;
|
|
|
|
|
#include "parser.h"
|
|
|
|
|
|
|
|
|
|
typedef void *yyscan_t;
|
|
|
|
|
typedef struct yy_buffer_state* YY_BUFFER_STATE;
|
|
|
|
|
extern int yylex_init(yyscan_t* scanner);
|
|
|
|
|
extern int yylex_destroy(yyscan_t scanner);
|
|
|
|
|
extern int yylex(YYSTYPE* yylval_param, YYLTYPE* yyloc, yyscan_t yyscanner);
|
|
|
|
|
extern YY_BUFFER_STATE yy_scan_string(const char* buffer, yyscan_t scanner);
|
|
|
|
|
|
|
|
|
|
/** error handler for bison */
|
|
|
|
|
void yyerror(YYLTYPE* yyloc, struct sql_context* sql_ctx, char const* msg);
|
2002-01-24 12:34:10 +00:00
|
|
|
|
2020-08-18 12:09:11 +10:00
|
|
|
typedef struct sql_context
|
2002-01-24 12:34:10 +00:00
|
|
|
{
|
2020-08-18 12:09:11 +10:00
|
|
|
// lexer context
|
|
|
|
|
yyscan_t flex_scanner;
|
2002-01-24 12:34:10 +00:00
|
|
|
|
2020-08-18 12:09:11 +10:00
|
|
|
MdbSQL *mdb;
|
|
|
|
|
} sql_context;
|
|
|
|
|
|
|
|
|
|
#define scanner parser_ctx->flex_scanner
|
2002-01-24 12:34:10 +00:00
|
|
|
|
2020-08-18 12:09:11 +10:00
|
|
|
// we want verbose error messages
|
|
|
|
|
#define YYERROR_VERBOSE 1
|
2001-04-08 01:32:43 +00:00
|
|
|
%}
|
|
|
|
|
|
2020-08-18 12:09:11 +10:00
|
|
|
// make the parser reentrant
|
|
|
|
|
%locations
|
|
|
|
|
%define api.pure
|
|
|
|
|
%lex-param {void * scanner}
|
|
|
|
|
%parse-param {struct sql_context* parser_ctx}
|
|
|
|
|
|
2001-04-08 01:32:43 +00:00
|
|
|
%union {
|
|
|
|
|
char *name;
|
|
|
|
|
double dval;
|
|
|
|
|
int ival;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 12:09:11 +10:00
|
|
|
%start stmt
|
2001-05-16 00:21:17 +00:00
|
|
|
|
2015-08-20 14:35:09 +02:00
|
|
|
%token <name> IDENT NAME PATH STRING NUMBER
|
2017-12-03 17:02:15 +01:00
|
|
|
%token SELECT FROM WHERE CONNECT DISCONNECT TO LIST TABLES AND OR NOT LIMIT COUNT STRPTIME
|
2020-09-26 11:28:34 +01:00
|
|
|
%token DESCRIBE TABLE TOP PERCENT
|
2004-02-07 00:58:29 +00:00
|
|
|
%token LTEQ GTEQ LIKE IS NUL
|
2001-04-08 01:32:43 +00:00
|
|
|
|
2001-05-16 00:21:17 +00:00
|
|
|
%type <name> database
|
|
|
|
|
%type <name> constant
|
|
|
|
|
%type <ival> operator
|
2004-02-06 02:34:20 +00:00
|
|
|
%type <ival> nulloperator
|
2004-02-08 21:54:20 +00:00
|
|
|
%type <name> identifier
|
2001-05-16 00:21:17 +00:00
|
|
|
|
2020-08-23 07:55:41 +10:00
|
|
|
//
|
|
|
|
|
// operator precedence
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
// left associativity means that 1+2+3 translates to (1+2)+3
|
|
|
|
|
// the order of operators here determines their precedence
|
|
|
|
|
|
|
|
|
|
%left OR
|
|
|
|
|
%left AND
|
|
|
|
|
%right NOT
|
|
|
|
|
%left EQ LTEQ GTEQ LT GT LIKE IS
|
|
|
|
|
|
2001-04-08 01:32:43 +00:00
|
|
|
%%
|
|
|
|
|
|
2003-01-20 16:04:24 +00:00
|
|
|
stmt:
|
|
|
|
|
query
|
2020-08-18 12:09:11 +10:00
|
|
|
| error { yyclearin; mdb_sql_reset(parser_ctx->mdb); }
|
2003-01-20 16:04:24 +00:00
|
|
|
;
|
|
|
|
|
|
2001-04-08 01:32:43 +00:00
|
|
|
query:
|
2020-09-26 11:28:34 +01:00
|
|
|
SELECT top_clause column_list FROM table where_clause limit_clause {
|
2020-08-18 12:09:11 +10:00
|
|
|
mdb_sql_select(parser_ctx->mdb);
|
2001-04-08 01:32:43 +00:00
|
|
|
}
|
|
|
|
|
| CONNECT TO database {
|
2020-08-18 12:09:11 +10:00
|
|
|
mdb_sql_open(parser_ctx->mdb, $3); free($3);
|
2001-04-08 01:32:43 +00:00
|
|
|
}
|
2001-04-20 21:06:46 +00:00
|
|
|
| DISCONNECT {
|
2020-08-18 12:09:11 +10:00
|
|
|
mdb_sql_close(parser_ctx->mdb);
|
2001-04-20 21:06:46 +00:00
|
|
|
}
|
|
|
|
|
| DESCRIBE TABLE table {
|
2020-08-18 12:09:11 +10:00
|
|
|
mdb_sql_describe_table(parser_ctx->mdb);
|
2001-04-20 21:06:46 +00:00
|
|
|
}
|
2001-04-08 01:32:43 +00:00
|
|
|
| LIST TABLES {
|
2020-08-18 12:09:11 +10:00
|
|
|
mdb_sql_listtables(parser_ctx->mdb);
|
2001-04-08 01:32:43 +00:00
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
|
2020-09-26 11:28:34 +01:00
|
|
|
top_clause:
|
|
|
|
|
/* empty */
|
|
|
|
|
| TOP NUMBER { mdb_sql_add_limit(parser_ctx->mdb, $2, 0); free($2); }
|
|
|
|
|
| TOP NUMBER PERCENT {
|
|
|
|
|
if (mdb_sql_add_limit(parser_ctx->mdb, $2, 1)) {
|
|
|
|
|
yyerror(NULL, parser_ctx, "Percent values must be between 0 and 100");
|
|
|
|
|
}
|
|
|
|
|
free($2);
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
|
2001-04-08 01:32:43 +00:00
|
|
|
where_clause:
|
|
|
|
|
/* empty */
|
|
|
|
|
| WHERE sarg_list
|
|
|
|
|
;
|
|
|
|
|
|
2016-08-30 08:43:34 -04:00
|
|
|
limit_clause:
|
|
|
|
|
/* empty */
|
2020-09-26 11:28:34 +01:00
|
|
|
| LIMIT NUMBER {
|
|
|
|
|
if (mdb_sql_get_limit(parser_ctx->mdb) != -1) {
|
|
|
|
|
yyerror(NULL, parser_ctx, "Can not have TOP and LIMIT clauses");
|
|
|
|
|
} else {
|
|
|
|
|
mdb_sql_add_limit(parser_ctx->mdb, $2, 0);
|
|
|
|
|
}
|
|
|
|
|
free($2);
|
|
|
|
|
}
|
2016-08-30 08:43:34 -04:00
|
|
|
;
|
|
|
|
|
|
2001-04-08 01:32:43 +00:00
|
|
|
sarg_list:
|
2003-01-20 16:04:24 +00:00
|
|
|
sarg
|
|
|
|
|
| '(' sarg_list ')'
|
2020-08-18 12:09:11 +10:00
|
|
|
| NOT sarg_list { mdb_sql_add_not(parser_ctx->mdb); }
|
|
|
|
|
| sarg_list OR sarg_list { mdb_sql_add_or(parser_ctx->mdb); }
|
|
|
|
|
| sarg_list AND sarg_list { mdb_sql_add_and(parser_ctx->mdb); }
|
2001-04-08 01:32:43 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
|
|
sarg:
|
2004-02-08 21:54:20 +00:00
|
|
|
identifier operator constant {
|
2020-08-18 12:09:11 +10:00
|
|
|
mdb_sql_add_sarg(parser_ctx->mdb, $1, $2, $3);
|
2001-05-16 00:21:17 +00:00
|
|
|
free($1);
|
|
|
|
|
free($3);
|
2001-04-08 01:32:43 +00:00
|
|
|
}
|
2004-02-08 21:54:20 +00:00
|
|
|
| constant operator identifier {
|
2020-08-18 12:09:11 +10:00
|
|
|
mdb_sql_add_sarg(parser_ctx->mdb, $3, $2, $1);
|
2001-05-16 00:21:17 +00:00
|
|
|
free($1);
|
|
|
|
|
free($3);
|
2001-04-08 01:32:43 +00:00
|
|
|
}
|
2004-02-08 21:54:20 +00:00
|
|
|
| constant operator constant {
|
2020-08-18 12:09:11 +10:00
|
|
|
mdb_sql_eval_expr(parser_ctx->mdb, $1, $2, $3);
|
2005-11-09 13:24:26 +00:00
|
|
|
free($1);
|
|
|
|
|
free($3);
|
2004-02-08 21:54:20 +00:00
|
|
|
}
|
|
|
|
|
| identifier nulloperator {
|
2020-08-18 12:09:11 +10:00
|
|
|
mdb_sql_add_sarg(parser_ctx->mdb, $1, $2, NULL);
|
2004-02-06 02:34:20 +00:00
|
|
|
free($1);
|
|
|
|
|
}
|
2001-04-08 01:32:43 +00:00
|
|
|
;
|
|
|
|
|
|
2004-02-08 21:54:20 +00:00
|
|
|
identifier:
|
|
|
|
|
NAME
|
|
|
|
|
| IDENT
|
|
|
|
|
;
|
|
|
|
|
|
2001-04-08 01:32:43 +00:00
|
|
|
operator:
|
2020-08-23 07:55:41 +10:00
|
|
|
EQ { $$ = MDB_EQUAL; }
|
|
|
|
|
| GT { $$ = MDB_GT; }
|
|
|
|
|
| LT { $$ = MDB_LT; }
|
2001-05-16 00:21:17 +00:00
|
|
|
| LTEQ { $$ = MDB_LTEQ; }
|
|
|
|
|
| GTEQ { $$ = MDB_GTEQ; }
|
|
|
|
|
| LIKE { $$ = MDB_LIKE; }
|
2001-04-08 01:32:43 +00:00
|
|
|
;
|
2004-02-06 02:34:20 +00:00
|
|
|
|
|
|
|
|
nulloperator:
|
2004-02-07 00:58:29 +00:00
|
|
|
IS NUL { $$ = MDB_ISNULL; }
|
|
|
|
|
| IS NOT NUL { $$ = MDB_NOTNULL; }
|
2004-02-06 02:34:20 +00:00
|
|
|
;
|
|
|
|
|
|
2001-04-08 01:32:43 +00:00
|
|
|
constant:
|
2015-08-30 11:23:57 +02:00
|
|
|
STRPTIME '(' constant ',' constant ')' {
|
2020-08-18 12:09:11 +10:00
|
|
|
$$ = mdb_sql_strptime(parser_ctx->mdb, $3, $5);
|
2015-08-30 11:23:57 +02:00
|
|
|
free($3);
|
|
|
|
|
free($5);
|
|
|
|
|
}
|
|
|
|
|
| NUMBER { $$ = $1; }
|
2001-05-16 00:21:17 +00:00
|
|
|
| STRING { $$ = $1; }
|
2001-04-08 01:32:43 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
|
|
database:
|
|
|
|
|
PATH
|
|
|
|
|
| NAME
|
2003-01-20 16:04:24 +00:00
|
|
|
;
|
2001-04-08 01:32:43 +00:00
|
|
|
|
|
|
|
|
table:
|
2020-08-18 12:09:11 +10:00
|
|
|
identifier { mdb_sql_add_table(parser_ctx->mdb, $1); free($1); }
|
2001-04-08 01:32:43 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
|
|
column_list:
|
2020-08-18 12:09:11 +10:00
|
|
|
COUNT '(' '*' ')' { mdb_sql_sel_count(parser_ctx->mdb); }
|
|
|
|
|
| '*' { mdb_sql_all_columns(parser_ctx->mdb); }
|
2001-04-08 01:32:43 +00:00
|
|
|
| column
|
|
|
|
|
| column ',' column_list
|
|
|
|
|
;
|
|
|
|
|
|
2015-08-20 14:35:09 +02:00
|
|
|
|
2001-04-08 01:32:43 +00:00
|
|
|
column:
|
2020-08-18 12:09:11 +10:00
|
|
|
identifier { mdb_sql_add_column(parser_ctx->mdb, $1); free($1); }
|
2001-04-08 01:32:43 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
|
|
%%
|
2020-08-18 12:09:11 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
int parse_sql( MdbSQL * mdb, const gchar *str)
|
|
|
|
|
{
|
|
|
|
|
sql_context ctx;
|
|
|
|
|
ctx.mdb = mdb;
|
|
|
|
|
|
|
|
|
|
yylex_init(&ctx.flex_scanner);
|
|
|
|
|
yy_scan_string(str, ctx.flex_scanner);
|
|
|
|
|
int res = yyparse(&ctx);
|
|
|
|
|
yylex_destroy(ctx.flex_scanner);
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void yyerror(YYLTYPE* yyloc,sql_context* sql_ctx, char const * msg)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr,"Error at Line : %s\n", msg);
|
|
|
|
|
mdb_sql_error(sql_ctx->mdb, "%s", msg);
|
|
|
|
|
}
|