Skip to content

Commit c2e2171

Browse files
author
rblazek
committed
fixed ticket #1173, new script which runs both db.connect and db.login (if necessary) for postgres database, connection is tested
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@10795 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 0109880 commit c2e2171

7 files changed

Lines changed: 136 additions & 1 deletion

File tree

‎src/plugins/grass/CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
SUBDIRS(config modules themes)
2+
SUBDIRS(config modules scripts themes)
33

44
ADD_DEFINITIONS(-DGRASS_BASE=\\\"${GRASS_PREFIX}\\\")
55
ADD_DEFINITIONS(-DHAVE_OPENPTY=${HAVE_OPENPTY})

‎src/plugins/grass/config/default.qgc‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@
403403
<section label="Database management">
404404
<grass name="db.connect"/>
405405
<grass name="db.connect.schema"/>
406+
<grass name="qgis.db.connect-login.pg"/>
406407
<grass name="v.db.reconnect.all"/>
407408
<grass name="db.login"/>
408409
<grass name="db.in.ogr"/>
4.85 KB
Loading
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE qgisgrassmodule SYSTEM "http://mrcc.com/qgisgrassmodule.dtd">
3+
4+
<qgisgrassmodule label="Set PostgreSQL DB connection" module="qgis.db.connect-login.pg.py">
5+
<option key="database" />
6+
<option key="schema" />
7+
<option key="host" />
8+
<option key="port"/>
9+
<option key="user"/>
10+
<option key="password"/>
11+
</qgisgrassmodule>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
FILE (GLOB MODULE_FILES *.py )
3+
INSTALL (FILES ${MODULE_FILES}
4+
DESTINATION ${QGIS_DATA_DIR}/grass/scripts
5+
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE )
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#!/usr/bin/env python
2+
3+
############################################################################
4+
#
5+
# MODULE: qgis.db.connect-login.py
6+
# AUTHOR(S): Radim Blazek
7+
#
8+
# PURPOSE: Connect to Postgresql
9+
# COPYRIGHT: (C) 2009 by Radim Blazek
10+
#
11+
# This program is free software under the GNU General Public
12+
# License (>=v2). Read the file COPYING that comes with GRASS
13+
# for details.
14+
#
15+
#############################################################################
16+
17+
#%Module
18+
#% description: Make connection to PostgreSQL database and login.
19+
#% keywords: database
20+
#%End
21+
22+
#%option
23+
#% key: host
24+
#% type: string
25+
#% label: Host
26+
#% description: Host name of the machine on which the server is running.
27+
#% required : no
28+
#%end
29+
30+
#%option
31+
#% key: port
32+
#% type: integer
33+
#% label: Port
34+
#% description: TCP port on which the server is listening, usually 5432.
35+
#% required : no
36+
#%end
37+
38+
#%option
39+
#% key: database
40+
#% type: string
41+
#% key_desc : name
42+
#% gisprompt: old_dbname,dbname,dbname
43+
#% label: Database
44+
#% description: Database name
45+
#% required : yes
46+
#%end
47+
48+
#%option
49+
#% key: schema
50+
#% type: string
51+
#% label: Schema
52+
#% description: Database schema.
53+
#% required : no
54+
#%end
55+
56+
#%option
57+
#% key: user
58+
#% type: string
59+
#% label: User
60+
#% description: Connect to the database as the user username instead of the default.
61+
#% required : no
62+
#%end
63+
64+
#%option
65+
#% key: password
66+
#% type: string
67+
#% label: Password
68+
#% description: Password will be stored in file!
69+
#% required : no
70+
#%end
71+
72+
import sys
73+
import os
74+
import string
75+
import grass
76+
77+
def main():
78+
host = options['host']
79+
port = options['port']
80+
database = options['database']
81+
schema = options['schema']
82+
user = options['user']
83+
password = options['password']
84+
85+
#if not maptable:
86+
# grass.fatal("There is no table connected to this map. Cannot join any column.")
87+
88+
# Test connection
89+
conn = "dbname=" + database
90+
if host: conn += ",host=" + host
91+
if port: conn += ",port=" + host
92+
93+
# Unfortunately we cannot test untill user/password is set
94+
if user or password:
95+
print "Setting login (db.login) ... "
96+
sys.stdout.flush()
97+
if grass.run_command('db.login', driver = "pg", database = conn, user = user, password = password) != 0:
98+
grass.fatal("Cannot login")
99+
100+
# Try to connect
101+
print "Testing connection ..."
102+
sys.stdout.flush()
103+
if grass.run_command('db.select', quiet = True, flags='c', driver= "pg", database=conn, sql="select version()" ) != 0:
104+
if user or password:
105+
print "Deleting login (db.login) ..."
106+
sys.stdout.flush()
107+
if grass.run_command('db.login', quiet = True, driver = "pg", database = conn, user = "", password = "") != 0:
108+
print "Cannot delete login."
109+
sys.stdout.flush()
110+
grass.fatal("Cannot connect to database.")
111+
112+
if grass.run_command('db.connect', driver = "pg", database = conn, schema = schema) != 0:
113+
grass.fatal("Cannot connect to database.")
114+
115+
if __name__ == "__main__":
116+
options, flags = grass.parser()
117+
main()

‎src/providers/grass/qgsgrass.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ void GRASS_EXPORT QgsGrass::init( void )
181181
#endif
182182
QString path = "PATH=" + gisBase + "/bin";
183183
path.append( sep + gisBase + "/scripts" );
184+
path.append( sep + QgsApplication::pkgDataPath() + "/grass/scripts/" );
184185

185186
// On windows the GRASS libraries are in
186187
// QgsApplication::prefixPath(), we have to add them

0 commit comments

Comments
 (0)