summaryrefslogtreecommitdiffstats
path: root/kexi/kexidb/parser/parser.cpp
blob: 2a9a0e4cbc6d6bc25d7e182817fbd5a1224efe00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* 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., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, 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; }

TQString
Parser::operationString() const
{
	switch((OPCode)d->operation) {
		case OP_Error:
			return "Error";
		case OP_CreateTable:
			return "CreateTable";
		case OP_AlterTable:
			return "AlterTable";
		case OP_Select:
			return "Select";
		case OP_Insert:
			return "Insert";
		case OP_Update:
			return "Update";
		case OP_Delete:
			return "Delete";
		default: //OP_None
			return "None";
	}
}

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; }

TQString 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 TQString &statement)
{
	init();
	clear();
	d->statement = statement;

	KexiDB::Parser *oldParser = parser;
	KexiDB::Field *oldField = field;
	bool res = parseData(this, statement.utf8());
	parser = oldParser;
	field = oldField;
	return res;
}

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

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

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

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

ParserError::~ParserError()
{
}