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

History | View | Annotate | Download (5.32 KB)

1
package org.gvsig.expressionevaluator.impl;
2

    
3
import org.gvsig.expressionevaluator.LexicalAnalyzer;
4

    
5
public class SQLLexicalAnalyzer extends AbstractLexicalAnalyzer {
6

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

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

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

    
33
        return other;
34
    }
35

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

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

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

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

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

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

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

    
178
        token.set(Token.EOF, null, null);
179
        return token;
180
    }
181

    
182
}