|
Description:
Automates the creation of SQL INSERT statements for the "simple" attributes in a python object by creating a string of an object's attribute names and a corresponding string of that object's attribute values. Simple attributes are those that are one of the following types: string, int, long, float, boolean, None.
Source: Text Source
import types
def makeObjInsertStrings( obj, tplObjects = None, blnUseParens=True, blnGetAllAttrib=True ):
if not tplObjects:
return None, None, None
if isinstance( obj, tplObjects ):
strDblQuote = '"'
lstCols = list()
lstVals = list()
lstExcludedAttrib = list()
dctObj = vars( obj )
lstObjVarNames = dctObj.keys()
if blnGetAllAttrib:
tplValidTypes = ( types.BooleanType, types.FloatType, types.IntType, types.LongType, types.StringType, types.StringTypes, types.NoneType )
for varName in lstObjVarNames:
val = dctObj[ varName ]
if isinstance( val, tplValidTypes ):
lstCols.append( varName )
if val or val == 0:
lstVals.append( dctObj[ varName ] )
else:
lstVals.append('')
else:
lstExcludedAttrib.append( varName )
if blnUseParens:
strCols = joinListItems( lstCols )
strVals = joinListItems( lstVals )
else:
strCols = joinListItems( lstCols, blnUseParens=False )
strCols = joinListItems( lstVals, blnUseParens=False )
strCols = strCols.replace('"', '')
return strCols, strVals, lstExcludedAttrib
else:
print 'No object passed.'
return None, None, None
def getValueStrings( val, blnUgly=True ):
tplStrings = (types.StringType, types.StringTypes )
tplNums = ( types.FloatType, types.IntType, types.LongType, types.BooleanType )
if isinstance( val, tplNums ):
return '#num#'+ str( val ) + '#num#'
elif isinstance( val, tplStrings ):
strDblQuote = '"'
return strDblQuote + val + strDblQuote
else:
if blnUgly == True:
return "Error: nonconvertable value passed - value type: %s" % type(val )
else:
return None
def joinListItems( lstStart, strDelim = ',', strNumDelim='#num#', blnUseParens = True ):
if strDelim == ',':
strResult = reduce( joinWithComma, lstStart )
strResult = strResult.replace(strNumDelim+'"', '')
strResult = strResult.replace(strNumDelim, '')
strResult = '(' + strResult + ')'
strResult = strResult.replace('", ")', '", "")')
strResult = strResult.replace(', ",', ', "",')
strResult = strResult.replace('"", ",', '"", "",')
if strResult[0:3] == '(",':
strResult = '("",' + strResult[3:]
if not blnUseParens:
strResult = strResult[1:len(strResult)-1]
return strResult
def joinWithComma( x, y ):
strX = getValueStrings( x )
strY = getValueStrings( y )
if strX:
if strY:
strResult = strX + ', ' + strY
strResult = strResult.replace('""', '"')
else:
strResult = x
else:
if strY:
strResult = y
else:
strResult = ""
return strResult
if __name__ == '__main__':
class simple():
def __init__(self):
self.Name = ""
self.Codes = list()
self.NumCodes = 0
self.EntryType = None
self.OtherValue = 0
self.Comment = None
t = simple()
t.Name = "MyName"
t.Comment = ""
t.NumCodes = 1
t.OtherValue = 0
tplObjects = ( simple )
strCols, strVals, lstExcluded = makeObjInsertStrings( t, tplObjects )
print 'Columns: %s\nValues: %s' % (strCols, strVals )
print 'List of Attributes ignored:'
for attr in lstExcluded:
print " Name of attribute in t: ", attr, ' - which is of type ', type( eval("t."+attr) )
Discussion:
First, I'd like to thank everyone for sharing their recipes. It has made my process of learning Python much easier.
This is my first shared recipe, so I apologize in advance if it isn't up to snuff, but I thought someone else might find some value in it. I've identified places where I might extend it in the future.
Any comments or suggestions (including, but not limited to flames) are appreciated. I hope at least one person finds this helpful.
I created these functions to facilitate the creation of SQL Insert statements from objects. The function is designed to generate strings to be copied into a SQL statement of the form:
INSERT INTO SOMETABLE (colname1, colname2, . . .)
VALUES (col1value, col2value, . . . )
The first two values in the returned tuple ( strCols and StrVals ) can be pasted into the above statement, which, when assigned to a string variable, would look like:
strSQLStatement = "INSERT INTO SOMETABLE " + strCols + " Values " + strValues
(Note: by default, the function returns strCols and strVals already enclosed in parentheses.)
The resulting statment can then be passed to SQL via a cursor.execute() command. It does assume that there exists a db connection and that a database has been chosen that has a table named SOMETABLE that has column names that correspond exactly to the attribute names of the object passed to the function.
If an invalid object is passed, the function returns the tuple None, None, None.
The function currently requires at least two parameters, an instance of an object and a tuple of all objects that you want to store in a SQL table.
I hope to add a feature in the future that will allow the valid object tuple to be optional and the default would be all user-defined objects in the current namespace.
The third value in the tuple returned by the function is a list of the object attribute names that were skipped because they were not either a string, number, boolean or null value. Lists, dictionaries, tuples and other objects are excluded from both lists (as they won't make much sense to a normal SQL table).
The values in strVals will be separated by commas and string values will be surrounded with double quotes, numbers will not.
Please feel free to send comments/suggestions/problems to me directly (konsta@speakeasy.org) or just post them here.
---Andrew
|