Because the book that bought this machine was Python, so learning Python made a simple MVC small framework of web development with Python. The whole framework is divided into physical layer, data access layer, business base class layer, Web request forwarding layer, control layer and view layer. There are, of course, some ancillary tool classes. Here is a brief introduction, I hope the same as I beginner python a little help:
1) Physical Layer
Consists of the entity base class and the user entity class. Entity classes can be automatically generated by the database and provide an entity that dbtools to automatically generate rules. Here is the entity base class:
Import threading# user error class for throwing custom exceptions class Customerror (runtimeerror): def __init__ (Self,args): self.args=args# entity The base class. Class Entityb:def __init__ (self): self. Currfields=[] #根据属性名获取属性值 def getvaluebyname (self,fieldname): If Hasattr (self,fieldname): return g Etattr (self,fieldname) return None #根据属性名设置属性值 def setvaluebyname (self,fieldname,value): If Hasattr (SE Lf,fieldname): Return SetAttr (Self,fieldname,value) #定义了该属性, Object Enumerable. def __getitem__ (Self,key): If Type (key) ==type (' abc '): Return self. Getvaluebyname (key) if Type (key) ==type (1): Thefld = self. Currfields[key] return self. Getvaluebyname (THEFLD) return None #设置属性值, key can be an index, or it can be a property name. def __setitem__ (self,key,value): If Type (key) ==type (' abc '): Self. Setvaluebyname (Key,value) if Type (key) ==type (1): Thefld = self. Currfields[key] Self. Setvaluebyname (ThefLd,value) #获取实体的表名. def gettablename (self): Thetype = Type (self) if hasattr (Thetype, ' TableName '): Return GetAttr (TheT ype, ' TableName ') return ' #获取关键字段名 def Getkeyfield (self): Thetype = Type (self) if hasattr (TheT ype, ' Keyfield '): Return GetAttr (Thetype, ' Keyfield ') return ' #获取字段名集合 def getfields (self): Thetype = Type (self) if hasattr (Thetype, "Fields"): Return GetAttr (Thetype, ' Fields ') return [] Insertcondition = Threading. Condition () insertlocksign = False deletecondition = Threading. Condition () deletelocksign = False updateallcondition = Threading. Condition () updatealllocksign = False insertsqlname= ' __insertsql ' updateallsqlname= ' __updatesql ' delbypksq Lname= ' __delbypksql ' defaultselectsql= ' __defaultselectsql ' #根据属性名获取类型的属性值. def _getclassvalue (self,attrname): Theselftype = Type (self) thevalue = "If HasatTR (theselftype,attrname): Thevalue=getattr (theselftype,attrname) return thevalue #根据属性名设置类型的属性值. def _setclassvalue (self,attrname,value): Theselftype = Type (self) thevalue = "If Hasattr (theselftype , attrname): SetAttr (theselftype,attrname,value) #获取字段参数 def getfieldparams (self): return self._getc Lassvalue (' Fieldparams ') #获取插入的SQL def getinsertsql (self): Thesql =self._getclassvalue (Entityb.insertsqlname) if (Thesql==none or thesql== "): EntityB.InsertCondition.acquire () try:if entit YB.InsertLockSign:EntityB.InsertCondition.wait () insertlocksign = True TheS QL =self._getclassvalue (Entityb.insertsqlname) if (Thesql==none or thesql== "): Thetablena Me=self. Gettablename () thefields=self. GetFields () if thetablename== "or thefields==[]: RAIse customerror (' Table name or field is empty! ') thesql= ' INSERT into ' + thetablename theflds= ' thevals= ' Thefldparams = self. Getfieldparams () for Thef in Thefields:if theflds== ': Theflds + = Thef thevals + = thefldparams[thef][' dsfmt '] else: Theflds + = ', ' +thef thevals + = ', ' +thefldparams[thef][' dsfmt '] the sql+= ' (' +theflds+ ') VALUES (' +thevals+ ') ' Self._setclassvalue (entityb.insertsqlname,thesql) return thesql Finally:insertlocksign=false EntityB.InsertCondition.notify () EntityB.InsertCondition.release () Else:return thesql #获取根据主键删除SQL def getdelbypksql (self): Thesql =self._getclassvalue (Entityb.delbypksqlname) if (Thesql==none or thesql== "): EntityB.DeleteCondition.acquire () try:if entityb.deletelocksign: EntityB.DeleteCondition.wait () deletelocksign = True Thesql =self._getclassvalue (entityb . Delbypksqlname) if (Thesql==none or thesql== "): Thetablename=self. Gettablename () thekeyfield=self. Getkeyfield () If thetablename== ' or thekeyfield== ': Raise Customerror (' table name or PRIMARY key is empty! ') Thefldparams = self. Getfieldparams () thesql= ' DELETE from ' + thetablename+ ' WHERE ' +thekeyfield+ ' = ' +thefldparams[thekeyfield] [' Dsfmt '] self._setclassvalue (entityb.delbypksqlname,thesql) return Thesql final Ly:deletelocksign=false EntityB.DeleteCondition.notify () entityb.deleteconditio N.release () Else:return Thesql #获取更新所有字段的SQL语句 (updated based on primary key) def getupdateallsql (self): Thesql =self._getclassvalue (Entityb.updateallsqlname) if (Thesql==none or TheSQL = = "): EntityB.UpdateAllCondition.acquire () try:if entityb.updatealllocksign: EntityB.UpdateAllCondition.wait () updatealllocksign = True Thesql =self._getclassvalu E (Entityb.updateallsqlname) if (Thesql==none or thesql== "): Thetablename=self. Gettablename () thefields=self. GetFields () thekeyfield=self. Getkeyfield () If thetablename== ' or thefields==[] or thekeyfield== ': Raise Custo Merror (' table name, primary key, or field is empty! ') thesql= ' UPDATE ' + thetablename + ' SET ' theflds= ' thefldparams = Self. Getfieldparams () for Thef in Thefields:if (thef! = Thekeyfield): If theflds== ': Theflds + = thef+ ' = ' +thefldparams[thef][' dsfmt '] else: Theflds + = ', ' +thef+ ' = ' +thefldparams[thef][' dsfmt '] thesql+= theflds + ' WHERE ' +thekeyfield+ ' = ' + Thefldparams[thekeyfield] Self._setclassvalue (entityb.updateallsqlname,thesql) return TheS QL Finally:updatealllocksign=false EntityB.UpdateAllCondition.notify () EntityB.UpdateAllCondition.release () Else:return thesql #获取缺省的查询SQL def getdefaultselectsql (self ): Thetablename=self. Gettablename () return ' SELECT * from ' + thetablename + ' WHERE 1=1 ' #def __delitem__ (self)
The general operation of SQL for entities is generated dynamically, but not every time, but rather cached under a type. For the entity, make the following recommendations:
1) Common SQL can be generated, or dynamically generated, but cached;
2) General query SQL, especially with conditional, try not to generate dynamically, do not use similar to the EF framework, whether it is management performance or expansion is a disaster;
3) Generally do not solidify the entity relationship, such as early hibernate, that is, in the database also do not build outside relations, to business needs to translate into business logic, the general system is larger, or in a distributed structure, to solidify the entity relationship, or to put the entity relationship mapping into memory or configuration file, is a disaster.
The following class is the Accessibility class:
#Generate entitiesimport businessbasedef getfmtstr (datatype): If Datatype.find (' varchar ') >=0:return '%s ' Elif datatype.find (' date ') >=0:return '%s ' Else:return '%s ' def inttostr (idata=0): if Idata==none: Return ' 0 ' return '%d '% Idata;thefile = open (R "entitiesl.py", "w") Try:thedb = Businessbase.businessbase (obj ECT) #这里需改为从你自己的数据库XXXX thetabs = thedb.getdbtables (' XXXX ') thefile.write ("Import entitybase\n") for TheTa B in TheTabs:theFile.write (' Class m_ ' +thetab.tablename.upper () + ' (entitybase.entityb): \ n ') thefile.write (' def __init__ (self): \ n ') thefile.write (' m_ ' +thetab.tablename.upper () + '). Count + = 1\n ') thefile.write (' Self. Refdict={}\n ') thefile.write (' Self. Currfields=[]\n ') thefile.write (' count=0\n ') thefile.write (' TableName =\ ' +thetab.tablename+ ' \ ' \ n ') thekeyfield= ' theflds= ' thefile.write (' Fieldparams={\n ') thefields=thedb.gettabfields (' Tian ', thetab.tablename) Theindex =0 for Thef in Thefields: If Thef.iskey==1:thekeyfield = thef.column_name Theindex + = 1 if (theindex>1): Thefile.write (', \ n ') theflds+= ', \ ' +thef.column_ Name+ ' \ ' else:theflds= ' \ ' +thef.column_name+ ' thefile.write (' \ ' +thef.column_name+ ' \ ': \ n ') thefile.write (' {\ n ') thefile.write (' \ ' dsfmt\ ': \ ' +getfmtstr (thef.data_type) + ' \ ', \ n ') thefile.write (' \ ' DataType \ ': \ ' +thef.data_type+ ' \ ', \ n ') thefile.write (' \ ' length\ ': ' +inttostr (THEF.LENGTHB) + ', \ n ') ) thefile.write (' \ ' precision\ ': ' +inttostr (Thef.precisionlen) + ', \ n ') thefile. Write (' \ ' scale\ ': ' +inttostr (thef.scalelen) + ' \ n ') thefile.write ('}\n ') THEFILE.WR Ite ('}\n ') thefile.write (' Keyfield =\ ' +thekeyfield+ ' \ \ n ') thefile.write (' Fields =[ ' +theflds+ ']\n ') finally:theFile.close ()
#这个类用于获取对象的自定义属性.
Import Inspectimport typesclass objopt: @staticmethod def isproperty (obj): if (obj.__class__ is types. Functiontype): return False else: return True @staticmethod def getpropertynames (obj): Theattrs = Inspect.getmembers (obj,objopt.isproperty) theretattrs = [] for attr in Theattrs: bb=attr[0]. StartsWith (' __ ') if Bb==false: theretattrs.append (attr[0]) return theretattrs; #获取类名 @staticmethod def getclassname (OBJ): return obj.__name__
This is an example of an automatically generated entity class:
Class M_ss01_sys_usr (ENTITYBASE.ENTITYB): def __init__ (self): M_SS01_SYS_USR. Count + = 1 self. refdict={} self. currfields=[] count=0 TableName = ' ss01_sys_usr ' keyfield= ' usr_id ' fields=[' usr_id ', ' usr_name ', ' usr_pwd '] Fi eldparams={' usr_id ': {' dsfmt ': '%s ', ' DataType ': ' varchar ', ' Length ': ' Precision ': 0, ' scale ': 0}, ' Usr_name ': {' dsfmt ': '%s ', ' DataType ': ' varchar ', ' Length ': ' Precision ': 0, ' scale ': 0}, ' Usr_ PWD ': {' dsfmt ': '%s ', ' DataType ': ' varchar ', ' L Ength ':, ' Precision ': 0, ' scale ': 0} }
A simple Python MVC framework (1)