-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from yuu-nkjm/develop
Fixing
- Loading branch information
Showing
21 changed files
with
137 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.nkjmlab.sorm4j.core.util; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public final class SqlUtils { | ||
|
||
private SqlUtils() {} | ||
|
||
/** | ||
* Returns single quoted expression. If it includes single quotations, they will be escaped. | ||
* | ||
* @param expr | ||
* @return | ||
*/ | ||
public static String quote(String str) { | ||
return wrapSingleQuote(str.contains("'") ? str.replaceAll("'", "''") : str); | ||
} | ||
|
||
private static String wrapSingleQuote(String str) { | ||
return "'" + str + "'"; | ||
} | ||
|
||
/** | ||
* Convert the given arguments to SQL literal. | ||
* | ||
* @param element | ||
* @return | ||
*/ | ||
public static String literal(Object element) { | ||
if (element == null) { | ||
return "null"; | ||
} else if (element instanceof Number || element instanceof Boolean) { | ||
return element.toString(); | ||
} else if (element instanceof List) { | ||
return String.join(", ", | ||
((List<?>) element).stream().map(e -> literal(e)).collect(Collectors.toList())); | ||
} | ||
String str = element.toString(); | ||
switch (str) { | ||
case "?": | ||
return str; | ||
default: | ||
return quote(str); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.