1515System information functions:
1616http://www.postgresql.org/docs/8.0/static/functions-info.html
1717"""
18+ from __future__ import print_function
19+ from builtins import str
20+ from builtins import map
21+ from qgis .core import QgsDataSourceUri
22+ from qgis .PyQt .QtCore import QSettings
23+ from qgis .PyQt .QtGui import QIcon
24+ from qgis .PyQt .QtWidgets import QInputDialog
1825
19- from PyQt4 .QtCore import *
20- from PyQt4 .QtGui import *
2126
22- import qgis .core
2327
2428import psycopg2
2529import psycopg2 .extensions # for isolation levels
@@ -43,7 +47,7 @@ class TableConstraint(DbConn.TableConstraint):
4347
4448 def __init__ (self , row ):
4549 self .name , con_type , self .is_defferable , self .is_deffered , keys = row [:5 ]
46- self .keys = map (int , keys .split (' ' ))
50+ self .keys = list ( map (int , keys .split (' ' ) ))
4751 self .con_type = TableConstraint .types [con_type ] # convert to enum
4852 if self .con_type == TableConstraint .TypeCheck :
4953 self .check_src = row [5 ]
@@ -58,7 +62,7 @@ def __init__(self, row):
5862class TableIndex (DbConn .TableIndex ):
5963 def __init__ (self , row ):
6064 self .name , columns = row
61- self .columns = map (int , columns .split (' ' ))
65+ self .columns = list ( map (int , columns .split (' ' ) ))
6266
6367
6468class TableTrigger (DbConn .TableTrigger ):
@@ -74,12 +78,12 @@ def __init__(self, row):
7478class DbError (DbConn .DbError ):
7579 def __init__ (self , error , query = None ):
7680 # save error. funny that the variables are in utf8, not
77- msg = unicode ( error .args [0 ], 'utf-8' )
81+ msg = str ( error .args [0 ], 'utf-8' )
7882 if query == None :
7983 if hasattr (error , "cursor" ) and hasattr (error .cursor , "query" ):
80- query = unicode (error .cursor .query , 'utf-8' )
84+ query = str (error .cursor .query , 'utf-8' )
8185 else :
82- query = unicode (query )
86+ query = str (query )
8387 DbConn .DbError .__init__ (self , msg , query )
8488
8589
@@ -118,8 +122,8 @@ def connect(self, selected, parent=None):
118122 if not settings .contains ( "database" ): # non-existent entry?
119123 raise DbError ( 'there is no defined database connection "%s".' % selected )
120124
121- get_value_str = lambda x : unicode (settings .value (x ) if Utils .isSIPv2 () else settings .value (x ).toString ())
122- host , port , database , username , password = map (get_value_str , ["host" , "port" , "database" , "username" , "password" ])
125+ get_value_str = lambda x : str (settings .value (x ) if Utils .isSIPv2 () else settings .value (x ).toString ())
126+ service , host , port , database , username , password = list ( map (get_value_str , ["service" , " host" , "port" , "database" , "username" , "password" ]) )
123127
124128 # qgis1.5 use 'savePassword' instead of 'save' setting
125129 isSave = settings .value ("save" ) if Utils .isSIPv2 () else settings .value ("save" ).toBool ()
@@ -130,44 +134,55 @@ def connect(self, selected, parent=None):
130134
131135 settings .endGroup ()
132136
133- uri = qgis .core .QgsDataSourceURI ()
134- uri .setConnection (host , port , database , username , password )
137+ uri = qgis .core .QgsDataSourceUri ()
138+ if service :
139+ uri .setConnection (service , database , username , password )
140+ else :
141+ uri .setConnection (host , port , database , username , password )
142+
135143 return Connection (uri )
136144
137145
138146 def __init__ (self , uri ):
139147 DbConn .Connection .__init__ (self , uri )
140148
149+ self .service = uri .service ()
141150 self .host = uri .host ()
142151 self .port = uri .port ()
143152 self .dbname = uri .database ()
144153 self .user = uri .username ()
145154 self .passwd = uri .password ()
146155
147- if self .dbname == '' or self .dbname is None :
148- self .dbname = self .user
149-
150156 try :
151157 self .con = psycopg2 .connect (self .con_info ())
152- except psycopg2 .OperationalError , e :
158+ except psycopg2 .OperationalError as e :
153159 raise DbError (e )
160+
161+ if not self .dbname :
162+ self .dbname = self .get_dbname ()
154163
155164 self .has_spatial = self .check_spatial ()
156165
157166 self .check_geometry_columns_table ()
158167
159168 # a counter to ensure that the cursor will be unique
160169 self .last_cursor_id = 0
161-
170+
162171 def con_info (self ):
163172 con_str = ''
164- if self .host : con_str += "host='%s' " % self .host
165- if self .port : con_str += "port=%s " % self .port
166- if self .dbname : con_str += "dbname='%s' " % self .dbname
167- if self .user : con_str += "user='%s' " % self .user
168- if self .passwd : con_str += "password='%s' " % self .passwd
173+ if self .service : con_str += "service='%s' " % self .service
174+ if self .host : con_str += "host='%s' " % self .host
175+ if self .port : con_str += "port=%s " % self .port
176+ if self .dbname : con_str += "dbname='%s' " % self .dbname
177+ if self .user : con_str += "user='%s' " % self .user
178+ if self .passwd : con_str += "password='%s' " % self .passwd
169179 return con_str
170180
181+ def get_dbname (self ):
182+ c = self .con .cursor ()
183+ self ._exec_sql (c , "SELECT current_database()" )
184+ return c .fetchone ()[0 ]
185+
171186 def get_info (self ):
172187 c = self .con .cursor ()
173188 self ._exec_sql (c , "SELECT version()" )
@@ -711,7 +726,7 @@ def sr_info_for_srid(self, srid):
711726 if x is not None :
712727 srtext = x .group ()
713728 return srtext
714- except DbError , e :
729+ except DbError as e :
715730 return "Unknown"
716731
717732 def insert_table_row (self , table , values , schema = None , cursor = None ):
@@ -777,7 +792,7 @@ def get_named_cursor(self, table=None):
777792 def _exec_sql (self , cursor , sql ):
778793 try :
779794 cursor .execute (sql )
780- except psycopg2 .Error , e :
795+ except psycopg2 .Error as e :
781796 # do the rollback to avoid a "current transaction aborted, commands ignored" errors
782797 self .con .rollback ()
783798 raise DbError (e )
@@ -793,12 +808,12 @@ def _exec_sql_and_commit(self, sql):
793808 # raise
794809
795810 def _quote (self , identifier ):
796- identifier = unicode (identifier ) # make sure it's python unicode string
811+ identifier = str (identifier ) # make sure it's python unicode string
797812 return u'"%s"' % identifier .replace ('"' , '""' )
798813
799814 def _quote_str (self , txt ):
800815 """ make the string safe - replace ' with '' """
801- txt = unicode (txt ) # make sure it's python unicode string
816+ txt = str (txt ) # make sure it's python unicode string
802817 return txt .replace ("'" , "''" )
803818
804819 def _table_name (self , schema , table ):
@@ -813,25 +828,34 @@ def _table_name(self, schema, table):
813828
814829 db = GeoDB (host = 'localhost' ,dbname = 'gis' ,user = 'gisak' ,passwd = 'g' )
815830
816- print db .list_schemas ()
817- print '=========='
831+ # fix_print_with_import
832+ print (db .list_schemas ())
833+ # fix_print_with_import
834+ print ('==========' )
818835
819836 for row in db .list_geotables ():
820- print row
837+ # fix_print_with_import
838+ print (row )
821839
822- print '=========='
840+ # fix_print_with_import
841+ print ('==========' )
823842
824843 for row in db .get_table_indexes ('trencin' ):
825- print row
844+ # fix_print_with_import
845+ print (row )
826846
827- print '=========='
847+ # fix_print_with_import
848+ print ('==========' )
828849
829850 for row in db .get_table_constraints ('trencin' ):
830- print row
851+ # fix_print_with_import
852+ print (row )
831853
832- print '=========='
854+ # fix_print_with_import
855+ print ('==========' )
833856
834- print db .get_table_rows ('trencin' )
857+ # fix_print_with_import
858+ print (db .get_table_rows ('trencin' ))
835859
836860 #for fld in db.get_table_metadata('trencin'):
837861 # print fld
@@ -841,3 +865,4 @@ def _table_name(self, schema, table):
841865 #except DbError, e:
842866 # print e.message, e.query
843867
868+ # vim: noet ts=8 :
0 commit comments