Esempio n. 1
0
  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));
  }