Skip to content

Passing agtype argument to prepared statement in Golang #2225

@P34K1N

Description

@P34K1N

Hello, I'm trying to use AGE's prepared statement to bulk insert data into my graph. To work with my Postgres database I use standard golang sql package. My code looks like this:

data := "<some data as serialized JSON>"

query := `SELECT *
FROM cypher('some_graph', $$
	<some code>
$$, $1)
AS (v agtype);`

stmt, err := conn.PrepareContext(ctx, query)
if err != nil {
	return fmt.Errorf("error on preparing statement: %w", err)
}

_, err = stmt.ExecContext(ctx, data)
if err != nil {
	return fmt.Errorf("error on executing statement: %w", err)
}

err = stmt.Close()
if err != nil {
	return fmt.Errorf("error on closing statement: %w", err)
}

If I execute this code I get the following error:
ERROR: invalid input syntax for type agtype (SQLSTATE 22P02)

If I try to cast $1 to agtype in any way (for example, $1::varchar::agtype) I get this:
ERROR: third argument of cypher function must be a parameter (SQLSTATE 22023)

So I need to somehow pass my argument in a way that makes PG recognize it as agtype without any typecasting. How do I do this?

My PG version is 17 if that matters.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions