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

Docs: Use a raw query? #383

Open
IngwiePhoenix opened this issue Feb 11, 2025 · 3 comments
Open

Docs: Use a raw query? #383

IngwiePhoenix opened this issue Feb 11, 2025 · 3 comments

Comments

@IngwiePhoenix
Copy link

I made my SurrealDB driver work and am currently testing it - and while testing the more "not so standart" parts of SurrealDB, I realized that... I had no idea how to run raw queries.

func main() {
	adapter, err := srel.Open("ws://root:root@127.0.0.1:8000/rpc?method=root")
	if err != nil {
		log.Fatal(err.Error())
	}
	defer adapter.Close()

	repo := rel.New(adapter)
	sql := rel.SQL("INFO FOR ROOT;")
	cursor, err := adapter.Query(context.TODO(), rel.Query{
		SQLQuery: sql,
	})
	if err != nil {
		log.Fatal(err.Error())
	}
	fields, err := cursor.Fields()
	if err != nil {
		log.Fatal(err.Error())
	}
	fmt.Print(fields)
}

Is there an easier way to construct raw queries? I can't imagine it's this convoluted... x)

Kind regards,
Ingwie

@Fs02
Copy link
Member

Fs02 commented Feb 11, 2025

Hi Ingwie,

you can run raw query just like this:

sql := rel.SQL("SELECT id, title, price, orders = (SELECT COUNT(t.id) FROM [transactions] t WHERE t.book_id = b.id) FROM books b where b.id=?", 1)
err := repo.Find(ctx, &book, sql)

https://go-rel.github.io/queries/#native-sql-query

@IngwiePhoenix
Copy link
Author

Great, thank you! Must've not seen the forest for all it's trees...

Last thing: Is there a better way to stringify generated queries? In my tests, I'd like to verify if my SQL generates correctly. Currently, I just cast the adapter to the initial instance returned by New(...); basically, in my case, adapter.(*SurrealDB). From there, I use it's QueryBuilder and pass it whatever query I had built (i.e. rel.From("books").Select("author")) to see what it generated.

inst := adapter.(*SurrealDB)
q := rel.From("books").Select("author")
sql, _ := inst.QueryBuilder.Build(q)
fmt.Println(sql)

Is there a more elegant solution rather than casting directly to the adapter type? I couldn't find a direct function for that in the interface.

By the way, the driver is here: https://github.com/IngwiePhoenix/surrealdb-driver/blob/master/pkg/rel/ - I have a lot of test cases to write yet, but this was implemented in but a day, which was quite easy to do actually.

Thanks and kind regards!

@Fs02
Copy link
Member

Fs02 commented Feb 18, 2025

the query builder is an internal adapter implementation right now, so there's no direct way to access it from rel as of now

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