| 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 | | | package org.jcoderz.phoenix.sqlparser; |
| 34 | | | |
| 35 | | | import java.io.File; |
| 36 | | | import java.io.FileFilter; |
| 37 | | | import java.io.FileInputStream; |
| 38 | | | import java.io.FileNotFoundException; |
| 39 | | | import java.io.FileOutputStream; |
| 40 | | | import java.io.IOException; |
| 41 | | | import java.io.PrintWriter; |
| 42 | | | import java.util.ArrayList; |
| 43 | | | import java.util.Arrays; |
| 44 | | | import java.util.HashMap; |
| 45 | | | import java.util.Iterator; |
| 46 | | | import java.util.List; |
| 47 | | | import java.util.Map; |
| 48 | | | import javax.xml.bind.JAXBContext; |
| 49 | | | import javax.xml.bind.JAXBException; |
| 50 | | | import javax.xml.bind.Unmarshaller; |
| 51 | | | |
| 52 | | | import org.jcoderz.commons.util.Constants; |
| 53 | | | import org.jcoderz.commons.util.IoUtil; |
| 54 | | | import org.jcoderz.phoenix.sqlparser.jaxb.Index; |
| 55 | | | import org.jcoderz.phoenix.sqlparser.jaxb.SqlMetainf; |
| 56 | | | import org.jcoderz.phoenix.sqlparser.jaxb.Table; |
| 57 | | | |
| 58 | | | |
| 59 | | | @author |
| 60 | | | |
| 61 | | | public class SqlTransformer |
| 62 | | | { |
| 63 | | | private static final String DEFAULT_KEY = "default"; |
| 64 | | | private static final int INITIAL = 0; |
| 65 | | | private static final int IN_CREATE = 1; |
| 66 | | | private final File mInputFile; |
| 67 | | | private final File mOutputFile; |
| 68 | | | private File mMetainfFile; |
| 69 | | | private final boolean mForce; |
| 70 | | | |
| 71 | | | private int mState; |
| 72 | | | private TokenType mObjectType; |
| 73 | | | private String mObjectName; |
| 74 | | | |
| 75 | 100 | | private final Map mIndexMap = new HashMap(); |
| 76 | 100 | | private final Map mTableMap = new HashMap(); |
| 77 | | | |
| 78 | | | |
| 79 | | | |
| 80 | | | @param |
| 81 | | | @param |
| 82 | | | @param |
| 83 | | | @param |
| 84 | | | |
| 85 | | | public SqlTransformer ( |
| 86 | | | String inFile, String outFile, String metainfFile, boolean force) |
| 87 | 100 | | { |
| 88 | 100 | | mInputFile = new File(inFile); |
| 89 | 100 | | mOutputFile = new File(outFile); |
| 90 | 100 | | if (metainfFile != null) |
| 91 | | | { |
| 92 | 100 | | mMetainfFile = new File(metainfFile); |
| 93 | 100 | | parseMetainfFile(); |
| 94 | | | } |
| 95 | 100 | | mForce = force; |
| 96 | 100 | | } |
| 97 | | | |
| 98 | | | |
| 99 | | | |
| 100 | | | |
| 101 | | | public void execute () |
| 102 | | | { |
| 103 | | | try |
| 104 | | | { |
| 105 | 100 | | if (checkFiles()) |
| 106 | | | { |
| 107 | 100 | | filterComments(); |
| 108 | | | } |
| 109 | 0 | | else if (mForce) |
| 110 | | | { |
| 111 | 0 | | System.out.println("Forcing transformation..."); |
| 112 | 0 | | filterComments(); |
| 113 | | | } |
| 114 | | | else |
| 115 | | | { |
| 116 | 0 | | System.out.println( |
| 117 | | | "Output file is newer than input file, skipping."); |
| 118 | | | } |
| 119 | | | } |
| 120 | | | |
| 121 | 0 | | catch (Exception e) |
| 122 | | | { |
| 123 | 0 | | System.err.println( |
| 124 | | | "SQL Transformation failed for file " + mInputFile); |
| 125 | 0 | | throw new RuntimeException("SQL Transformation failed: " + e, e); |
| 126 | 100 | | } |
| 127 | 100 | | } |
| 128 | | | |
| 129 | | | |
| 130 | | | |
| 131 | | | @return |
| 132 | | | |
| 133 | | | @throws |
| 134 | | | |
| 135 | | | public boolean checkFiles () |
| 136 | | | throws IOException |
| 137 | | | { |
| 138 | 100 | | boolean result = true; |
| 139 | 100 | | if (! mInputFile.exists()) |
| 140 | | | { |
| 141 | 0 | | throw new IOException("Input file " + mInputFile + " does not exist."); |
| 142 | | | } |
| 143 | | | |
| 144 | 50 | | if (mOutputFile.exists() |
| 145 | | | && mInputFile.lastModified() < mOutputFile.lastModified()) |
| 146 | | | { |
| 147 | 0 | | result = false; |
| 148 | | | } |
| 149 | | | |
| 150 | 100 | | return result; |
| 151 | | | } |
| 152 | | | |
| 153 | | | |
| 154 | | | |
| 155 | | | |
| 156 | | | |
| 157 | | | @throws |
| 158 | | | @throws |
| 159 | | | |
| 160 | | | public void filterComments () throws FileNotFoundException, ParseException |
| 161 | | | { |
| 162 | | | |
| 163 | | | |
| 164 | 100 | | PrintWriter p2w = null; |
| 165 | 100 | | final FileInputStream in = new FileInputStream(mInputFile); |
| 166 | | | try |
| 167 | | | { |
| 168 | 100 | | final ScannerInterface scanner = new SqlScanner(in); |
| 169 | 100 | | scanner.setReportWhitespace(true); |
| 170 | 100 | | p2w = new PrintWriter(new FileOutputStream(mOutputFile)); |
| 171 | | | |
| 172 | | | Token token; |
| 173 | 100 | | final StringBuffer sbuf = new StringBuffer(); |
| 174 | 100 | (1) | while ((token = scanner.nextToken()).getType() != TokenType.EOF) |
| 175 | | | { |
| 176 | 100 | | parserHook(token, sbuf); |
| 177 | | | |
| 178 | 100 | | final TokenType type = token.getType(); |
| 179 | | | |
| 180 | | | |
| 181 | 100 | | if (type == TokenType.NEWLINE) |
| 182 | | | { |
| 183 | 100 | | final String s = sbuf.toString(); |
| 184 | 100 | (2) | if (! (s.trim().length() == 0)) |
| 185 | | | { |
| 186 | | | |
| 187 | 100 | | p2w.println(s); |
| 188 | | | } |
| 189 | | | else |
| 190 | | | { |
| 191 | | | |
| 192 | | | } |
| 193 | 100 | | sbuf.setLength(0); |
| 194 | 100 | | } |
| 195 | 100 | | else if (type != TokenType.COMMENT) |
| 196 | | | { |
| 197 | 100 | | sbuf.append(token.getValue()); |
| 198 | 100 | | if (type == TokenType.SEMICOLON) |
| 199 | | | { |
| 200 | | | |
| 201 | 100 | | sbuf.append('\n'); |
| 202 | | | } |
| 203 | | | } |
| 204 | 100 | | } |
| 205 | 100 | | if (! (sbuf.toString().trim().length() == 0)) |
| 206 | | | { |
| 207 | 0 | | p2w.println(sbuf.toString()); |
| 208 | | | } |
| 209 | | | } |
| 210 | | | finally |
| 211 | | | { |
| 212 | 50 | | IoUtil.close(p2w); |
| 213 | 50 | | IoUtil.close(in); |
| 214 | 100 | | } |
| 215 | 100 | | } |
| 216 | | | |
| 217 | | (3) | private void parserHook (Token token, StringBuffer out) |
| 218 | | | { |
| 219 | 100 | | final TokenType type = token.getType(); |
| 220 | 100 | | if (type == TokenType.WHITESPACE || type == TokenType.NEWLINE) |
| 221 | | | { |
| 222 | | | |
| 223 | | | } |
| 224 | 100 | | else if (type == TokenType.CREATE) |
| 225 | | | { |
| 226 | 100 | | if (mState != INITIAL) |
| 227 | | | { |
| 228 | 0 | (4) | throw new IllegalStateException("Expected state to be INITIAL"); |
| 229 | | | } |
| 230 | 100 | | mState = IN_CREATE; |
| 231 | | | } |
| 232 | 100 | | else if (type == TokenType.TABLE |
| 233 | | | || type == TokenType.INDEX) |
| 234 | | | { |
| 235 | 100 | | if (mState == IN_CREATE) |
| 236 | | | { |
| 237 | 100 | | mObjectType = type; |
| 238 | | | } |
| 239 | | | } |
| 240 | 100 | | else if (mState == IN_CREATE && mObjectType != null |
| 241 | | | && mObjectName == null) |
| 242 | | | { |
| 243 | 100 | | if (type != TokenType.IDENTIFIER) |
| 244 | | | { |
| 245 | 0 | | throw new IllegalStateException( |
| 246 | | | "Expected identifier but got " + token); |
| 247 | | | } |
| 248 | 100 | | mObjectName = token.getValue(); |
| 249 | | | } |
| 250 | 100 | | else if (type == TokenType.SEMICOLON) |
| 251 | | | { |
| 252 | 100 | | if (mState == IN_CREATE && mObjectType != null && mObjectName != null) |
| 253 | | | { |
| 254 | | | |
| 255 | | | |
| 256 | 100 | | printMetaInf(out); |
| 257 | | | } |
| 258 | 100 | | mState = INITIAL; |
| 259 | 100 | | mObjectType = null; |
| 260 | 100 | | mObjectName = null; |
| 261 | | | } |
| 262 | 100 | | } |
| 263 | | | |
| 264 | | | |
| 265 | | | |
| 266 | | | @param |
| 267 | | | |
| 268 | | | private void printMetaInf (StringBuffer out) |
| 269 | | | { |
| 270 | 100 | | if (mMetainfFile != null) |
| 271 | | | { |
| 272 | | | final String metaInf; |
| 273 | 100 | | final Map metainfMap = getMetainfMap(mObjectType); |
| 274 | | | |
| 275 | 100 | | if (metainfMap.get(mObjectName.toUpperCase( |
| 276 | | | Constants.SYSTEM_LOCALE)) == null) |
| 277 | | | { |
| 278 | 100 | | metaInf = (String) metainfMap.get(DEFAULT_KEY); |
| 279 | | | } |
| 280 | | | else |
| 281 | | | { |
| 282 | 0 | (5)(6) | metaInf = (String) metainfMap.get(mObjectName.toUpperCase()); |
| 283 | | | } |
| 284 | | | |
| 285 | 100 | | out.append('\n'); |
| 286 | 100 | | out.append(metaInf); |
| 287 | | | } |
| 288 | 100 | | } |
| 289 | | | |
| 290 | | | private Map getMetainfMap (TokenType t) |
| 291 | | | { |
| 292 | | | final Map result; |
| 293 | 100 | | if (t == TokenType.INDEX) |
| 294 | | | { |
| 295 | 100 | | result = mIndexMap; |
| 296 | | | } |
| 297 | 100 | | else if (t == TokenType.TABLE) |
| 298 | | | { |
| 299 | 100 | | result = mTableMap; |
| 300 | | | } |
| 301 | | | else |
| 302 | | | { |
| 303 | 0 | | throw new IllegalArgumentException("Illegal Token Type: " + t); |
| 304 | | | } |
| 305 | 100 | | return result; |
| 306 | | | } |
| 307 | | | |
| 308 | | | private void parseMetainfFile () |
| 309 | | | { |
| 310 | 100 | | SqlMetainf metaInf = null; |
| 311 | | | try |
| 312 | | | { |
| 313 | 100 | | final JAXBContext ctx |
| 314 | | | = JAXBContext.newInstance("org.jcoderz.phoenix.sqlparser.jaxb", |
| 315 | | | this.getClass().getClassLoader()); |
| 316 | 100 | | final Unmarshaller unmarsh = ctx.createUnmarshaller(); |
| 317 | 100 | (7) | unmarsh.setValidating(true); |
| 318 | 100 | | metaInf = (SqlMetainf) unmarsh.unmarshal(mMetainfFile); |
| 319 | | | } |
| 320 | 0 | | catch (JAXBException e) |
| 321 | | | { |
| 322 | 0 | | e.printStackTrace(); |
| 323 | 0 | (8) | System.exit(1); |
| 324 | 100 | | } |
| 325 | 100 | | mTableMap.put(DEFAULT_KEY, metaInf.getCreateTable().getDefault()); |
| 326 | 100 | | for (final Iterator it = metaInf.getCreateTable().getTable().iterator(); |
| 327 | 100 | | it.hasNext(); ) |
| 328 | | | { |
| 329 | 100 | | final Table tab = (Table) it.next(); |
| 330 | 100 | | if (mTableMap.containsKey(tab.getName())) |
| 331 | | | { |
| 332 | 0 | | throw new IllegalArgumentException( |
| 333 | | | "Table " + tab.getName() + " exists twice in " |
| 334 | | | + mMetainfFile.getName()); |
| 335 | | | } |
| 336 | 100 | | mTableMap.put(tab.getName().toUpperCase(Constants.SYSTEM_LOCALE), |
| 337 | | | tab.getValue()); |
| 338 | 100 | | } |
| 339 | | | |
| 340 | 100 | | mIndexMap.put(DEFAULT_KEY, metaInf.getCreateIndex().getDefault()); |
| 341 | 100 | | for (final Iterator it = metaInf.getCreateIndex().getIndex().iterator(); |
| 342 | 100 | | it.hasNext(); ) |
| 343 | | | { |
| 344 | 100 | | final Index ind = (Index) it.next(); |
| 345 | 100 | | if (mIndexMap.containsKey(ind.getName())) |
| 346 | | | { |
| 347 | 0 | | throw new IllegalArgumentException( |
| 348 | | | "Index " + ind.getName() + " exists twice in " |
| 349 | | | + mMetainfFile.getName()); |
| 350 | | | } |
| 351 | 100 | (9) | mIndexMap.put(ind.getName().toUpperCase(), ind.getValue()); |
| 352 | 100 | | } |
| 353 | 100 | | } |
| 354 | | | |
| 355 | | | |
| 356 | | | |
| 357 | | | @param |
| 358 | | | |
| 359 | | | public static void main (String[] args) |
| 360 | | | { |
| 361 | 100 | | final Options opts = parseCommandLine(args); |
| 362 | 100 | | checkOptions(opts); |
| 363 | | | |
| 364 | 100 | | if (opts.mUseFiles) |
| 365 | | | { |
| 366 | 100 | | final SqlTransformer filter = new SqlTransformer( |
| 367 | | | opts.mInputFile, opts.mOutputFile, opts.mMetainfFile, |
| 368 | | | opts.mForce); |
| 369 | 100 | | filter.execute(); |
| 370 | 100 | | } |
| 371 | | | else |
| 372 | | | { |
| 373 | 100 | | final List files = checkAndListFiles(opts); |
| 374 | 100 | | for (final Iterator it = files.iterator(); it.hasNext(); ) |
| 375 | | | { |
| 376 | 100 | | final File inFile = (File) it.next(); |
| 377 | 100 | | final File outFile = new File(opts.mOutDir, inFile.getName()); |
| 378 | 100 | | final SqlTransformer filter = new SqlTransformer( |
| 379 | | | inFile.getAbsolutePath(), outFile.getAbsolutePath(), |
| 380 | | | opts.mMetainfFile, opts.mForce); |
| 381 | 100 | | filter.execute(); |
| 382 | 100 | | } |
| 383 | | | } |
| 384 | 100 | | } |
| 385 | | | |
| 386 | | | private static List checkAndListFiles (Options opts) |
| 387 | | | { |
| 388 | 100 | | final File inDir = new File(opts.mInDir); |
| 389 | 100 | | checkDir(inDir); |
| 390 | 100 | | final File outDir = new File(opts.mOutDir); |
| 391 | 100 | | checkDir(outDir); |
| 392 | | | |
| 393 | 100 | | final List list = new ArrayList(); |
| 394 | 100 | | final FileFilter ff = new FileFilter() { |
| 395 | | | public boolean accept (File pathname) |
| 396 | | | { |
| 397 | | | final boolean result; |
| 398 | 100 | | if (pathname.getName().endsWith(".sql")) |
| 399 | | | { |
| 400 | 100 | | result = true; |
| 401 | | | } |
| 402 | | | else |
| 403 | | | { |
| 404 | 100 | | result = false; |
| 405 | | | } |
| 406 | 100 | | return result; |
| 407 | | | } |
| 408 | | | }; |
| 409 | 100 | | final File[] inFiles = inDir.listFiles(ff); |
| 410 | 100 | | list.addAll(Arrays.asList(inFiles)); |
| 411 | 100 | | return list; |
| 412 | | | } |
| 413 | | | |
| 414 | | | private static void checkDir (File dir) |
| 415 | | | { |
| 416 | 100 | | if (! dir.exists()) |
| 417 | | | { |
| 418 | 0 | | System.err.println("Directory " + dir + " does not exist"); |
| 419 | 0 | (10) | System.exit(1); |
| 420 | | | } |
| 421 | 100 | | } |
| 422 | | | |
| 423 | | | |
| 424 | | | @param |
| 425 | | | |
| 426 | | | private static void checkOptions (final Options opts) |
| 427 | | | { |
| 428 | 100 | | if (opts.mUseFiles && opts.mUseDirs) |
| 429 | | | { |
| 430 | 0 | | System.err.println("Specify either '-i' and '-o' options " |
| 431 | | | + "or '-d' and '-t' options."); |
| 432 | 0 | | usage(); |
| 433 | | | } |
| 434 | | | |
| 435 | 100 | | if (opts.mUseFiles) |
| 436 | | | { |
| 437 | 100 | | if (opts.mInputFile == null || opts.mOutputFile == null) |
| 438 | | | { |
| 439 | 0 | | usage(); |
| 440 | | | } |
| 441 | | | } |
| 442 | 100 | | else if (opts.mUseDirs) |
| 443 | | | { |
| 444 | 100 | | if (opts.mInDir == null || opts.mOutDir == null) |
| 445 | | | { |
| 446 | 0 | | usage(); |
| 447 | | | } |
| 448 | | | } |
| 449 | | | else |
| 450 | | | { |
| 451 | 0 | | usage(); |
| 452 | | | } |
| 453 | 100 | | } |
| 454 | | | |
| 455 | | | private static Options parseCommandLine (String[] args) |
| 456 | | | { |
| 457 | 100 | | final Options opts = new Options(); |
| 458 | 100 | | int i = 0; |
| 459 | | | try |
| 460 | | | { |
| 461 | 100 | | for (i = 0; i < args.length; i++) |
| 462 | | | { |
| 463 | 100 | (11) | if (args[i].equals("-i")) |
| 464 | | | { |
| 465 | 100 | | opts.mInputFile = args[++i]; |
| 466 | 100 | | opts.mUseFiles = true; |
| 467 | | | } |
| 468 | 100 | (12) | else if (args[i].equals("-o")) |
| 469 | | | { |
| 470 | 100 | | opts.mOutputFile = args[++i]; |
| 471 | 100 | | opts.mUseFiles = true; |
| 472 | | | } |
| 473 | 100 | (13) | else if (args[i].equals("-d")) |
| 474 | | | { |
| 475 | 100 | | opts.mInDir = args[++i]; |
| 476 | 100 | | opts.mUseDirs = true; |
| 477 | | | } |
| 478 | 100 | (14) | else if (args[i].equals("-t")) |
| 479 | | | { |
| 480 | 100 | | opts.mOutDir = args[++i]; |
| 481 | 100 | | opts.mUseDirs = true; |
| 482 | | | } |
| 483 | 100 | (15) | else if (args[i].equals("-f")) |
| 484 | | | { |
| 485 | 100 | | opts.mForce = true; |
| 486 | | | } |
| 487 | 100 | (16) | else if (args[i].equals("-m")) |
| 488 | | | { |
| 489 | 100 | | opts.mMetainfFile = args[++i]; |
| 490 | | | } |
| 491 | | | else |
| 492 | | | { |
| 493 | 0 | | usage(); |
| 494 | | | } |
| 495 | | | } |
| 496 | | | } |
| 497 | 0 | | catch (ArrayIndexOutOfBoundsException x) |
| 498 | | | { |
| 499 | 0 | | System.err.println("Error: argument " |
| 500 | | | + args[i - 1] + " requires an option"); |
| 501 | 0 | | usage(); |
| 502 | 100 | | } |
| 503 | 100 | | return opts; |
| 504 | | | } |
| 505 | | | |
| 506 | | | private static void usage () |
| 507 | | | { |
| 508 | 0 | | System.err.println("Usage: SqlCommentFilter"); |
| 509 | 0 | | System.err.println(" -i <input_file> ... input file to transform"); |
| 510 | 0 | | System.err.println(" -o <output_file> ... output file to write to"); |
| 511 | 0 | | System.err.println(" -d <input_dir> ... transform all files from" |
| 512 | | | + " directory"); |
| 513 | 0 | | System.err.println(" -t <to_dir> ... write files to directory"); |
| 514 | 0 | | System.err.println(" -m <metainf_file> ... use sql metainf file"); |
| 515 | 0 | | System.err.println(" -f ... force transformation even" |
| 516 | | | + " if input file is newer"); |
| 517 | 0 | | System.err.println("Note: you can either give '-i' and '-o' or "); |
| 518 | 0 | | System.err.println(" '-d' and '-t'"); |
| 519 | 0 | (17) | System.exit(1); |
| 520 | 0 | | } |
| 521 | | | |
| 522 | 100 | (18) | private static class Options |
| 523 | | | { |
| 524 | 100 | (19) | private String mInputFile = null; |
| 525 | 100 | (20) | private String mOutputFile = null; |
| 526 | 100 | (21) | private boolean mUseFiles = false; |
| 527 | | | |
| 528 | 100 | (22) | private String mInDir = null; |
| 529 | 100 | (23) | private String mOutDir = null; |
| 530 | 100 | (24) | private boolean mUseDirs = false; |
| 531 | | | |
| 532 | 100 | (25) | private String mMetainfFile = null; |
| 533 | 100 | (26) | private boolean mForce = false; |
| 534 | | | } |
| 535 | | | } |