Connecting Tech Pros Worldwide Help | Site Map

SQL helper function updates

  #1  
Old October 18th, 2007, 08:38 AM
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,400
Here are the latest versions of My (as in mine) SQL helper functions. Please feel free to rename them if you use them.

The SELECT helper:
Expand|Select|Wrap|Line Numbers
  1. def MySQLSelect(table, arglist=(), argdict={}, **kwargs):
  2.     """Build an SQL SELECT command from the arguments:
  3.     Return a single string which can be 'execute'd.
  4.     arglist is a list of strings that are column names to get.
  5.     argdict and kwargs are two way to evaluate 'colName'=value
  6.     for the WHERE clause"""
  7.     # Allow NULL columns in the result set # pyodbc makes rows mutable so, use it!
  8.     a = ', '.join((arg, 'NULL')[arg is None] for arg in arglist)
  9.     args = argdict.copy()
  10.     args.update(kwargs)
  11.     for key, value in args.items():
  12.         args[key] = (str(value), repr(value))[isinstance(value, str)]
  13.     b = ''
  14.     if args:
  15.         b = 'WHERE %s' %' AND '.join(key + '=' + value
  16.                                      for key, value in args.items())
  17.  
  18.     return ' '.join(['SELECT', (a or '*'), 'FROM', table, b])
The INSERT and UPDATE helpers now convert None to NULL:
Expand|Select|Wrap|Line Numbers
  1.  
  2. def MySQLInsert(table, argdict={}, **kwargs):
  3.     """Build an SQL INSERT command from the arguments:
  4.     Return a single string which can be 'execute'd.
  5.     argdict is a dictionary of 'column_name':value items.
  6.     **kwargs is the same but passed in as column_name=value"""
  7.     args = argdict.copy()   # don't modify caller dictionary!
  8.     args.update(kwargs)
  9.     keys = args.keys() # an ordered list #
  10.     argslist = []
  11.     for key in keys:
  12.         a = args[key]  # argslist will match the order from above #
  13.         argslist.append(((str(a), repr(a))[isinstance(a, str)], "NULL")[a is None])
  14.     # wrap comma separated values in parens
  15.     a = '(%s)' %', '.join(field for field in keys)
  16.     b = '(%s)' %', '.join(argslist)
  17.     return ' '.join(['INSERT', table, a, 'VALUES', b])
  18.  
  19.  
  20. def MySQLUpdate(table, valuedict, argdict={}, **kwargs):
  21.     """Build an SQL SELECT command from the arguments:
  22.     Return a single string which can be 'execute'd.
  23.     valuedict is a dictionary of column_names:value to update.
  24.     argdict and kwargs are two way to evaluate 'colName'=value
  25.     for the WHERE clause."""
  26.     vargs = valuedict.copy()
  27.     for key, value in vargs.items():
  28.         vargs[key] = ((str(value), repr(value))[type(value) == str], "NULL")[value is None]
  29.     a = 'SET %s' % ', '.join(key + '=' + value
  30.                                   for key, value in vargs.items())
  31.     args = argdict.copy()
  32.     args.update(kwargs)
  33.     for key, value in args.items():
  34.         args[key] = (str(value), repr(value))[type(value) == str]
  35.     b = ''
  36.     if args:
  37.         b = 'WHERE %s' % ' AND '.join(key + '=' + value
  38.                                       for key, value in args.items())
  39.  
  40.     return ' '.join(['UPDATE', table, a, b])



Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Asp.net Important Topics. shamirza answers 0 January 18th, 2007 05:15 AM
Redirecting from a class Ryan Ternier answers 6 November 19th, 2005 03:08 PM