Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v.db.addcolumn: fix enclosing column name with SQL standard double quotes #3633

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading