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 @ 43983

History | View | Annotate | Download (5.43 KB)

1
package org.gvsig.expressionevaluator.impl;
2

    
3
import org.gvsig.expressionevaluator.spi.AbstractLexicalAnalyzer;
4
import org.gvsig.expressionevaluator.LexicalAnalyzer;
5

    
6
public class SQLLexicalAnalyzer extends AbstractLexicalAnalyzer {
7

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

    
24
    public SQLLexicalAnalyzer() {
25
        this(null);
26
    }
27

    
28
    @Override
29
    public LexicalAnalyzer clone() throws CloneNotSupportedException {
30
        // As this implementation does not add state to the abstract class, we 
31
        // just call the super class.
32
        SQLLexicalAnalyzer other = (SQLLexicalAnalyzer) super.clone();
33

    
34
        return other;
35
    }
36

    
37
    
38
    @Override
39
    protected Token getToken() {
40
        skipblanks();
41
        char ch = getch();
42
        switch( ch ) {
43
        case EOF:
44
            token.set(Token.EOF, null, null);
45
            return token;
46
        case '~':
47
            token.set(Token.OP_REGEXP, "~");
48
            return token;
49
        case '*':
50
            token.set(Token.OP_MULT, "*");
51
            return token;
52
        case '/':
53
            token.set(Token.OP_DIV, "/");
54
            return token;
55
        case '%':
56
            token.set(Token.OP_MOD, "%");
57
            return token;
58
        case '=':
59
            token.set(Token.OP_EQ, "=");
60
            return token;
61

    
62
        case '<':
63
            ch = getch();
64
            switch( ch ) {
65
            case '>':
66
                token.set(Token.OP_NE, "<>");
67
                return token;
68
            case '=':
69
                token.set(Token.OP_LE, "<=");
70
                return token;
71
            }
72
            ungetch();
73
            token.set(Token.OP_LT, "<");
74
            return token;
75

    
76
        case '>':
77
            ch = getch();
78
            if( ch == '=' ) {
79
                token.set(Token.OP_GE, ">=");
80
                return token;
81
            }
82
            ungetch();
83
            token.set(Token.OP_GT, ">");
84
            return token;
85

    
86
        case '.': // SQL Extension to access object methods and attributes
87
            token.set(Token.OP_GETATTR, ".");
88
            return token;
89
        case ',':
90
            token.set(Token.COMA, ",");
91
            return token;
92
        case '(':
93
            token.set(Token.PARENTHESIS_OPEN, "(");
94
            return token;
95
        case ')':
96
            token.set(Token.PARENTHESIS_CLOSE, ")");
97
            return token;
98
        case '+':
99
            ch = getch();
100
            if( !Character.isDigit(ch) ) {
101
                ungetch();
102
                token.set(Token.OP_ADD, "+");
103
                return token;
104
            }
105
            ungetch();
106
            parseNumber();
107
            token.setLiteral("+" + token.getLiteral());
108
            return token;
109
        case '-':
110
            ch = getch();
111
            if( ch=='>' ) {
112
                // SQL Extension to access object methods and attributes
113
                token.set(Token.OP_GETATTR, "->");
114
                return token;
115
            }
116
            if( Character.isDigit(ch) ) {
117
                ungetch();
118
                ungetch();
119
                parseNumber();
120
                return token;
121
            }
122
            token.set(Token.OP_SUBST, "-");
123
            return token;
124

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

    
141
        case '[':
142
            buffer.clear();
143
            ch = getch();
144
            while( ch != ']' ) {
145
                if( ch == EOF ) {
146
                    throw new RuntimeException("Found end of source and expected end of string");
147
                }
148
                buffer.add(ch);
149
                ch = getch();
150
            }
151
            if( buffer.length() < 1 ) {
152
                throw new RuntimeException();
153
            }
154
            token.set(Token.IDENTIFIER, buffer.toString());
155
            return token;
156

    
157
        case '\'':
158
            parseString();
159
            return token;
160
        }
161
        if( Character.isDigit(ch) ) {
162
            ungetch();
163
            parseNumber();
164
            return token;
165
        }
166
        if( Character.isAlphabetic(ch) ) {
167
            buffer.clear();
168
            while( Character.isLetterOrDigit(ch) || ch == '_' ) {
169
                buffer.add(ch);
170
                ch = getch();
171
            }
172
            if( !isEOF() ) {
173
                ungetch();
174
            }
175
            String id = buffer.toString();
176
            int type = this.tokens.getOrDefault(id.toLowerCase(), Token.IDENTIFIER);
177
            token.set(type, id);
178
            return token;
179
        }
180

    
181
        token.set(Token.EOF, null, null);
182
        return token;
183
    }
184

    
185
}