pganalyze / pg_query_go

Go library to parse and normalize SQL queries using the PostgreSQL query parser
BSD 3-Clause "New" or "Revised" License
663 stars 79 forks source link

Structed error call back #75

Closed mhkarimi1383 closed 1 year ago

mhkarimi1383 commented 1 year ago

Hi I need to translate parse errors to pgproto error responses with the struct below

type ErrorResponse struct {
    Severity            string
    SeverityUnlocalized string // only in 9.6 and greater
    Code                string
    Message             string
    Detail              string
    Hint                string
    Position            int32
    InternalPosition    int32
    InternalQuery       string
    Where               string
    SchemaName          string
    TableName           string
    ColumnName          string
    DataTypeName        string
    ConstraintName      string
    File                string
    Line                int32
    Routine             string

    UnknownFields map[byte]string
}

but I was not able to find a way to query parse errors (like invalid queries, etc.)

lfittl commented 1 year ago

@mhkarimi1383 Can you describe a bit more what your use case is?

Technically some of this information is available (see the complete error object in libpg_query), but today we don't use it in the Go library:

https://github.com/pganalyze/pg_query_go/blob/main/parser/parser.go#L82

We could expand that error handling, but it would require a bit of an API redesign, so it would be good to understand a bit better what you're trying to do. Note that some of this information (e.g. SchemaName, etc) is not available because pg_query works with the raw parse tree (i.e. without parse analysis that maps to actual database objects).

mhkarimi1383 commented 1 year ago

I'm working on a project and I want to return that error to user directly without sending that query to postgres so I need full structured error

lfittl commented 1 year ago

@mhkarimi1383 Makes sense - just saw your pg_pro project on your GitHub profile - that looks like a cool project!

This actually was pretty straightforward to add, I just did a quick try at this in #76

Do you want to test that PR and see whether that solves your use case?

As mentioned this is not all the possible error fields, but for basic errors like "this query fails to parse" it should match what Postgres itself would return to a client, except for the SQL error code, which is generated outside of the part of Postgres we currently work with -- I believe that's represented as Code in your struct above (you could just hard code that to the SQL error code matching parser errors, I believe that might be always the same).

mhkarimi1383 commented 1 year ago

Thanks I will test as I get into my system

mhkarimi1383 commented 1 year ago

@lfittl Worked thanks, but I was not able to map Funcname to one of those fields in that struct, also I mapped Context field to Details

mhkarimi1383 commented 1 year ago

@lfittl any updates?