com.xmlserv.util
Class SQLUtil

java.lang.Object
  |
  +--com.xmlserv.util.SQLUtil

public class SQLUtil
extends java.lang.Object

A collection of static helper methods for handling SQL statements.

Version:
$Id: SQLUtil.java,v 1.17 2003/01/09 16:11:14 hans Exp $
Author:
Wolfram Saringer.

Field Summary
static char[] chars
          The characters to escape/unescape using escape/unscape utility methods.
 
Method Summary
static java.lang.String boolean2String(boolean flag)
          Convert a Boolean value to an SQL string representation.
static java.lang.String checkID(java.sql.Connection con, java.lang.String tablename, java.lang.String id)
          Check if the given value is a number and exists in the given table as an ID value.
static java.lang.String escape(java.lang.String input)
          Add backslashes before offensive characters to make the string usable in an SQL query.
Specifically the following transformations are done: The following characters are prefixed with a backslash: ' ; \
static java.lang.String[] escape(java.lang.String[] input)
           
static java.lang.String escape(java.lang.StringBuffer work)
          Avoid thread concurrency problems: work StringBuffer must be provided by caller.
static org.jdom.Element getElement(java.sql.ResultSet rs, java.lang.String fieldname)
          See above.
static org.jdom.Element getElement(java.sql.ResultSet rs, java.lang.String fieldname, java.lang.String tagname)
          Parse the given field in the ResultSet, returning the named (firstlevel) child as a detached org.jdom.Element to be inserted into another JDOM tree.
static java.lang.String getID(java.sql.Connection con, java.lang.String table, java.lang.String column, java.lang.String value)
          Get the ID for the given name.
static java.lang.String md5(java.lang.String input)
          Add missing MD5() function for Postgres.
static java.lang.String unescape(java.lang.String input)
          Remove backslashes added by escape().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

chars

public static char[] chars
The characters to escape/unescape using escape/unscape utility methods. This can be set, although it is not recommended to do so.
Method Detail

escape

public static java.lang.String escape(java.lang.String input)
Add backslashes before offensive characters to make the string usable in an SQL query.
Specifically the following transformations are done:

escape

public static java.lang.String escape(java.lang.StringBuffer work)
Avoid thread concurrency problems: work StringBuffer must be provided by caller.
Parameters:
input - The string to process (as a StringBuffer).
Returns:
The escaped string.

escape

public static java.lang.String[] escape(java.lang.String[] input)

unescape

public static java.lang.String unescape(java.lang.String input)
Remove backslashes added by escape().
Parameters:
input - The input string to unescape.
Returns:
The String with the backslashes before offending characters removed.

md5

public static java.lang.String md5(java.lang.String input)
Add missing MD5() function for Postgres.
Parameters:
input - The String to calculate the hash for.
Returns:
The MD5 sum of the given String.

getElement

public static org.jdom.Element getElement(java.sql.ResultSet rs,
                                          java.lang.String fieldname,
                                          java.lang.String tagname)
                                   throws java.sql.SQLException,
                                          org.jdom.JDOMException,
                                          XMLServException
Parse the given field in the ResultSet, returning the named (firstlevel) child as a detached org.jdom.Element to be inserted into another JDOM tree.
Parameters:
rs - The ResulktSet to read data from.
fieldname - The name of the datafield.
tagname - The name of the tag to extract. If null, the root Element is returned.
Returns:
The Element.
Throws:
java.sql.SQLException - Upon error reading from the ResultSet.
org.jdom.JDOMException - Upon an error parsing the field content.

getElement

public static org.jdom.Element getElement(java.sql.ResultSet rs,
                                          java.lang.String fieldname)
                                   throws java.sql.SQLException,
                                          org.jdom.JDOMException,
                                          XMLServException
See above. Get root element.
See Also:
getElement(ResultSet, String, String)

checkID

public static java.lang.String checkID(java.sql.Connection con,
                                       java.lang.String tablename,
                                       java.lang.String id)
                                throws java.sql.SQLException,
                                       java.lang.NumberFormatException
Check if the given value is a number and exists in the given table as an ID value.
Parameters:
con - The DB connection object.
tablename - The name of the table.
id - The ID to search for.
Returns:
The number as a String if everything is OK.
Throws:
java.sql.SQLException - If the ID does not exist or on other SQL error.
java.lang.NumberFormatException - If the given String cannot be converted to a number.

getID

public static java.lang.String getID(java.sql.Connection con,
                                     java.lang.String table,
                                     java.lang.String column,
                                     java.lang.String value)
                              throws java.sql.SQLException
Get the ID for the given name.
Parameters:
con - A Connection.
table - The tablename.
column - The fieldname to match (must be a unique column).
value - The value to search for.
Returns:
The ID associated with the given name.
Throws:
java.sql.SQLException - A DB error occurred.

boolean2String

public static java.lang.String boolean2String(boolean flag)
Convert a Boolean value to an SQL string representation.
Parameters:
flag - The value to convert.
Returns:
The SQL Strign representation.