The framework offers a specific API for this:
>>> from Modeling.EditingContext import EditingContext >>> import pprint, StoreEmployees >>> ec = EditingContext() >>> raw_employees = ec.fetch('Employee', isDeep=1, rawRows=1) >>> pprint.pprint(raw_employees) [{'firstName': 'Jeanne', 'fkStoreId': 1, 'id': 2, 'lastName': 'Cleese', 'storeArea': 'DE'}, {'firstName': 'John Jr.', 'fkStoreId': 1, 'id': 1, 'lastName': 'Cleese', 'storeArea': 'AB'}, {'firstName': 'John', 'fkStoreId': 1, 'id': 3, 'lastName': 'Cleese', 'officeLocation': '4XD7'}]
As you can see, with parameter rawRows
you get the raw dictionary
directly from the database. No object is initialized by such a fetch. However,
you can use every functionality we already saw for fetching (inheritance with
parameter isDeep
, qualifiers), as is.
The very same rule applies to raw fetch as to ``normal'' fetch, in particular, everything we saw in the section 4.5.9 is still valid. If your EditingContext contains some newly inserted objects, you'll them appear; and deleted objects won't appear. For example:
>>> from StoreEmployees.SalesClerk import SalesClerk >>> terry=SalesClerk() >>> terry.setFirstName('Terry'); terry.setLastName('Gilliam') >>> ec.insert(terry) >>> salesClerks=ec.fetch('SalesClerk', rawRows=1) >>> pprint.pprint(salesClerks) [{'firstName': 'Jeanne', 'fkStoreId': 1, 'id': 2, 'lastName': 'Cleese', 'storeArea': 'DE'}, {'firstName': 'John Jr.', 'fkStoreId': 1, 'id': 1, 'lastName': 'Cleese', 'storeArea': 'AB'}, {'firstName': 'Terry', 'fkStoreId': None, 'id': <Modeling.GlobalID.TemporaryGlobalID instance at 0x84fcc64>, 'lastName': 'Gilliam', 'storeArea': None}] >>> salesClerk.globalID() <Modeling.GlobalID.TemporaryGlobalID instance at 0x84fcc64>
You probably already noticed the particular value associated to Terry's
'id
' field. Since this object is not saved in the database yet, its
current id
(the primary key for SalesClerk objects) is not
determined yet either. Instead, the framework returns its identifier, which is
a TemporaryGlobalID.
The same phenomenon appear on foreign keys when an object is related to a newly inserted object; continuing the previous example:
>>> pprint.pprint(ec.fetch('Executive', rawRows=1)) # No modifications yet [{'firstName': 'John', 'fkStoreId': 1, 'id': 3, 'lastName': 'Cleese', 'officeLocation': '4XD7'}] >>> from StoreEmployees.Store import Store >>> parrot_store=Store() >>> parrot_store.setCorporateName('We sell parrots') >>> ec.insert(parrot_store) >>> john=ec.fetch('Executive', 'firstName=="John"')[0] >>> john.getToStore().removeFromEmployees(john) >>> john.setToStore(parrot_store) ; parrot_store.addToEmployees(john) >>> pprint.pprint(ec.fetch('Executive', rawRows=1)) [{'firstName': 'John', 'fkStoreId': <Modeling.GlobalID.TemporaryGlobalID instance at 0x8364a0c>, 'id': 3, 'lastName': 'Cleese', 'officeLocation': '4XD7'}] >>> parrot_store.globalID() <Modeling.GlobalID.TemporaryGlobalID instance at 0x8364a0c>
We see clearly here that this time, fkStoreId
gets a
TemporaryGlobalID corresponding to parrot_store
's global
id. Moreover, we also remark that even if the modifications are not saved into
the database yet, raw fetching returns the objects as they currently are in
the EditingContext, just as with normal fetching.
Comments are welcome: Sebastien Bigaret / Modeling Home Page