elgris / sqrl

Fluent SQL generation for golang
MIT License
279 stars 38 forks source link

"Clone" on assignment? #23

Closed fuintis closed 6 years ago

fuintis commented 6 years ago

Hello to everyone:

I know that sqrl is not thread safe, but: In a webservice context, What if on every request a cached pointer to an SelectBuilder is dereferenced and assigned to a variable?

Something like this:

var sqlSelect sqrl.Select("").From("users") // sqr.SelectBuilder . . . func calledOnEveryRequest() (error) { query := *sqlSelect // Is "query" a completely new copy (deep copy) of sqlSelect value? . . . }

Many thanks in advance.

elgris commented 6 years ago

Hi @fuintis ! Thanks for asking!

Well, it won't be a complete deep copy. For instance, sqrl.Select("*").From("users") returns you an instance of SelectBuilder which contains a bunch of slices. Slice is a pointer type, so if you try to copy it, you'll copy a pointer to the same memory block.

That means, that in your example sqlSelect and query refer to the same memory blocks. It can be dangerous. Imagine that sqlSelect internally changes its whereParts. That change will affect query as well. And you probably don't want such a surprise in your system :)