public static String sqlQueryInstantiateConstraints(String sql, KeyValueList bindings) {
    { int start = 0;
      int end = sql.length();
      String variable = null;
      Stella_Object value = null;
      int varstart = 0;
      int varend = 0;
      int colstart = 0;
      Keyword constraint = RDBMS.KWD_VALUE;
      String quotes = "'`\"";
      char quote = Stella.NULL_CHARACTER;
      Cons result = Stella.NIL;
      String truecondition = "1=1";
      String falsecondition = "0=1";

      loop000 : for (;;) {
        varstart = Native.string_position(sql, '?', varend);
        if (varstart == Stella.NULL_INTEGER) {
          if (result == Stella.NIL) {
            return (sql);
          }
          else {
            result = Cons.cons(StringWrapper.wrapString(Native.string_subsequence(sql, start, Stella.NULL_INTEGER)), result);
            break loop000;
          }
        }
        variable = null;
        { StringWrapper var = null;
          Stella_Object val = null;
          KvCons iter000 = bindings.theKvList;

          loop001 : for (;iter000 != null; iter000 = iter000.rest) {
            var = ((StringWrapper)(iter000.key));
            val = iter000.value;
            if (Stella.startsWithP(sql, var.wrapperValue, varstart)) {
              variable = var.wrapperValue;
              value = val;
              varend = varstart + variable.length();
              if ((varend == end) ||
                  ((Stella.$CHARACTER_TYPE_TABLE$[(int) (sql.charAt(varend))] == RDBMS.KWD_WHITE_SPACE) ||
                   Native.string_memberP(quotes, sql.charAt(varend)))) {
                break loop001;
              }
            }
          }
        }
        if (variable == null) {
          varend = varstart + 1;
          continue loop000;
        }
        quote = Stella.NULL_CHARACTER;
        if (sql.charAt((Stella.integer_max(varstart - 1, 0))) == '=') {
          colstart = varstart - 2;
          constraint = RDBMS.KWD_VALUE;
        }
        else if ((sql.charAt((Stella.integer_max(varstart - 2, 0))) == '=') &&
            Native.string_memberP(quotes, sql.charAt((varstart - 1)))) {
          colstart = varstart - 3;
          constraint = RDBMS.KWD_VALUE;
          quote = sql.charAt((varstart - 1));
        }
        else if (Stella.startsWithP(sql, " IN ", Stella.integer_max(varstart - 4, 0)) ||
            Stella.startsWithP(sql, " in ", Stella.integer_max(varstart - 4, 0))) {
          colstart = varstart - 5;
          constraint = RDBMS.KWD_SET;
        }
        else if ((Stella.startsWithP(sql, " IN ", Stella.integer_max(varstart - 5, 0)) ||
            Stella.startsWithP(sql, " in ", Stella.integer_max(varstart - 5, 0))) &&
            Native.string_memberP(quotes, sql.charAt((varstart - 1)))) {
          colstart = varstart - 6;
          constraint = RDBMS.KWD_SET;
          quote = sql.charAt((varstart - 1));
        }
        else {
          constraint = RDBMS.KWD_NONE;
          continue loop000;
        }
        if ((constraint == RDBMS.KWD_VALUE) &&
            (Stella_Object.consP(value) &&
             (!(value == Stella.NIL)))) {
          constraint = RDBMS.KWD_SET;
          result = Cons.cons(StringWrapper.wrapString(Native.string_subsequence(sql, start, colstart + 1)), result);
          result = Cons.cons(StringWrapper.wrapString(" IN "), result);
          start = ((quote != Stella.NULL_CHARACTER) ? (varstart - 1) : varstart);
        }
        if ((value == null) ||
            (value == Stella.NIL)) {
          loop002 : for (;;) {
            if (colstart <= 0) {
              colstart = 0;
              break loop002;
            }
            else if (Stella.$CHARACTER_TYPE_TABLE$[(int) (sql.charAt(colstart))] == RDBMS.KWD_WHITE_SPACE) {
              colstart = colstart + 1;
              break loop002;
            }
            else {
              colstart = colstart - 1;
            }
          }
          if (quote != Stella.NULL_CHARACTER) {
            varend = varend + 1;
          }
          result = Cons.cons(StringWrapper.wrapString(Native.string_subsequence(sql, start, colstart)), result);
          result = Cons.cons(((value == Stella.NIL) ? StringWrapper.wrapString(falsecondition) : StringWrapper.wrapString(truecondition)), result);
        }
        else if (constraint == RDBMS.KWD_SET) {
          if (quote != Stella.NULL_CHARACTER) {
            varstart = varstart - 1;
            varend = varend + 1;
          }
          { OutputStringStream set = OutputStringStream.newOutputStringStream();

            set.nativeStream.print("(");
            { Stella_Object elt = null;
              Cons iter001 = value.consify();
              int i = Stella.NULL_INTEGER;
              int iter002 = 1;

              for (;!(iter001 == Stella.NIL); iter001 = iter001.rest, iter002 = iter002 + 1) {
                elt = iter001.value;
                i = iter002;
                set.nativeStream.print((((i > 1) ? "," : "")));
                if (quote != Stella.NULL_CHARACTER) {
                  set.nativeStream.print(quote + edu.isi.sdbc.Sdbc.sqlEscapeString(((StringWrapper)(elt)).wrapperValue, quote) + quote);
                }
                else {
                  set.nativeStream.print(StringWrapper.unwrapString(((StringWrapper)(elt))));
                }
              }
            }
            set.nativeStream.print(")");
            result = Cons.cons(StringWrapper.wrapString(Native.string_subsequence(sql, start, varstart)), result);
            result = Cons.cons(StringWrapper.wrapString(set.theStringReader()), result);
          }
        }
        else {
          result = Cons.cons(StringWrapper.wrapString(Native.string_subsequence(sql, start, varstart)), result);
          result = Cons.cons(((quote != Stella.NULL_CHARACTER) ? StringWrapper.wrapString(edu.isi.sdbc.Sdbc.sqlEscapeString(((StringWrapper)(value)).wrapperValue, quote)) : value), result);
        }
        start = varend;
      }
      { OutputStringStream res = OutputStringStream.newOutputStringStream();

        { StringWrapper piece = null;
          Cons iter003 = result.reverse();

          for (;!(iter003 == Stella.NIL); iter003 = iter003.rest) {
            piece = ((StringWrapper)(iter003.value));
            res.nativeStream.print(StringWrapper.unwrapString(piece));
          }
        }
        return (res.theStringReader());
      }
    }
  }