@@ -40,7 +40,7 @@ class Pool(ConnectionInfo):
40
40
41
41
def __init__ (self ):
42
42
super (Pool , self ).__init__ ()
43
- self .all_connections = {}
43
+ self ._connections = {}
44
44
self ._cache = {
45
45
'server_version' : {'storage' : {}},
46
46
'bootstrap' : {'storage' : {}, 'counter' : 0 , 'cache' : 10 },
@@ -49,24 +49,27 @@ def __init__(self):
49
49
'pgproee' : {'storage' : {}}
50
50
}
51
51
52
+ def get_with_default_db (self , db = None ):
53
+ if db is None :
54
+ return self .default_db
55
+ return db
56
+
52
57
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 ()
55
60
56
61
def query (self , query , db = None ):
57
- if db is None :
58
- db = self .db
62
+ db = self .get_with_default_db (db )
59
63
self ._init_connection (db )
60
- return self .all_connections [db ].query (query )
64
+ return self ._connections [db ].query (query )
61
65
62
66
def server_version (self , db = None ):
63
67
if db in self ._cache ['server_version' ]['storage' ]:
64
68
return self ._cache ['server_version' ]['storage' ][db ]
65
69
if platform .PY2 :
66
70
result = self .query ('show server_version' , db )[0 ][0 ]
67
71
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' )
70
73
self ._cache ['server_version' ]['storage' ][db ] = '{0}' .format (
71
74
result .decode ('ascii' ))
72
75
return self ._cache ['server_version' ]['storage' ][db ]
@@ -84,7 +87,7 @@ def in_recovery(self, db=None):
84
87
return self ._cache ['recovery' ]['storage' ][db ]
85
88
self ._cache ['recovery' ]['counter' ] = 0
86
89
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 ]
88
91
return self ._cache ['recovery' ]['storage' ][db ]
89
92
90
93
def is_bootstraped (self , db = None ):
@@ -98,10 +101,10 @@ def is_bootstraped(self, db=None):
98
101
result = int (self .query (sql , db )[0 ][0 ])
99
102
self ._cache ['bootstrap' ]['storage' ][db ] = (result == 1 )
100
103
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' )
102
105
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' )
105
108
return self ._cache ['bootstrap' ]['storage' ][db ]
106
109
107
110
def is_pgpro (self , db = None ):
@@ -150,8 +153,9 @@ def run_sql_type(self, typ, db=None):
150
153
return self .query (self .get_sql (typ , db ), db )
151
154
152
155
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 :
154
158
# 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