Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@flswld
Copy link

@flswld flswld commented Aug 7, 2025

问题描述

在构建 INSERT/UPDATE 语句时,gtime.Time 对象会根据 MySQL 字段类型(如 DATE)自动格式化为 'YYYY-MM-DD',但在 WHERE 条件中却会变成 'YYYY-MM-DD HH:MM:SS',导致条件匹配失败。

修复方案

在 formatWhereHolder 方法中,对 WHERE 条件的字段值进行类型感知转换,确保 DATE 类型字段的值格式与 INSERT/UPDATE 语句一致。

复用 ConvertValueForField 方法,保持类型转换逻辑统一。

影响范围

仅影响涉及 YEAR/DATE/TIME 字段的 WHERE 条件构建。

不会影响现有非时间类型的查询。

@houseme houseme requested a review from Copilot September 1, 2025 06:31
@houseme houseme requested a review from hailaz September 1, 2025 06:31
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes incorrect DATE field formatting in WHERE conditions for the GDB database package. The issue occurred when gtime.Time objects were formatted as 'YYYY-MM-DD HH:MM:SS' in WHERE clauses but as 'YYYY-MM-DD' in INSERT/UPDATE statements for DATE type fields, causing condition matching failures.

Key changes:

  • Added type-aware value conversion in formatWhereHolder method for time-related fields
  • Reuses existing ConvertValueForField method to ensure consistent type conversion logic
  • Only affects WHERE conditions with YEAR/DATE/TIME fields without impacting other query types

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +529 to +545
for fieldName, fieldValue := range data {
switch fieldValue.(type) {
case time.Time, *time.Time, gtime.Time, *gtime.Time:
var fieldType = db.GetCore().GetFieldTypeStr(ctx, fieldName, in.Table, in.Schema)
convFieldValue, err := db.GetCore().ConvertValueForField(
ctx,
fieldType,
fieldValue,
)
if err != nil || convFieldValue == nil {
continue
}
data[fieldName] = convFieldValue
default:
continue
}
}
Copy link

Copilot AI Sep 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The GetFieldTypeStr method is called inside the loop for each time field, which could result in repeated database metadata queries. Consider caching field types or retrieving them once before the loop to improve performance when multiple time fields are present.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants