[go: up one dir, main page]

File: tosqlparse.h

package info (click to toggle)
tora 1.3.23-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 15,984 kB
  • ctags: 14,460
  • sloc: cpp: 123,554; sh: 16,181; makefile: 966; xml: 69
file content (394 lines) | stat: -rw-r--r-- 13,260 bytes parent folder | download | duplicates (2)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*****
*
* TOra - An Oracle Toolkit for DBA's and developers
* Copyright (C) 2003-2005 Quest Software, Inc
* Portions Copyright (C) 2005 Other Contributors
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation;  only version 2 of
* the License is valid for this program.
* 
* 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*
*      As a special exception, you have permission to link this program
*      with the Oracle Client libraries and distribute executables, as long
*      as you follow the requirements of the GNU GPL in regard to all of the
*      software in the executable aside from Oracle client libraries.
*
*      Specifically you are not permitted to link this program with the
*      Qt/UNIX, Qt/Windows or Qt Non Commercial products of TrollTech.
*      And you are not permitted to distribute binaries compiled against
*      these libraries without written consent from Quest Software, Inc.
*      Observe that this does not disallow linking to the Qt Free Edition.
*
*      You may link this product with any GPL'd Qt library such as Qt/Free
*
* All trademarks belong to their respective owners.
*
*****/

#ifndef TOSQLPARSE_H
#define TOSQLPARSE_H

#include "config.h"

#include <list>
#include <algorithm>
#include <qstring.h>

class toConnection;
class toMarkedText;
class toSyntaxAnalyzer;

/** A bunch of functions to parse and indent SQL text.
 */

class toSQLParse
{
public:
    struct settings
    {
        bool ExpandSpaces;
        bool CommaBefore;
        bool BlockOpenLine;
        bool OperatorSpace;
        bool KeywordUpper;
        bool RightSeparator;
        bool EndBlockNewline;
        int IndentLevel;
        int CommentColumn;
    };

    /** Structure the statement is parsed into.
     */
    class statement
    {
        /** Subtokens to this token.
         */
        std::list<statement> *SubTokens;
    public:
        /** Type of this token.
         */
        enum type {
            /** Start of block.
             */
            Block,
            /** Start of statement.
             */
            Statement,
            /** Sub list
             */
            List,
            /** Parameter of statement.
             */
            Keyword,
            /** Another token of whatever it is detected in.
             */
            Token,
            /** Unparsed data
             */
            Raw
            /** Type of token.
             */
        } Type;
        /** Was there a comment attached to this token
         */
        QString Comment;
        /** The actual token.
         */
        QString String;
        /** The original line of the token (0 is first line)
         */
        int Line;
        /** Create statement
         */
        statement(type ntype = Token, const QString &token = QString::null, int cline = -1);
        /** Allocate subtokens if not available and return reference to subtokens
         */
        std::list<statement> &subTokens();
        /** Copy constructor
         */
        statement(const statement &);
        /** Copy operator
         */
        const statement &operator = (const statement &);
        /** Equality operator. Does not include the line for comparison.
         */
        bool operator == (const statement &) const;
        /** Non rquality operator. Does not include the line for comparison.
         */
        bool operator != (const statement &stat) const
        {
            return !((*this) == stat);
        }
        /** Destroy statement
         */
        ~statement();
    };

    /** Abstract class to define a source of tokens for the parser.
     */
    class tokenizer
    {
    private:
        toSyntaxAnalyzer *Analyzer;
    protected:
        int Offset;
        int Line;
    public:
        /** Create a tokenizer. Optionally specify which line and offset to start at.
         */
        tokenizer(int offset = 0, int line = 0)
        {
            Line = line;
            Offset = offset;
            Analyzer = NULL;
        }
        /** Create a tokenizer. Optionally specify which line and offset to start at.
         */
        tokenizer(toSyntaxAnalyzer &analyzer, int offset = 0, int line = 0)
        {
            Line = line;
            Offset = offset;
            Analyzer = &analyzer;
        }
        virtual ~tokenizer()
        { }
        /** Get a token from the string.
         * @param forward Go forward or backwards to get next token.
         * @param comment Include comments as tokens.
         */
        virtual QString getToken(bool forward = true, bool comment = false) = 0;
        /** Specify the syntax analyzer to use for the tokanizer
         * @param analyzer
         */
        virtual void setAnalyzer(toSyntaxAnalyzer &analyzer)
        {
            Analyzer = &analyzer;
        }
        virtual toSyntaxAnalyzer &analyzer();
        /** Get the current line of the tokenizer. A line is defined by a \n character
         */
        virtual int line(void)
        {
            return Line;
        }
        /** Current offset of the tokenizer (Should point to the character after the last token in the specified direction)
         */
        virtual int offset(void)
        {
            return Offset;
        }
        /** Set a new offset of tokenzer.
         */
        virtual void setOffset(int offset)
        {
            Offset = offset;
        }
        /** Set new current line of tokenizer.
         * This will not affect the current position of the tokenizer.
         */
        virtual void setLine(int line)
        {
            Line = line;
        }
        /** Get the data remaining after the current position.
         * @param eol If true end of line, otherwise end of tokenizer.
         */
        virtual QString remaining(bool eol) = 0;
    };

    /** Tokenizer class which gets tokens from a string.
     */
class stringTokenizer : public tokenizer
    {
        QString String;
    public:
        stringTokenizer(const QString &str, int offset = 0, int line = 0)
                : tokenizer(offset, line)
        {
            String = str;
        }
        stringTokenizer(const QString &str, toSyntaxAnalyzer &analyzer, int offset = 0, int line = 0)
                : tokenizer(analyzer, offset, line)
        {
            String = str;
        }
        /** Get a token from the string.
         * @param forward Go forward or backwards to get next token.
         * @param comment Include comments as tokens.
         */
        virtual QString getToken(bool forward = true, bool comment = false);
        /** Get the data remaining after the current position.
         * @param eol If true end of line, otherwise end of tokenizer.
         */
        virtual QString remaining(bool eol);
    };

    /** Tokenizer class which gets tokens from an editor.
     */
class editorTokenizer : public tokenizer
    {
        toMarkedText *Editor;
    public:
        /** Create a tokenizer which takes its input from an editor.
         * @param editor The editor to read from. Observe that if this
         * is a syntax highlighted editor the syntax analyzer is used
         * that is used for the editor.
         */
        editorTokenizer(toMarkedText *editor, int offset = 0, int line = 0);
        /** Get a token from the string.
         * @param forward Go forward or backwards to get next token.
         * @param comment Include comments as tokens.
         */
        virtual QString getToken(bool forward = true, bool comment = false);
        /** Get the data remaining after the current position.
         * @param eol If true end of line, otherwise end of tokenizer.
         */
        virtual QString remaining(bool eol);
    };

    /** Parse a string.
     * @param tokens Tokenizer provider to generate parsed tree from.
     * @return Parsed statement tree.
     */
    static std::list<statement> parse(tokenizer &tokens);
    /** Parse a string.
     * @param str String to parse.
     * @return Parsed statement tree.
     */
    static std::list<statement> parse(const QString &str)
    {
        stringTokenizer tokens(str);
        return parse(tokens);
    }
    /** Parse a string.
     * @param str String to parse.
     * @param conn Connection to determine SQL dialect. (For future use)
     * @return Parsed statement tree.
     */
    static std::list<statement> parse(const QString &str, toConnection &conn);

    /** Get one statement (or block) from the root of an editor or string.
     * @param tokens Tokenizer to get tokens from.
     * @param conn Connection to determine SQL dialog. (For future use)
     */
    static statement parseStatement(tokenizer &tokens);
    /** Get one statement (or block) from the root of an editor or string.
     * @param str Tokenizer to get tokens from.
     * @param conn Connection to determine SQL dialog. (For future use)
     */
    static statement parseStatement(const QString &str)
    {
        stringTokenizer tokens(str);
        return parseStatement(tokens);
    }
    /** Get one statement (or block) from the root of an editor or string.
     * @param tokens Tokenizer to get tokens from.
     * @param conn Connection to determine SQL dialog. (For future use)
     */
    static statement parseStatement(tokenizer &tokens, toConnection &conn);
    /** Get one statement (or block) from the root of an editor or string.
     * @param str Tokenizer to get tokens from.
     * @param conn Connection to determine SQL dialog. (For future use)
     */
    static statement parseStatement(const QString &str, toConnection &conn)
    {
        stringTokenizer tokens(str);
        return parseStatement(tokens, conn);
    }

    /** Indent a string.
     * @param str String to indent.
     * @return An indented string.
     */
    static QString indent(const QString &str);
    /** Indent a parse statement structure into a string.
     * @param stat Statement to indent.
     * @param level Initial indentation level to use.
     * @param syntax The syntax analyzer to use.
     * @return A string with the indented statement.
     */
    static QString indentStatement(statement &stat, toConnection &conn, int level = 0);
    /** Indent a parse statement structure into a string.
     * @param stat Statement to indent.
     * @param level Initial indentation level to use.
     * @return A string with the indented statement.
     */
    static QString indentStatement(statement &stat, int level = 0);
    /** Indent a string.
     * @param str String to indent.
     * @param conn Connection to determine SQL dialect. (For future use)
     * @return An indented string.
     */
    static QString indent(const QString &str, toConnection &conn);

    /** Create an indentation string.
     * @param level Number of characters to indent.
     */
    static QString indentString(int level);

    /** Indent a string.
     * @param str List of statements
     * @param conn Connection to determine SQL dialect. (For future use)
     * @return An indented string.
     */
    static QString indent(std::list<statement> &stat, toConnection &conn);
    /** Indent a string.
     * @param stat List of statements
     * @return An indented string.
     */
    static QString indent(std::list<statement> &stat);

    /** Count indentation level of a string.
     * @param str String to check.
     * @param chars Position in string.
     */
    static int countIndent(const QString &str, int &chars);
    /** Indent a string.
     * @param str String to indent.
     * @param syntax Syntax analyzer to determine SQL dialect. (For future use)
     * @return An indented string.
     */
    static QString indent(const QString &str, toSyntaxAnalyzer &syntax);
    /** Indent a string.
     * @param stat List of statements
     * @param syntax Syntax analyzer to determine SQL dialect. (For future use)
     * @return An indented string.
     */
    static QString indent(std::list<statement> &stat, toSyntaxAnalyzer &syntax);
private:
    static settings Settings;
    static statement parseStatement(tokenizer &tokens,
                                    bool declare, bool lst);
    /** Indent a parse statement structure into a string.
     * @param stat Statement to indent.
     * @param level Initial indentation level to use.
     * @param syntax The syntax analyzer to use.
     * @return A string with the indented statement.
     */
    static QString indentStatement(statement &stat, int level, toSyntaxAnalyzer &conn);
public:
    /** Get current settings.
     */
    static settings getSetting(void)
    {
        return Settings;
    }
    /** Get current settings.
     */
    static void setSetting(const settings &setting)
    {
        Settings = setting;
    }
};

#endif