Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion database/gdb/gdb_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ func (c *Core) SetTableFields(ctx context.Context, table string, fields map[stri
func (c *Core) GetTablesWithCache() ([]string, error) {
var (
ctx = c.db.GetCtx()
cacheKey = fmt.Sprintf(`Tables:%s`, c.db.GetGroup())
cacheKey = genTableNamesCacheKey(c.db.GetGroup())
cacheDuration = gcache.DurationNoExpire
innerMemCache = c.GetInnerMemCache()
)
Expand Down
12 changes: 12 additions & 0 deletions database/gdb/gdb_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,7 @@ func FormatMultiLineSqlToSingle(sql string) (string, error) {
return sql, nil
}

// genTableFieldsCacheKey generates cache key for table fields.
func genTableFieldsCacheKey(group, schema, table string) string {
return fmt.Sprintf(
`%s%s@%s#%s`,
Expand All @@ -976,6 +977,7 @@ func genTableFieldsCacheKey(group, schema, table string) string {
)
}

// genSelectCacheKey generates cache key for select.
func genSelectCacheKey(table, group, schema, name, sql string, args ...any) string {
if name == "" {
name = fmt.Sprintf(
Expand All @@ -988,3 +990,13 @@ func genSelectCacheKey(table, group, schema, name, sql string, args ...any) stri
}
return fmt.Sprintf(`%s%s`, cachePrefixSelectCache, name)
}

// genTableNamesCacheKey generates cache key for table names.
func genTableNamesCacheKey(group string) string {
return fmt.Sprintf(`Tables:%s`, group)
}

// genSoftTimeFieldNameTypeCacheKey generates cache key for soft time field name and type.
func genSoftTimeFieldNameTypeCacheKey(schema, table string, candidateFields []string) string {
return fmt.Sprintf(`getSoftFieldNameAndType:%s#%s#%s`, schema, table, strings.Join(candidateFields, ","))
Comment thread
LanceAdd marked this conversation as resolved.
Outdated
}
6 changes: 3 additions & 3 deletions database/gdb/gdb_model_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func (m *Model) Delete(where ...any) (result sql.Result, err error) {
var (
conditionWhere, conditionExtra, conditionArgs = m.formatCondition(ctx, false, false)
conditionStr = conditionWhere + conditionExtra
fieldNameDelete, fieldTypeDelete = m.softTimeMaintainer().GetFieldNameAndTypeForDelete(
ctx, "", m.tablesInit,
fieldNameDelete, fieldTypeDelete = m.softTimeMaintainer().GetFieldInfo(
ctx, "", m.tablesInit, SoftTimeFieldDelete,
)
)
if m.unscoped {
Expand All @@ -52,7 +52,7 @@ func (m *Model) Delete(where ...any) (result sql.Result, err error) {

// Soft deleting.
if fieldNameDelete != "" {
dataHolder, dataValue := m.softTimeMaintainer().GetDataByFieldNameAndTypeForDelete(
dataHolder, dataValue := m.softTimeMaintainer().GetDeleteData(
ctx, "", fieldNameDelete, fieldTypeDelete,
)
in := &HookUpdateInput{
Expand Down
12 changes: 6 additions & 6 deletions database/gdb/gdb_model_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ func (m *Model) doInsertWithOption(ctx context.Context, insertOption InsertOptio
var (
list List
stm = m.softTimeMaintainer()
fieldNameCreate, fieldTypeCreate = stm.GetFieldNameAndTypeForCreate(ctx, "", m.tablesInit)
fieldNameUpdate, fieldTypeUpdate = stm.GetFieldNameAndTypeForUpdate(ctx, "", m.tablesInit)
fieldNameDelete, fieldTypeDelete = stm.GetFieldNameAndTypeForDelete(ctx, "", m.tablesInit)
fieldNameCreate, fieldTypeCreate = stm.GetFieldInfo(ctx, "", m.tablesInit, SoftTimeFieldCreate)
fieldNameUpdate, fieldTypeUpdate = stm.GetFieldInfo(ctx, "", m.tablesInit, SoftTimeFieldUpdate)
fieldNameDelete, fieldTypeDelete = stm.GetFieldInfo(ctx, "", m.tablesInit, SoftTimeFieldDelete)
)
// m.data was already converted to type List/Map by function Data
newData, err := m.filterDataForInsertOrUpdate(m.data)
Expand Down Expand Up @@ -295,20 +295,20 @@ func (m *Model) doInsertWithOption(ctx context.Context, insertOption InsertOptio
if !m.unscoped && isSoftTimeFeatureEnabled {
for k, v := range list {
if fieldNameCreate != "" && empty.IsNil(v[fieldNameCreate]) {
fieldCreateValue := stm.GetValueByFieldTypeForCreateOrUpdate(ctx, fieldTypeCreate, false)
fieldCreateValue := stm.GetFieldValue(ctx, fieldTypeCreate, false)
if fieldCreateValue != nil {
v[fieldNameCreate] = fieldCreateValue
}
}
if fieldNameUpdate != "" && empty.IsNil(v[fieldNameUpdate]) {
fieldUpdateValue := stm.GetValueByFieldTypeForCreateOrUpdate(ctx, fieldTypeUpdate, false)
fieldUpdateValue := stm.GetFieldValue(ctx, fieldTypeUpdate, false)
if fieldUpdateValue != nil {
v[fieldNameUpdate] = fieldUpdateValue
}
}
// for timestamp field that should initialize the delete_at field with value, for example 0.
if fieldNameDelete != "" && empty.IsNil(v[fieldNameDelete]) {
fieldDeleteValue := stm.GetValueByFieldTypeForCreateOrUpdate(ctx, fieldTypeDelete, true)
fieldDeleteValue := stm.GetFieldValue(ctx, fieldTypeDelete, true)
if fieldDeleteValue != nil {
v[fieldNameDelete] = fieldDeleteValue
}
Expand Down
2 changes: 1 addition & 1 deletion database/gdb/gdb_model_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ func (m *Model) formatCondition(
}
// WHERE
conditionWhere, conditionArgs = m.whereBuilder.Build()
softDeletingCondition := m.softTimeMaintainer().GetWhereConditionForDelete(ctx)
softDeletingCondition := m.softTimeMaintainer().GetDeleteCondition(ctx)
if m.rawSql != "" && conditionWhere != "" {
if gstr.ContainsI(m.rawSql, " WHERE ") {
conditionWhere = " AND " + conditionWhere
Expand Down
Loading
Loading