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

Incorrect query in sqlserver dialect #411

Open
3 tasks
pawan-lambda opened this issue Mar 18, 2024 · 0 comments
Open
3 tasks

Incorrect query in sqlserver dialect #411

pawan-lambda opened this issue Mar 18, 2024 · 0 comments

Comments

@pawan-lambda
Copy link

pawan-lambda commented Mar 18, 2024

I am using goqu to create query and sqlx to execute using go-mssql . I am facing issue while inserting marshalled map to db , as it is storing value {\"AutomationAnalyticsUI\":\"heelo\",\"CheckAutoreview\":\"2.0\",\"MlDashboardToShow\":\"2.0\",\"OnboardingScreenShown\":false,\"rai\":\"pawan\"} in db which is creating error in unmarshalling.

To Reproduce

`package main

import (
"encoding/json"
"fmt"

"github.com/jmoiron/sqlx"

"github.com/doug-martin/goqu/v9"
_ "github.com/doug-martin/goqu/v9/dialect/mysql"
_ "github.com/doug-martin/goqu/v9/dialect/sqlserver"

// other imports
_ "github.com/go-sql-driver/mysql"
_ "github.com/microsoft/go-mssqldb"

)

type metadata struct {
ID int64 db:"id" json:"id" goqu:"skipinsert,skipupdate"
Metadata string db:"metadata_column" json:"metadata_colummn"
}

func main() {
dbAddress := "sqlserver://sa:Test12345@localhost:1433?database=lums"
//dbAddress := "root:test@(localhost:3306)/test?parseTime=true"
// Assuming db is your *sql.DB
db, err := sqlx.Connect("sqlserver", dbAddress) // Make sure to use the correct dialect
if err != nil {
fmt.Print(err.Error())
return
}

// Create an instance of your data
data := make(map[string]interface{}, 0)
data["AutomationAnalyticsUI"] = "heelo"
data["MlDashboardToShow"] = "2.0"
data["CheckAutoreview"] = "2.0"
data["OnboardingScreenShown"] = false
// Marshal your struct to a JSON string
jsonData, err := json.Marshal(data)
if err != nil {
	fmt.Print(err.Error())
	// handle error
	return
}

updateJson := string(jsonData)
data1 := metadata{}
data1.Metadata = updateJson

// Convert jsonData (which is []byte) to string for insertion

dialect := goqu.Dialect("sqlserver")

//Use jsonString in your insert query
insertStatement := dialect.Insert("dummy").Rows(data1)

sql, _, err := insertStatement.ToSQL()
if err != nil {
	fmt.Print(err.Error())
	return
}
fmt.Println(sql)

// // Execute the insert statement
if _, err := db.Exec(sql); err != nil {
	// handle error
	fmt.Print(err.Error())
	return
}

var getData metadata
getSQL := dialect.From("dummy").Select(getData)
sql, _, err = getSQL.ToSQL()
if err != nil {
	fmt.Print(err.Error())
	return
}
// // query the insert statement
if err := db.QueryRowx(sql).StructScan(&getData); err != nil {
	// handle error
	fmt.Println(err.Error())
	return
}

metadataObj1 := make(map[string]interface{})

err = json.Unmarshal([]byte(getData.Metadata), &metadataObj1)
if err != nil {
	fmt.Print(err.Error())
}

fmt.Println("------getData--------")
fmt.Println(getData)
fmt.Println("------metadata--------")
fmt.Println(metadataObj1)

}
`

Expected behavior
Query should look like this - INSERT INTO "dummy" ("metadata_column") VALUES ('{"AutomationAnalyticsUI":"heelo","CheckAutoreview":"2.0","MlDashboardToShow":"2.0","OnboardingScreenShown":false}')

Dialect:

  • postgres
  • mysql
  • sqlite3
  • [T] sqlserver
@pawan-lambda pawan-lambda changed the title invalid character '\\' looking for beginning of object key Incorrect query in sqlserver dialect Mar 18, 2024
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

1 participant