private String addConstantVarialbePlaceHolderIn( String sqlQuery, List<CMSEntityEntry> constAndVarialbeMetaDataList) { for (CMSEntityEntry entry : constAndVarialbeMetaDataList) { String replacement = entry.getSqlElement(); if (replacement.matches(".+'\\w+'.*")) { Matcher theMatcher = Pattern.compile("'\\w+'").matcher(replacement); while (theMatcher.find()) { String group = theMatcher.group(); String variable = entry.getEntityAttribute() + "_" + group.replaceAll("\\W", ""); // UPDATE entry.addToVariableAttrMap(variable, entry.getEntityAttribute()); replacement = replacement.replace(group, ":" + entry.getVariable(variable)); } // REPLACE sqlQuery = sqlQuery.replaceFirst( entry.getSqlElement().replace("(", "\\(").replace(")", "\\)"), replacement); } else { String variable = replacement.replaceFirst(".+:", ""); // UPDATE entry.addToVariableAttrMap(variable, entry.getEntityAttribute()); // REPLACE sqlQuery = sqlQuery.replaceFirst( replacement, replacement.replaceFirst(":.+", ":" + entry.getVariable(variable))); } } return sqlQuery; }
private String addIntoPlaceHolderIn( String sqlQuery, List<CMSEntityEntry> intoStatementMetaDataList) { for (CMSEntityEntry entry : intoStatementMetaDataList) { if (entry.getSqlElement().endsWith(STAR)) { Set<String> entityAttrs = entry.getEntityAttrSet(); StringWriter out = new StringWriter(); PrintWriter pw = new PrintWriter(out); // pw can do println, so I don't have to hardcode this "\n". for (String entityAttr : entityAttrs) { String[] split = entityAttr.split("\\."); // e.g. claimcycle.claimcycleid // UPDATE entry.addToVariableAttrMap( split[1], entityAttr); // so we will have attr_a mapping to attr String tableAlias = entry.getTableAlias(split[0].toUpperCase()); pw.println( tableAlias + "." + split[1] + "{" + entry.getVariable(split[1]) + "}" + ","); // e.g.: cc.claimcycleid{claimcycleid_a} } // REPLACE if (entry.getSqlElement().equals("*") == false) { sqlQuery = sqlQuery.replace(entry.getSqlElement(), "@"); } sqlQuery = sqlQuery.replaceFirst("[*@]", out.toString() + "@"); pw.close(); } else { String variable = entry.getColumnAlias().isEmpty() ? entry.getEntityAttribute() : entry.getColumnAlias(); // UPDATE entry.addToVariableAttrMap(variable, entry.getEntityAttribute()); // REPALCE sqlQuery = sqlQuery.replaceFirst( entry.getSqlElement(), entry.getSqlElement() + "{" + entry.getVariable(variable) + "}"); } } StringWriter out = new StringWriter(); PrintWriter pw = new PrintWriter(out); // pw can do println, so I don't have to hardcode this "\n". int indexToInsertIntoClause = sqlQuery.lastIndexOf("}") + 1; pw.println(sqlQuery.substring(0, indexToInsertIntoClause)); // SELECT CLAUSE pw.println("INTO"); Pattern pattern = Pattern.compile("\\{\\w+}"); Matcher matcher = pattern.matcher(sqlQuery); while (matcher.find()) { pw.println(":" + matcher.group().replaceAll("[\\{}]", "") + ","); } pw.println("<FROM>"); String finalQuery = out.toString().replaceAll("\\{\\w+}", ""); pw.close(); finalQuery = finalQuery.substring(0, finalQuery.lastIndexOf(",")) // trim the last ","; + finalQuery.substring( finalQuery.lastIndexOf(",") + 1); // keep the FROM and the "\r\n" around it String fromClause = sqlQuery.substring(indexToInsertIntoClause).replaceFirst("(F|f)(R|r)(O|o)(M|m)", "FROM"); fromClause = fromClause.substring(fromClause.indexOf("FROM")); // From clause finalQuery = finalQuery.replace("<FROM>", fromClause).trim(); return finalQuery; }