Package marauroa.server.db
Class StringChecker
java.lang.Object
marauroa.server.db.StringChecker
Helper class to validate strings and escape SQL strings.
- Author:
- miguel
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
escapeSQLString(String param)
Escapes ' and \ in a string so that the result can be passed into an SQL command.static String
trimAndEscapeSQLString(String param, int size)
Trims the string to the specified size without error in case it is already shorter.static String
trims a string to nullstatic boolean
validString(String string)
This method returns true if a string is valid because it lacks of any kind of control or escape character.
-
Constructor Details
-
StringChecker
public StringChecker()
-
-
Method Details
-
validString
This method returns true if a string is valid because it lacks of any kind of control or escape character.- Parameters:
string
- The string to check- Returns:
- true if the string is valid for storing it at database or as XML.
-
escapeSQLString
Escapes ' and \ in a string so that the result can be passed into an SQL command. The parameter has be quoted using ' in the sql. Most database engines accept single quotes around numbers as well.Please note that special characters for LIKE and other matching commands are not quotes. The result of this method is suitable for INSERT, UPDATE and an "=" operator in the WHERE part.
- Parameters:
param
- string to quote- Returns:
- quoted string
-
trimAndEscapeSQLString
Trims the string to the specified size without error in case it is already shorter. Escapes ' and \ in a string so that the result can be passed into an SQL command. The parameter has be quoted using ' in the sql. Most database engines accept single quotes around numbers as well.Please note that special characters for LIKE and other matching commands are not quotes. The result of this method is suitable for INSERT, UPDATE and an "=" operator in the WHERE part.
- Parameters:
param
- string to quotesize
- maximal length of this string before encoding- Returns:
- quoted string
-
trimEmptyToNull
trims a string to null- Parameters:
s
- string- Returns:
- null, if the string was null or empty, s.trim() otherweise
-