-
Notifications
You must be signed in to change notification settings - Fork 5
Use "?" for parameter markers in both native query and query translation #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright 2025-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.mongodb.hibernate.jdbc; | ||
|
||
class MongoParameterRecognizer { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adopted from JsonScanner in Java driver. |
||
|
||
static String replace(String json) { | ||
StringBuilder builder = new StringBuilder(json.length()); | ||
NathanQingyangXu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
int i = 0; | ||
while (i < json.length()) { | ||
char c = json.charAt(i++); | ||
switch (c) { | ||
case '{': | ||
case '}': | ||
case '[': | ||
case ']': | ||
case ':': | ||
case ',': | ||
case ' ': | ||
builder.append(c); | ||
break; | ||
case '\'': | ||
case '"': | ||
i = scanString(c, i, json, builder); | ||
break; | ||
case '?': | ||
builder.append("{$undefined: true}"); | ||
NathanQingyangXu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
break; | ||
default: | ||
if (c == '-' || Character.isDigit(c)) { | ||
i = scanNumber(c, i, json, builder); | ||
} else if (c == '$' || c == '_' || Character.isLetter(c)) { | ||
i = scanUnquotedString(c, i, json, builder); | ||
} else { | ||
builder.append(c); // or throw exception, as this isn't valid JSON | ||
} | ||
} | ||
} | ||
return builder.toString(); | ||
} | ||
|
||
private static int scanNumber(char firstCharacter, int startIndex, String json, StringBuilder builder) { | ||
builder.append(firstCharacter); | ||
int i = startIndex; | ||
char c = json.charAt(i++); | ||
while (i < json.length() && Character.isDigit(c)) { | ||
builder.append(c); | ||
c = json.charAt(i++); | ||
} | ||
return i - 1; | ||
} | ||
|
||
private static int scanUnquotedString(final char firstCharacter, final int startIndex, final String json, final StringBuilder builder) { | ||
builder.append(firstCharacter); | ||
int i = startIndex; | ||
char c = json.charAt(i++); | ||
while (i < json.length() && Character.isLetterOrDigit(c)) { | ||
builder.append(c); | ||
c = json.charAt(i++); | ||
} | ||
return i - 1; | ||
} | ||
|
||
private static int scanString(final char quoteCharacter, final int startIndex, final String json, final StringBuilder builder) { | ||
int i = startIndex; | ||
builder.append(quoteCharacter); | ||
while (i < json.length()) { | ||
char c = json.charAt(i++); | ||
if (c == '\\') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't include explicit support for escapes like |
||
builder.append(c); | ||
if (i < json.length()) { | ||
c = json.charAt(i++); | ||
builder.append(c); | ||
} | ||
} else if (c == quoteCharacter) { | ||
builder.append(c); | ||
return i; | ||
} else { | ||
builder.append(c); | ||
} | ||
} | ||
return i; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.