go-gorm / playground

GORM Playground (Please Create PR for this project to report issues)
MIT License
89 stars 678 forks source link

test: ? literal in regexp_like #699

Open ericsun2 opened 6 months ago

ericsun2 commented 6 months ago

Explain your user case and expected results

When ? literal is part of the SQL statement for regexp pattern (not as a placeholder), gorm will still treat such literal as a placeholder and hence throw an error.

For example:

select name, age, birthday, active from users where name ~ '(zh)?.*' and name like ?

This is a bug in https://github.com/go-gorm/gorm/blob/master/clause/expression.go - https://github.com/go-gorm/gorm/issues/6889

=== RUN   TestGORM

2024/03/10 15:31:27 /Users/ericsun/workspace/git/gorm-playground/main_test.go:14
[1.757ms] [rows:1] INSERT INTO "users" ("created_at","updated_at","deleted_at","name","age","birthday","company_id","manager_id","active") VALUES ('2024-03-10 15:31:27.611','2024-03-10 15:31:27.611',NULL,'jinzhu',0,NULL,NULL,NULL,false) RETURNING "id"

2024/03/10 15:31:27 /Users/ericsun/workspace/git/gorm-playground/main_test.go:17
[0.826ms] [rows:1] SELECT * FROM "users" WHERE "users"."id" = 1 AND "users"."deleted_at" IS NULL ORDER BY "users"."id" LIMIT 1

2024/03/10 15:31:27 /Users/ericsun/workspace/git/gorm-playground/main_test.go:22
[0.649ms] [rows:-] select name, age, birthday, active from users where name ~ '(zh){0,}.*' and name like '%in%'
    main_test.go:25: Succeeded, got result: true

2024/03/10 15:31:27 /Users/ericsun/workspace/git/gorm-playground/main_test.go:29 ERROR: syntax error at end of input (SQLSTATE 42601)
[24.548ms] [rows:-] select name, age, birthday, active from users where name ~ '(zh)'%in%'.*' and name like ?
    main_test.go:30: Failed, got error: ERROR: syntax error at end of input (SQLSTATE 42601)
--- FAIL: TestGORM (0.03s)
FAIL
gg1229505432 commented 5 months ago

you can use '%in%' instead of Regex