buildsqlx icon indicating copy to clipboard operation
buildsqlx copied to clipboard

Where always returns empty data set

Open jmd3-sivailango opened this issue 6 years ago • 9 comments

Simple where condition not working in PostgreSQL

qDb := db.Table("roles").Select("role").Where("role", "=", "general")

above snippet always returns null

jmd3-sivailango avatar Nov 04 '20 18:11 jmd3-sivailango

Where clause must definitely return:

*DB

struct, but if u need the result of a query, u should use .Get() function:

.Where("role", "=", "general").Get()

which returns []map[string]interface{}

Can you show an example of creating a new connection? Like this:

var db = NewDb(NewConnection("postgres", "host=postgres user=postgres dbname=postgres password=postgres sslmode=disable"))

PS all tests passed in Actions, so it can't be non-working. https://github.com/arthurkushman/buildsqlx/runs/1194180939

arthurkushman avatar Nov 04 '20 19:11 arthurkushman

var db = buildsqlx.NewDb(buildsqlx.NewConnection("postgres", "user=postgres dbname=postgres password=postgres sslmode=disable"))

qDb := db.Table("roles").Select("role").Where("role", "=", "general")

res, err := qDb.Get()

if err != nil {
	fmt.Println(err)
}

return c.JSON(&fiber.Map{
	"status": true,
	"data":   res,
})

I am using gofiber framework to send JSON response

jmd3-sivailango avatar Nov 05 '20 04:11 jmd3-sivailango

Hi, what I've done to test your example and abstract away an application that uses it from buildsqlx, is that:

  • created new dir for go project
  • set-up there a simple docker-compose file with content:
db:
  image: postgres
  ports:
    - "5432:5432"
  environment:
    POSTGRES_USER: "postgres"
    POSTGRES_PASSWORD: "postgres"

then executed the code:

package main

import (
	"fmt"
	"github.com/arthurkushman/buildsqlx"
)

func main() {
	var db = buildsqlx.NewDb(buildsqlx.NewConnection("postgres", "host=127.0.0.1 user=postgres dbname=postgres password=postgres sslmode=disable"))

	qDb := db.Table("pg_roles").Select("rolname").Where("rolname", "=", "postgres")

	res, err := qDb.Get()

	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(res)
}

1-st thing that u should check is the errors in console, because there is no roles default pg schema, but pg_roles, also there is no column role, but pg_roles. It works perfectly fine on my local machine, with docker instance and after docker-compose up successful run.

arthurkushman avatar Nov 05 '20 11:11 arthurkushman

@arthurkushman I've kept database as postgres just for example on above code snippet. There is a roles table in my database. Is there a way I can see actual SQL query formed from builder?

jmd3-sivailango avatar Nov 05 '20 11:11 jmd3-sivailango

@arthurkushman I've kept database as postgres just for example on above code snippet. There is a roles table in my database. Is there a way I can see actual SQL query formed from builder?

Sure @sivailangos u can add .Dump or .Dd at any chain call: https://github.com/arthurkushman/buildsqlx/blob/bcead85cce9ff0c65ccab9c8aae885b6e14ef11f/builder.go#L495

It is in readme: https://github.com/arthurkushman/buildsqlx#user-content-dump-dd

Also, I don't know/see your db schemas, nor where it is set-up, or whether it has been set-up correctly, if u want to make an adequate dialog - please provide full information.

arthurkushman avatar Nov 05 '20 11:11 arthurkushman

Still same issue, below is screenshot of raw query, image

Below is code snippet image

Am I missing anything? If I remove where condition then it works perfectly

jmd3-sivailango avatar Nov 05 '20 12:11 jmd3-sivailango

It seems that u are correct about this behaviour with Where clause, when it is used with string values, unfortunately native sql.Query() function can't do this conversion - https://github.com/arthurkushman/buildsqlx/blob/bcead85cce9ff0c65ccab9c8aae885b6e14ef11f/factory.go#L40

though it has been built by code correctly - https://github.com/arthurkushman/buildsqlx/blob/bcead85cce9ff0c65ccab9c8aae885b6e14ef11f/factory.go#L83

It seems like there is a need of recode the Query behaviour, but I suppose not with something fmt.Sprintf which will leave the holes in security, but the alternative ways like .Scan for example seems even more difficult and non-resembling to the code-base.

May be u can offer an elegant solution? But as for me I don't see at this moment anything except for those two.

arthurkushman avatar Nov 05 '20 16:11 arthurkushman

Hi, good news incorrect bindings on string values had been fixed - https://github.com/arthurkushman/buildsqlx/releases/tag/v0.7.5 All tests passed + 1 for Where clause only https://github.com/arthurkushman/buildsqlx/runs/1367896340?check_suite_focus=true

arthurkushman avatar Nov 07 '20 14:11 arthurkushman

Hi, good news incorrect bindings on string values had been fixed - https://github.com/arthurkushman/buildsqlx/releases/tag/v0.7.5 All tests passed + 1 for Where clause only https://github.com/arthurkushman/buildsqlx/runs/1367896340?check_suite_focus=true

It works now!!! Thanks for quick turn around!

jmd3-sivailango avatar Nov 08 '20 17:11 jmd3-sivailango