Skip to content

Commit

Permalink
vector: use full db connection settings (#3462)
Browse files Browse the repository at this point in the history
fix `db.dropcolumn`, `db.in.ogr`, `v.db.renamecolumn`
  • Loading branch information
metzm committed Feb 28, 2024
1 parent 6a52add commit 7318eca
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
23 changes: 19 additions & 4 deletions scripts/db.dropcolumn/db.dropcolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
# % required : yes
# %end

# %option G_OPT_DB_DATABASE
# %end

# %option G_OPT_DB_DRIVER
# % options: dbf,odbc,ogr,sqlite,pg
# %end

import sys
import string

Expand All @@ -45,14 +52,19 @@
def main():
table = options["table"]
column = options["column"]
database = options["database"]
driver = options["driver"]
force = flags["f"]

# check if DB parameters are set, and if not set them.
gscript.run_command("db.connect", flags="c")

kv = gscript.db_connection()
database = kv["database"]
driver = kv["driver"]
if not database or not driver:
kv = gscript.db_connection()
if not database:
database = kv["database"]
if not driver:
driver = kv["driver"]
# schema needed for PG?

if force:
Expand All @@ -67,7 +79,10 @@ def main():
% column
)

cols = [f[0] for f in gscript.db_describe(table)["cols"]]
cols = [
f[0]
for f in gscript.db_describe(table, database=database, driver=driver)["cols"]
]
if column not in cols:
gscript.fatal(_("Column <%s> not found in table") % column)

Expand Down
13 changes: 11 additions & 2 deletions scripts/db.in.ogr/db.in.ogr.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ def main():
else:
grass.fatal(_("Input DSN <%s> not found or not readable") % input)

# save db connection settings of the output
f = grass.vector_layer_db(output, "1")

table = f["table"]
database = f["database"]
driver = f["driver"]

# rename ID col if requested from cat to new name
if key:
grass.write_command(
Expand Down Expand Up @@ -173,14 +180,16 @@ def main():
"db.dropcolumn",
quiet=True,
flags="f",
table=output,
table=table,
database=database,
driver=driver,
column="cat",
stdout=nuldev,
stderr=nuldev,
)
nuldev.close()

records = grass.db_describe(output)["nrows"]
records = grass.db_describe(table, database=database, driver=driver)["nrows"]
grass.message(_("Imported table <%s> with %d rows") % (output, records))


Expand Down
2 changes: 1 addition & 1 deletion scripts/v.db.renamecolumn/v.db.renamecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def main():

# describe old col
oldcoltype = None
for f in grass.db_describe(table)["cols"]:
for f in grass.db_describe(table, database=database, driver=driver)["cols"]:
if f[0] != oldcol:
continue
oldcoltype = f[1]
Expand Down

0 comments on commit 7318eca

Please sign in to comment.