示例#1
0
def cdate(input_, doquote=True, isfunction=False):
    """
    Returns a formatted string safe for use in SQL. If None or an empty
    string is passed, it will return 'NULL' so as to insert a NULL value
    into the database.
    
    B{Todo:}
    I{This function needs to be able to receive datetime.datetime types too.}
    
    @param input_: Date to be cleaned
    @type input_: String
    @return: String, or 'NULL'
    
    >>> print 'SET updated = %s;' % cdate('1/1/2005')
    SET updated = '1/1/2005';
    
    >>> print 'SET updated = %s;' % cdate('now()', isfunction=True)
    SET updated = now();
    """
    
    if input_ in [None, '', 'NULL']:
        return 'NULL'

    elif isfunction:
        return input_

    else:
        input_ = str(input_)
        if data.isdate(input_):
            if doquote:
                input_ = data.wrap(input_, "'")
        else:
            raise error.TypeConversionError(input_, 'sql date')

    return input_
示例#2
0
def cstr(input_, doquote=True, doescape=True):
    """
    Returns a formatted string safe for use in SQL. If None is passed, it
    will return 'NULL' so as to insert a NULL value into the database.
    Single quotes will be escaped.
    
    @param input_: String to be cleaned
    @type input_: String
    @param doquote: I{OPTIONAL}: Wrapped in single quotes, defaults to B{True}
    @type doquote: bool
    @param doescape: I{OPTIONAL}: Escape single quotes, defaults to B{True}
    @type doescape: bool
    @return: String, or 'NULL'
    
    >>> print 'SET description = %s;' % cstr("I don't")
    SET description = 'I don''t';
    >>> print 'SET now = %s;' % cstr("CURRENT_TIME", doquote=False)
    SET now = CURRENT_TIME;
    """
    
    if input_ is None:
        return 'NULL'
    
    input_ = str(input_) 
    if doescape:
        escape = {"'":"''", "\\":"\\\\"}
        input_ = data.replace_all(escape, input_)

    if doquote:
        return data.wrap(input_, "'")
    else:
        return input_
示例#3
0
def cstr(string, doquote=True, doescape=True):
    """
    Returns a formatted string safe for use in SQL. If :class:`None`
    is passed, it will return ``NULL`` so as to insert a NULL value
    into the database.  Single quotes will be escaped.

    :param string: String to be cleaned
    :type string: :class:`str`
    :param doquote: Optionally wrap in single quotes, default is :class:`True`
    :type doquote: bool
    :param doescape: Optionally escape single quotes, default is :class:`True`
    :type doescape: :class:`bool`
    :rtype: :class:`str`, or ``NULL``

    >>> from chula.db import functions
    >>> print 'SET description = %s;' % functions.cstr("I don't")
    SET description = 'I don''t';
    >>>
    >>> print 'SET now = %s;' % functions.cstr("CURRENT_TIME", doquote=False)
    SET now = CURRENT_TIME;
    """

    if string is None:
        return 'NULL'

    string = str(string)
    if doescape:
        escape = {"'":"''", "\\":"\\\\"}
        string = data.replace_all(escape, string)

    if doquote:
        return data.wrap(string, "'")
    else:
        return string
示例#4
0
def cregex(input_, doquote=True):
    """
    Returns a regular expression safe for use in SQL.  If None is
    passed if will raise an exception as None is not a valid regular
    expression.  The intented use is with regex based SQL expressions.


    @param input_: Value to evaluate
    @type input_: str
    @param doquote: I{OPTIONAL}: Wrapped in single quotes, defaults to B{True}
    @type doquote: bool
    @return: str
    """

    if data.isregex(input_):
        if doquote:
            return data.wrap(input_, "'")
        else:
            return input_
    else:
        raise error.TypeConversionError(input_, 'sql regex')
示例#5
0
def cregex(string, doquote=True):
    """
    Returns a regular expression safe for use in SQL.  If
    :class:`None` is passed if will raise an exception as None is not
    a valid regular expression.  The intented use is with regex based
    SQL expressions.


    :param string: Value to evaluate
    :type string: :class:`str`
    :param doquote: optionally wrap in single quotes, default is :class:`True`
    :type doquote: :class:`bool`
    :rtype: :class:`str`
    """

    if data.isregex(string):
        if doquote:
            return data.wrap(string, "'")
        else:
            return string
    else:
        raise error.TypeConversionError(string, 'sql regex')
示例#6
0
def cdate(string, doquote=True, isfunction=False):
    """
    Returns a formatted string safe for use in SQL. If :class:`None` or an empty
    string is passed, it will return ``NULL`` so as to insert a NULL value
    into the database.

    .. note::

       Todo: This function needs to be able to receive
       datetime.datetime types too.

    :param string: Date to be cleaned
    :type string: :class:`str`
    :rtype: :class:`str`, or ``NULL``

    >>> from chula.db import functions
    >>> print 'SET updated = %s;' % functions.cdate('1/1/2005')
    SET updated = '1/1/2005';

    >>> print 'SET updated = %s;' % functions.cdate('now()', isfunction=True)
    SET updated = now();
    """

    if string in [None, '', 'NULL']:
        return 'NULL'

    elif isfunction:
        return string

    else:
        string = str(string)
        if data.isdate(string):
            if doquote:
                string = data.wrap(string, "'")
        else:
            raise error.TypeConversionError(string, 'sql date')

    return string