1 16 17 package org.webdocwf.util.loader.generator; 18 19 import java.util.*; 20 21 import org.webdocwf.util.loader.logging.Logger; 22 import org.webdocwf.util.loader.logging.StandardLogger; 23 24 30 public class SqlStatements { 31 32 private List list = new ArrayList(); 33 private List hsqlPKlist = new ArrayList(); 34 private String [] targetDBWithoutAlterTable = { "xml", "csv", "excel" }; 42 43 private boolean incompatible = false; 44 private boolean incompatibleDBTarget = false; 45 private boolean incompatibleDBSource = false; 46 private Logger logger = null; 47 56 57 public SqlStatements(String nameSQL, String tableName, ImportDefinitionAttributes importDefinitionAttributes, RelationshipsAttributes relationshipsAttributes, int k, InputParameters generatorParameters) { 58 setLogger(); 59 this.logger.write("full", "SqlStatements is started."); 60 String targetDBType = generatorParameters.getTargetType(); 61 String sourceDBType = generatorParameters.getSourceType(); 62 checkDBTypeTarget(targetDBType); 63 checkDBTypeSource(sourceDBType); 64 65 if (nameSQL.equalsIgnoreCase("CreateTables")) { 66 list.add("Create table" + " " + tableName); 67 list.add("("); 68 if (generatorParameters.getSourceType().equalsIgnoreCase("csv")) { 69 for (int i = 0; i < importDefinitionAttributes.getTagSourceColumnName().length; i++) { 70 if (i == importDefinitionAttributes.getTagSourceColumnName().length - 1) 71 list.add(importDefinitionAttributes.getTagSourceColumnName()[i] + " " + "VARCHAR" + " (254) " + " "); 72 else 73 list.add(importDefinitionAttributes.getTagSourceColumnName()[i] + " " + "VARCHAR" + " (254) " + " ,"); 74 } 75 list.add(");"); 76 list.add(""); 77 list.add(""); 78 list.add(""); 79 } else if (generatorParameters.getSourceType().equalsIgnoreCase("access")) { 80 String end = " ,"; 81 for (int i = 0; i < importDefinitionAttributes.getTagSourceColumnName().length; i++) { 82 if (i == importDefinitionAttributes.getTagSourceColumnName().length - 1) 83 end = " "; 84 if (importDefinitionAttributes.getTagColumnLenght()[i].equalsIgnoreCase("")) 85 list.add(importDefinitionAttributes.getTagSourceColumnName()[i] + " " + (importDefinitionAttributes.getTagColumnType()[i]).toUpperCase() + importDefinitionAttributes.getTagAllowNulls()[i] + end); 86 else 87 list.add(importDefinitionAttributes.getTagSourceColumnName()[i] + " " + (importDefinitionAttributes.getTagColumnType()[i]).toUpperCase() + "(" + importDefinitionAttributes.getTagColumnLenght()[i] + ") " + importDefinitionAttributes.getTagAllowNulls()[i] + end); 88 } 89 list.add(");"); 90 list.add(""); 91 list.add(""); 92 list.add(""); 93 } else { 94 String end = " "; 95 for (int i = 0; i < importDefinitionAttributes.getTagSourceColumnName().length; i++) { 96 boolean pk = true; 97 for (int j = 0; j < relationshipsAttributes.getPrimaryKeys().length; j = j + 2) { 98 if (relationshipsAttributes.getPrimaryKeys()[j + 1].equalsIgnoreCase(importDefinitionAttributes.getTagSourceColumnName()[i])) { 99 pk = false; 100 break; 101 } 102 } 103 if (i != importDefinitionAttributes.getTagSourceColumnName().length - 1) 104 end = " ,"; 105 else 106 end = " "; 107 108 if (generatorParameters.getAlterTablePrimaryKey().equalsIgnoreCase("true")) { 113 list.add(importDefinitionAttributes.getTagSourceColumnName()[i] + " " + (importDefinitionAttributes.getTagColumnType()[i]).toUpperCase() +importDefinitionAttributes.getTagColumnLenght()[i] +importDefinitionAttributes.getTagAllowNulls()[i] + end); 117 } else { 118 if (pk) { 121 list.add(importDefinitionAttributes.getTagSourceColumnName()[i] + " " + (importDefinitionAttributes.getTagColumnType()[i]).toUpperCase() 123 +importDefinitionAttributes.getTagColumnLenght()[i] + 125 importDefinitionAttributes.getTagAllowNulls()[i] + end); 128 } else { 129 if (generatorParameters.getTargetType().equalsIgnoreCase("HypersonicSQL")){ 132 hsqlPKlist.add(importDefinitionAttributes.getTagSourceColumnName()[i]); 134 list.add(importDefinitionAttributes.getTagSourceColumnName()[i] + " " + (importDefinitionAttributes.getTagColumnType()[i]).toUpperCase() 135 +importDefinitionAttributes.getTagColumnLenght()[i] 136 +", "); 137 }else{ 138 list.add(importDefinitionAttributes.getTagSourceColumnName()[i] + " " + (importDefinitionAttributes.getTagColumnType()[i]).toUpperCase() +importDefinitionAttributes.getTagColumnLenght()[i] +"PRIMARY KEY " + end); 144 } 145 } 146 } 147 } 178 if (generatorParameters.getTargetType().equalsIgnoreCase("HypersonicSQL")){ 181 182 if (hsqlPKlist.size()>0) { 183 184 String columnNames = ""; 185 columnNames = columnNames+" ("; 186 187 for (int i = 0; i < hsqlPKlist.size()-1; i++) { 188 columnNames = columnNames + hsqlPKlist.get(i) + ","; 189 190 } 191 192 if (hsqlPKlist.size() >= 2){ 193 columnNames = columnNames + hsqlPKlist.get(hsqlPKlist.size()-1) + ")"; 194 195 }else if (hsqlPKlist.size() == 1){ 196 columnNames = columnNames + hsqlPKlist.get(0) + ")"; 197 198 } 199 list.add(", CONSTRAINT" + " " +importDefinitionAttributes.getTableName()+"_PK" +" " 200 +"PRIMARY KEY " + columnNames); 201 } 202 } 203 list.add(");"); 205 list.add(""); 206 list.add(""); 207 list.add(""); 208 } 209 } else if (nameSQL.equalsIgnoreCase("CreatePrimary")) { 210 list.add(""); 211 if (incompatibleDBSource) { 212 if (k == 0) { 213 list.add(""); 214 } 215 } else if (incompatibleDBTarget) { 216 if (k == 0) { 217 list.add(""); 218 } 219 } else { 220 if (relationshipsAttributes.getPrimaryKeys().length != 0) { 221 int primaryCount = relationshipsAttributes.getPrimaryKeys().length; 222 for (int i = 0; i < primaryCount; i = i + 2) { 223 String arrayOfPrimaryNames = relationshipsAttributes.getPrimaryKeys()[i + 1]; 224 for (int m = i + 2; m < primaryCount; m = m + 2) { 225 if (relationshipsAttributes.getPrimaryKeys()[i].equalsIgnoreCase(relationshipsAttributes.getPrimaryKeys()[m])) { 226 227 arrayOfPrimaryNames = arrayOfPrimaryNames + "," + relationshipsAttributes.getPrimaryKeys()[m + 1]; 228 i = i + 2; 229 } else 230 break; 231 } 232 233 list.add("ALTER TABLE " + " " + relationshipsAttributes.getTableName() + " " + "ADD CONSTRAINT " + relationshipsAttributes.getPrimaryKeys()[i] + " PRIMARY KEY" + "(" + 234 arrayOfPrimaryNames + ") ;"); 236 } 237 } else { 238 list.add(""); 239 } 240 } } else if (nameSQL.equalsIgnoreCase("CreateIndex")) { 242 list.add(""); 243 if (incompatibleDBSource) { 244 if (k == 0) { 245 list.add(""); 246 } 247 } else if (incompatibleDBTarget) { 248 if (k == 0) { 249 list.add(""); 250 } 251 } else { 252 if (relationshipsAttributes.getIndexVariables().length != 0) { 253 int indexCount = relationshipsAttributes.getIndexVariables().length; 254 for (int i = 0; i < indexCount; i = i + 3) { 255 String arrayOfIndexNames = relationshipsAttributes.getIndexVariables()[i + 2]; 256 for (int m = i + 3; m < indexCount; m = m + 3) { 257 if (relationshipsAttributes.getIndexVariables()[i + 1].equalsIgnoreCase(relationshipsAttributes.getIndexVariables()[m + 1])) { 258 arrayOfIndexNames = arrayOfIndexNames + "," + relationshipsAttributes.getIndexVariables()[m + 2]; 259 i = i + 3; 260 } else 261 break; 262 } 263 if (relationshipsAttributes.getIndexVariables()[i].equalsIgnoreCase("0") || relationshipsAttributes.getIndexVariables()[i].equalsIgnoreCase("1")) { 264 int j = Integer.parseInt(relationshipsAttributes.getIndexVariables()[i]); 266 String unique; 267 if (j == 1) 268 unique = ""; 269 else 270 unique = "UNIQUE"; 271 list.add("CREATE " + unique + " INDEX " + relationshipsAttributes.getIndexVariables()[i + 1] + 272 " ON " + tableName + "(" + arrayOfIndexNames + ") ;"); 274 275 } else if (relationshipsAttributes.getIndexVariables()[i].equalsIgnoreCase("true") || relationshipsAttributes.getIndexVariables()[i].equalsIgnoreCase("false")) { 276 String unique; 278 String j = relationshipsAttributes.getIndexVariables()[i]; 279 if (j.equalsIgnoreCase("true")) 280 unique = ""; 281 else 282 unique = "UNIQUE"; 283 list.add("CREATE " + unique + " INDEX " + relationshipsAttributes.getIndexVariables()[i + 1] + 284 " ON " + tableName + "(" + arrayOfIndexNames + ") ;"); 286 } 287 } 288 } else { 289 list.add(""); 291 } 292 } 293 } else if (nameSQL.equalsIgnoreCase("CreateIntegrity")) { 294 list.add(""); 295 if (incompatibleDBSource) { 296 if (k == 0) { 297 list.add(""); 298 } 299 } else if (incompatibleDBTarget) { 300 if (k == 0) { 301 list.add(""); 302 } 303 } else { 304 if (relationshipsAttributes.getForeignVariables().length != 0 && relationshipsAttributes.getForeignVariables().length >= 5) { 306 307 for (int i = 0; i < relationshipsAttributes.getForeignVariables().length; i = i + 5) { 308 309 list.add("ALTER TABLE " + relationshipsAttributes.getForeignVariables()[i] + " ADD CONSTRAINT " + relationshipsAttributes.getForeignVariables()[i + 1] + " FOREIGN KEY (" + relationshipsAttributes.getForeignVariables()[i + 2] + ")" + " REFERENCES " + relationshipsAttributes.getForeignVariables()[i + 3] + " (" + relationshipsAttributes.getForeignVariables()[i + 4] + ") ;"); 310 } 311 } else { 312 list.add(""); 313 } 314 } 315 } else if (nameSQL.equalsIgnoreCase("DropTables")) { 316 list.add(""); 317 list.add("DROP TABLE " + tableName + " ;"); 318 list.add(""); 319 } else if (nameSQL.equalsIgnoreCase("DropIntegrity")) { 320 if (relationshipsAttributes.getForeignVariables().length != 0 && relationshipsAttributes.getForeignVariables().length >= 2) { 322 323 for (int i = 0; i < relationshipsAttributes.getForeignVariables().length; i = i + 5) { 324 list.add(""); 325 list.add("ALTER TABLE " + relationshipsAttributes.getForeignVariables()[i] + " DROP CONSTRAINT " + relationshipsAttributes.getForeignVariables()[i + 1] + " ;"); 326 list.add(""); 327 } 328 329 } else { 330 list.add(""); 331 } 332 333 } 334 this.logger.write("full", "SqlStatements is finished."); 335 } 336 337 341 public void setCreateStream(String [] create_Stream) { 342 list = Arrays.asList(create_Stream); 343 } 344 345 349 public String [] getCreateStream() { 350 String [] ret = new String [list.size()]; 351 list.toArray(ret); 352 return ret; 353 } 354 355 373 private boolean checkDBTypeTarget(String type) { 374 for (int i = 0; i < targetDBWithoutAlterTable.length; i++) { 375 if (type.equalsIgnoreCase(targetDBWithoutAlterTable[i])) { 376 this.incompatibleDBTarget = true; 377 break; 378 } else { 379 this.incompatibleDBTarget = false; 380 } 381 } 382 return incompatibleDBTarget; 383 } 384 385 private boolean checkDBTypeSource(String type) { 386 for (int i = 0; i < targetDBWithoutAlterTable.length; i++) { 387 if (type.equalsIgnoreCase(targetDBWithoutAlterTable[i])) { 388 this.incompatibleDBSource = true; 389 break; 390 } else { 391 this.incompatibleDBSource = false; 392 } 393 } 394 return incompatibleDBSource; 395 } 396 397 401 private void setLogger() { 402 this.logger = StandardLogger.getCentralLogger(); 403 } 404 } 405 | Popular Tags |