Commits

Marcin Kuzminski committed bf5a340

some fixes to psycopg wrapper

  • Participants
  • Parent commits 640ea1c

Comments (0)

Files changed (1)

 
     
 class DbPsyco(object):
+    """
 
-    def __init__(self, config=None):
+    config = DbConfig()
+    config.database = 'dbname'
+    config.user = 'postgres'
+    config.password = 'qwe'
+    config.host = '127.0.0.1'
+    db = DbPsyco(config)
+    
+    print db.nextval('firma_idfirma_seq')
+    for i in db.execute("SELECT * FROM FIRMA LIMIT 10").all():
+        print i
+    
+    for i in db.execute("SELECT * FROM FIRMA LIMIT 10").all('dict'):
+        print i
+    
+    for i in db.execute("SELECT * FROM FIRMA LIMIT 10").all('list'):
+        print i
+    
+    print db.execute("SELECT * FROM FIRMA LIMIT 10").one()    
+    
+    
+    """
+
+    def __init__(self, config=None, debug=False):
         if config and not isinstance(config, DbConfig):
             raise Exception('config has to be an instance of DbConfig') 
         self.config = config or DbConfig()
-
+        self.debug = True
+        
     def __get_psyco_conn(self, verbose=True):
         """GETS PSYCODB CONNECTION"""
         try:
             database=self.config.database,
             user=self.config.user,
             password=self.config.password)
-            if verbose:
+            if verbose or self.debug:
                 print "\n## connected to: %s @ %s ##" % (self.config.database, self.config.host)
-        except psycopg2.DatabaseError as err:
+        except psycopg2.DatabaseError:
             raise
         return db_
 
         """
         return self.connection.cursor()
 
-    def execute(self, sql, params=None):
+    def execute(self, sql, *params):
         """
         Executes a query and returns an special object that holds result
         """
-        
         try:
             if params:
-                self.cursor.execute(sql, [params])
+                self.cursor.execute(sql, [1, ])
             else:
                 self.cursor.execute(sql)
             
         except:
             self.connection.rollback()
             raise
-        
+        if self.debug:
+            print self.cursor.query
         return ResultContainer(self.cursor)
     
-    def execute_unmannaged(self, sql, params=None):
+    def execute_unmannaged(self, sql, *params):
         if params:
-            self.cursor.execute(sql, [params])
+            self.cursor.execute(sql, params)
         else:
             self.cursor.execute(sql)
+        if self.debug:
+            print self.cursor.query
+                    
+        return ResultContainer(self.cursor)    
+        
     
     def nextval(self, seq_name):
         """
         gets nextval from given sequence
         """
         self.cursor.execute("SELECT nextval FROM nextval(%s)", [seq_name])
-        return self.cursor.fetchone()[0]    
+        return self.cursor.fetchone()[0]     
         
 
 if __name__ == '__main__':