Ejemplo n.º 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_
Ejemplo n.º 2
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
Ejemplo n.º 3
0
 def test_isdate(self):
     self.assertEqual(data.isdate('1/1/2005'), True)
     self.assertEqual(data.isdate('1-1-2005'), True)
     self.assertEqual(data.isdate('2005-01-01'), True)
     self.assertEqual(data.isdate('1/1/2005 10:45'), True)
     self.assertEqual(data.isdate('1/1/2005 10:45:00'), True)
     self.assertEqual(data.isdate('1/1/20050'), False)
     self.assertEqual(data.isdate(None), False)
     self.assertEqual(data.isdate('a'), False)
     self.assertEqual(data.isdate(1), False)
     self.assertEqual(data.isdate(''), False)
     self.assertEqual(data.isdate("'"), False)
Ejemplo n.º 4
0
 def test_isdate(self):
     self.assertEqual(data.isdate("1/1/2005"), True)
     self.assertEqual(data.isdate("1-1-2005"), True)
     self.assertEqual(data.isdate("2005-01-01"), True)
     self.assertEqual(data.isdate("1/1/2005 10:45"), True)
     self.assertEqual(data.isdate("1/1/2005 10:45:00"), True)
     self.assertEqual(data.isdate("1/1/20050"), False)
     self.assertEqual(data.isdate(None), False)
     self.assertEqual(data.isdate("a"), False)
     self.assertEqual(data.isdate(1), False)
     self.assertEqual(data.isdate(""), False)
     self.assertEqual(data.isdate("'"), False)