mirror of
https://github.com/simonw/dclient.git
synced 2026-09-24 19:04:13 +02:00
--ignore and --replace for insert, refs #8
This commit is contained in:
parent
0d9c9119d6
commit
7ea598ceef
1 changed files with 30 additions and 3 deletions
|
|
@ -97,6 +97,10 @@ def query(url_or_alias, sql, token):
|
||||||
@click.option(
|
@click.option(
|
||||||
"--no-detect-types", is_flag=True, help="Don't detect column types for CSV/TSV"
|
"--no-detect-types", is_flag=True, help="Don't detect column types for CSV/TSV"
|
||||||
)
|
)
|
||||||
|
@click.option(
|
||||||
|
"--replace", is_flag=True, help="Replace rows with a matching primary key"
|
||||||
|
)
|
||||||
|
@click.option("--ignore", is_flag=True, help="Ignore rows with a matching primary key")
|
||||||
@click.option("--create", is_flag=True, help="Create table if it does not exist")
|
@click.option("--create", is_flag=True, help="Create table if it does not exist")
|
||||||
@click.option(
|
@click.option(
|
||||||
"pks",
|
"pks",
|
||||||
|
|
@ -115,6 +119,8 @@ def insert(
|
||||||
format_json,
|
format_json,
|
||||||
format_nl,
|
format_nl,
|
||||||
no_detect_types,
|
no_detect_types,
|
||||||
|
replace,
|
||||||
|
ignore,
|
||||||
create,
|
create,
|
||||||
pks,
|
pks,
|
||||||
token,
|
token,
|
||||||
|
|
@ -202,7 +208,14 @@ def insert(
|
||||||
row[key] = float(value)
|
row[key] = float(value)
|
||||||
first = False
|
first = False
|
||||||
_insert_batch(
|
_insert_batch(
|
||||||
url=url, table=table, batch=batch, token=token, create=create, pks=pks
|
url=url,
|
||||||
|
table=table,
|
||||||
|
batch=batch,
|
||||||
|
token=token,
|
||||||
|
create=create,
|
||||||
|
pks=pks,
|
||||||
|
replace=replace,
|
||||||
|
ignore=ignore,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -333,12 +346,16 @@ def _batches(iterable, size):
|
||||||
yield batch
|
yield batch
|
||||||
|
|
||||||
|
|
||||||
def _insert_batch(*, url, table, batch, token, create, pks):
|
def _insert_batch(*, url, table, batch, token, create, pks, replace, ignore):
|
||||||
if create:
|
if create:
|
||||||
data = {
|
data = {
|
||||||
"table": table,
|
"table": table,
|
||||||
"rows": batch,
|
"rows": batch,
|
||||||
}
|
}
|
||||||
|
if replace:
|
||||||
|
data["replace"] = True
|
||||||
|
if ignore:
|
||||||
|
data["ignore"] = True
|
||||||
if pks:
|
if pks:
|
||||||
if len(pks) == 1:
|
if len(pks) == 1:
|
||||||
data["pk"] = pks[0]
|
data["pk"] = pks[0]
|
||||||
|
|
@ -349,6 +366,10 @@ def _insert_batch(*, url, table, batch, token, create, pks):
|
||||||
data = {
|
data = {
|
||||||
"rows": batch,
|
"rows": batch,
|
||||||
}
|
}
|
||||||
|
if replace:
|
||||||
|
data["replace"] = True
|
||||||
|
if ignore:
|
||||||
|
data["ignore"] = True
|
||||||
url = "{}/{}/-/insert".format(url, table)
|
url = "{}/{}/-/insert".format(url, table)
|
||||||
response = httpx.post(
|
response = httpx.post(
|
||||||
url,
|
url,
|
||||||
|
|
@ -359,5 +380,11 @@ def _insert_batch(*, url, table, batch, token, create, pks):
|
||||||
json=data,
|
json=data,
|
||||||
timeout=40.0,
|
timeout=40.0,
|
||||||
)
|
)
|
||||||
response.raise_for_status()
|
if str(response.status_code)[0] != "2":
|
||||||
|
# Is there an error we can show?
|
||||||
|
if "/json" in response.headers["content-type"]:
|
||||||
|
data = response.json()
|
||||||
|
if "errors" in data:
|
||||||
|
raise click.ClickException("\n".join(data["errors"]))
|
||||||
|
response.raise_for_status()
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue