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: Return type:
- 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: 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'