| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.database.sqlite; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
|
| 22 |
import java.util.ArrayList; |
| 23 |
import java.util.List; |
| 24 |
import java.util.Locale; |
| 25 |
import java.util.function.Consumer; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class SQLiteTokenizer { |
| 40 |
private static boolean isAlpha(char ch) { |
| 41 |
return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || (ch == '_'); |
| 42 |
} |
| 43 |
|
| 44 |
private static boolean isNum(char ch) { |
| 45 |
return ('0' <= ch && ch <= '9'); |
| 46 |
} |
| 47 |
|
| 48 |
private static boolean isAlNum(char ch) { |
| 49 |
return isAlpha(ch) || isNum(ch); |
| 50 |
} |
| 51 |
|
| 52 |
private static boolean isAnyOf(char ch, String set) { |
| 53 |
return set.indexOf(ch) >= 0; |
| 54 |
} |
| 55 |
|
| 56 |
private static IllegalArgumentException genException(String message, String sql) { |
| 57 |
throw new IllegalArgumentException(message + " in '" + sql + "'"); |
| 58 |
} |
| 59 |
|
| 60 |
private static char peek(String s, int index) { |
| 61 |
return index < s.length() ? s.charAt(index) : '\0'; |
| 62 |
} |
| 63 |
|
| 64 |
public static final int OPTION_NONE = 0; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public static final int OPTION_TOKEN_ONLY = 1 << 0; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public static List<String> tokenize(@Nullable String sql, int options) { |
| 78 |
final ArrayList<String> res = new ArrayList<>(); |
| 79 |
tokenize(sql, options, res::add); |
| 80 |
return res; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public static void tokenize(@Nullable String sql, int options, Consumer<String> checker) { |
| 90 |
if (sql == null) { |
| 91 |
return; |
| 92 |
} |
| 93 |
int pos = 0; |
| 94 |
final int len = sql.length(); |
| 95 |
while (pos < len) { |
| 96 |
final char ch = peek(sql, pos); |
| 97 |
|
| 98 |
|
| 99 |
if (isAlpha(ch)) { |
| 100 |
final int start = pos; |
| 101 |
pos++; |
| 102 |
while (isAlNum(peek(sql, pos))) { |
| 103 |
pos++; |
| 104 |
} |
| 105 |
final int end = pos; |
| 106 |
|
| 107 |
final String token = sql.substring(start, end); |
| 108 |
checker.accept(token); |
| 109 |
|
| 110 |
continue; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
if (isAnyOf(ch, "'\"`")) { |
| 115 |
final int quoteStart = pos; |
| 116 |
pos++; |
| 117 |
|
| 118 |
for (;;) { |
| 119 |
pos = sql.indexOf(ch, pos); |
| 120 |
if (pos < 0) { |
| 121 |
throw genException("Unterminated quote", sql); |
| 122 |
} |
| 123 |
if (peek(sql, pos + 1) != ch) { |
| 124 |
break; |
| 125 |
} |
| 126 |
|
| 127 |
pos += 2; |
| 128 |
} |
| 129 |
final int quoteEnd = pos; |
| 130 |
pos++; |
| 131 |
|
| 132 |
if (ch != '\'') { |
| 133 |
|
| 134 |
final String tokenUnquoted = sql.substring(quoteStart + 1, quoteEnd); |
| 135 |
|
| 136 |
final String token; |
| 137 |
|
| 138 |
|
| 139 |
if (tokenUnquoted.indexOf(ch) >= 0) { |
| 140 |
token = tokenUnquoted.replaceAll( |
| 141 |
String.valueOf(ch) + ch, String.valueOf(ch)); |
| 142 |
} else { |
| 143 |
token = tokenUnquoted; |
| 144 |
} |
| 145 |
checker.accept(token); |
| 146 |
} else { |
| 147 |
if ((options &= OPTION_TOKEN_ONLY) != 0) { |
| 148 |
throw genException("Non-token detected", sql); |
| 149 |
} |
| 150 |
} |
| 151 |
continue; |
| 152 |
} |
| 153 |
|
| 154 |
if (ch == '[') { |
| 155 |
final int quoteStart = pos; |
| 156 |
pos++; |
| 157 |
|
| 158 |
pos = sql.indexOf(']', pos); |
| 159 |
if (pos < 0) { |
| 160 |
throw genException("Unterminated quote", sql); |
| 161 |
} |
| 162 |
final int quoteEnd = pos; |
| 163 |
pos++; |
| 164 |
|
| 165 |
final String token = sql.substring(quoteStart + 1, quoteEnd); |
| 166 |
|
| 167 |
checker.accept(token); |
| 168 |
continue; |
| 169 |
} |
| 170 |
if ((options &= OPTION_TOKEN_ONLY) != 0) { |
| 171 |
throw genException("Non-token detected", sql); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
if (ch == '-' && peek(sql, pos + 1) == '-') { |
| 176 |
pos += 2; |
| 177 |
pos = sql.indexOf('\n', pos); |
| 178 |
if (pos < 0) { |
| 179 |
|
| 180 |
throw genException("Unterminated comment", sql); |
| 181 |
} |
| 182 |
pos++; |
| 183 |
|
| 184 |
continue; |
| 185 |
} |
| 186 |
if (ch == '/' && peek(sql, pos + 1) == '*') { |
| 187 |
pos += 2; |
| 188 |
pos = sql.indexOf("*/", pos); |
| 189 |
if (pos < 0) { |
| 190 |
throw genException("Unterminated comment", sql); |
| 191 |
} |
| 192 |
pos += 2; |
| 193 |
|
| 194 |
continue; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
if (ch == ';') { |
| 199 |
throw genException("Semicolon is not allowed", sql); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
pos++; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public static boolean isKeyword(@NonNull String token) { |
| 215 |
switch (token.toUpperCase(Locale.US)) { |
| 216 |
case "ABORT": case "ACTION": case "ADD": case "AFTER": |
| 217 |
case "ALL": case "ALTER": case "ANALYZE": case "AND": |
| 218 |
case "AS": case "ASC": case "ATTACH": case "AUTOINCREMENT": |
| 219 |
case "BEFORE": case "BEGIN": case "BETWEEN": case "BINARY": |
| 220 |
case "BY": case "CASCADE": case "CASE": case "CAST": |
| 221 |
case "CHECK": case "COLLATE": case "COLUMN": case "COMMIT": |
| 222 |
case "CONFLICT": case "CONSTRAINT": case "CREATE": case "CROSS": |
| 223 |
case "CURRENT": case "CURRENT_DATE": case "CURRENT_TIME": case "CURRENT_TIMESTAMP": |
| 224 |
case "DATABASE": case "DEFAULT": case "DEFERRABLE": case "DEFERRED": |
| 225 |
case "DELETE": case "DESC": case "DETACH": case "DISTINCT": |
| 226 |
case "DO": case "DROP": case "EACH": case "ELSE": |
| 227 |
case "END": case "ESCAPE": case "EXCEPT": case "EXCLUDE": |
| 228 |
case "EXCLUSIVE": case "EXISTS": case "EXPLAIN": case "FAIL": |
| 229 |
case "FILTER": case "FOLLOWING": case "FOR": case "FOREIGN": |
| 230 |
case "FROM": case "FULL": case "GLOB": case "GROUP": |
| 231 |
case "GROUPS": case "HAVING": case "IF": case "IGNORE": |
| 232 |
case "IMMEDIATE": case "IN": case "INDEX": case "INDEXED": |
| 233 |
case "INITIALLY": case "INNER": case "INSERT": case "INSTEAD": |
| 234 |
case "INTERSECT": case "INTO": case "IS": case "ISNULL": |
| 235 |
case "JOIN": case "KEY": case "LEFT": case "LIKE": |
| 236 |
case "LIMIT": case "MATCH": case "NATURAL": case "NO": |
| 237 |
case "NOCASE": case "NOT": case "NOTHING": case "NOTNULL": |
| 238 |
case "NULL": case "OF": case "OFFSET": case "ON": |
| 239 |
case "OR": case "ORDER": case "OTHERS": case "OUTER": |
| 240 |
case "OVER": case "PARTITION": case "PLAN": case "PRAGMA": |
| 241 |
case "PRECEDING": case "PRIMARY": case "QUERY": case "RAISE": |
| 242 |
case "RANGE": case "RECURSIVE": case "REFERENCES": case "REGEXP": |
| 243 |
case "REINDEX": case "RELEASE": case "RENAME": case "REPLACE": |
| 244 |
case "RESTRICT": case "RIGHT": case "ROLLBACK": case "ROW": |
| 245 |
case "ROWS": case "RTRIM": case "SAVEPOINT": case "SELECT": |
| 246 |
case "SET": case "TABLE": case "TEMP": case "TEMPORARY": |
| 247 |
case "THEN": case "TIES": case "TO": case "TRANSACTION": |
| 248 |
case "TRIGGER": case "UNBOUNDED": case "UNION": case "UNIQUE": |
| 249 |
case "UPDATE": case "USING": case "VACUUM": case "VALUES": |
| 250 |
case "VIEW": case "VIRTUAL": case "WHEN": case "WHERE": |
| 251 |
case "WINDOW": case "WITH": case "WITHOUT": |
| 252 |
return true; |
| 253 |
default: |
| 254 |
return false; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
public static boolean isFunction(@NonNull String token) { |
| 264 |
switch (token.toLowerCase(Locale.US)) { |
| 265 |
case "abs": case "avg": case "char": case "coalesce": |
| 266 |
case "count": case "glob": case "group_concat": case "hex": |
| 267 |
case "ifnull": case "instr": case "length": case "like": |
| 268 |
case "likelihood": case "likely": case "lower": case "ltrim": |
| 269 |
case "max": case "min": case "nullif": case "random": |
| 270 |
case "randomblob": case "replace": case "round": case "rtrim": |
| 271 |
case "substr": case "sum": case "total": case "trim": |
| 272 |
case "typeof": case "unicode": case "unlikely": case "upper": |
| 273 |
case "zeroblob": |
| 274 |
return true; |
| 275 |
default: |
| 276 |
return false; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
public static boolean isType(@NonNull String token) { |
| 285 |
switch (token.toUpperCase(Locale.US)) { |
| 286 |
case "INT": case "INTEGER": case "TINYINT": case "SMALLINT": |
| 287 |
case "MEDIUMINT": case "BIGINT": case "INT2": case "INT8": |
| 288 |
case "CHARACTER": case "VARCHAR": case "NCHAR": case "NVARCHAR": |
| 289 |
case "TEXT": case "CLOB": case "BLOB": case "REAL": |
| 290 |
case "DOUBLE": case "FLOAT": case "NUMERIC": case "DECIMAL": |
| 291 |
case "BOOLEAN": case "DATE": case "DATETIME": |
| 292 |
return true; |
| 293 |
default: |
| 294 |
return false; |
| 295 |
} |
| 296 |
} |
| 297 |
} |