logo       

Emulating a sequence: msg#00013

python.db.pysqlite.user

Subject: Emulating a sequence

I was thinking about faking a sequence. My first thought was to
create a sequence table with a single row where a field is a sequence
value and you just increment the value and retreive it as anything else.
It's simple but particulary good.

My second thought was that maybe a class with sequence methods could
plug into the connection, override methods in the aggregate_class -
something like the outline below where it could be subclassed to provide
the generic methods.

class SqliteSeq:
def __init__(self, start=0, increment=1, minvalue=None, maxvalue=None,
cycle=False,):
self.start = start
self.increment = increment
self.minvalue = minvalue
self.maxvalue = maxvalue
self.cycle = cycle

def getval(self):
""" Get current value """
def prevval(self):
""" Get previous value """
def nextval(self):
""" Get next value """
def step(self):
""" Increment current value """
return self.current_value += self.increment

def finalize(self):
""" Override to aggregate class """
return self.nextval

Class CommitSeq(SqliteSeq):

def __init__.(self):

SqliteSeq.__init__(self, start=0, increment=1, minvalue=None,
maxvalue=None


But where to store it's state? Maybe in a pickle on the filesystem.
Could also store it in the db using another connection object perhaps.

conn = sqlite.connect(":memory:")
conn.create_aggregate("commit_seq", 1, CommitSeq)
cur.execute("select commit_seq.nextval() from test")

Can you use dotted notation in a select statement for a method of an
aggregate class?

Anyway, I'd be interested in what other folks are doing to emulate
sequences and whether this kind of approach to getting the functionality
of a sequence has any merit.

Regards,
David


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise