kexi

parser.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003 Lucijan Busch <lucijan@kde.org>
3 Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include <connection.h>
22#include <tableschema.h>
23#include "parser.h"
24#include "parser_p.h"
25#include "sqlparser.h"
26
27extern const char * reserved_keywords[];
28
29using namespace KexiDB;
30
32 : d(new ParserPrivate)
33{
34 d->db = db;
35}
36
37Parser::~Parser()
38{
39 delete d;
40}
41
42Parser::OPCode Parser::operation() const { return (OPCode)d->operation; }
43
44TQString
46{
47 switch((OPCode)d->operation) {
48 case OP_Error:
49 return "Error";
50 case OP_CreateTable:
51 return "CreateTable";
52 case OP_AlterTable:
53 return "AlterTable";
54 case OP_Select:
55 return "Select";
56 case OP_Insert:
57 return "Insert";
58 case OP_Update:
59 return "Update";
60 case OP_Delete:
61 return "Delete";
62 default: //OP_None
63 return "None";
64 }
65}
66
67TableSchema *Parser::table() { TableSchema *t = d->table; d->table=0; return t; }
68
69QuerySchema *Parser::query() { QuerySchema *s = d->select; d->select=0; return s; }
70
71Connection *Parser::db() const { return d->db; }
72
73ParserError Parser::error() const { return d->error; }
74
75TQString Parser::statement() const { return d->statement; }
76
77void Parser::setOperation(OPCode op) { d->operation = op; }
78
79QuerySchema *Parser::select() const { return d->select; }
80
81void Parser::setError(const ParserError &err) { d->error = err; }
82
83void
84Parser::createTable(const char *t)
85{
86 if (d->table)
87 return;
88
89 d->table = new KexiDB::TableSchema(t);
90}
91
92void
93Parser::setQuerySchema(QuerySchema *query)
94{
95 if (d->select)
96 delete d->select;
97
98 d->select = query;
99}
100
101void Parser::init()
102{
103 if (d->initialized)
104 return;
105#define INS(p) d->reservedKeywords.insert(p, (char*)1, 0)
106#include "tokens.cpp"
107 d->initialized = true;
108}
109
110bool Parser::isReservedKeyword(const char *str)
111{
112 return d->reservedKeywords.find(str);
113}
114
115bool
116Parser::parse(const TQString &statement)
117{
118 init();
119 clear();
120 d->statement = statement;
121
122 KexiDB::Parser *oldParser = parser;
123 KexiDB::Field *oldField = field;
124 bool res = parseData(this, statement.utf8());
125 parser = oldParser;
126 field = oldField;
127 return res;
128}
129
130void
132{
133 d->clear();
134}
135
136//-------------------------------------
137
139: m_at(-1)
140{
141// m_isNull = true;
142}
143
144ParserError::ParserError(const TQString &type, const TQString &error, const TQString &hint, int at)
145{
146 m_type = type;
147 m_error = error;
148 m_hint = hint;
149 m_at = at;
150}
151
155
Provides database connection, allowing queries and data modification.
Definition connection.h:62
Meta-data for a field.
Definition field.h:73
Provides detailed i18n'ed error description about the Parser .
Definition parser.h:43
~ParserError()
Destructor.
Definition parser.cpp:152
TQString type()
Definition parser.h:69
TQString error()
Definition parser.h:74
ParserError()
Empty constructor.
Definition parser.cpp:138
Parser for SQL statements.
Definition parser.h:113
QuerySchema * query()
Definition parser.cpp:69
TQString operationString() const
Definition parser.cpp:45
bool parse(const TQString &statement)
clears previous results and runs the parser
Definition parser.cpp:116
ParserError error() const
Definition parser.cpp:73
Parser(Connection *connection)
constructs an empty object of the parser
Definition parser.cpp:31
TableSchema * table()
Definition parser.cpp:67
Connection * db() const
Definition parser.cpp:71
void clear()
rests results
Definition parser.cpp:131
TQString statement() const
Definition parser.cpp:75
OPCode
The operation-code of the statement.
Definition parser.h:120
@ OP_Select
Alter an existing table.
Definition parser.h:125
@ OP_Update
Insert new content.
Definition parser.h:127
@ OP_Insert
Query-statement.
Definition parser.h:126
@ OP_CreateTable
Error while parsing.
Definition parser.h:123
@ OP_AlterTable
Create a table.
Definition parser.h:124
@ OP_Delete
Update existing content.
Definition parser.h:128
@ OP_Error
No statement parsed or reseted.
Definition parser.h:122
bool isReservedKeyword(const char *str)
Definition parser.cpp:110
OPCode operation() const
Definition parser.cpp:42
KexiDB::QuerySchema provides information about database query.
High-level database connectivity library with database backend drivers.