2 Mutating Data Class Instances

If you don't want your data class instances to be mutable (for whatever reason), assign 1 to the mutable attribute in your class definition.

To mutate an object, you use the dictionary-style mutation interface. For example, to change the value of USERNAME in the current row:

>>> newUser['USERNAME'] = 'fred'

What this will do is cause the following UPDATE query to be sent to the connection.

(bind variables :p1 = 'fred' and :p2 = 1)

One might ask: "how the hell did that happen?" The answer is this: it got the table from the table specified in the data class description

> table = 'USERS'

The attribute name is the item you assigned to. The OID = :p2 part is a bit more interesting. If you look above, you'll see:

> unique = ['OID', 'USERNAME']

What PyDO does is this: it loops over the unique list and for each item in the list is determines if it is a tuple or string. If it's a string, it's the name of an field that uniquely identifies a row in the table (here 'OID'). If the current object has that key-value pair, it stops having found an identifying field and so composes the where clause. If it is a tuple, it is a set of fields that uniquely identify the row. If all such fields are populated in the current object, it will stop and compose the where clause from the ANDing check of those fields in the current object. In the case where either there is no unique line specified or the key-value pairs aren't defined in the current object, an exception saying "No way to get unique row!" will be raised.

Ideally, if you want to update more than one field in your object in one UPDATE query, you can use the update method (from the dictionary interface) to accomplish this.

> newUser.update({'USERNAME': 'barney', PASSWORD='iF0rG0t'})

This will update both column values in one UPDATE query.

You might now say, well, that's all fine an dandy, but to do this correctly, I want to make sure that the LAST_MOD field gets updated appropriately when people change the object! Well, be at rest, we can do that too. Behind the scenes, the __getitem__ and update methods call an instance method updateValues that actually does the hard work and we can override this to update LAST_MOD as appropriate.