|
| 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() |
0 commit comments