Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autorization error #67

Open
Abhijeetsingh4 opened this issue Dec 23, 2021 · 2 comments
Open

Autorization error #67

Abhijeetsingh4 opened this issue Dec 23, 2021 · 2 comments

Comments

@Abhijeetsingh4
Copy link

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
Copy link
Author

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
Copy link

micywin commented May 24, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants