Functions to make working with databases easier
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;
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();
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;
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;
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: |
|
---|---|
Return type: | str |
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: |
|
---|---|
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;
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'
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
Return string not padded with single quotes. This is useful to clean something changed by cstr()
Param : | string |
---|---|
Return type: | str, or input unchanged |