Skip to content

Commit

Permalink
v.db.addcolumn: fix enclosing column name with SQL standard double qu…
Browse files Browse the repository at this point in the history
…otes (#3633)

* Fix fatal message

* Fix maximum number of string split by whitespace string

* Fix add new column name type
  • Loading branch information
tmszi committed Apr 20, 2024
1 parent 307bfcd commit ef730b9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 5 additions & 1 deletion python/grass/script/testsuite/test_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ def works_for_vector_table_column(self, name):
try:
gs.run_command("v.edit", map=name, tool="create")
gs.run_command("v.db.addtable", map=name)
gs.run_command("v.db.addcolumn", map=name, columns=name)
gs.run_command(
"v.db.addcolumn",
map=name,
columns=f"{name} integer",
)
works = True
except gs.CalledModuleError:
works = False
Expand Down
15 changes: 13 additions & 2 deletions scripts/v.db.addcolumn/v.db.addcolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

import atexit
import os
import re

from grass.exceptions import CalledModuleError
import grass.script as grass

Expand Down Expand Up @@ -98,17 +100,26 @@ def main():
column_existing = grass.vector_columns(map, int(layer)).keys()

add_str = "BEGIN TRANSACTION\n"
pattern = re.compile(r"\s+")
for col in columns:
if not col:
grass.fatal(_("There is an empty column. Did you leave a trailing comma?"))
col_name = col.split(" ")[0].strip()
whitespace = re.search(pattern, col)
if not whitespace:
grass.fatal(
_(
"Incorrect new column(s) format, use"
" <'name type [,name type, ...]'> format, please."
)
)
col_name, col_type = col.split(whitespace.group(0), 1)
if col_name in column_existing:
grass.error(
_("Column <{}> is already in the table. Skipping.").format(col_name)
)
continue
grass.verbose(_("Adding column <{}> to the table").format(col_name))
add_str += f"ALTER TABLE {table} ADD COLUMN {col};\n"
add_str += f'ALTER TABLE {table} ADD COLUMN "{col_name}" {col_type};\n'
add_str += "END TRANSACTION"
sql_file = grass.tempfile()
rm_files.append(sql_file)
Expand Down

0 comments on commit ef730b9

Please sign in to comment.