Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.impl / src / main / java / org / gvsig / expressionevaluator / impl / SQLLexicalAnalyzer.java @ 43532

History | View | Annotate | Download (4.63 KB)

1
package org.gvsig.expressionevaluator.impl;
2

    
3
public class SQLLexicalAnalyzer extends AbstractLexicalAnalyzer {
4

    
5
    public SQLLexicalAnalyzer(String source) {
6
        super(source);
7
        this.tokens.put("null", Token.NULL);
8
        this.tokens.put("true", Token.TRUE);
9
        this.tokens.put("false", Token.FALSE);
10
        this.tokens.put("not", Token.OP_NOT);
11
        this.tokens.put("like", Token.PRED_LIKE);
12
        this.tokens.put("ilike", Token.PRED_ILIKE);
13
        this.tokens.put("is", Token.PRED_IS);
14
        this.tokens.put("between", Token.PRED_BETWEEN);
15
        this.tokens.put("and", Token.OP_AND);
16
        this.tokens.put("or", Token.OP_OR);
17
        this.tokens.put("isnull", Token.ISNULL);
18
        this.tokens.put("notnull", Token.NOTNULL);
19
    }
20

    
21
    public SQLLexicalAnalyzer() {
22
        this(null);
23
    }
24

    
25
    @Override
26
    protected Token getToken() {
27
        skipblanks();
28
        char ch = getch();
29
        switch( ch ) {
30
        case EOF:
31
            token.set(Token.EOF, null, null);
32
            return token;
33
        case '~':
34
            token.set(Token.OP_REGEXP, "~");
35
            return token;
36
        case '*':
37
            token.set(Token.OP_MULT, "*");
38
            return token;
39
        case '/':
40
            token.set(Token.OP_DIV, "/");
41
            return token;
42
        case '%':
43
            token.set(Token.OP_MOD, "%");
44
            return token;
45
        case '=':
46
            token.set(Token.OP_EQ, "=");
47
            return token;
48

    
49
        case '<':
50
            ch = getch();
51
            switch( ch ) {
52
            case '>':
53
                token.set(Token.OP_NE, "<>");
54
                return token;
55
            case '=':
56
                token.set(Token.OP_LE, "<=");
57
                return token;
58
            }
59
            ungetch();
60
            token.set(Token.OP_LT, "<");
61
            return token;
62

    
63
        case '>':
64
            ch = getch();
65
            if( ch == '=' ) {
66
                token.set(Token.OP_GE, ">=");
67
                return token;
68
            }
69
            ungetch();
70
            token.set(Token.OP_GT, ">");
71
            return token;
72

    
73
        case ',':
74
            token.set(Token.COMA, ",");
75
            return token;
76
        case '(':
77
            token.set(Token.PARENTHESIS_OPEN, "(");
78
            return token;
79
        case ')':
80
            token.set(Token.PARENTHESIS_CLOSE, ")");
81
            return token;
82
        case '+':
83
            ch = getch();
84
            if( !Character.isDigit(ch) ) {
85
                ungetch();
86
                token.set(Token.OP_ADD, "+");
87
                return token;
88
            }
89
            ungetch();
90
            parseNumber();
91
            token.literal = "+" + token.literal;
92
            return token;
93
        case '-':
94
            ch = getch();
95
            if( Character.isDigit(ch) ) {
96
                ungetch();
97
                ungetch();
98
                parseNumber();
99
                return token;
100
            }
101
            token.set(Token.OP_SUBST, "-");
102
            return token;
103

    
104
        case '"':
105
            buffer.clear();
106
            ch = getch();
107
            while( ch != '"' ) {
108
                if( ch == EOF ) {
109
                    throw new RuntimeException("Found end of source and expected end of string");
110
                }
111
                buffer.add(ch);
112
                ch = getch();
113
            }
114
            if( buffer.length() < 1 ) {
115
                throw new RuntimeException();
116
            }
117
            token.set(Token.IDENTIFIER, buffer.toString());
118
            return token;
119

    
120
        case '[':
121
            buffer.clear();
122
            ch = getch();
123
            while( ch != ']' ) {
124
                if( ch == EOF ) {
125
                    throw new RuntimeException("Found end of source and expected end of string");
126
                }
127
                buffer.add(ch);
128
                ch = getch();
129
            }
130
            if( buffer.length() < 1 ) {
131
                throw new RuntimeException();
132
            }
133
            token.set(Token.IDENTIFIER, buffer.toString());
134
            return token;
135

    
136
        case '\'':
137
            parseString();
138
            return token;
139
        }
140
        if( Character.isDigit(ch) ) {
141
            ungetch();
142
            parseNumber();
143
            return token;
144
        }
145
        if( Character.isAlphabetic(ch) ) {
146
            buffer.clear();
147
            while( Character.isLetterOrDigit(ch) || ch == '_' ) {
148
                buffer.add(ch);
149
                ch = getch();
150
            }
151
            ungetch();
152
            String id = buffer.toString();
153
            int type = this.tokens.getOrDefault(id.toLowerCase(), Token.IDENTIFIER);
154
            token.set(type, id);
155
            return token;
156
        }
157

    
158
        token.set(Token.EOF, null, null);
159
        return token;
160
    }
161

    
162
}