[Twisted-web] pgasync INTERVAL

Andrea Arcangeli andrea at cpushare.com
Fri Dec 16 06:19:08 MST 2005


I added the interval type conversion to allow KLive to work on top of
pgasync without requiring changes to the KLive code.

However in trying to run cpushare on top of pgasync, I'm getting this
error that I didn't finish debugging yet.

2005/12/16 14:18 CET [PgProtocol,client] [Failure instance: Traceback
(failure with no frames): pgasync.errors.DatabaseError: 34000: cursor
"none" does not exist
2005/12/16 14:18 CET [PgProtocol,client] ]

I didn't extract any meaningful stack trace yet.

Index: pgasync/pgasync/fe.py
===================================================================
--- pgasync/pgasync/fe.py	(revision 86)
+++ pgasync/pgasync/fe.py	(working copy)
@@ -349,7 +349,7 @@
 
 		DEFERRED
 
-		Execute a query with he given parameters properly formatted.
+		Execute a query with the given parameters properly formatted.
 
 		The parameter style is 'pyparam,' which uses a syntax like the 
 		following:
Index: pgasync/pgasync/pgtypes.py
===================================================================
--- pgasync/pgasync/pgtypes.py	(revision 86)
+++ pgasync/pgasync/pgtypes.py	(working copy)
@@ -120,7 +120,7 @@
 	@staticmethod
 	def fromDatabase(ins):
 		if ins.count("-"):
-			ins = ins.split("-")[0] #discarding timestamp for right tnow
+			ins = ins.split("-")[0] #discarding timestamp for right now
 				
 		try:
 			h,m,sparts = ins.split(":")
@@ -157,6 +157,44 @@
 		return datetime.datetime(dt.year,dt.month,dt.day,
 		tm.hour,tm.minute,tm.second,tm.microsecond)
 
+class INTERVAL(datetime.timedelta): 
+	def __init__(self, *args):
+		datetime.timedelta.__init__(self, *args)
+
+	@staticmethod
+	def toDatabase(s):
+		return "'%d days %d seconds %d microseconds'" % (s.days, s.seconds, s.microsecond)
+
+	def __str__(self):
+		return INTERVAL.toDatabase(self)
+
+	@staticmethod
+	def fromDatabase(ins):
+		try:
+			ins = re.split(' days? ', ins)
+			if len(ins) == 1:
+				ins = ins[0]
+				d = 0
+			elif len(ins) == 2:
+				d = int(ins[0])
+				ins = ins[1]
+
+			h,m,sparts = ins.split(":")
+			h,m = map(int,(h,m))
+			if sparts.count("."):
+				ssegs = sparts.split(".")
+				s,ms = map(int,ssegs)
+				if ms:
+					l = len(ssegs[1])
+					ms *= 10 ** (6 - l)
+			else:
+				s = int(sparts)
+				ms = 0
+		except:
+			raise
+			raise DataError, ("Cannot convert string '%s' to datetime.timedelta" % ins)
+		return datetime.timedelta(days=d,hours=h,minutes=m,seconds=s,microseconds=ms)
+
 class BOOL:
 	def __init__(self, b):
 		self.__b = b
Index: pgasync/pgasync/__init__.py
===================================================================
--- pgasync/pgasync/__init__.py	(revision 86)
+++ pgasync/pgasync/__init__.py	(working copy)
@@ -47,6 +47,7 @@
 registerAdapter(DATE, [datetime.date], [PgOids.DATE])
 registerAdapter(TIME, [datetime.time], [PgOids.TIME,PgOids.ABSTIME,PgOids.RELTIME])
 registerAdapter(DATETIME, [datetime.datetime], [PgOids.TIMESTAMP])
+registerAdapter(INTERVAL, [datetime.timedelta], [PgOids.INTERVAL,PgOids.TINTERVAL])
 
 registerAdapter(BOOL, [bool], [PgOids.BOOL])
 



More information about the Twisted-web mailing list