-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
fix: [QueryBuilder] getOperatorFromWhereKey() misses EXISTS, BETWEEN #7155
fix: [QueryBuilder] getOperatorFromWhereKey() misses EXISTS, BETWEEN #7155
Conversation
c43b840
to
cd7b887
Compare
cd7b887
to
bc2d692
Compare
With this PR, the following test passes: $builder = $this->db->table('jobs');
$where = "created_on BETWEEN '2022-07-01' AND '2022-12-31'";
$builder->where($where, '', false); // empty string
$expectedSQL = "SELECT * FROM \"jobs\" WHERE created_on BETWEEN '2022-07-01' AND '2022-12-31' ";
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
$expectedBinds = [
"created_on BETWEEN '2022-07-01' AND '2022-12-31'" => [
0 => '',
1 => false,
],
];
$this->assertSame($expectedBinds, $builder->getBinds()); But I still think this is a misuse, because the binding is completely wrong, and the output SQL has an extra space in the end. |
@codeigniter4/database-team please take a look. @kenjis Let me know if I need to do this one. |
I don't think we should support BETWEEN, EXISTS, IS NULL, IS NOT NULL, IS TRUE, IS FALSE, etc. I'm not sure that these work the same across DBMS. If your using these you should just do it with RawSql or with escape. Another example: SELECT 'something' AS test
WHERE IF(RAND()>=0.5,TRUE,FALSE) This WHERE statement is great for randomly selecting rows or used with a weighted column. |
0cc87e9
to
c160153
Compare
If we will support BETWEEN, EXISTS, the difference is only to add |
What do you mean? $builder = $this->db->table('jobs');
$where = "IF(RAND()>=0.5,TRUE,FALSE)";
$builder->where($where, null, false);
$expectedSQL = 'SELECT * FROM "jobs" WHERE IF(RAND()>=0.5,TRUE,FALSE)';
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
$expectedBinds = [];
$this->assertSame($expectedBinds, $builder->getBinds()); |
Yes this is correct: $where = "IF(RAND()>=0.5,TRUE,FALSE)";
$builder->where($where, null, false); I was trying to say we shouldn't support things like: $builder->where('column', 'IS NULL'); |
The following is not supported before or after this PR: $builder->where('column', 'IS NULL');
// 'SELECT * FROM "users" WHERE "column" = 'IS NULL'' The following is also not supported. $builder->where('column', 'IS NULL', false);
// 'SELECT * FROM "users" WHERE column = IS NULL' |
@codeigniter4/database-team Can someone review? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a fan of allowing:
$builder->where($where, '', false);
But since developers were using it this way, we shouldn't break their code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why we need all of these spelled out.
There are a lot more of these.
SOUNDS LIKE
RLIKE
NOT RLIKE
IS TRUE
IS FALSE
FIND_IN_SET
Probably no user used these, or did not send a bug report. In my opinion it is not a good practice to write SQL strings in the key of |
@tangix With this PR, your issue #7117 will be fixed in v4.3.2. |
Description
Follow-up #6986
Ref #7151, #7117
BaseBuilder::getOperatorFromWhereKey()
missesEXISTS
orBETWEEN
Checklist: