1 22 23 28 29 package org.xquark.mapper.dbms; 30 31 import java.io.*; 32 import java.sql.SQLException ; 33 34 import javax.xml.parsers.SAXParserFactory ; 35 36 import org.xml.sax.InputSource ; 37 import org.xml.sax.SAXException ; 38 import org.xquark.mapper.RepositoryException; 39 import org.xquark.mapper.mapping.*; 40 import org.xquark.mapper.metadata.RepositoryConstants; 41 42 52 public class SQLTableBuilder implements RepositoryConstants 53 { 54 private static final String RCSRevision = "$Revision: 1.1 $"; 55 private static final String RCSName = "$Name: $"; 56 static StringBuffer pkConstraintStmt = new StringBuffer (); 57 static StringBuffer fkConstraintStmt1 = new StringBuffer (); 58 static StringBuffer fkConstraintStmt2 = new StringBuffer (); 59 60 public static void generateScripts(_RepositoryConnection repConn, String mappingURL, File wPath) 61 throws Exception 62 { 63 final String SQLCreateFile = "CreateTables.sql"; 64 final String SQLDropFile = "DropTables.sql"; 65 66 RepositoryMapping mapping = null; 67 68 70 System.out.println("Loading Schema and Mapping..."); 71 try { 72 MappingFactory factory = new MappingFactory( 73 repConn.getSchemaManager(), 74 repConn 75 ); 76 mapping = (RepositoryMapping)factory.createTree(); 77 SAXParserFactory parserFactory = SAXParserFactory.newInstance(); 78 parserFactory.setNamespaceAware(true); 79 Loader loader = new Loader(parserFactory.newSAXParser().getXMLReader(), 80 repConn.getConnection(), repConn.getSchemaManager(), true); 81 InputSource source = new InputSource (mappingURL); 82 mapping = loader.load(source, factory, false); 83 } 84 catch(SAXException e) { 85 throw new RepositoryException(RepositoryException.SAX_OUTPUT_ERROR, e.getMessage(), e.getException()); 86 } 87 catch(SQLException e) { 88 throw new RepositoryException(RepositoryException.DB_ERROR, "JDBC error while loading mapping", e); 89 } 90 91 93 94 PrintStream createFile = null; 95 PrintStream dropFile = null; 96 try { 97 createFile = new PrintStream(new FileOutputStream(new File(wPath, SQLCreateFile)), true); 98 dropFile = new PrintStream(new FileOutputStream(new File(wPath, SQLDropFile)), true); 99 } 100 catch(FileNotFoundException e) { 101 throw new RepositoryException(RepositoryException.SYSTEM_ERROR, "Could not open files to generate", e); 102 } 103 104 105 System.out.println("Generating SQL script..."); 106 generateSQL(repConn, createFile, dropFile, mapping); 107 createFile.close(); 108 dropFile.close(); 109 } 110 111 private static void generateSQL(_RepositoryConnection repConn, 112 PrintStream outPrinter, 113 PrintStream dropPrinter, 114 RepositoryMapping mapping) 115 throws RepositoryException 116 { 117 MappingSetIterator itt = mapping.getSortedTableMappingSetIterator(false); 118 TableMapping tableMapping; 119 120 121 while (itt.hasNext()) 122 { 123 pkConstraintStmt.setLength(0); 124 fkConstraintStmt1.setLength(0); 125 fkConstraintStmt2.setLength(0); 126 boolean firstColumn = true; 127 tableMapping = itt.next().getTableMapping(); 128 129 130 outPrinter.print("CREATE TABLE "); 131 outPrinter.print(tableMapping.getTableName()); 132 outPrinter.println("("); 133 134 135 ColumnMapping[] columns = tableMapping.getColumnMappings(); 136 for(int i = 0; i < columns.length; i++) 137 { 138 if (firstColumn) 139 firstColumn = false; 140 else 141 outPrinter.println(","); generateSQLforColumn(repConn, outPrinter, columns[i], mapping); 143 } 144 145 if (pkConstraintStmt.length() > 0) 146 { 147 pkConstraintStmt.setLength(pkConstraintStmt.length() - 1); 148 outPrinter.println(","); 149 outPrinter.print("PRIMARY KEY(" + pkConstraintStmt + ")"); 150 } 151 152 if (fkConstraintStmt1.length() > 0) 153 { 154 fkConstraintStmt1.setLength(fkConstraintStmt1.length() - 1); 155 fkConstraintStmt2.setLength(fkConstraintStmt2.length() - 1); 156 outPrinter.println(","); 157 outPrinter.print("FOREIGN KEY(" + fkConstraintStmt1 + ")"); 158 outPrinter.print(" REFERENCES " + fkConstraintStmt2 + ")"); 159 } 160 161 outPrinter.println(); 162 outPrinter.println(");"); 163 outPrinter.println(); 164 } 165 166 itt = mapping.getSortedTableMappingSetIterator(true); 167 168 169 while (itt.hasNext()) 170 { 171 tableMapping = itt.next().getTableMapping(); 172 173 dropPrinter.print("DROP TABLE "); 174 dropPrinter.println(tableMapping.getTableName() + ";"); 175 } 176 } 177 178 private static void generateSQLforColumn(_RepositoryConnection repConn, 179 PrintStream outPrinter, 180 ColumnMapping mapping, 181 RepositoryMapping repMapping) 182 throws RepositoryException 183 { 184 TableMapping refTableMapping = null; 185 ColumnMapping refColumnMapping = null; 186 StringBuffer columnStmt = new StringBuffer (); 187 188 189 columnStmt.append(mapping.getColumnName()); 190 191 192 columnStmt.append(" "); 193 columnStmt.append(mapping.getTypeInfo().getTypeCreationString(repConn.getConnection())); 194 195 196 if (mapping.getKeyColumnIndex() != -1) 197 { 198 pkConstraintStmt.append(mapping.getColumnName()); 199 pkConstraintStmt.append(","); 200 columnStmt.append(" NOT NULL"); 201 } 202 outPrinter.print(columnStmt); 203 204 205 if (mapping.getTableRefIndex() != -1) 206 { 207 refTableMapping = repMapping.getTableMapping(mapping.getTableRefIndex()); 208 refColumnMapping = ((ColumnRefGenerator)mapping.getGenerator()).getColumnRef(); 209 fkConstraintStmt1.append(mapping.getColumnName()); 210 fkConstraintStmt1.append(","); 211 212 if (fkConstraintStmt2.length() == 0) 213 { 214 fkConstraintStmt2.append(refTableMapping.getTableName()); 215 fkConstraintStmt2.append("("); 216 } 217 fkConstraintStmt2.append(refColumnMapping.getColumnName()); 218 fkConstraintStmt2.append(","); 219 } 220 } 221 } 222 | Popular Tags |