Octo.jl icon indicating copy to clipboard operation
Octo.jl copied to clipboard

Can support use case: Insert statement values has sql function

Open huaxk opened this issue 5 years ago • 3 comments

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?

huaxk avatar Apr 24 '19 15:04 huaxk

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.

wookay avatar Apr 24 '19 19:04 wookay

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.

huaxk avatar May 10 '19 15:05 huaxk

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

wookay avatar May 10 '19 15:05 wookay