-
-
Notifications
You must be signed in to change notification settings - Fork 3.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
Fixed "*DB.Raw()" bug where single or double quotes containing '@' were incorrectly identified as named variables #7207
Open
Rankgice
wants to merge
6
commits into
go-gorm:master
Choose a base branch
from
Rankgice:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c930baa
Fixed bug where '@' was incorrectly identified as a named variable in…
Rankgice 599aad6
Added test file raw_test.go
Rankgice 96c8a2e
Merge remote-tracking branch 'upstream/master'
Rankgice 202cbae
Added test file raw_test.go
Rankgice fa23822
Added test file raw_test.go
Rankgice 8e9c135
update test file raw_test.go
Rankgice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package tests_test | ||
|
||
import ( | ||
. "gorm.io/gorm/utils/tests" | ||
"testing" | ||
) | ||
|
||
func TestRawSelect(t *testing.T) { | ||
users := []User{ | ||
*GetUser("raw1", Config{}), | ||
*GetUser("raw2", Config{}), | ||
*GetUser("raw3", Config{}), | ||
*GetUser("@name", Config{}), | ||
*GetUser("@age", Config{}), | ||
} | ||
|
||
if err := DB.Create(&users).Error; err != nil { | ||
t.Fatalf("errors happened when create users: %v", err) | ||
} | ||
tests := []struct { | ||
TestName string | ||
Sql string | ||
args map[string]interface{} | ||
Expect []User | ||
}{ | ||
{ | ||
"raw_test1", | ||
`select * from users where name like @name and age = 18`, | ||
map[string]interface{}{ | ||
"name": "raw1", | ||
}, | ||
[]User{ | ||
users[0], | ||
}, | ||
}, | ||
{ | ||
"raw_test2", | ||
`select * from users where name like @name and age = 18`, | ||
map[string]interface{}{ | ||
"name": "@name", | ||
}, | ||
[]User{ | ||
users[3], | ||
}, | ||
}, | ||
{ | ||
"raw_test3", | ||
`select * from users where name like @name and age = 18`, | ||
map[string]interface{}{ | ||
"name": "@age", | ||
}, | ||
[]User{ | ||
users[4], | ||
}, | ||
}, | ||
{ | ||
"raw_test4", | ||
`select * from users where name like 'raw3' and age = @age`, | ||
map[string]interface{}{ | ||
"age": 18, | ||
}, | ||
[]User{ | ||
users[2], | ||
}, | ||
}, | ||
{ | ||
"raw_test5", | ||
`select * from users where name like '@name' and age = @age`, | ||
map[string]interface{}{ | ||
"age": 18, | ||
}, | ||
[]User{ | ||
users[3], | ||
}, | ||
}, | ||
{ | ||
"raw_test6", | ||
`select * from users where name like '@age' and age = @age`, | ||
map[string]interface{}{ | ||
"age": 18, | ||
}, | ||
[]User{ | ||
users[4], | ||
}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.TestName, func(t *testing.T) { | ||
var results []User | ||
if err := DB.Raw(test.Sql, test.args).Scan(&results).Error; err != nil { | ||
t.Errorf("errors %s: %v", test.TestName, err) | ||
} else { | ||
if len(results) != len(test.Expect) { | ||
t.Errorf("errors %s: %v", test.TestName, err) | ||
} else { | ||
for i := 0; i < len(results); i++ { | ||
CheckUser(t, results[i], test.Expect[i]) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think it would be better to add a check here to ensure that the previous character is not a " or '
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.
The logic of this code is
The second if:
When we first encounter '' or '', quotationMarks will be marked as' 'or' 'accordingly`
The first if:
When we have already been marked and encountered that mark (end mark) again, we will clear the mark
The third if:
When we have been marked and have not encountered the end mark, we will output it as it is
I didn't understand where you wanted to add a judgment