[Python-ideas] Add recordlcass to collections module
Chris Angelico
rosuav at gmail.com
Mon Sep 3 04:16:45 EDT 2018
On Mon, Sep 3, 2018 at 5:23 PM, Jacco van Dorp <j.van.dorp at deonet.nl> wrote:
> This feels really useful to me to make some quick changes to a database -
> perhaps a database layer could return an class of type Recordclass, and then
> you just simply mutate it and shove it back into the database. Pseudocode:
>
> record = database.execute("SELECT * FROM mytable WHERE primary_key = 15")
> record.mostRecentLoggedInTime = time.time()
> database.execute(f"UPDATE mytable SET mostRecentLoggedInTime =
> {record.mostRecentLoggedInTime} WHERE primary_key = {record.primary_key}":)
>
> Or any smart database wrapper might just go:
>
> database.updateOrInsert(table = mytable, record = record)
>
> And be smart enough to figure out that we already have a primary key unequal
> to some sentinel value like None, and do an update, while it could do an
> insert if the primary key WAS some kind of sentinel value.
In its purest form, what you're asking for is an "upsert" or "merge" operation:
https://en.wikipedia.org/wiki/Merge_(SQL)
In a multi-user transactional database, there are some fundamentally
hard problems to implementing a merge. I'm not 100% certain, so I
won't say "impossible", but it is certainly *extremely difficult* to
implement an operation like this in application-level software without
some form of race condition.
ChrisA
More information about the Python-ideas
mailing list