private static int getParamStartIndex( String sql, char findChar, int startIndex, QuotChecker quotChecker) { Comment status = Comment.NONE; for (int i = startIndex, iCnt = sql.length() - 2; i < iCnt; i++) { char currC = sql.charAt(i); char nextC = sql.charAt(i + 1); if (quotChecker != null) { quotChecker.check(currC); } switch (status) { case NONE: if (currC == findChar && nextC == '{') return i; // '/*' 형식의 주석시작지점 검색 if (currC == '/' && nextC == '*') { status = Comment.BLOCK; i++; // '--' 형식의 주석시작지점 검색 } else if (currC == '-' && nextC == '-') { status = Comment.LINE; i++; } break; case BLOCK: if (currC == '*' && nextC == '/') { status = Comment.NONE; i++; } break; case LINE: if (currC == '\n') { status = Comment.NONE; } break; } } return -1; }
private void makeSql(String sql, NMap param) { QuotChecker quotChecker = new QuotChecker(); int previousStartIndex = 0; while (true) { int startIndex = getParamStartIndex(sql, '#', previousStartIndex, quotChecker); if (startIndex == -1) break; int endIndex = getParamEndIndex(sql, startIndex + 2); String key = sql.substring(startIndex + 2, endIndex); String prevPhrase = sql.substring(previousStartIndex, startIndex); binarySql.add(prevPhrase); if (!hasKey(param, key)) { bindParams.put(key, null); } else { BindStruct bindStruct = new BindStruct(key, quotChecker.isOn()); BindParam bindParam = null; try { bindParam = bindStruct.toBindParam(getValue(param, bindStruct.getKey())); } catch (JsonPathNotFoundException e) { } bindStructs.put(key, bindStruct); bindParams.put(key, bindParam); if (bindParam.getType() == SqlType.LIST) { int index = 0; for (Object o : (List) bindParam.getValue()) { String subKey = String.format("%s_%d", key, index++); BindStruct bindStructSub = new BindStruct( subKey, bindStruct.getType(), bindStruct.isOut(), quotChecker.isOn()); BindParam bindParamSub = bindStruct.toBindParam(o); bindStructs.put(subKey, bindStructSub); bindParams.put(subKey, bindParamSub); } } } BindParam bindParam = bindParams.get(key); if (bindParam == null) { binarySql.add(String.format("#{%s}", key)); } else if (bindParam.getType() == SqlType.LIST) { int size = ((List) bindParam.getValue()).size(); for (int i = 0; i < size; i++) { String subKey = String.format("%s_%d", bindParam.getKey(), i); binaryKeys.add(subKey); if (i != 0) binarySql.add(","); binarySql.add(null); } } else { binaryKeys.add(key); binarySql.add(null); } previousStartIndex = endIndex + 1; } binarySql.add(sql.substring(previousStartIndex)); }