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

Can support use case: Insert statement values has sql function #11

Open
huaxk opened this issue Apr 24, 2019 · 3 comments
Open

Can support use case: Insert statement values has sql function #11

huaxk opened this issue Apr 24, 2019 · 3 comments

Comments

@huaxk
Copy link

huaxk commented Apr 24, 2019

Table DDL:

CREATE TABLE heres(
  id serial,
  name varchar,
  lnglat geometry("POINT", 4326),
  primary key (id)
);

INSERT INTO heres (name, lnglat) VALUES ('test2',ST_GeomFromEWKT('SRID=4326;POINT(12 34)'))
sql works fine.

julia code:

struct Here
end

Schema.model(Here, table_name="heres", primary_key="id")
heres = from(Here, :heres)

@sql_functions ST_GeomFromEWKT
Repo.insert!(Here, (name="test1", lnglat=ST_GeomFromEWKT("SRID=4326;POINT(12 34)")))

The error message as follows:

[ Info: INSERT INTO heres (name, lnglat) VALUES ($1, $2)   (name = "test1", lnglat = SQLFunction(:ST_GeomFromEWKT, ("SRID=4326;POINT(12 34)",)))
ERROR: ERROR:  parse error - invalid geometry
HINT:  "SQ" <-- parse error at position 2 within geometry

What is the problem?

@wookay
Copy link
Owner

wookay commented Apr 24, 2019

it's not supported Repo.insert! with sql functions.

you can debug the internal code at
https://github.com/wookay/Octo.jl/blob/master/src/Backends/PostgreSQL.jl#L74

function execute(prepared::String, nts::Vector{<:NamedTuple})::ExecuteResult
    conn = current_conn()
    stmt = LibPQ.prepare(conn, prepared)
    for tup in nts
        @info :tup tup
        LibPQ.execute(stmt, collect(tup))
    end
    ExecuteResult()
end

collect(tup) should be changed as for SQLFunction.

@huaxk
Copy link
Author

huaxk commented May 10, 2019

Repo.insert!(Here, (name="test1", lnglat=ST_GeomFromEWKT("SRID=4326;POINT(12 34)")))
generate error sql:
INSERT INTO heres (name, lnglat) VALUES ($1, $2) (name = "test1", lnglat = SQLFunction(:ST_GeomFromEWKT, ("SRID=4326;POINT(12 34)",)))
I think it should be correct to generate such code:
INSERT INTO heres (name, lnglat) VALUES ($1, ST_GeomFromEWKT($2)) (name = "test1", lnglat = "SRID=4326;POINT(12 34)")
sql function should place in the list of VALUES.

@wookay
Copy link
Owner

wookay commented May 10, 2019

hmm. currently it's not supported Repo.insert! with sql functions. see the above comment in 16 days ago.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants