prestodb / presto-go-client

A Presto client for the Go programming language.
Apache License 2.0
226 stars 58 forks source link

Autorization error #67

Open Abhijeetsingh4 opened 2 years ago

Abhijeetsingh4 commented 2 years ago

Hi,i was trying to connect to presto using this repo ,but i was getting authorization error for user which was registered.The reason for this i think is the quotes added against the username.for eg -> if my user name is abc ,it was becoming 'abc' while sending call hence causing unauthorzied

Screenshot 2021-12-23 at 12 26 20 PM Screenshot 2021-12-23 at 12 27 07 PM

I solved the problem by cloning repo to my local and making two changes to it.

  1. changing file serial.go line no 89 as-> case string: return strings.Replace(x, "'", "''", -1), nil

  2. removing line 614->query = "EXECUTE " + preparedStatementName + " USING " + strings.Join(ss, ", ") from presto.go file

is there a better way of doing this?

thank you

Abhijeetsingh4 commented 2 years ago

Code used to connect to presto->

package hive

import ( "database/sql" "log"

// presto import
_ "github.com/experimentation-platform/presto"

)

var dsn = "http://abhijeet.singh5@gmail.com@localhost:8889?catalog=hive&schema=test"

// MakeQueryOnHive provides connection to hive func MakeQueryOnHive() []map[string]interface{} {

db, err := open()
if err != nil {
    log.Fatal(err)
}
defer close(db)
sqltxt := "select * from test.test limit 10"
tables, err := query(db, sqltxt)
if err != nil {
    panic(err)
}
return tables

}

func query(db *sql.DB, sqltxt string) ([]map[string]interface{}, error) { var tables []map[string]interface{}

rows, err := db.Query(sqltxt, sql.Named("X-Presto-User", string("abhijeet.singh5@gamil.com")))
if err != nil {
    return tables, err
}
defer rows.Close()
tables, err = sqlrows2Maps(rows)
if err != nil {
    return tables, err
}
return tables, nil

}

// open the connection func open() (*sql.DB, error) { db, err := sql.Open("presto", dsn) if err != nil { return db, err } err = db.Ping() if err != nil { return db, err } return db, nil }

// Close the connection func close(db *sql.DB) { db.Close() }

// Sqlrows2Maps sql query result rows converted to maps func sqlrows2Maps(rws *sql.Rows) ([]map[string]interface{}, error) { rowMaps := make([]map[string]interface{}, 0) var columns []string columns, err := rws.Columns() if err != nil { return rowMaps, err } values := make([]sql.RawBytes, len(columns)) scans := make([]interface{}, len(columns)) for i := range values { scans[i] = &values[i] } for rws.Next() { _ = rws.Scan(scans...) each := map[string]interface{}{} for i, col := range values { each[columns[i]] = string(col) }

    rowMaps = append(rowMaps, each)
}
return rowMaps, nil

}

micywin commented 2 years ago

rows, err := db.Query(sqltxt, sql.Named("X-Presto-User", string("abhijeet.singh5@gamil.com"))) you can try to Delete the sql.Named params