by AtNightWeCode · 2y ago · 0 comments
WHERE 1=1
AND ...[usual-where-clause]...
A performance and security compared to doing WHERE ...[usual-where-clause]...In [1]: "... WHERE " + " AND ".join(str(i) for i in range(4))
Out[1]: '... WHERE 0 AND 1 AND 2 AND 3'
Very strange.
So you end up with things like this.
"SELECT * FROM Music WHERE 1=1" + "AND category='rock'"
The risk is now that you by mistake allow for SQL-injections but also every genre will generate a different query plan. Depending on what SQL engine you use this may hurt performance.
And one would think that this is a thing of the past. But it is not.
If I'm inspecting a dataset I use WHERE 1=1 so I can add and remove conditions more easily.
I realise the confusion is in my wording of dynamic - I might amend the README.md to clarify. Thanks for the feedback!
1 = 1 is at least handy for simply joining a variadic amount of other clauses with ' AND ' rather than counting if there's any to add at all.
OP doesn't know what they are talking about because adding 1=1 is not a security risk. 1=1 is related to sql injections where a malicious attacker injects 'OR 1=1' into the end of the where clause to disable the where clause completely. OP probably saw '1=1' and threw that into the comment.