/* This file is part of the KDE project
   Copyright (C) 2003 Lucijan Busch <lucijan@kde.org>
   Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>

   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 License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include <connection.h>
#include <tableschema.h>
#include "parser.h"
#include "parser_p.h"
#include "sqlparser.h"

extern const char * reserved_keywords[];

using namespace KexiDB;

Parser::Parser(Connection *db)
 : d(new ParserPrivate)
{
	d->db = db;
}

Parser::~Parser()
{
	delete d;
}

Parser::OPCode Parser::operation() const { return (OPCode)d->operation; }

TableSchema *Parser::table() { TableSchema *t = d->table; d->table=0; return t; }

QuerySchema *Parser::query() { QuerySchema *s = d->select; d->select=0; return s; }

Connection *Parser::db() const { return d->db; }

ParserError Parser::error() const { return d->error; }

QString Parser::statement() const { return d->statement; }

void Parser::setOperation(OPCode op) { d->operation = op; }

QuerySchema *Parser::select() const { return d->select; }

void Parser::setError(const ParserError &err) { d->error = err; }

void
Parser::createTable(const char *t)
{
	if (d->table)
		return;

	d->table = new KexiDB::TableSchema(t);
}

void
Parser::setQuerySchema(QuerySchema *query)
{
	if (d->select)
		delete d->select;
	
	d->select = query;
}

void Parser::init()
{
	if (d->initialized)
		return;
#define INS(p) d->reservedKeywords.insert(p, (char*)1, 0)
#include "tokens.cpp"
	d->initialized = true;
}

bool Parser::isReservedKeyword(const char *str)
{
	return d->reservedKeywords.find(str);
}

bool
Parser::parse(const QString &statement)
{
	init();
	clear();
	d->statement = statement;
	return parseData(this, statement.latin1());
}

void
Parser::clear()
{
	d->clear();
}

//-------------------------------------

ParserError::ParserError()
: m_at(-1)
{
//	m_isNull = true;
}

ParserError::ParserError(const QString &type, const QString &error, const QString &hint, int at)
{
	m_type = type;
	m_error = error;
	m_hint = hint;
	m_at = at;
}

ParserError::~ParserError()
{
}

