Browse Source

v.db.addtable

fix for default connection containing "$MAP"

do not secretly convert user-defined column names to lower case
Markus Metz 6 years ago
parent
commit
c0c8ab15a8
1 changed files with 8 additions and 6 deletions
  1. 8 6
      scripts/v.db.addtable/v.db.addtable.py

+ 8 - 6
scripts/v.db.addtable/v.db.addtable.py

@@ -95,6 +95,8 @@ def main():
     driver = kv['driver']
     schema = kv['schema']
 
+    database2 = database.replace('$MAP/', map_name + '/')
+
     # maybe there is already a table linked to the selected layer?
     nuldev = open(os.devnull, 'w')
     try:
@@ -104,22 +106,21 @@ def main():
         pass
 
     # maybe there is already a table with that name?
-    tables = grass.read_command('db.tables', flags='p', database=database, driver=driver,
+    tables = grass.read_command('db.tables', flags='p', database=database2, driver=driver,
                                 stderr=nuldev)
     tables = decode(tables)
 
     if not table in tables.splitlines():
         colnames = []
+        column_def = []
         if columns:
             column_def = []
-            for x in ' '.join(columns.lower().split()).split(','):
-                colname = x.split()[0]
+            for x in ' '.join(columns.split()).split(','):
+                colname = x.lower().split()[0]
                 if colname in colnames:
                     grass.fatal(_("Duplicate column name '%s' not allowed") % colname)
                 colnames.append(colname)
                 column_def.append(x)
-        else:
-            column_def = []
 
         # if not existing, create it:
         if not key in colnames:
@@ -131,13 +132,14 @@ def main():
         sql = "CREATE TABLE %s (%s)" % (table, column_def)
         try:
             grass.run_command('db.execute',
-                              database=database, driver=driver, sql=sql)
+                              database=database2, driver=driver, sql=sql)
         except CalledModuleError:
             grass.fatal(_("Unable to create table <%s>") % table)
 
     # connect the map to the DB:
     if schema:
         table = '{schema}.{table}'.format(schema=schema, table=table)
+    grass.verbose(_("Connecting new table to vector map <%s>...") % map_name)
     grass.run_command('v.db.connect', quiet=True,
                       map=map_name, database=database, driver=driver,
                       layer=layer, table=table, key=key)