chula.db.functions – Database helper functions

Functions to make working with databases easier

chula.db.functions.cbool(string)

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.

Parameters:string (str) – String to be cleaned
Return type:str TRUE, FALSE, or NULL
>>> from chula.db import functions
>>> print 'SET active = %s;' % functions.cbool(True)
SET active = TRUE;
>>>
>>> print 'SET active = %s;' % functions.cbool(False)
SET active = FALSE;
>>>
>>> print 'SET active = %s;' % functions.cbool(None)
SET active = NULL;
chula.db.functions.cdate(string, 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.

Note

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

Parameters:string (str) – Date to be cleaned
Return type: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();
chula.db.functions.cfloat(flt)

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.

Parameters:flt (Anything) – Float to be cleaned
Return type:float, or NULL
>>> from chula.db import functions
>>> print 'WHERE field = %s;' % functions.cfloat("45")
WHERE field = 45.0;
>>>
>>> print 'WHERE field = %s;' % functions.cfloat(None)
WHERE field = NULL;
chula.db.functions.cint(integer)

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.

Parameters:integer (Anything) – Integer to be cleaned
Return type:int, or NULL
>>> from chula.db import functions
>>> print 'WHERE field = %s;' % functions.cint("45")
WHERE field = 45;
chula.db.functions.cregex(string, 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.

Parameters:
  • string (str) – Value to evaluate
  • doquote (bool) – optionally wrap in single quotes, default is True
Return type:

str

chula.db.functions.cstr(string, 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.

Parameters:
  • string (str) – String to be cleaned
  • doquote (bool) – Optionally wrap in single quotes, default is True
  • doescape (bool) – Optionally escape single quotes, default is True
Return type:

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;
chula.db.functions.ctags(string)

Returns a string safe for use in a sql statement

Param :string
Return type:NULL, or str
>>> from chula.db import functions
>>> print functions.ctags('')
NULL
>>> print functions.ctags('linux git foo')
'foo git linux'
chula.db.functions.empty2null(string)

Returns NULL if an empty string or None is passed, else returns the string string.

Param :string
Return type:NULL, or str
>>> from chula.db import functions
>>> print functions.empty2null('')
NULL
chula.db.functions.unquote(string)

Return string not padded with single quotes. This is useful to clean something changed by cstr()

Param :string
Return type:str, or input unchanged

Previous topic

chula.data – Data helper methods

Next topic

chula.ecalendar – Calendar helper

This Page