summaryrefslogtreecommitdiffstats
path: root/kexi/kexidb/parser/parser.h
blob: ec2942e1f1d019712be0a2322cf0bcc4e911703b (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* 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.
*/

#ifndef KEXIDBPARSER_H
#define KEXIDBPARSER_H

#include <qobject.h>
#include <qptrlist.h>
#include <qvariant.h>

#include <kexidb/field.h>
#include <kexidb/expression.h>

namespace KexiDB
{

class Connection;
class QuerySchema;
class TableSchema;
class Field;

/**
 * Provides detailed i18n'ed error description about the \a Parser .
 */
class KEXI_DB_EXPORT ParserError
{
	public:

		/**
		 * Empty constructor.
		 */
		ParserError();

		/**
		 * Constructor.
		 *
		 * \param type The errortype.
		 * \param error A description of the error.
		 * \param hint Token where the error happend.
		 * \param at The position where the error happend.
		 */
		ParserError(const QString &type, const QString &error, const QString &hint, int at);

		/**
		 * Destructor.
		 */
		~ParserError();

		/**
		 * \return the errortype.
		 */
		QString	type() { return m_type; }

		/**
		 * \return a descriping error message.
		 */
		QString	error() { return m_error; }

		/**
		 * \return position where the error happend.
		 */
		int	at() { return m_at; }

	private:
		QString m_type;
		QString m_error;
		QString m_hint;
		int m_at;
//		bool	m_isNull;
};

class ParserPrivate;

/**
 * Parser for SQL statements.
 *
 * The best and prefeerred way to run queries is using the KexiDB::Parser functionality
 * and use the resulting QuerySchema object since this offers a database-backend-independent 
 * way to deal with SQL statements on the one hand and offers high level 
 * functionality on the other. Also BLOBs like images are handled that way.
 *
 * For example if we like to use the SELECT statement
 * "SELECT dir.path, media.filename FROM dir, media WHERE dir.id=media.dirId AND media.id=%s"
 * we are able to use the \a Connection::prepareStatement method which takes the type of
 * the statement (in our case \a PreparedStatement::SelectStatement ), a list of fields (in
 * our case dir.path and media.filename) and returns a \a PreparedStatement::Ptr instance.
 * By using the \a QuerySchema::addRelationship and \a QuerySchema::addToWhereExpression methods
 * the SQL statement could be extended with relationships and WHERE expressions.
 *
 * For more, see \a KexiDB::PreparedStatement and \a Connection::selectStatement() . A more
 * complex example that looks at what the user has defined and carefully builds 
 * \a KexiDB::QuerySchema object, including the WHERE expression can be found in 
 * the Query Designer's source code in the method \a KexiQueryDesignerGuiEditor::buildSchema().
 */
class KEXI_DB_EXPORT Parser
{
	public:

		/**
		 * The operation-code of the statement.
		 */
		enum OPCode
		{
			OP_None = 0, /// No statement parsed or reseted.
			OP_Error, /// Error while parsing.
			OP_CreateTable, /// Create a table.
			OP_AlterTable, /// Alter an existing table
			OP_Select, /// Query-statement.
			OP_Insert, /// Insert new content.
			OP_Update, /// Update existing content.
			OP_Delete  /// Delete existing content.
		};

		/**
		 * constructs an empty object of the parser
		 * \param connection is used for things like wildcard resolution. If 0 parser works in "pure mode"
		 */
		Parser(Connection *connection);
		~Parser();

		/**
		 * clears previous results and runs the parser
		 */
		bool parse(const QString &statement);

		/**
		 * rests results
		 */
		void clear();

		/**
		 * \return the resulting operation or OP_Error if failed
		 */
		OPCode operation() const;

		/**
		 * \return the resulting operation as string.
		 */
		QString operationString() const;

		/**
		 * \return a pointer to a KexiDBTable on CREATE TABLE
		 * or 0 on any other operation or error. Returned object is owned by you.
		 * You can call this method only once every time after doing parse().
		 * Next time, the call will return 0.
		 */
		TableSchema *table();

		/**
		 * \return a pointer to KexiDBSelect if 'SELECT ...' was called
		 * or 0 on any other operation or error. Returned object is owned by you.
		 * You can call this method only once every time after doing parse().
		 * Next time, the call will return 0.
		 */
		QuerySchema *query();

		/**
		 * \return a pointer to the used database connection or 0 if not set
		 * You can call this method only once every time after doing parse().
		 * Next time, the call will return 0.
		 */
		Connection	*db() const;

		/**
		 * \return detailed information about last error.
		 * If no error occurred ParserError isNull()
		 */
		ParserError error() const;

		/**
		 * \return the statement passed on the last \a parse method-call.
		 */
		QString statement() const;

		/**
		 * \internal
		 * sets the operation (only parser will need to call this)
		 */
		void setOperation(OPCode op);

		/**
		 * \internal
		 * creates a new table (only parser will need to call this)
		 */
		void createTable(const char *t);

		/**
		 * \internal
		 * sets \a query schema object (only parser will need to call this)
		 */
//todo: other query types
		void setQuerySchema(QuerySchema *query);

		/**
		 * \internal
		 * \return query schema
		 */
		QuerySchema *select() const;

		/**
		 * \internal
		 * INTERNAL use only: sets a error
		 */
		void setError(const ParserError &err);

		/**
		 * \return true if the \param str is an reserved
		 * keyword (see tokens.cpp for a list of reserved
		 * keywords).
		 */
		bool isReservedKeyword(const char *str);

	protected:
		void init();

		ParserError m_error; //!< detailed information about last error.
		ParserPrivate *d; //!< \internal d-pointer class.
};

}

#endif