@@ -40,7 +40,7 @@ class Pool(ConnectionInfo):
4040
4141 def __init__ (self ):
4242 super (Pool , self ).__init__ ()
43- self .all_connections = {}
43+ self ._connections = {}
4444 self ._cache = {
4545 'server_version' : {'storage' : {}},
4646 'bootstrap' : {'storage' : {}, 'counter' : 0 , 'cache' : 10 },
@@ -49,24 +49,27 @@ def __init__(self):
4949 'pgproee' : {'storage' : {}}
5050 }
5151
52+ def get_with_default_db (self , db = None ):
53+ if db is None :
54+ return self .default_db
55+ return db
56+
5257 def connection_string (self , db = None ):
53- self ._init_connection (db )
54- return self .all_connections [db ].conn_str ()
58+ db = self .get_with_default_db (db )
59+ return self ._connections [db ]._connection_string ()
5560
5661 def query (self , query , db = None ):
57- if db is None :
58- db = self .db
62+ db = self .get_with_default_db (db )
5963 self ._init_connection (db )
60- return self .all_connections [db ].query (query )
64+ return self ._connections [db ].query (query )
6165
6266 def server_version (self , db = None ):
6367 if db in self ._cache ['server_version' ]['storage' ]:
6468 return self ._cache ['server_version' ]['storage' ][db ]
6569 if platform .PY2 :
6670 result = self .query ('show server_version' , db )[0 ][0 ]
6771 elif platform .PY3 :
68- result = bytes (
69- self .query ('show server_version' , db )[0 ][0 ], 'utf-8' )
72+ result = bytes (self .query ('show server_version' , db )[0 ][0 ], 'utf-8' )
7073 self ._cache ['server_version' ]['storage' ][db ] = '{0}' .format (
7174 result .decode ('ascii' ))
7275 return self ._cache ['server_version' ]['storage' ][db ]
@@ -84,7 +87,7 @@ def in_recovery(self, db=None):
8487 return self ._cache ['recovery' ]['storage' ][db ]
8588 self ._cache ['recovery' ]['counter' ] = 0
8689 self ._cache ['recovery' ]['storage' ][db ] = self .query (
87- "select pg_catalog.pg_is_in_recovery()" )[0 ][0 ]
90+ "select pg_catalog.pg_is_in_recovery()" , db )[0 ][0 ]
8891 return self ._cache ['recovery' ]['storage' ][db ]
8992
9093 def is_bootstraped (self , db = None ):
@@ -98,10 +101,10 @@ def is_bootstraped(self, db=None):
98101 result = int (self .query (sql , db )[0 ][0 ])
99102 self ._cache ['bootstrap' ]['storage' ][db ] = (result == 1 )
100103 if self ._cache ['bootstrap' ]['storage' ][db ]:
101- self .all_connections [db ].log .info ('Found mamonsu bootstrap' )
104+ self ._connections [db ].log .info ('Found mamonsu bootstrap' )
102105 else :
103- self .all_connections [db ].log .info ('Can\' t found mamonsu bootstrap' )
104- self .all_connections [db ].log .info ('hint: run `mamonsu bootstrap` if you want to run without superuser rights' )
106+ self ._connections [db ].log .info ('Can\' t found mamonsu bootstrap' )
107+ self ._connections [db ].log .info ('hint: run `mamonsu bootstrap` if you want to run without superuser rights' )
105108 return self ._cache ['bootstrap' ]['storage' ][db ]
106109
107110 def is_pgpro (self , db = None ):
@@ -150,8 +153,9 @@ def run_sql_type(self, typ, db=None):
150153 return self .query (self .get_sql (typ , db ), db )
151154
152155 def _init_connection (self , db ):
153- if db not in self .all_connections :
156+ db = self .get_with_default_db (db )
157+ if db not in self ._connections :
154158 # create new connection
155- info = self .info
156- info ['db' ] = db
157- self .all_connections [db ] = Connection (info )
159+ connection_info = self .connection_info
160+ connection_info ['db' ] = db
161+ self ._connections [db ] = Connection (connection_info )
0 commit comments