Skip to content

Commit 1ebc3f2

Browse files
committed
fix: insert heap
1 parent f88a24d commit 1ebc3f2

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

crypto/query/query.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,33 @@ func InsertWithHeap(c *crypto.Crypto, ctx context.Context, tx *sql.Tx, tableName
3737
entityValue := reflect.ValueOf(entity)
3838
entityType := entityValue.Type()
3939

40-
fieldNames := make([]string, entityType.NumField())
41-
placeholders := make([]string, entityType.NumField())
42-
args := make([]interface{}, entityType.NumField())
40+
var fieldNames []string
41+
var args []interface{}
42+
var placeholders []string
4343

4444
var th []TextHeap
4545
for i := 0; i < entityType.NumField(); i++ {
4646
field := entityType.Field(i)
47-
fieldNames[i] = field.Tag.Get("db")
48-
args[i] = entityValue.Field(i).Interface()
47+
fieldName := field.Tag.Get("db")
48+
if fieldName == "" {
49+
continue
50+
}
4951

50-
if field.Tag.Get("bidx_col") != "" {
51-
fieldNames = append(fieldNames, field.Tag.Get("bidx_col"))
52+
fieldNames = append(fieldNames, fieldName)
53+
args = append(args, entityValue.Field(i).Interface())
54+
55+
if bidxCol := field.Tag.Get("bidx_col"); bidxCol != "" {
56+
fieldNames = append(fieldNames, bidxCol)
5257
placeholders = append(placeholders, "$"+fmt.Sprint(len(placeholders)+1))
5358

54-
switch entityValue.Field(i).Interface().(type) {
59+
switch fieldValue := entityValue.Field(i).Interface().(type) {
5560
case types.AESChiper:
56-
fieldValue := entityValue.Field(i).Interface().(types.AESChiper)
5761
str, heaps := BuildHeap(c, fieldValue.To(), field.Tag.Get("txt_heap_table"))
5862
th = append(th, heaps...)
5963
args = append(args, str)
6064
}
6165
}
62-
placeholders[i] = "$" + fmt.Sprint(i+1)
66+
placeholders = append(placeholders, "$"+fmt.Sprint(len(placeholders)+1))
6367
}
6468

6569
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", tableName, strings.Join(fieldNames, ", "), strings.Join(placeholders, ", "))

0 commit comments

Comments
 (0)