[Osaas-commits] r62 - in trunk: . server/osaas

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Jan 14 11:54:01 CET 2010


Author: bh
Date: 2010-01-14 11:54:00 +0100 (Thu, 14 Jan 2010)
New Revision: 62

Modified:
   trunk/ChangeLog
   trunk/server/osaas/dbbackend.py
Log:
* server/osaas/dbbackend.py (DBBackend.get_insert_statement): New
method to generate and cache the insert statement for records.
(DBBackend.handle_record): Use the new method to get the insert
statement to avoid generating it again for each record and to make
the code cleaner in preparation for better database error
handling.


Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2009-09-18 11:19:26 UTC (rev 61)
+++ trunk/ChangeLog	2010-01-14 10:54:00 UTC (rev 62)
@@ -1,3 +1,12 @@
+2010-01-14  Bernhard Herzog  <bh at intevation.de>
+
+	* server/osaas/dbbackend.py (DBBackend.get_insert_statement): New
+	method to generate and cache the insert statement for records.
+	(DBBackend.handle_record): Use the new method to get the insert
+	statement to avoid generating it again for each record and to make
+	the code cleaner in preparation for better database error
+	handling.
+
 2009-09-18  Stephan Holl  <stephan.holl at intevation.de>
 
 	* server/demo-config.xml: corrected path to default

Modified: trunk/server/osaas/dbbackend.py
===================================================================
--- trunk/server/osaas/dbbackend.py	2009-09-18 11:19:26 UTC (rev 61)
+++ trunk/server/osaas/dbbackend.py	2010-01-14 10:54:00 UTC (rev 62)
@@ -1,4 +1,4 @@
-# Copyright (C) 2007, 2008 by Intevation GmbH
+# Copyright (C) 2007, 2008, 2010 by Intevation GmbH
 # Authors:
 # Bernhard Herzog <bh at intevation.de>
 #
@@ -68,6 +68,7 @@
         self.connection_parameters = connection_parameters
         self.rules = rules
         self.logger = logger
+        self.insert_statement = None
 
     def log_debug(self, message, *args):
         if self.logger is not None:
@@ -97,18 +98,25 @@
 
         return template % name
 
+    def get_insert_statement(self):
+        if self.insert_statement is None:
+            rule = self.rules[0]
+            names = ", ".join([f.column for f in rule.fields])
+            statement = ("INSERT INTO %s (ID, %s) VALUES (%s, %s)"
+                         % (rule.table, names,
+                            self.db_api.osaas_nextval(rule.table + "_seq"),
+                            ", ".join([self.named_parameter(f.column)
+                                       for f in rule.fields])))
+            self.insert_statement = statement
+        return self.insert_statement
+
     def handle_record(self, record):
-        self.ensure_connection()
-        rule = self.rules[0]
-        names = ", ".join([f.column for f in rule.fields])
-        statement = ("INSERT INTO %s (ID, %s) VALUES (%s, %s)"
-                     % (rule.table, names,
-                        self.db_api.osaas_nextval(rule.table + "_seq"),
-                        ", ".join([self.named_parameter(f.column)
-                                   for f in rule.fields])))
+        statement = self.get_insert_statement()
         values = {}
-        for f in rule.fields:
+        for f in self.rules[0].fields:
             values[f.column] = getattr(record, f.parameter)
+
+        self.ensure_connection()
         cursor = self.connection.cursor()
         try:
             cursor.execute(statement, values)



More information about the Osaas-commits mailing list