Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/twisted/enterprise/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyc

ó
[³XMc@sˆddlmZddlmZmZmZmZddlmZddlm	Z	ddl
mZdejfd„ƒYZ
dgZdS(	iÿÿÿÿ(t	reflector(tDBErrortgetKeyColumntquotetsafe(t
_TableInfo(t	RowObject(treflecttSQLReflectorcBsðeZdZdZidej6dej6dej6dej6Z	d„Z
d„Zd„Zd	„Z
d
„Zd„Zddddd„Zd
„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„ZRS(s
    DEPRECATED.
    
    I reflect on a database and load RowObjects from it.

    In order to do this, I interrogate a relational database to
    extract schema information and interface with RowObject class
    objects that can interact with specific tables. 
    it=t<t>tlikecCs tjj||ƒ||_dS(s*Initialize me against a database.
        N(Rt	Reflectort__init__tdbpool(tselfRt
rowClasses((sC/usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyRscCs|jƒdS(N(t_transPopulateSchema(R((sC/usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyt	_populate%scCs\xL|jD]A}t|tƒs>tdtj|jƒƒ‚n|j|ƒq
Wd|_dS(sCUsed to construct the row classes in a single interaction.
        s-Stub class (%s) is not derived from RowObjectiN(	Rt
issubclassRRRtqualtrowClasst_populateSchemaFort	populated(Rtrc((sC/usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyR(s
cCs’d}x6|D].}t||ƒs
td||fƒ‚q
q
Wt|ƒ}|j|ƒ|_|j|ƒ|_|j|ƒ|_|j	|ƒdS(s‰Construct all the SQL templates for database operations on
        <tableName> and populate the class <rowClass> with that info.
        t
rowColumnst
rowKeyColumnstrowTableNames(RowClass %s must have class variable: %sN(s
rowColumnss
rowKeyColumnssrowTableName(
thasattrRRtbuildUpdateSQLt	updateSQLtbuildInsertSQLt	insertSQLtbuildDeleteSQLt	deleteSQLtpopulateSchemaFor(RRt
attributestattt	tableInfo((sC/usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyR2s
cCs
t|ƒS(säEscape a string for use in an SQL statement. The default
        implementation escapes ' with '' and \ with \. Redefine this
        function in a subclass if your database server uses different
        escaping rules.
        (R(Rttext((sC/usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyt
escape_stringAscCst||d|jƒS(s›Format a value for use in an SQL statement.

        @param value: a value to format as data in SQL.
        @param type: a key in util.dbTypeMap.
        tstring_escaper(RR)(Rtvaluettype((sC/usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pytquote_valueIscCs…|r|rtdƒ‚n|rT|j|ƒ}|j|ƒ}|j||ƒ}n|r]ng}|jj|j|||||ƒS(seLoad a set of RowObjects from a database.

        Create a set of python objects of <rowClass> from the contents
        of a table populated with appropriate data members.
        Example::

          |  class EmployeeRow(row.RowObject):
          |      pass
          |
          |  def gotEmployees(employees):
          |      for emp in employees:
          |          emp.manager = "fred smith"
          |          manager.updateRow(emp)
          |
          |  reflector.loadObjectsFrom("employee",
          |                          data = userData,
          |                          whereClause = [("manager" , EQUAL, "fred smith")]
          |                          ).addCallback(gotEmployees)

        NOTE: the objects and all children should be loaded in a single transaction.
        NOTE: can specify a parentRow _OR_ a whereClause.

        s.Must specify one of parentRow _OR_ whereClause(RtgetTableInfotgetRelationshipFortbuildWhereClauseRtrunInteractiont
_rowLoader(Rt	tableNamet	parentRowtdatatwhereClauset
forceChildrentinfotrelationship((sC/usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pytloadObjectsFromQs	c
Cs§|j|}d}d}	x>|jD]3\}
}|	r>d}	n
|d}|d|
}q#W|d|}|rü|d7}d}	x{|D]p}|	ršd}	n
|d7}|\}
}}|j||
ƒ}|j||ƒ}|d	|
|j||f7}q…Wn|j|ƒ|jƒ}g}g}xó|D]ë}i}xutdt|ƒƒD]^}|j|dj	ƒ}x>|j
jD]0\}}|j	ƒ|krt||||<PqtqtWqJW|j|j
|ƒ}|s|jd}||j
||ƒ}|j
|ƒ|j|ƒn|j|ƒq(W|r3|j|||ƒnxm|jD]b}|rZ|jrZq=nxB|D]:}|j||ƒ}|j||jj||||ƒqaWq=W|S(
sMimmediate loading of rowobjects from the table with the whereClause.
        sSELECT iit,s %ss	 FROM %s s WHERE s AND s%s %s %s(tschemaRtfindTypeForR-tconditionalLabelstexecutetfetchalltrangetlentlowerRtfindInCachetrowFactoryMethodt
addToCachetappendtaddToParentt
relationshipstautoLoadR0R2t
childRowClassR(RttransactionR3R4R5R6R7R'tsqltfirsttcolumnR,twItemt
columnNametcondR+tttquotedValuetrowstresultstnewRowstargstkwtit
ColumnNametattrtresultObjecttmethR9trowtchildWhereClause((sC/usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyR2xsf
	


	





		cCsM|j|}|jƒ}x-|jD]"\}}|jƒ|kr#|Sq#WdS(N(R<RCR(RR3RQR'ROR,((sC/usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyR=¿s

cCsÍd|j}d}x\|jD]Q\}}t|j|ƒrAqn|sT|d}n|d|df}d}qWd}|d}xD|jD]9\}}|s«|d}n|d	|df}d}qŒW|S(
s(Internal) Build SQL template to update a RowObject.

        Returns: SQL that is used to contruct a rowObject class.
        s
UPDATE %s SETis, s %s = %ss%sis WHERE s AND s	 %s = %s (RRRRR(RR'RMRNROR,t	keyColumn((sC/usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyRÆs"





cCs«d|j}d}x:|jD]/\}}|s<|d}n||}d}qW|d}d}x:|jD]/\}}|s‰|d}n|d}d}qjW|d}|S(s²(Internal) Build SQL template to insert a new row.

        Returns: SQL that is used to insert a new row for a rowObject
        instance not created from the database.
        sINSERT INTO %s (is, is ) VALUES (s%st)(RR(RR'RMRNROR,((sC/usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyR às 








cCshd|j}d}|d}xD|jD]9\}}|sF|d}n|d|df}d}q'W|S(s?Build the SQL template to delete a row from the table.
        sDELETE FROM %s is WHERE s AND s	 %s = %s s%si(RR(RR'RMRNRaR,((sC/usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyR"üs



cCs½g}|j|j}xN|jD]C\}}t|j|ƒs |j|j|j|ƒ|ƒƒq q Wx9|jD].\}}|j|j|j|ƒ|ƒƒqqW|j	|ƒj
t|ƒS(s7Build SQL to update the contents of rowObject.
        (R<RRRt	__class__RGR-t
findAttributeRR.Rttuple(Rt	rowObjectRXR'ROR,Ra((sC/usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pytupdateRowSQL
scCs,|j|ƒ}|jdƒ|jj|ƒS(s:Update the contents of rowObject to the database.
        i(RgtsetDirtyRtrunOperation(RRfRM((sC/usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyt	updateRows
cCslg}|j|j}x9|jD].\}}|j|j|j|ƒ|ƒƒq W|j|ƒjt|ƒS(s7Build SQL to insert the contents of rowObject.
        (	R<RRRGR-RdR.R!Re(RRfRXR'ROR,((sC/usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pytinsertRowSQL"s
&cCs,|jdƒ|j|ƒ}|jj|ƒS(s(Insert a new row for rowObject.
        i(RhRkRRi(RRfRM((sC/usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyt	insertRow,s
cCslg}|j|j}x9|jD].\}}|j|j|j|ƒ|ƒƒq W|j|ƒjt|ƒS(s9Build SQL to delete rowObject from the database.
        (	R<RRRGR-RdR.R#Re(RRfRXR'RaR,((sC/usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pytdeleteRowSQL3scCs,|j|ƒ}|j|ƒ|jj|ƒS(s8Delete the row for rowObject from the database.
        (RmtremoveFromCacheRRi(RRfRM((sC/usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyt	deleteRow?s
N(t__name__t
__module__t__doc__RRtEQUALtLESSTHANtGREATERTHANtLIKER>RRRRR)R-tNoneR:R2R=RR R"RgRjRkRlRmRo(((sC/usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyR
s4	



			
			&	G							
		N(ttwisted.enterpriseRttwisted.enterprise.utilRRRRRttwisted.enterprise.rowRttwisted.pythonRR
Rt__all__(((sC/usr/lib/python2.7/dist-packages/twisted/enterprise/sqlreflector.pyt<module>s"ÿ;