| 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.FileInputStream; |
| 37 | | | import java.io.FileNotFoundException; |
| 38 | | | import java.io.FileOutputStream; |
| 39 | | | import java.io.PrintWriter; |
| 40 | | | import java.util.Iterator; |
| 41 | | | import java.util.List; |
| 42 | | | |
| 43 | | | import org.jcoderz.commons.util.IoUtil; |
| 44 | | | |
| 45 | | | |
| 46 | | | @author |
| 47 | | | |
| 48 | | | public class SqlToXml |
| 49 | | | { |
| 50 | | | private final File mInputFile; |
| 51 | | | private final File mOutputFile; |
| 52 | | | |
| 53 | | (1) | public SqlToXml (String inFileName, String outFileName) |
| 54 | 100 | | { |
| 55 | 100 | | mInputFile = new File(inFileName); |
| 56 | 100 | | mOutputFile = new File(outFileName); |
| 57 | 100 | | } |
| 58 | | | |
| 59 | | (2) | public void transformSqlToXml () |
| 60 | | | throws FileNotFoundException, ParseException |
| 61 | | | { |
| 62 | 100 | | final ScannerInterface scanner |
| 63 | | | = new SqlScanner(new FileInputStream(mInputFile)); |
| 64 | 100 | | final SqlParser parser = new SqlParser(scanner); |
| 65 | 100 | | final PrintWriter pw = new PrintWriter(new FileOutputStream(mOutputFile)); |
| 66 | | | try |
| 67 | | | { |
| 68 | 100 | | pw.println("<tables>"); |
| 69 | | | |
| 70 | 100 | | final List statements = parser.parse(); |
| 71 | 100 | | for (final Iterator it = statements.iterator(); it.hasNext(); ) |
| 72 | | | { |
| 73 | 100 | | final SqlStatement stmt = (SqlStatement) it.next(); |
| 74 | 100 | (3) | if (stmt instanceof CreateTableStatement) |
| 75 | | | { |
| 76 | 100 | | transformStatementToXml((CreateTableStatement) stmt, pw); |
| 77 | | | } |
| 78 | 0 | | else if (stmt instanceof CreateSequenceStatement) |
| 79 | | | { |
| 80 | 0 | | transformStatementToXml((CreateSequenceStatement) stmt, pw); |
| 81 | | | } |
| 82 | 100 | | } |
| 83 | | | |
| 84 | 100 | | pw.println("</tables>"); |
| 85 | | | } |
| 86 | | | finally |
| 87 | | | { |
| 88 | 50 | | IoUtil.close(pw); |
| 89 | 100 | | } |
| 90 | 100 | | } |
| 91 | | | |
| 92 | | | private void transformStatementToXml (CreateSequenceStatement stmt, |
| 93 | | | PrintWriter pw) |
| 94 | | | { |
| 95 | 0 | (4) | pw.println("<sequence name=\"" + stmt.getName() + "\">"); |
| 96 | 0 | | pw.println(" <desc>"); |
| 97 | 0 | | pw.println(stmt.getAnnotation() != null ? stmt.getAnnotation() : ""); |
| 98 | 0 | | pw.println(" </desc>"); |
| 99 | 0 | | pw.println("</sequence>"); |
| 100 | 0 | | } |
| 101 | | | |
| 102 | | (5)(6)(7) | private void transformStatementToXml (CreateTableStatement stmt, |
| 103 | | | PrintWriter out) |
| 104 | | | { |
| 105 | 100 | | out.println("<table name=\"" + stmt.getTableName() + "\">"); |
| 106 | | | |
| 107 | 100 | | if (stmt.getAnnotation() != null) |
| 108 | | | { |
| 109 | 0 | | out.println(stmt.getAnnotation()); |
| 110 | | | } |
| 111 | | | |
| 112 | 100 | | for (final Iterator it = stmt.getColumns().iterator(); it.hasNext(); ) |
| 113 | | | { |
| 114 | 100 | | final ColumnSpec column = (ColumnSpec) it.next(); |
| 115 | 100 | | out.print(" <column " |
| 116 | | | + "name=\"" + column.getColumnName() + "\" " |
| 117 | | | + "type=\"" + column.getColumnType()); |
| 118 | 100 | | StringBuffer sbuf = null; |
| 119 | 100 | | for (final Iterator it2 = column.getDatatypeAttributes().iterator(); |
| 120 | 100 | | it2.hasNext(); ) |
| 121 | | | { |
| 122 | 100 | | final ColumnAttribute attr = (ColumnAttribute) it2.next(); |
| 123 | 100 | | if (sbuf == null) |
| 124 | | | { |
| 125 | 100 | | sbuf = new StringBuffer(); |
| 126 | 100 | | sbuf.append('('); |
| 127 | | | } |
| 128 | 0 | | else if (attr instanceof StringAttribute) |
| 129 | | | { |
| 130 | 0 | | sbuf.append(' '); |
| 131 | | | } |
| 132 | | | else |
| 133 | | | { |
| 134 | 0 | | sbuf.append(','); |
| 135 | | | } |
| 136 | 100 | | sbuf.append(attr.getValue()); |
| 137 | 100 | | } |
| 138 | 100 | | if (sbuf != null) |
| 139 | | | { |
| 140 | 100 | | sbuf.append(')'); |
| 141 | 100 | | out.print(sbuf); |
| 142 | | | } |
| 143 | | | |
| 144 | 100 | | out.println("\">"); |
| 145 | 100 | | if (column.getAnnotation() != null) |
| 146 | | | { |
| 147 | 0 | | out.println(column.getAnnotation()); |
| 148 | | | } |
| 149 | 100 | | if (column.isNotNull()) |
| 150 | | | { |
| 151 | 100 | | out.println(" <notnull/>"); |
| 152 | | | } |
| 153 | 100 | | if (column.isPrimaryKey()) |
| 154 | | | { |
| 155 | 100 | | out.println(" <primarykey/>"); |
| 156 | | | } |
| 157 | 100 | | if (column.isUnique()) |
| 158 | | | { |
| 159 | 0 | | out.println(" <unique/>"); |
| 160 | | | } |
| 161 | 100 | | out.println(" </column>"); |
| 162 | 100 | | } |
| 163 | | | |
| 164 | 100 | | for (final Iterator it = stmt.getFkConstraints().iterator(); |
| 165 | 100 | | it.hasNext(); ) |
| 166 | | | { |
| 167 | 0 | | final FkConstraint fk = (FkConstraint) it.next(); |
| 168 | 0 | | out.println(" <fk name=\"" + fk.getName() + "\"" |
| 169 | | | + " references=\"" + fk.getRefTable() + "\">"); |
| 170 | 0 | | out.println(" <columns>"); |
| 171 | 0 | | for (final Iterator it2 = fk.getColumns().iterator(); it2.hasNext(); ) |
| 172 | | | { |
| 173 | 0 | | out.println(" <col>" + it2.next() + "</col>"); |
| 174 | | | } |
| 175 | 0 | | out.println(" </columns>"); |
| 176 | 0 | | out.println(" <refcolumns>"); |
| 177 | 0 | | for (final Iterator it2 = fk.getRefColumns().iterator(); |
| 178 | 0 | | it2.hasNext(); ) |
| 179 | | | { |
| 180 | 0 | | out.println(" <col>" + it2.next() + "</col>"); |
| 181 | | | } |
| 182 | 0 | | out.println(" </refcolumns>"); |
| 183 | 0 | | out.println(" </fk>"); |
| 184 | 0 | | } |
| 185 | | | |
| 186 | 100 | | for (final Iterator it = stmt.getIndexes().iterator(); it.hasNext(); ) |
| 187 | | | { |
| 188 | 100 | | final CreateIndexStatement idxStmt = (CreateIndexStatement) it.next(); |
| 189 | 100 | | out.println(" <index name=\"" + idxStmt.getIndexName() + "\">"); |
| 190 | 100 | | if (idxStmt.isUnique()) |
| 191 | | | { |
| 192 | 100 | | out.println(" <unique/>"); |
| 193 | | | } |
| 194 | 100 | | out.println(" <desc>"); |
| 195 | 75 | | out.println( |
| 196 | | | idxStmt.getAnnotation() != null ? idxStmt.getAnnotation() : ""); |
| 197 | 100 | | out.println(" </desc>"); |
| 198 | 100 | | for (final Iterator it2 = idxStmt.getColumnNames().iterator(); |
| 199 | 100 | | it2.hasNext(); ) |
| 200 | | | { |
| 201 | 100 | | out.println(" <column name=\"" + it2.next() + "\"/>"); |
| 202 | | | } |
| 203 | 100 | | out.println(" </index>"); |
| 204 | 100 | | } |
| 205 | 100 | | out.println("</table>"); |
| 206 | 100 | | } |
| 207 | | | |
| 208 | | (8) | public static void main (String[] args) |
| 209 | | | { |
| 210 | 0 | | String inputFile = null; |
| 211 | 0 | | String outputFile = null; |
| 212 | | | |
| 213 | 0 | | int i = 0; |
| 214 | | | try |
| 215 | | | { |
| 216 | 0 | | for (i = 0; i < args.length; i++) |
| 217 | | | { |
| 218 | 0 | (9) | if (args[i].equals("-i")) |
| 219 | | | { |
| 220 | 0 | | inputFile = args[++i]; |
| 221 | | | } |
| 222 | 0 | (10) | else if (args[i].equals("-o")) |
| 223 | | | { |
| 224 | 0 | | outputFile = args[++i]; |
| 225 | | | } |
| 226 | | | } |
| 227 | | | |
| 228 | | | } |
| 229 | 0 | | catch (ArrayIndexOutOfBoundsException x) |
| 230 | | | { |
| 231 | 0 | | System.err.println("Error: argument " |
| 232 | | | + args[i - 1] + " requires an option"); |
| 233 | 0 | | usage(); |
| 234 | 0 | | } |
| 235 | | | |
| 236 | 0 | | if (inputFile == null || outputFile == null) |
| 237 | | | { |
| 238 | 0 | | usage(); |
| 239 | | | } |
| 240 | | | |
| 241 | 0 | | final SqlToXml transformer = new SqlToXml(inputFile, outputFile); |
| 242 | | | try |
| 243 | | | { |
| 244 | 0 | | transformer.transformSqlToXml(); |
| 245 | | | } |
| 246 | 0 | | catch (Exception e) |
| 247 | | | { |
| 248 | 0 | | System.err.println("File transformation failed: " + e.getMessage()); |
| 249 | 0 | | e.printStackTrace(System.err); |
| 250 | 0 | | System.exit(1); |
| 251 | 0 | | } |
| 252 | 0 | | } |
| 253 | | | |
| 254 | | | private static void usage () |
| 255 | | | { |
| 256 | 0 | | System.err.println("Usage: SqlToXml -i <input_file> -o <output_file>"); |
| 257 | 0 | (11) | System.exit(1); |
| 258 | 0 | | } |
| 259 | | | } |