pgtoolkit/pgmanager.py
changeset 71 4251068a251a
parent 70 77e65040711c
child 75 39f777341db4
equal deleted inserted replaced
70:77e65040711c 71:4251068a251a
     5 # Requires: Python 3.2, psycopg2
     5 # Requires: Python 3.2, psycopg2
     6 #
     6 #
     7 # Part of pgtoolkit
     7 # Part of pgtoolkit
     8 # http://hg.devl.cz/pgtoolkit
     8 # http://hg.devl.cz/pgtoolkit
     9 #
     9 #
    10 # Copyright (c) 2010, 2011, 2012  Radek Brich <radek.brich@devl.cz>
    10 # Copyright (c) 2010, 2011, 2012, 2013  Radek Brich <radek.brich@devl.cz>
    11 #
    11 #
    12 # Permission is hereby granted, free of charge, to any person obtaining a copy
    12 # Permission is hereby granted, free of charge, to any person obtaining a copy
    13 # of this software and associated documentation files (the "Software"), to deal
    13 # of this software and associated documentation files (the "Software"), to deal
    14 # in the Software without restriction, including without limitation the rights
    14 # in the Software without restriction, including without limitation the rights
    15 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    15 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    27 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    27 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    28 # THE SOFTWARE.
    28 # THE SOFTWARE.
    29 
    29 
    30 """Postgres database connection manager
    30 """Postgres database connection manager
    31 
    31 
    32 PgManager wraps psycopg2 connect function, adding following features:
    32 PgManager wraps psycopg2, adding following features:
    33 
    33 
    34  * Save and reuse database connection parameters
    34  * Save and reuse database connection parameters
    35 
    35 
    36  * Connection pooling
    36  * Connection pooling
    37 
    37 
    39 
    39 
    40  * Dictionary rows
    40  * Dictionary rows
    41 
    41 
    42 Example usage:
    42 Example usage:
    43 
    43 
    44     import pgmanager
    44     from pgtoolkit import pgmanager
    45 
    45 
    46     pgm = pgmanager.get_instance()
    46     pgm = pgmanager.get_instance()
    47     pgm.create_conn(hostaddr='127.0.0.1', dbname='postgres')
    47     pgm.create_conn(hostaddr='127.0.0.1', dbname='postgres')
    48 
    48 
    49     with pgm.cursor() as curs:
    49     with pgm.cursor() as curs:
   472         if not ci.isolation_level is None:
   472         if not ci.isolation_level is None:
   473             conn.set_isolation_level(ci.isolation_level)
   473             conn.set_isolation_level(ci.isolation_level)
   474         if ci.init_statement:
   474         if ci.init_statement:
   475             curs = conn.cursor()
   475             curs = conn.cursor()
   476             curs.execute(ci.init_statement)
   476             curs.execute(ci.init_statement)
       
   477             curs.connection.commit()
   477             curs.close()
   478             curs.close()
   478         return conn
   479         return conn
   479 
   480 
   480     def _normalize_isolation_level(self, level):
   481     def _normalize_isolation_level(self, level):
   481         if type(level) == str:
   482         if type(level) == str: