[Zope-dev] adding a ZSQLmethod programmaticaly

Steve Spicklemire steve@spvi.com
Tue, 7 Aug 2001 15:11:45 -0500


Hi Joachim,

	Here is a python method I've used to automate the creation of=20
select queries. It uses ZPatterns to create an instance to get the field=20=

names etc... but the query object creation part should not depend on=20
that.

-steve

import string
newObj =3D context.newItem('foobie' + `container.idCount`) # create a=20
bogus temp instance..
container.manage_changeProperties(idCount =3D container.idCount + 1)

typeMap =3D {'string':'string', 'date':'float'}

if not primaryKey:
	primaryKey =3D string.lower(context.id[:-1]) + 'ID'

argumentList =3D ['%s=3D""' % primaryKey]
itemList =3D [primaryKey]
typeList =3D ['string']

templateString =3D ''

for item_pair in newObj.propertysheets.items():
			=09
	if item_pair[0] !=3D 'webdav':
		name, ps_value =3D item_pair
	=09
		pm =3D ps_value.propertyMap()
	=09
		templateString =3D templateString + "\n select "
	=09
		for item in pm:
			itemList.append(item['id'])
			typeList.append(item['type'])
			argumentList.append('%s=3D""' % item['id'])

		templateString =3D templateString + =
string.join(itemList,', ')

newObj.manage_delete()

argumentString =3D string.join(argumentList,' ')

whereClause =3D """
<dtml-sqlgroup where>
%s
</dtml-sqlgroup>
"""

simpleGroup =3D """
<dtml-sqlgroup>
<dtml-if %s>
<dtml-sqltest %s type=3D%s>
</dtml-if>
</dtml-sqlgroup>
"""

whereList =3D []

for i in range(len(itemList)):
	item =3D itemList[i]
	theType =3D typeMap.get(typeList[i],'string')
=09
	whereList.append( simpleGroup % (item, item, theType))
=09
whereTemplate =3D string.join(whereList,'<dtml-and>')

templateString =3D templateString + " from %s_%s " % (context.id, name)

templateString =3D templateString + whereClause % whereTemplate

if not hasattr(context,sqlName):
	fc =3D context.manage_addProduct['ZSQLMethods']
	fc.add.manage_addZSQLMethod(sqlName,'', connection_id=3DdbName,=20=

arguments=3DargumentString, template=3DtemplateString)

else:
	theSQL =3D getattr(context, sqlName)
	theSQL.manage_edit( '', connection_id=3DdbName,=20
arguments=3DargumentString, template=3DtemplateString)
=09
return "OK"


On Tuesday, August 7, 2001, at 01:06 PM, Joachim Schmitz wrote:

>

> http://www.myhost.de/manage_addZSQLMethod
>
> I am quite sure, that the name is correct, so what's wrong here ?
>
>
> Mit freundlichen Gr=FC=DFen
>
> Joachim Schmitz
>
> AixtraWare, Ing. B=FCro f=FCr Internetanwendungen
> H=FCsgenstr. 33a, D-52457 Aldenhoven
> Telefon: +49-2464-8851, FAX: +49-2464-905163
>
>
> _______________________________________________
> Zope-Dev maillist  -  Zope-Dev@zope.org
> http://lists.zope.org/mailman/listinfo/zope-dev
> **  No cross posts or HTML encoding!  **
> (Related lists -
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope )