-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Feature/search-by * Update Searching * FTS Fixes * Update READMEs * Go v1.19 changes * Added Examples to README * Spelling fix Co-authored-by: Caleb Horst <chorst@infoblox.com> Co-authored-by: Caleb Horst <chorst@infoblox.com>
- Loading branch information
Showing
26 changed files
with
393 additions
and
74 deletions.
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
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
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
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
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,58 @@ | ||
package gorm | ||
|
||
import ( | ||
"reflect" | ||
"time" | ||
) | ||
|
||
// GetFullTextSearchDBMask ... | ||
func GetFullTextSearchDBMask(object interface{}, fields []string, separator string) string { | ||
mask := "" | ||
objectVal := indirectValue(reflect.ValueOf(object)) | ||
if objectVal.Kind() != reflect.Struct { | ||
return mask | ||
} | ||
fieldsSize := len(fields) | ||
for i, fieldName := range fields { | ||
fieldVal := objectVal.FieldByName(camelCase(fieldName)) | ||
if !fieldVal.IsValid() { | ||
continue | ||
} | ||
underlyingVal := indirectValue(fieldVal) | ||
if !underlyingVal.IsValid() { | ||
switch fieldVal.Interface().(type) { | ||
case *time.Time: | ||
underlyingVal = fieldVal | ||
default: | ||
continue | ||
} | ||
} | ||
switch underlyingVal.Interface().(type) { | ||
case int32: | ||
mask += fieldName | ||
case string: | ||
mask += fieldName | ||
mask += " || '" + separator + "' || " | ||
mask += "replace(" + fieldName + ", '@', ' ')" | ||
mask += " || '" + separator + "' || " | ||
mask += "replace(" + fieldName + ", '.', ' ')" | ||
case *time.Time: | ||
mask += "coalesce(to_char(" + fieldName + ", 'MM/DD/YY HH:MI pm'), '')" | ||
case bool: | ||
mask += fieldName | ||
default: | ||
continue | ||
} | ||
if i != fieldsSize-1 { | ||
mask += " || '" + separator + "' || " | ||
} | ||
} | ||
|
||
return mask | ||
} | ||
|
||
// FormFullTextSearchQuery ... | ||
func FormFullTextSearchQuery(mask string) string { | ||
fullTextSearchQuery := "to_tsvector('simple', " + mask + ") @@ to_tsquery('simple', ?)" | ||
return fullTextSearchQuery | ||
} |
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
Oops, something went wrong.