1 16 package scriptella.jdbc; 17 18 import java.io.IOException ; 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 30 final class CachedSqlTokenizer implements SqlTokenizer { 31 private SqlTokenizer target; 32 private List <String > statements; 33 private String [] statementsArray; private List <int[]> injections; 35 private int[][] injectionsArray; private int index=-1; 37 38 39 44 public CachedSqlTokenizer(SqlTokenizer target) { 45 this.target = target; 46 } 47 48 public String nextStatement() throws IOException { 49 if (target != null) { 50 String st = target.nextStatement(); 51 if (st != null) { 52 if (statements == null) { 53 statements = new ArrayList <String >(); 54 injections = new ArrayList <int[]>(); 55 } 56 statements.add(st); 57 } 58 return st; 59 } else { 60 index++; 61 if (statementsArray == null || index >= statementsArray.length) { 62 return null; 63 } 64 return statementsArray[index]; 65 } 66 } 67 68 73 public int[] getInjections() { 74 if (target != null) { 75 int[] inj = target.getInjections(); 76 injections.add(inj); 77 return inj; 78 } else { 79 if (injectionsArray != null && index<injectionsArray.length) { 80 return injectionsArray[index]; 81 } 82 return null; 83 } 84 } 85 86 96 public void close() throws IOException { 97 if (target != null) { 98 if (statements!=null) { 99 statementsArray=statements.toArray(new String [statements.size()]); 100 injectionsArray=injections.toArray(new int[injections.size()][]); 101 statements=null; 102 injections=null; 103 } 104 try { 105 target.close(); 106 } finally { 107 target = null; } 109 } else { 110 index = -1; 111 } 112 113 } 114 115 } 116 | Popular Tags |