org.eclipse.datatools.enablement.sybase.asa.schemaobjecteditor.examples.utils
Class SQLUtil

java.lang.Object
  extended by org.eclipse.datatools.enablement.sybase.asa.schemaobjecteditor.examples.utils.SQLUtil

public class SQLUtil
extends java.lang.Object

Utility class for database/SQL related stuff.

Author:
Yang Liu

Field Summary
static int MATCHING_DOUBLE_QUOTES
           
static int MATCHING_SINGLE_QUOTES
           
static int MAX_NAME_LENGTH_FOR_MESSAGE_DIALOGS
           
static int NO_MATCHING_QUOTES
           
static int NO_QUOTES
           
static int TYPE_MUTIL_RECORDS
           
static int TYPE_XML_RESULT
           
 
Constructor Summary
SQLUtil()
           
 
Method Summary
static java.lang.String addDefOfAss(java.lang.String sql, org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier, java.util.Map varDecs, java.util.ArrayList refWithoutDef)
          add definition of assigned params to the orginal SQL string, add the referenced params to list "refWithoutDef".
static java.lang.String addDefOfAssAndRef(java.lang.String sql, org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier, java.util.Map varDecs)
          add definitions statements of referenced or assigned variables/params to the original sql statement
static java.lang.String constructDetailCALLSPUDFString(org.eclipse.datatools.sqltools.core.ProcIdentifier proc, java.util.List values, org.eclipse.datatools.sqltools.core.dbitem.ParameterDescriptor[] pds, boolean quoted_id)
           
static java.lang.String constructSPUDFString(org.eclipse.datatools.sqltools.core.ProcIdentifier proc, java.util.List values, org.eclipse.datatools.sqltools.core.dbitem.ParameterDescriptor[] pds, boolean quoted_id)
           
static java.lang.String constructTriggerEventString(org.eclipse.datatools.sqltools.core.ProcIdentifier proc, java.util.Map values, boolean quoted_id)
           
static int convert2SQLType(java.lang.String typeName)
           
static boolean equals(int parserType, int procType, java.lang.String objstr, java.lang.String defDB, java.lang.String defOwner, org.eclipse.datatools.sqltools.core.ProcIdentifier proc, boolean caseSensitive)
          Return whether objstr and proc represent the same database object.
static boolean equalsIgnoreQuote(java.lang.String s1, java.lang.String s2, boolean caseSensitive)
          Returns whether the 2 Strings are equal by ignoring the surrounding quotes
static int findQuotes(java.lang.String content)
           
static java.lang.String[] getAvaiableCurrencySymbols()
          return the all currencySymbols
static java.lang.String getCallablePrefix(int type)
           
static java.lang.String getCallableStatementPrefix(int type)
           
static java.sql.Connection getConnection(org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier)
          Returns the reusable connection from the control connection.
static org.eclipse.datatools.sqltools.core.IControlConnection getControlConnection(org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier)
          Get Control Connection from profile name
static IDatatype getDatatypeByName(java.lang.String typeName, DBObject[] dTypes)
          given the name and UDT list,return the IDatatype object
static int getDefaultPrecision(int sqlType)
           
static int getDefaultScale(int sqlType)
           
static char getEscapeCharacter(char character)
           
static java.lang.String[] getEventParameters(org.eclipse.datatools.sqltools.core.ProcIdentifier proc)
          parse the event handler body and return all the parameters of "event_parameter" function(ASA)
static org.eclipse.datatools.sqltools.core.dbitem.ParameterDescriptor[] getParameterDescriptors(org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier, java.sql.Connection connection, int type, org.eclipse.datatools.sqltools.core.ProcIdentifier proc)
           
static char getPeerCharacter(char character)
           
static java.lang.String getQuotedString(java.lang.String dataType, java.lang.String inputValue, org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier)
          This method is to convert the String(quoted or unquoted) to quoted String.
static java.util.Map getSPParamDefaultValues(org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier, java.lang.String sp)
          Parse the given sp definition and returns the parameter name/default value pairs.
static java.util.HashMap getSPParams(org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier, java.lang.String sp)
          Parse the given sp definition and returns the parameter name/IASTSQLParam pairs.
static java.util.Map getSPParamTypeNameMapFromParser(org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier, java.lang.String sp)
           
static int getSQLType(org.eclipse.datatools.sqltools.core.ProcIdentifier procId)
           
static int getSQLType(SQLEditor sqlEditor)
           
static boolean isBinaryType(IDatatype datatype)
           
static boolean isBinaryType(java.lang.String datatype)
           
static boolean isNumericType(IDatatype datatype)
           
static boolean isNumericType(int datatype)
           
static boolean isNumericType(java.lang.String datatype)
           
static boolean isSameType(int parserType, int procType)
          Return whether the object type represented by parserType and that by procType are of the same type
static boolean isStringType(IDatatype datatype)
           
static boolean isStringType(int sqlType)
           
static boolean isStringType(java.lang.String datatype)
           
static boolean justWarnings(java.sql.SQLException sqlexception)
          test whether a SQLException is warning only.
static java.lang.String[] parseDatabaseObject(java.lang.String objstr)
          "objstr" is a string representing a database object.
static java.lang.String quote(java.lang.String in, char quoteChar)
           
static java.lang.String quote(java.lang.String content, java.lang.String quoteMark)
          surround content with quoteMark and double every quoteMark inside content
static java.lang.String quoteStringWhenNecessary(java.lang.String objstr)
          Quotes the string when it contains space or single quote and is not properly quoted.
static java.lang.String quoteWhenNecessary(java.lang.String content, org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier)
          This is a convenience method of quoteWhenNecessary(String content, DatabaseIdentifier _databaseIdentifier, String quote)
static java.lang.String quoteWhenNecessary(java.lang.String id, org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier, java.lang.String quote)
          Checks the validity and quoted identifier setting and surrounds content with proper quotation mark if necessary.
static java.lang.String quoteWhenNecessary(java.lang.String id, org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier, java.lang.String quote, int identiferType)
          Checks the validity and quoted identifier setting and surrounds content with proper quotation mark if necessary.
static java.sql.SQLException retrieveDetailException(java.lang.Throwable throwable)
          Returns an instance of SQLException containing detailed error information
static void saveResultToStream(java.io.ObjectOutputStream oos, java.lang.Object result)
           
static java.lang.String stripNameForMessages(java.lang.String name)
          This method chops out a given String to a string with lenthg MAX_NAME_LENGTH_FOR_MESSAGE_DIALOGS, The usage is when an identifier is too long as much as 255 characters the message dialog cannot accomodate it
static java.lang.String unquote(java.lang.String quoted)
          remove the surrounding quotation mark and restore 2 successive quotation marks to 1
static java.lang.String unquoteWhenNecessary(java.lang.String id, org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier)
          Checks the validity of the unquoted identifier and unquotes content if necessary.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TYPE_MUTIL_RECORDS

public static final int TYPE_MUTIL_RECORDS
See Also:
Constant Field Values

TYPE_XML_RESULT

public static final int TYPE_XML_RESULT
See Also:
Constant Field Values

NO_QUOTES

public static final int NO_QUOTES
See Also:
Constant Field Values

MATCHING_SINGLE_QUOTES

public static final int MATCHING_SINGLE_QUOTES
See Also:
Constant Field Values

MATCHING_DOUBLE_QUOTES

public static final int MATCHING_DOUBLE_QUOTES
See Also:
Constant Field Values

NO_MATCHING_QUOTES

public static final int NO_MATCHING_QUOTES
See Also:
Constant Field Values

MAX_NAME_LENGTH_FOR_MESSAGE_DIALOGS

public static int MAX_NAME_LENGTH_FOR_MESSAGE_DIALOGS
Constructor Detail

SQLUtil

public SQLUtil()
Method Detail

parseDatabaseObject

public static java.lang.String[] parseDatabaseObject(java.lang.String objstr)
"objstr" is a string representing a database object. Possibly in following formats:
  • objname
  • ownername.objname
  • databasename.ownername.objname
  • databasename..objname
  • This method will try to figure it out, and return a string array. with the first element be the last segment in "objstr". In case of invalid objstr, will return null NOTE: it is allowed to have whitespace in objname/ownername/databasename, in that case, the name is quoted in "'". See SQL grammer for detail.

    Parameters:
    objstr - a string identifying a database object.
    Returns:
    string array

    equals

    public static boolean equals(int parserType,
                                 int procType,
                                 java.lang.String objstr,
                                 java.lang.String defDB,
                                 java.lang.String defOwner,
                                 org.eclipse.datatools.sqltools.core.ProcIdentifier proc,
                                 boolean caseSensitive)
    Return whether objstr and proc represent the same database object. If db name or owner name is omitted int objstr, defDB or defOwner will be used.

    Parameters:
    objstr -
    proc -
    Returns:

    equalsIgnoreQuote

    public static boolean equalsIgnoreQuote(java.lang.String s1,
                                            java.lang.String s2,
                                            boolean caseSensitive)
    Returns whether the 2 Strings are equal by ignoring the surrounding quotes

    Parameters:
    s1 -
    s2 -
    caseSensitive - whether to consider case
    Returns:

    isSameType

    public static boolean isSameType(int parserType,
                                     int procType)
    Return whether the object type represented by parserType and that by procType are of the same type

    Parameters:
    parserType - type defined in SQLParserContants
    procType - type defined in ProcIdentifier
    Returns:

    getParameterDescriptors

    public static org.eclipse.datatools.sqltools.core.dbitem.ParameterDescriptor[] getParameterDescriptors(org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier,
                                                                                                           java.sql.Connection connection,
                                                                                                           int type,
                                                                                                           org.eclipse.datatools.sqltools.core.ProcIdentifier proc)
                                                                                                    throws java.sql.SQLException
    Parameters:
    con -
    type -
    runstring -
    Returns:
    @throws SQLException
    Throws:
    java.sql.SQLException

    getEventParameters

    public static java.lang.String[] getEventParameters(org.eclipse.datatools.sqltools.core.ProcIdentifier proc)
                                                 throws java.sql.SQLException,
                                                        org.eclipse.datatools.sqltools.core.profile.NoSuchProfileException
    parse the event handler body and return all the parameters of "event_parameter" function(ASA)

    Parameters:
    profileName -
    eventName -
    Returns:
    @throws SQLException
    Throws:
    org.eclipse.datatools.sqltools.core.profile.NoSuchProfileException
    java.sql.SQLException

    getCallablePrefix

    public static java.lang.String getCallablePrefix(int type)
    Returns:
    "exec ", "call ", "TRIGGER EVENT " or "" based on type

    getCallableStatementPrefix

    public static java.lang.String getCallableStatementPrefix(int type)
    Returns:
    "exec ", "call ", "TRIGGER EVENT " or "" based on type

    constructDetailCALLSPUDFString

    public static java.lang.String constructDetailCALLSPUDFString(org.eclipse.datatools.sqltools.core.ProcIdentifier proc,
                                                                  java.util.List values,
                                                                  org.eclipse.datatools.sqltools.core.dbitem.ParameterDescriptor[] pds,
                                                                  boolean quoted_id)
    Parameters:
    proc - can be null
    list - must be valid value list
    pds -
    quoted_id -
    Returns:

    constructSPUDFString

    public static java.lang.String constructSPUDFString(org.eclipse.datatools.sqltools.core.ProcIdentifier proc,
                                                        java.util.List values,
                                                        org.eclipse.datatools.sqltools.core.dbitem.ParameterDescriptor[] pds,
                                                        boolean quoted_id)
    Parameters:
    proc - can be null
    list - must be valid value list
    pds -
    quoted_id -
    Returns:

    quote

    public static java.lang.String quote(java.lang.String in,
                                         char quoteChar)

    constructTriggerEventString

    public static java.lang.String constructTriggerEventString(org.eclipse.datatools.sqltools.core.ProcIdentifier proc,
                                                               java.util.Map values,
                                                               boolean quoted_id)
    Parameters:
    proc - can be null
    quoted_id -
    triggerParams -
    Returns:

    justWarnings

    public static boolean justWarnings(java.sql.SQLException sqlexception)
    test whether a SQLException is warning only.

    Parameters:
    sqlexception -
    Returns:

    saveResultToStream

    public static void saveResultToStream(java.io.ObjectOutputStream oos,
                                          java.lang.Object result)
                                   throws java.io.IOException
    Throws:
    java.io.IOException

    getSPParamDefaultValues

    public static java.util.Map getSPParamDefaultValues(org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier,
                                                        java.lang.String sp)
    Parse the given sp definition and returns the parameter name/default value pairs.

    Parameters:
    profileName - used to determine the db type
    sp - the sp definition
    Returns:

    getSPParamTypeNameMapFromParser

    public static java.util.Map getSPParamTypeNameMapFromParser(org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier,
                                                                java.lang.String sp)

    getSPParams

    public static java.util.HashMap getSPParams(org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier,
                                                java.lang.String sp)
    Parse the given sp definition and returns the parameter name/IASTSQLParam pairs.

    Parameters:
    databaseIdentifier - used to determine the db type
    sp - the sp definition
    Returns:

    convert2SQLType

    public static int convert2SQLType(java.lang.String typeName)

    getDefaultPrecision

    public static int getDefaultPrecision(int sqlType)

    getDefaultScale

    public static int getDefaultScale(int sqlType)

    getSQLType

    public static int getSQLType(org.eclipse.datatools.sqltools.core.ProcIdentifier procId)

    getSQLType

    public static int getSQLType(SQLEditor sqlEditor)

    getQuotedString

    public static java.lang.String getQuotedString(java.lang.String dataType,
                                                   java.lang.String inputValue,
                                                   org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier)
                                            throws java.lang.Exception
    This method is to convert the String(quoted or unquoted) to quoted String. If the original string is incorrectly quoted, Exception will be thrown.

    Parameters:
    pd -
    inputeValue -
    Returns:
    String
    Throws:
    java.lang.Exception

    findQuotes

    public static int findQuotes(java.lang.String content)

    quoteStringWhenNecessary

    public static java.lang.String quoteStringWhenNecessary(java.lang.String objstr)
    Quotes the string when it contains space or single quote and is not properly quoted.

    Parameters:
    objstr -
    Returns:

    quoteWhenNecessary

    public static java.lang.String quoteWhenNecessary(java.lang.String content,
                                                      org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier)
    This is a convenience method of quoteWhenNecessary(String content, DatabaseIdentifier _databaseIdentifier, String quote)

    Parameters:
    content - original identifier
    Returns:

    quoteWhenNecessary

    public static java.lang.String quoteWhenNecessary(java.lang.String id,
                                                      org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier,
                                                      java.lang.String quote,
                                                      int identiferType)
    Checks the validity and quoted identifier setting and surrounds content with proper quotation mark if necessary.

    Parameters:
    id - Identifier
    databaseIdentifier - database Identfier
    quote - quotes, maybe single quote or double quote
    IdentifierType - identfierType,see IIdentfierValidator API
    Returns:
    quoted String

    quoteWhenNecessary

    public static java.lang.String quoteWhenNecessary(java.lang.String id,
                                                      org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier,
                                                      java.lang.String quote)
    Checks the validity and quoted identifier setting and surrounds content with proper quotation mark if necessary.

    Parameters:
    id - original identifier
    Returns:

    unquoteWhenNecessary

    public static java.lang.String unquoteWhenNecessary(java.lang.String id,
                                                        org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier)
    Checks the validity of the unquoted identifier and unquotes content if necessary.

    Parameters:
    id - original identifier
    Returns:

    quote

    public static java.lang.String quote(java.lang.String content,
                                         java.lang.String quoteMark)
    surround content with quoteMark and double every quoteMark inside content

    Parameters:
    content -
    quoteMark -
    Returns:

    unquote

    public static java.lang.String unquote(java.lang.String quoted)
    remove the surrounding quotation mark and restore 2 successive quotation marks to 1

    Parameters:
    quoted -
    Returns:

    getPeerCharacter

    public static char getPeerCharacter(char character)

    getEscapeCharacter

    public static char getEscapeCharacter(char character)

    getDatatypeByName

    public static IDatatype getDatatypeByName(java.lang.String typeName,
                                              DBObject[] dTypes)
    given the name and UDT list,return the IDatatype object

    Parameters:
    typeName -
    dTypes -
    Returns:

    isBinaryType

    public static boolean isBinaryType(IDatatype datatype)

    isBinaryType

    public static boolean isBinaryType(java.lang.String datatype)

    isNumericType

    public static boolean isNumericType(java.lang.String datatype)

    isNumericType

    public static boolean isNumericType(int datatype)

    isNumericType

    public static boolean isNumericType(IDatatype datatype)

    isStringType

    public static boolean isStringType(java.lang.String datatype)

    isStringType

    public static boolean isStringType(IDatatype datatype)

    isStringType

    public static boolean isStringType(int sqlType)

    addDefOfAss

    public static java.lang.String addDefOfAss(java.lang.String sql,
                                               org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier,
                                               java.util.Map varDecs,
                                               java.util.ArrayList refWithoutDef)
    add definition of assigned params to the orginal SQL string, add the referenced params to list "refWithoutDef".

    Parameters:
    sql -
    databaseIdentifier -
    Returns:

    addDefOfAssAndRef

    public static java.lang.String addDefOfAssAndRef(java.lang.String sql,
                                                     org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier,
                                                     java.util.Map varDecs)
    add definitions statements of referenced or assigned variables/params to the original sql statement

    Parameters:
    sql -
    databaseIdentifier -
    varDecs -
    Returns:

    getConnection

    public static java.sql.Connection getConnection(org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier)
    Returns the reusable connection from the control connection. Since this connection is shared by other components, do not close it.

    Parameters:
    databaseIdentifier -
    Returns:

    getControlConnection

    public static org.eclipse.datatools.sqltools.core.IControlConnection getControlConnection(org.eclipse.datatools.sqltools.core.DatabaseIdentifier databaseIdentifier)
    Get Control Connection from profile name

    Parameters:
    profile -
    Returns:

    getAvaiableCurrencySymbols

    public static java.lang.String[] getAvaiableCurrencySymbols()
    return the all currencySymbols

    Returns:

    stripNameForMessages

    public static java.lang.String stripNameForMessages(java.lang.String name)
    This method chops out a given String to a string with lenthg MAX_NAME_LENGTH_FOR_MESSAGE_DIALOGS, The usage is when an identifier is too long as much as 255 characters the message dialog cannot accomodate it

    Parameters:
    name -
    Returns:

    retrieveDetailException

    public static java.sql.SQLException retrieveDetailException(java.lang.Throwable throwable)
    Returns an instance of SQLException containing detailed error information

    Parameters:
    throwable -
    Returns: