/*
 * Copyright (c) 2002-2013 Balabit
 * Copyright (c) 1998-2013 Balázs Scheidler
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */

%code top {
#include "afsql-parser.h"

}


%code {

#include "afsql.h"
#include "cfg-grammar-internal.h"
#include "cfg-parser.h"
#include "messages.h"
#include "plugin.h"

}

%define api.prefix {afsql_}

/* this parameter is needed in order to instruct bison to use a complete
 * argument list for yylex/yyerror */

%lex-param {CfgLexer *lexer}
%parse-param {CfgLexer *lexer}
%parse-param {LogDriver **instance}
%parse-param {gpointer arg}


/* INCLUDE_DECLS */

%token KW_DEFAULT
%token KW_DBD_OPTION
%token KW_INDEXES
%token KW_VALUES
%token KW_SQL
%token KW_PASSWORD
%token KW_USERNAME
%token KW_DATABASE
%token KW_TABLE
%token KW_SESSION_STATEMENTS
%token KW_CREATE_STATEMENT_APPEND
%token KW_COLUMNS
%token KW_NULL
%token KW_IGNORE_TNS_CONFIG

%type   <ptr> dest_afsql
%type   <ptr> dest_afsql_params
%type   <ptr> dest_afsql_values
%type   <ptr> dest_afsql_values_build
%type   <num> dest_afsql_flags

%%

start
        : LL_CONTEXT_DESTINATION dest_afsql                        { YYACCEPT; }
        ;


dest_afsql
        : KW_SQL '(' dest_afsql_params ')'	{ $$ = $3; }
        ;

dest_afsql_params
        :
          {
            last_driver = *instance = afsql_dd_new(configuration);
          }
          dest_afsql_options			{ $$ = last_driver; }
        ;

dest_afsql_options
        : dest_afsql_option dest_afsql_options
        |
        ;

dest_afsql_option
        : KW_TYPE '(' string ')'		{ afsql_dd_set_type(last_driver, $3); free($3); }
        | KW_HOST '(' string ')'		{ afsql_dd_set_host(last_driver, $3); free($3); }
        | KW_PORT '(' string_or_number ')'
          {
            CHECK_ERROR(afsql_dd_check_port($3), @3, "Illegal sql port number: %s", $3);
            afsql_dd_set_port(last_driver, $3);
            free($3);
          }
        | KW_USERNAME '(' string ')'		{ afsql_dd_set_user(last_driver, $3); free($3); }
        | KW_DBD_OPTION '(' string LL_NUMBER ')' { afsql_dd_add_dbd_option_numeric(last_driver, $3, $4); free($3); }
        | KW_DBD_OPTION '(' string string ')'    { afsql_dd_add_dbd_option(last_driver, $3, $4); free($3); free($4); }
        | KW_PASSWORD '(' string ')'		{ afsql_dd_set_password(last_driver, $3); free($3); }
        | KW_DATABASE '(' string ')'		{ afsql_dd_set_database(last_driver, $3); free($3); }
        | KW_TABLE '(' template_content ')'	{ afsql_dd_set_table(last_driver, $3); }
        | KW_COLUMNS '(' string_list ')'	{ afsql_dd_set_columns(last_driver, $3); }
        | KW_INDEXES '(' string_list ')'        { afsql_dd_set_indexes(last_driver, $3); }
        | KW_VALUES '(' dest_afsql_values ')'		{ afsql_dd_set_values(last_driver, $3); }
        | KW_IGNORE_TNS_CONFIG '(' yesno ')'    { afsql_dd_set_ignore_tns_config(last_driver,$3); }
        | KW_NULL '(' string ')'                { afsql_dd_set_null_value(last_driver, $3); free($3); }
        | KW_SESSION_STATEMENTS '(' string_list ')' { afsql_dd_set_session_statements(last_driver, $3); }
        | KW_FLAGS '(' dest_afsql_flags ')'     { afsql_dd_set_flags(last_driver, $3); }
	| KW_CREATE_STATEMENT_APPEND '(' string ')' { afsql_dd_set_create_statement_append(last_driver, $3); free($3); }

	| { last_template_options = &((AFSqlDestDriver *) last_driver)->template_options; } template_option
	| threaded_dest_driver_general_option
        | threaded_dest_driver_batch_option
        ;

dest_afsql_values
        : dest_afsql_values_build               { $$ = g_list_reverse($1); }
        ;

dest_afsql_values_build
        : string dest_afsql_values_build	{ $$ = g_list_append($2, g_strdup($1)); free($1); }
        | KW_DEFAULT dest_afsql_values_build	{ $$ = g_list_append($2, GUINT_TO_POINTER(AFSQL_COLUMN_DEFAULT)); }
        |					{ $$ = NULL; }
        ;

dest_afsql_flags
        : normalized_flag dest_afsql_flags      { $$ = afsql_dd_lookup_flag($1) | $2; free($1); }
        |                                       { $$ = 0; }
        ;

/* INCLUDE_RULES */

%%
