|
305.
|
|
|
There are currently two available reflectors in Twisted Enterprise, the SQL Reflector for relational databases which uses the python DB API, and the XML Reflector which uses a file system containing XML files. The XML reflector is currently extremely slow.
|
|
|
type: Content of: <html><body><p>
|
|
|
|
现今在 Twisted 企业框架(Twisted Enterprise)中可用的 reflectors:一是 SQL Reflector:用于关系数据库,使用的是 python 的数据库 API,另外一个是 XML Reflector:用于包含 XML 文件的文件系统,但是使用起来速度非常缓慢。
|
|
Translated by
Kevin
|
|
Reviewed by
Fantix King
|
|
|
|
Located in
howto/row.xhtml:102
|
|
306.
|
|
|
An example class list for the RoomRow class we specified above using the SQLReflector:
|
|
|
type: Content of: <html><body><p>
|
|
|
|
来看个例子,学习下如何通过 SQLReflector 来使用上面创建的 RoomRow:
|
|
Translated by
Kevin
|
|
Reviewed by
Fantix King
|
|
|
|
Located in
howto/row.xhtml:107
|
|
307.
|
|
|
from twisted.enterprise.sqlreflector import SQLReflector
dbpool = adbapi.ConnectionPool("pyPgSQL.PgSQL")
reflector = SQLReflector( dbpool, [RoomRow] )
|
|
|
type: Content of: <html><body><pre>
|
|
|
represents a line break.
Start a new line in the equivalent position in the translation.
|
|
|
|
from twisted.enterprise.sqlreflector import SQLReflector
dbpool = adbapi.ConnectionPool("pyPgSQL.PgSQL")
reflector = SQLReflector( dbpool, [RoomRow] )
|
|
Translated and reviewed by
Fantix King
|
|
|
|
Located in
howto/row.xhtml:111
|
|
308.
|
|
|
Creating Row Objects
|
|
|
type: Content of: <html><body><h2>
|
|
|
|
创建 RowObject(Create RowObject)
|
|
Translated by
Kevin
|
|
Reviewed by
Fantix King
|
|
|
|
Located in
howto/row.xhtml:117
|
|
309.
|
|
|
There are two methods of creating RowObjects - loading from the database, and creating a new instance ready to be inserted.
|
|
|
type: Content of: <html><body><p>
|
|
|
|
这里有两个方法用于创建一个 RowObject —— 从数据库中取出已有的或者是创建一个新实例并插入到数据库中。
|
|
Translated by
Kevin
|
|
Reviewed by
Fantix King
|
|
|
|
Located in
howto/row.xhtml:119
|
|
310.
|
|
|
To load rows from the database and create RowObject instances for each of the rows, use the loadObjectsFrom method of the Reflector. This takes a tableName, an optional <q>user data</q> parameter, and an optional <q>where clause</q>. The where clause may be omitted which will retrieve all the rows from the table. For example:
|
|
|
type: Content of: <html><body><p>
|
|
|
|
为了从数据库中取出行或者新加行,可以使用Reflector的 loadObjectsFrom 方法。她的参数包括:表名,可选的“用户数据”参数,可选的“where子句”。当然了,当你想要取出所有的行的时候,“where子句”是可以省略的。例如:
|
|
Translated by
Kevin
|
|
Reviewed by
Fantix King
|
|
|
|
Located in
howto/row.xhtml:122
|
|
311.
|
|
|
def gotRooms(rooms):
for room in rooms:
print "Got room:", room.id
d = reflector.loadObjectsFrom("testrooms",
whereClause=[("id", reflector.EQUAL, 5)])
d.addCallback(gotRooms)
|
|
|
type: Content of: <html><body><pre>
|
|
|
represents a line break.
Start a new line in the equivalent position in the translation.
|
|
|
represents a space character.
Enter a space in the equivalent position in the translation.
|
|
|
|
def gotRooms(rooms):
for room in rooms:
print "Got room:", room.id
d = reflector.loadObjectsFrom("testrooms",
whereClause=[("id", reflector.EQUAL, 5)])
d.addCallback(gotRooms)
|
|
Translated and reviewed by
Liu, Kun
|
|
|
|
Located in
howto/row.xhtml:130
|
|
312.
|
|
|
For more advanced RowObject construction, loadObjectsFrom may use a factoryMethod that was specified as a class attribute for the RowClass derived class. This method will be called for each of the rows with the class object, the userData parameter, and a dictionary of data from the database keyed by column name. This factory method should return a fully populated RowObject instance and may be used to do pre-processing, lookups, and data transformations before exposing the data to user code. An example factory method:
|
|
|
type: Content of: <html><body><p>
|
|
|
|
对于更高级的 RowObject 结构,loadObjectsFrom 可以调用每个在 RowClass 子类中被声明为类属性的工厂方法(也就是上文提到的 factoyMethod)。对于每一个行对象,我们都会调用这个工厂方法(参数是类对象,用户数据以及以列名作为关键字的一个字典,其中存放了数据库中的数据)。这个工厂方法会返回一个完全组装好(fully populated)的RowObject实例,可以用来进行一些预处理,查询或者是数据格式的转换,以方便最终显示给用户。一个factory方法的例子如下:
|
|
Translated by
Kevin
|
|
Reviewed by
Fantix King
|
|
|
|
Located in
howto/row.xhtml:139
|
|
313.
|
|
|
def testRoomFactory(roomClass, userData, kw):
newRoom = roomClass(userData)
newRoom.__dict__.update(kw)
return newRoom
|
|
|
type: Content of: <html><body><pre>
|
|
|
represents a line break.
Start a new line in the equivalent position in the translation.
|
|
|
represents a space character.
Enter a space in the equivalent position in the translation.
|
|
|
|
def testRoomFactory(roomClass, userData, kw):
newRoom = roomClass(userData)
newRoom.__dict__.update(kw)
return newRoom
|
|
Translated and reviewed by
Liu, Kun
|
|
|
|
Located in
howto/row.xhtml:149
|
|
314.
|
|
|
The last method of creating a row object is for new instances that do not already exist in the database table. In this case, create a new instance and assign its primary key attributes and all of its member data attributes, then pass it to the <code>insertRow</code> method of the Reflector. For example:
|
|
|
type: Content of: <html><body><p>
|
|
|
|
我们还需要考虑如何往一个数据表中插入一个之前并不存在的实例。在这种情况下,可以创建一个新的实例,指明它的主键,并未数据域进行赋值,然后就可以将它穿递给 Reflector 的<code>insertRow</code>方法。来看个例子:
|
|
Translated by
Kevin
|
|
Reviewed by
Fantix King
|
|
|
|
Located in
howto/row.xhtml:154
|