SAP / go-ase

SAP ASE Database Client for Go
Apache License 2.0
25 stars 14 forks source link

Can't connect using sql.Open() and URI DSN #237

Closed burningalchemist closed 1 year ago

burningalchemist commented 1 year ago

Description

Hi team, I cannot connect to the database using URI DSN via database/sql module, only Simple DSN is possible and passing parameters from the environment variables (ref).

Expected behaviour

The expected behaviour is that we can pass the supported URI DSN without any string manipulation directly to sql.Open() and connect to the database. Code-wise, it'd be great to have a similar control flow as in here in the OpenConnector() function.

Current behaviour

Provided URI DSN string can't be parsed when passed to sql.Open() function, unless we explicitly call dsn.Parse function that has the control flow to detect :// and picks ParseURI function instead. This results in error:

Failed to open database: dsn: recognized DSN part does not contain key/value parts: [...]

The error comes from parseSimple function, which is not desired in my current use case.

Steps to Reproduce

Example from the README.md doesn't work:

package main

import (
    "database/sql"
    _ "github.com/SAP/go-ase"
)

func main() {
    db, err := sql.Open("ase", "ase://user:pass@host:port/")
    if err != nil {
        log.Printf("Failed to open database: %v", err)
        return
    }
    defer db.Close()

    if err := db.Ping(); err != nil {
        log.Printf("Failed to ping database: %v", err)
        return
    }
}

Additional context

I'm the maintainer of https://github.com/burningalchemist/sql_exporter project, and one of my users is experiencing issues connecting to the SAP ASE database. Since the project is database agnostic, we try to avoid adding custom handling for specific databases in order to keep the code small and simple.

ntnn commented 1 year ago

Hi, thanks for reporting this.

The problem is indeed odd as the entry function should be Parse which defers to ParseSimple when no :// is in the passed string.

I'll see what I can find.

ntnn commented 1 year ago

Actually the error is pretty simple, go-ase OpenConnector is calling ParseSimple directly instead of Parse.

I'm not entirely sure why ParseSimple why that was done but that is an easy fix.

burningalchemist commented 1 year ago

Perfect! Thanks a lot for the rapid fix! 🚀