This repository has been archived on 2024-07-15. You can view files and clone it, but cannot push or open issues or pull requests.
hedera/sql/parser/gram.y

764 lines
19 KiB
Plaintext
Raw Normal View History

2013-10-11 23:07:35 +00:00
%include
{
/*
* Copyright (C) 2012 - Juan Ferrer Toribio
*
* 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 3 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2013-10-14 12:08:06 +00:00
#include <stdlib.h>
#include <assert.h>
#include <sql/sql.h>
#include "gram.h"
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
typedef struct
{
SqlObject * object;
gchar * string;
gchar * current;
gboolean failed;
GError ** error;
2013-10-14 12:08:06 +00:00
}
ParseState;
typedef enum
{
SQL_PARSER_ERROR_EMPTY_QUERY = 1 << 0,
SQL_PARSER_ERROR_PARSING = 1 << 1,
SQL_PARSER_ERROR_STACK_OVERFLOW = 1 << 2,
}
SqlParserError;
2013-10-11 23:07:35 +00:00
}
2013-10-14 12:08:06 +00:00
%token_type { gchar * }
%token_destructor { g_free ($$); }
2013-10-11 23:07:35 +00:00
%token_prefix SQL_PARSER_
// Return pointer
%extra_argument { ParseState * state }
// Error treatment
%syntax_error
{
gchar * pref = ".", * err_str = NULL;
if (state->current)
{
err_str = state->current - 15 >= state->string ?
state->current - 10 : state->string;
pref = err_str == state->string ? " near: " : " near: [...]";
}
g_set_error (state->error, SQL_PARSER_LOG_DOMAIN, SQL_PARSER_ERROR_PARSING,
2013-10-11 23:07:35 +00:00
"Parsing error%s%s", pref, err_str);
state->failed = TRUE;
2013-10-11 23:07:35 +00:00
}
%stack_overflow
{
g_set_error (state->error, SQL_PARSER_LOG_DOMAIN, SQL_PARSER_ERROR_STACK_OVERFLOW,
"Parser stack overflow. Parsing terminated.\n");
state->failed = TRUE;
2013-10-11 23:07:35 +00:00
}
2013-10-14 12:08:06 +00:00
%parse_accept {}
2013-10-11 23:07:35 +00:00
// Operator precedence
%left UNION EXCEPT.
%left INSERSECT.
%left OR.
%left XOR.
%left AND.
%right NOT.
%right EQ NE.
%left GT GE LT LE.
%left LIKE.
%left IS.
%left PLUS MINUS.
%left STAR DIV MOD.
%right SIGN.
// Start symbol:
%start_symbol start
%type start {SqlObject *}
start ::= multi_stmt (A).
{
state->object = SQL_OBJECT (A);
}
2013-10-14 12:08:06 +00:00
// SqlMultiStmt
2013-10-11 23:07:35 +00:00
%type multi_stmt {SqlStmt *}
2013-10-14 12:08:06 +00:00
multi_stmt(multi) ::= stmt_list(stmt).
2013-10-11 23:07:35 +00:00
{
2013-10-14 12:08:06 +00:00
gint len = g_slist_length (stmt);
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
if (len > 1)
2013-10-11 23:07:35 +00:00
{
2013-10-14 12:08:06 +00:00
multi = SQL_STMT (sql_multi_stmt_new ());
GSList * n;
for (n = stmt; n; n = n->next)
sql_multi_stmt_add_stmt (SQL_MULTI_STMT (multi), n->data);
2013-10-11 23:07:35 +00:00
}
2013-10-14 12:08:06 +00:00
else if (len != 0)
multi = SQL_STMT (stmt->data);
2013-10-11 23:07:35 +00:00
else
2013-10-14 12:08:06 +00:00
multi = NULL;
g_slist_free (stmt);
2013-10-11 23:07:35 +00:00
}
2013-10-14 12:08:06 +00:00
%type stmt_list {GSList *}
%destructor stmt_list {g_slist_free ($$);}
stmt_list(A) ::= stmt(X) SC stmt_list(B). { A = g_slist_prepend (B, X); }
stmt_list(A) ::= stmt(X). { A = g_slist_prepend (A, X); }
stmt_list(A) ::= stmt(X) SC. { A = g_slist_prepend (A, X); }
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
// SqlStmt
2013-10-11 23:07:35 +00:00
%type stmt {SqlStmt *}
2013-10-14 12:08:06 +00:00
// SqlSelect
stmt(stmt) ::= multi_select(select). { stmt = SQL_STMT (select); }
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
%type multi_select {SqlSelect *}
multi_select(A) ::= multi_select(B) set_op(type) select_stmt(X).
2013-10-11 23:07:35 +00:00
{
2013-10-14 12:08:06 +00:00
sql_select_set_next (B, X, type);
2013-10-11 23:07:35 +00:00
A = B;
}
2013-10-14 12:08:06 +00:00
multi_select(A) ::= select_stmt(X). { A = X; }
2013-10-11 23:07:35 +00:00
%type set_op {SqlSelectType}
set_op(A) ::= UNION ANY. { A = SQL_SELECT_UNION_ANY; }
set_op(A) ::= UNION ALL. { A = SQL_SELECT_UNION_ALL; }
set_op(A) ::= UNION. { A = SQL_SELECT_UNION_ALL; }
set_op(A) ::= INTERSECT. { A = SQL_SELECT_INTERSECT; }
set_op(A) ::= EXCEPT. { A = SQL_SELECT_EXCEPT; }
2013-10-14 12:08:06 +00:00
%type select_stmt {SqlSelect *}
select_stmt(A) ::= LP select_stmt(X) RP. { A = X; }
select_stmt(select) ::= SELECT distinct(distinct) select_expr(expr_l)
from(target_l)
where(where)
group(group_l) having(having)
order(order_l) orderway(way)
limit(limit).
{
GSList * n;
select = sql_select_new ();
sql_select_set_distinct (select, distinct);
for (n = expr_l; n; n = n->next)
{
SelectExpr * se = n->data;
sql_select_add_expr (select, se->expr);
if (se->alias)
{
sql_select_set_alias (select, se->expr, se->alias);
g_free (se->alias);
}
g_free (se);
}
g_slist_free (expr_l);
for (n = target_l; n; n = n->next)
sql_dml_add_target (SQL_DML (select), n->data);
g_slist_free (target_l);
if (where)
sql_dml_set_where (SQL_DML (select), where);
if (group_l)
{
for (n = group_l; n; n = n->next)
sql_select_add_group (select, n->data);
g_slist_free (group_l);
if (having)
sql_select_set_having (select, having);
}
if (order_l)
{
for (n = order_l; n; n = n->next)
sql_select_add_order (select, n->data, way);
g_slist_free (order_l);
}
2013-10-11 23:07:35 +00:00
if (limit)
2013-10-14 12:08:06 +00:00
{
sql_select_set_limit (select
,limit[0] ? (guint) atoi (limit[0]): 0
,limit[1] ? (guint) atoi (limit[1]): 0
2013-10-11 23:07:35 +00:00
);
2013-10-14 12:08:06 +00:00
g_strfreev (limit);
}
2013-10-11 23:07:35 +00:00
}
%type distinct {gboolean}
distinct(A) ::= DISTINCT. { A = TRUE; }
distinct(A) ::= ALL. { A = FALSE; }
distinct(A) ::= . { A = FALSE; }
2013-10-14 12:08:06 +00:00
// Expressions with optional alias, only found in the SELECT clause
%type select_expr {GSList *}
%include
{
typedef struct
{
SqlExpr * expr;
gchar * alias;
}
SelectExpr;
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
static inline GSList * sql_parser_alias_expr_add
(GSList * list, SqlExpr * expr, gchar * alias)
{
SelectExpr * sexpr = g_new (SelectExpr, 1);
sexpr->expr = expr;
sexpr->alias = alias;
return g_slist_append (list, sexpr);
}
}
%destructor select_expr {g_slist_free_full ($$, (GDestroyNotify) g_free);}
select_expr(A) ::= select_expr(B) CM expr(X).
{ A = sql_parser_alias_expr_add (B, X, NULL); }
select_expr(A) ::= select_expr(B) CM expr(X) alias(Y).
{ A = sql_parser_alias_expr_add (B, X, Y); }
select_expr(A) ::= expr(X).
{ A = sql_parser_alias_expr_add (A, X, NULL); }
select_expr(A) ::= expr(X) alias(Y).
{ A = sql_parser_alias_expr_add (A, X, Y); }
%type from {GSList *}
%destructor from {g_slist_free ($$);}
2013-10-11 23:07:35 +00:00
from(A) ::= FROM target_list(X). { A = X; }
from(A) ::= . { A = NULL; }
2013-10-14 12:08:06 +00:00
%type group {GSList *}
%destructor group {g_slist_free ($$);}
2013-10-11 23:07:35 +00:00
group(A) ::= GROUP expr_list(X). { A = X; }
group(A) ::= . { A = NULL; }
%type having {SqlExpr *}
having(A) ::= HAVING expr(X). { A = X; }
having(A) ::= . { A = NULL; }
2013-10-14 12:08:06 +00:00
%type order {GSList *}
%destructor order {g_slist_free ($$);}
order(A) ::= ORDER expr_list (X). { A = X; }
2013-10-11 23:07:35 +00:00
order(A) ::= . { A = NULL; }
2013-10-14 12:08:06 +00:00
%type orderway {SqlOrderWay}
orderway(A) ::= ASC. { A = SQL_ORDER_ASC; }
orderway(A) ::= DESC. { A = SQL_ORDER_DESC; }
orderway(A) ::= . { A = SQL_ORDER_ASC; }
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
%type limit {gchar **}
2013-10-11 23:07:35 +00:00
%include
{
2013-10-14 12:08:06 +00:00
gchar ** sql_parser_limit (gchar * limit, gchar * offset)
{
gchar ** r = g_new (gchar *, 3);
r[0] = limit;
r[1] = offset;
r[2] = NULL;
return r;
2013-10-11 23:07:35 +00:00
}
2013-10-14 12:08:06 +00:00
}
limit(A) ::= LIMIT INTEGER(X). { A = sql_parser_limit (X, NULL); }
limit(A) ::= OFFSET INTEGER(Y). { A = sql_parser_limit (NULL, Y); }
limit(A) ::= LIMIT INTEGER(X)
OFFSET INTEGER(Y). { A = sql_parser_limit (X, Y); }
limit(A) ::= LIMIT INTEGER(X)
CM INTEGER(Y). { A = sql_parser_limit (Y, X); }
limit(A) ::= . { A = NULL; }
// SqlDelete
stmt(stmt) ::= delete_stmt(delete). { stmt = SQL_STMT (delete); }
%type delete_stmt {SqlDelete *}
delete_stmt(delete) ::= DELETE table_list(target) del_using(using) where(where).
{
GSList * n;
delete = sql_delete_new ();
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
for (n = target; n; n = n->next)
sql_dml_add_target (SQL_DML (delete), n->data);
g_slist_free (target);
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
for (n = using; n; n = n->next)
sql_delete_add_table (delete, n->data);
g_slist_free (using);
if (where)
sql_dml_set_where (SQL_DML (delete), where);
2013-10-11 23:07:35 +00:00
}
2013-10-14 12:08:06 +00:00
%type table_list {GSList *}
%destructor table_list {g_slist_free ($$);}
table_list(A) ::= table_list(B) CM table(X). { A = g_slist_append (B, X); }
table_list(A) ::= table(X). { A = g_slist_append (A, X); }
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
%type del_using {GSList *}
2013-10-11 23:07:35 +00:00
del_using(A) ::= USING target_list(X). { A = X; }
del_using(A) ::= . { A = NULL; }
2013-10-14 12:08:06 +00:00
// SqlUpdate
stmt(stmt) ::= update_stmt(update). { stmt = SQL_STMT (update); }
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
%type update_stmt {SqlUpdate *}
update_stmt(update) ::= UPDATE table(table) SET update_list(u_list) where(where).
2013-10-11 23:07:35 +00:00
{
2013-10-14 12:08:06 +00:00
GSList * n;
update = sql_update_new ();
sql_dml_add_target (SQL_DML (update), table);
for (n = u_list; n; n = n->next)
{
sql_update_add_set (update
,((SqlUpdateSet *) n->data)->field
,((SqlUpdateSet *) n->data)->expr
);
g_free (n->data);
}
g_slist_free (u_list);
if (where)
sql_dml_set_where (SQL_DML (update), where);
2013-10-11 23:07:35 +00:00
}
2013-10-14 12:08:06 +00:00
%type update_list {GSList *}
%destructor update_list {g_slist_free ($$);}
update_list(A) ::= update_list(B) CM update_set(X). { A = g_slist_append (B, X); }
update_list(A) ::= update_set(X). { A = g_slist_append (A, X); }
2013-10-11 23:07:35 +00:00
%type update_set {SqlUpdateSet *}
2013-10-14 12:08:06 +00:00
update_set(A) ::= field(X) EQ expr(Y).
2013-10-11 23:07:35 +00:00
{
2013-10-14 12:08:06 +00:00
A = g_new (SqlUpdateSet, 1);
A->field = SQL_FIELD (X);
A->expr = Y;
2013-10-11 23:07:35 +00:00
}
2013-10-14 12:08:06 +00:00
update_set(A) ::= field(X) EQ DEFAULT.
{
A = g_new (SqlUpdateSet, 1);
A->field = SQL_FIELD (X);
A->expr = NULL;
}
// Common to SELECT, DELETE and UPDATE
%type where {SqlExpr *}
where(A) ::= WHERE expr(X). { A = SQL_EXPR (X); }
where(A) ::= . { A = NULL; }
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
// SqlInsert
stmt(stmt) ::= insert_stmt(insert). { stmt = SQL_STMT (insert); }
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
%type insert_stmt {SqlInsert *}
insert_stmt(insert) ::= INSERT table(table) ins_fields(field) ins_values(value).
2013-10-11 23:07:35 +00:00
{
2013-10-14 12:08:06 +00:00
GSList * ll, * n;
insert = sql_insert_new ();
sql_insert_set_table (insert, SQL_TABLE (table));
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
for (n = field; n; n = n->next)
sql_insert_add_field (insert, n->data);
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
g_slist_free (field);
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
for (ll = value; ll; ll = ll->next)
{
sql_insert_add_row (insert);
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
for (n = ll->data; n; n = n->next)
sql_insert_add_expr (insert, n->data);
g_slist_free (ll->data);
}
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
g_slist_free (ll);
}
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
%type ins_fields {GSList *}
%destructor ins_fields {g_slist_free ($$);}
ins_fields(A) ::= LP field_list(X) RP. { A = X; }
ins_fields(A) ::= . { A = NULL; }
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
%type ins_values {GSList *}
ins_values ::= DEFAULT VALUES.
ins_values(A) ::= VALUES list_list(X). { A = X; }
//ins_values ::= multi_select.// not supported by the parsetree;
%type list_list {GSList *}
%destructor list_list {g_slist_free ($$);}
list_list(A) ::= list_list(B) CM LP def_expr_list(X) RP. { A = g_slist_append (B, X); }
list_list(A) ::= LP def_expr_list(X) RP. { A = g_slist_append (A, X); }
%type def_expr_list {GSList *}
%destructor def_expr_list {g_slist_free ($$);}
def_expr_list(A) ::= def_expr_list(B) CM def_expr(X). { A = g_slist_append (B, X); }
def_expr_list(A) ::= def_expr(X). { A = g_slist_append (A, X); }
2013-10-11 23:07:35 +00:00
%type def_expr {SqlExpr *}
def_expr(A) ::= DEFAULT. { A = NULL; }
2013-10-14 12:08:06 +00:00
def_expr(A) ::= expr(X). { A = X; }
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
// Common to all the statements
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
// SqlTarget
2013-10-11 23:07:35 +00:00
%type target {SqlTarget *}
2013-10-14 12:08:06 +00:00
//target(A) ::= LP target(B) RP. { A = B; }
target(A) ::= target(B) alias(X).
2013-10-11 23:07:35 +00:00
{
2013-10-14 12:08:06 +00:00
sql_target_set_alias (B, X);
g_free (X);
A = B;
2013-10-11 23:07:35 +00:00
}
2013-10-14 12:08:06 +00:00
%type target_list {GSList *}
%destructor target_list {g_slist_free ($$);}
target_list(A) ::= target_list(B) CM target(X). { A = g_slist_append (B, X); }
target_list(A) ::= target(X). { A = g_slist_append (A, X); }
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
target(A) ::= join(X). { A = X; }
target(A) ::= subquery(X). { A = X; }
target(A) ::= table(X). { A = X; }
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
// SqlJoin
2013-10-11 23:07:35 +00:00
%type join {SqlTarget *}
2013-10-14 12:08:06 +00:00
join(join) ::= target(left) join_type(jtype) target(right) join_cond(condition).
2013-10-11 23:07:35 +00:00
{
2013-10-14 12:08:06 +00:00
join = sql_join_new (left, right, jtype);
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
if (condition)
2013-10-11 23:07:35 +00:00
{
2013-10-14 12:08:06 +00:00
if (SQL_IS_EXPR (condition))
sql_join_set_condition (SQL_JOIN (join), condition);
else // USING list (GSList of SqlField)
2013-10-11 23:07:35 +00:00
{
2013-10-14 12:08:06 +00:00
SqlOperation * op = NULL;
GSList * n, * cond_list = condition;
gboolean use_cond = cond_list && cond_list->next ? TRUE : FALSE;
SqlOperation * cond = use_cond ?
sql_operation_new (SQL_OPERATION_TYPE_AND) :
NULL;
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
for (n = condition; n; n = n->next)
{
gchar * target = left->alias ?
left->alias : SQL_TABLE (left)->name;
gchar * schema = SQL_IS_TABLE(left) && SQL_TABLE (left)->schema ?
SQL_TABLE (left)->schema : NULL;
op = sql_operation_new (SQL_OPERATION_TYPE_EQUAL);
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
g_object_set (n->data, "target", target, "schema", schema, NULL);
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
sql_operation_add_expr (op, n->data);
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
target = right->alias ? right->alias : SQL_TABLE (right)->name;
schema = SQL_IS_TABLE (right) && SQL_TABLE (right)->schema ?
SQL_TABLE (right)->schema : NULL;
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
sql_operation_add_expr (op,
sql_field_new (SQL_FIELD (n->data)->name, target, schema));
if (use_cond)
sql_operation_add_expr (cond, SQL_EXPR (op));
}
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
g_slist_free (condition);
sql_join_set_condition (SQL_JOIN (join),
use_cond ? SQL_EXPR (cond) : SQL_EXPR (op));
}
2013-10-11 23:07:35 +00:00
}
}
%type join_type {SqlJoinType}
join_type(A) ::= INNER JOIN. { A = SQL_JOIN_TYPE_INNER; }
join_type(A) ::= JOIN. { A = SQL_JOIN_TYPE_INNER; }
join_type(A) ::= LEFT JOIN. { A = SQL_JOIN_TYPE_LEFT; }
join_type(A) ::= RIGHT JOIN. { A = SQL_JOIN_TYPE_RIGHT; }
%type join_cond {gpointer}
2013-10-14 12:08:06 +00:00
join_cond(A) ::= ON expr(X). { A = X; }// A : SqlExpr
join_cond(A) ::= USING LP field_list(X) RP. { A = X; }// A : GSList
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
// SqlSubquery
2013-10-11 23:07:35 +00:00
%type subquery {SqlTarget *}
2013-10-14 12:08:06 +00:00
subquery(sub) ::= LP multi_select(stmt) RP.
{ sub = SQL_TARGET (sql_subquery_new (stmt)); }
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
//SqlTable
2013-10-11 23:07:35 +00:00
%type table {SqlTarget *}
2013-10-14 12:08:06 +00:00
2013-10-11 23:07:35 +00:00
table(A) ::= identifier(X).
{
2013-10-14 12:08:06 +00:00
A = sql_table_new (X);
g_free (X);
2013-10-11 23:07:35 +00:00
}
table(A) ::= identifier(Y) DOT identifier(X).
{
2013-10-14 12:08:06 +00:00
A = sql_table_new (X);
g_object_set (A, "schema", Y, NULL);
g_free (Y);
g_free (X);
2013-10-11 23:07:35 +00:00
}
2013-10-14 12:08:06 +00:00
// SqlExpr
2013-10-11 23:07:35 +00:00
%type expr {SqlExpr *}
expr(A) ::= LP expr(X) RP. { A = X; }
2013-10-14 12:08:06 +00:00
%type expr_list {GSList *}
%destructor expr_list {g_slist_free ($$);}
expr_list(A) ::= expr_list(B) CM expr(X). { A = g_slist_append (B, X); }
expr_list(A) ::= expr(X). { A = g_slist_append (A, X); }
2013-10-11 23:07:35 +00:00
expr(A) ::= field(X). { A = X; }
expr(A) ::= function(X). { A = X; }
2013-10-14 12:08:06 +00:00
expr(A) ::= operation(X). { A = SQL_EXPR (X); }
2013-10-11 23:07:35 +00:00
expr(A) ::= value(X). { A = X; }
2013-10-14 12:08:06 +00:00
// SqlField
2013-10-11 23:07:35 +00:00
%type field {SqlExpr *}
2013-10-14 12:08:06 +00:00
field(A) ::= identifier(X).
2013-10-11 23:07:35 +00:00
{
2013-10-14 12:08:06 +00:00
A = sql_field_new (X, NULL, NULL);
g_free (X);
2013-10-11 23:07:35 +00:00
}
2013-10-14 12:08:06 +00:00
field(A) ::= STAR.
2013-10-11 23:07:35 +00:00
{
2013-10-14 12:08:06 +00:00
A = sql_field_new ("*", NULL, NULL);
2013-10-11 23:07:35 +00:00
}
2013-10-14 12:08:06 +00:00
field(A) ::= identifier(Y) DOT identifier(X).
2013-10-11 23:07:35 +00:00
{
2013-10-14 12:08:06 +00:00
A = sql_field_new (X, Y, NULL);
g_free (X);
g_free (Y);
2013-10-11 23:07:35 +00:00
}
2013-10-14 12:08:06 +00:00
field(A) ::= identifier(Y) DOT STAR.
2013-10-11 23:07:35 +00:00
{
2013-10-14 12:08:06 +00:00
A = sql_field_new ("*", Y, NULL);
g_free (Y);
}
field(A) ::= identifier(Z) DOT identifier(Y) DOT identifier(X).
{
A = sql_field_new (X, Y, Z);
g_free (X);
g_free (Y);
g_free (Z);
}
field(A) ::= identifier(Z) DOT identifier(Y) DOT STAR.
{
A = sql_field_new ("*", Y, Z);
g_free (Y);
g_free (Z);
2013-10-11 23:07:35 +00:00
}
2013-10-14 12:08:06 +00:00
%type field_list {GSList *}
%destructor field_list {g_slist_free ($$);}
field_list(A) ::= field_list(B) CM field(X). { A = g_slist_append (B, X); }
field_list(A) ::= field(X). { A = g_slist_append (A, X); }
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
// SqlFunction
2013-10-11 23:07:35 +00:00
%type function {SqlExpr *}
2013-10-14 12:08:06 +00:00
function(A) ::= identifier(Y) DOT identifier(X) LP RP.
2013-10-11 23:07:35 +00:00
{
2013-10-14 12:08:06 +00:00
A = SQL_EXPR (sql_function_new (X, Y));
g_free (X);
g_free (Y);
2013-10-11 23:07:35 +00:00
}
2013-10-14 12:08:06 +00:00
function(A) ::= identifier(X) LP RP.
2013-10-11 23:07:35 +00:00
{
2013-10-14 12:08:06 +00:00
A = SQL_EXPR (sql_function_new (X, NULL));
g_free (X);
}
function(A) ::= identifier(Z) DOT identifier(X) LP expr_list(Y) RP.
{
GSList * n = NULL;
A = SQL_EXPR (sql_function_new (X, Z));
for ( n = Y; n; n = n->next)
sql_function_add_param (SQL_FUNCTION (A), n->data);
g_slist_free (Y);
g_free (X);
}
function(A) ::= identifier(X) LP expr_list(Y) RP.
{
GSList * n = NULL;
A = SQL_EXPR (sql_function_new (X, NULL));
for ( n = Y; n; n = n->next)
sql_function_add_param (SQL_FUNCTION (A), n->data);
g_slist_free (Y);
g_free (X);
}
function(A) ::= expr(X) MOD expr(Y).
{
A = SQL_EXPR (sql_function_new ("MOD", NULL));
sql_function_add_param (SQL_FUNCTION (A), SQL_EXPR (X));
sql_function_add_param (SQL_FUNCTION (A), SQL_EXPR (Y));
2013-10-11 23:07:35 +00:00
}
2013-10-14 12:08:06 +00:00
// SqlOperation
%type operation {SqlOperation *}
2013-10-11 23:07:35 +00:00
%include
{
2013-10-14 12:08:06 +00:00
static inline SqlOperation * sql_parser_create_operation
(const gpointer X, const gpointer Y, const SqlOperationType type)
{
SqlOperation * op = sql_operation_new (type);
sql_operation_add_expr (op, SQL_EXPR (X));
if (Y) // Y == NULL for unary operations
sql_operation_add_expr (op, SQL_EXPR (Y));
return op;
2013-10-11 23:07:35 +00:00
}
2013-10-14 12:08:06 +00:00
}
2013-10-11 23:07:35 +00:00
operation(A) ::= MINUS expr(X). [SIGN]
{
GValue value = {0};
SqlExpr * minus = sql_value_new ();
g_value_init (&value, G_TYPE_INT);
g_value_set_int (&value, -1);
sql_value_set_value (SQL_VALUE (minus), &value);
2013-10-14 12:08:06 +00:00
A = sql_parser_create_operation (X, minus, SQL_OPERATION_TYPE_MULTIPLICATION);
2013-10-11 23:07:35 +00:00
}
operation(A) ::= PLUS expr(X). [SIGN]
2013-10-14 12:08:06 +00:00
{ A = sql_parser_create_operation (X, NULL, SQL_OPERATION_TYPE_SUM); }
2013-10-11 23:07:35 +00:00
operation(A) ::= expr(X) PLUS expr(Y).
2013-10-14 12:08:06 +00:00
{ A = sql_parser_create_operation (X, Y, SQL_OPERATION_TYPE_SUM); }
2013-10-11 23:07:35 +00:00
operation(A) ::= expr(X) MINUS expr(Y).
2013-10-14 12:08:06 +00:00
{ A = sql_parser_create_operation (X, Y, SQL_OPERATION_TYPE_REST); }
2013-10-11 23:07:35 +00:00
operation(A) ::= expr(X) STAR expr(Y).
2013-10-14 12:08:06 +00:00
{ A = sql_parser_create_operation (X, Y, SQL_OPERATION_TYPE_MULTIPLICATION); }
2013-10-11 23:07:35 +00:00
operation(A) ::= expr(X) DIV expr(Y).
2013-10-14 12:08:06 +00:00
{ A = sql_parser_create_operation (X, Y, SQL_OPERATION_TYPE_DIVISION); }
2013-10-11 23:07:35 +00:00
operation(A) ::= expr(X) OR expr(Y).
2013-10-14 12:08:06 +00:00
{ A = sql_parser_create_operation (X, Y, SQL_OPERATION_TYPE_OR); }
2013-10-11 23:07:35 +00:00
operation(A) ::= expr(X) XOR expr(Y).
2013-10-14 12:08:06 +00:00
{ A = sql_parser_create_operation (X, Y, SQL_OPERATION_TYPE_XOR); }
2013-10-11 23:07:35 +00:00
operation(A) ::= expr(X) AND expr(Y).
2013-10-14 12:08:06 +00:00
{ A = sql_parser_create_operation (X, Y, SQL_OPERATION_TYPE_AND); }
operation(A) ::= NOT expr(X).
{ A = sql_parser_create_operation (X, NULL, SQL_OPERATION_TYPE_NOT); }
2013-10-11 23:07:35 +00:00
operation(A) ::= expr(X) EQ expr(Y).
2013-10-14 12:08:06 +00:00
{ A = sql_parser_create_operation (X, Y, SQL_OPERATION_TYPE_EQUAL); }
2013-10-11 23:07:35 +00:00
operation(A) ::= expr(X) NE expr(Y).
2013-10-14 12:08:06 +00:00
{ A = sql_parser_create_operation (X, Y, SQL_OPERATION_TYPE_NOT_EQUAL); }
2013-10-11 23:07:35 +00:00
//XXX the following should be %nonassoc operators but are %left to avoid
// conflicts. The DB will warn about a bad use if used as %left.
operation(A) ::= expr(X) GT expr(Y).
2013-10-14 12:08:06 +00:00
{ A = sql_parser_create_operation (X, Y, SQL_OPERATION_TYPE_GREATER); }
2013-10-11 23:07:35 +00:00
operation(A) ::= expr(X) GE expr(Y).
2013-10-14 12:08:06 +00:00
{ A = sql_parser_create_operation (X, Y, SQL_OPERATION_TYPE_GREATER_EQUAL); }
2013-10-11 23:07:35 +00:00
operation(A) ::= expr(X) LT expr(Y).
2013-10-14 12:08:06 +00:00
{ A = sql_parser_create_operation (X, Y, SQL_OPERATION_TYPE_LOWER); }
2013-10-11 23:07:35 +00:00
operation(A) ::= expr(X) LE expr(Y).
2013-10-14 12:08:06 +00:00
{ A = sql_parser_create_operation (X, Y, SQL_OPERATION_TYPE_LOWER_EQUAL); }
2013-10-11 23:07:35 +00:00
operation(A) ::= expr(X) LIKE expr(Y).
2013-10-14 12:08:06 +00:00
{ A = sql_parser_create_operation (X, Y, SQL_OPERATION_TYPE_LIKE); }
2013-10-11 23:07:35 +00:00
operation(A) ::= expr(X) IS expr(Y).
2013-10-14 12:08:06 +00:00
{ A = sql_parser_create_operation (X, Y, SQL_OPERATION_TYPE_IS); }
2013-10-11 23:07:35 +00:00
2013-10-14 12:08:06 +00:00
// SqlValue
2013-10-11 23:07:35 +00:00
%type value {SqlExpr *}
value(A) ::= INTEGER(X).
{
GValue value = {0};
2013-10-14 12:08:06 +00:00
SqlExpr * v = sql_value_new ();
2013-10-11 23:07:35 +00:00
g_value_set_int (g_value_init (&value, G_TYPE_INT), atoi (X));
2013-10-14 12:08:06 +00:00
sql_value_set_value (SQL_VALUE (v), &value);
g_free (X);
A = v;
2013-10-11 23:07:35 +00:00
}
value(A) ::= FLOAT(X).
{
GValue value = {0};
2013-10-14 12:08:06 +00:00
SqlExpr * v = sql_value_new ();
2013-10-11 23:07:35 +00:00
g_value_set_double
(g_value_init (&value, G_TYPE_DOUBLE), g_ascii_strtod (X, NULL));
2013-10-14 12:08:06 +00:00
sql_value_set_value (SQL_VALUE (v), &value);
g_free (X);
A = v;
2013-10-11 23:07:35 +00:00
}
value(A) ::= STRING(X).
{
GValue value = {0};
2013-10-14 12:08:06 +00:00
SqlExpr * v = sql_value_new ();
2013-10-11 23:07:35 +00:00
g_value_set_string (g_value_init (&value, G_TYPE_STRING), X);
2013-10-14 12:08:06 +00:00
sql_value_set_value (SQL_VALUE (v), &value);
g_free (X);
A = v;
2013-10-11 23:07:35 +00:00
}
value(A) ::= BOOLEAN(X).
{
GValue value = {0};
2013-10-14 12:08:06 +00:00
SqlExpr * v = sql_value_new ();
2013-10-11 23:07:35 +00:00
g_value_set_boolean (g_value_init (&value, G_TYPE_BOOLEAN)
,(!g_strcmp0 (X, "TRUE") || !g_strcmp0 (X, "true")) ? TRUE : FALSE);
2013-10-14 12:08:06 +00:00
sql_value_set_value (SQL_VALUE (v), &value);
g_free (X);
A = v;
2013-10-11 23:07:35 +00:00
}
2013-10-14 12:08:06 +00:00
// Alias
2013-10-11 23:07:35 +00:00
%type alias {gchar *}
%destructor alias {g_free ($$);}
alias(A) ::= AS identifier(X). { A = X; }
alias(A) ::= identifier(X). { A = X; }
identifier(A) ::= IDENTIFIER(X). { A = X; }
2013-10-14 12:08:06 +00:00
// Placeholders
2013-10-11 23:07:35 +00:00
%type placeholder {gchar *}
%destructor expr {g_free ($$);}
placeholder(A) ::= PLACEHOLDER(X). { A = X; }
stmt ::= placeholder.
target ::= placeholder.
expr ::= placeholder.