Skip to content

Commit e8d70fa

Browse files
committed
time default
1 parent b995731 commit e8d70fa

File tree

5 files changed

+82
-27
lines changed

5 files changed

+82
-27
lines changed

orm/create_struct.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ func (q *Query[T]) CreateStruct(file ...string) error {
8080
structFile = file[0]
8181
} else {
8282
_, fs, _, _ := runtime.Caller(1)
83-
fmt.Println(fs)
8483
structFile = fs
8584
}
8685

orm/create_table.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const primaryKeyPrefix = "primary"
1212
const uniqueKeyPrefix = "unique"
1313
const keyPrefix = "index"
1414
const nullPrefix = "null"
15+
const notNullPrefix = "not null"
1516
const autoIncrementPrefix = "auto_increment"
1617
const createdAtColumn = "created_at"
1718
const updatedAtColumn = "updated_at"
@@ -143,7 +144,6 @@ func generateColumnStrings(dbColums []dBColumn) []string {
143144
} else {
144145
words = append(words, "not null")
145146
}
146-
147147
//add default
148148
if v.AutoIncrement {
149149
words = append(words, "auto_increment")
@@ -274,14 +274,16 @@ func getMigrateColumns(table *queryTable) []dBColumn {
274274
}
275275

276276
if column.Name == createdAtColumn {
277-
column.Type = "timestamp"
277+
column.Null = false
278+
column.Type = "datetime"
278279
column.Default = "CURRENT_TIMESTAMP"
279280
} else if column.Name == updatedAtColumn {
280-
column.Type = "timestamp"
281+
column.Null = false
282+
column.Type = "datetime"
281283
column.Default = "CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"
282284
} else if column.Name == deletedAtColumn {
283285
column.Null = true
284-
column.Type = "timestamp"
286+
column.Type = "datetime"
285287
column.Default = "Null"
286288
}
287289

@@ -297,16 +299,24 @@ func getMigrateColumns(table *queryTable) []dBColumn {
297299
}
298300
}
299301
}
302+
if strings.ToLower(column.Default) == "null" {
303+
column.Null = true
304+
}
300305

301306
if ormTags[0] != "" {
302307
overideColumn := dBColumn{}
303308

309+
overrideNull := false
304310
for k, v := range ormTags {
305311
if k == 0 {
306312
continue
307313
}
308314
if v == nullPrefix {
315+
overrideNull = true
309316
overideColumn.Null = true
317+
} else if v == notNullPrefix {
318+
overrideNull = true
319+
overideColumn.Null = false
310320
} else if v == autoIncrementPrefix {
311321
overideColumn.AutoIncrement = true
312322
} else if strings.HasPrefix(v, primaryKeyPrefix) {
@@ -328,7 +338,9 @@ func getMigrateColumns(table *queryTable) []dBColumn {
328338
}
329339
}
330340

331-
column.Null = overideColumn.Null
341+
if overrideNull {
342+
column.Null = overideColumn.Null
343+
}
332344
column.AutoIncrement = overideColumn.AutoIncrement
333345
column.Primary = overideColumn.Primary
334346
if overideColumn.Type != "" {
@@ -358,9 +370,11 @@ func getMigrateColumns(table *queryTable) []dBColumn {
358370

359371
func getTypeAndDefault(val reflect.Value) (string, string) {
360372
var types, defaults string
373+
typ := val.Type()
361374
kind := val.Kind()
362375
if kind == reflect.Ptr {
363-
kind = val.Elem().Kind()
376+
kind = val.Type().Elem().Kind()
377+
typ = typ.Elem()
364378
}
365379
switch kind {
366380
case reflect.Bool, reflect.Int8:
@@ -397,11 +411,21 @@ func getTypeAndDefault(val reflect.Value) (string, string) {
397411
types = "varchar(255)"
398412
default:
399413
if _, ok := val.Interface().(*time.Time); ok {
400-
types = "timestamp"
414+
types = "datetime"
401415
} else if _, ok := val.Interface().(time.Time); ok {
402-
types = "timestamp"
416+
types = "datetime"
403417
} else {
404-
types = "varchar(255)"
418+
realVal := reflect.New(typ)
419+
if _, ok := realVal.Elem().Field(0).Interface().(*time.Time); ok {
420+
types = "datetime"
421+
} else if _, ok := realVal.Elem().Field(0).Interface().(time.Time); ok {
422+
types = "datetime"
423+
} else {
424+
types = "varchar(255)"
425+
}
426+
}
427+
if types == "datetime" {
428+
defaults = "current_timestamp"
405429
}
406430
}
407431
return types, defaults

orm/query.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ func (q *Query[T]) Alias(alias string) *Query[T] {
9090
q.tables[0].alias = alias
9191
return q
9292
}
93+
func (q *Query[T]) TableName(override string) *Query[T] {
94+
q.tables[0].overrideTableName = override
95+
return q
96+
}
9397

9498
//query raw, tablename can be empty
9599
func newQueryRaw(tableName string, writeAndReadDbs ...*sql.DB) *Query[*SubQuery] {

orm/query_table.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ import (
66
)
77

88
type queryTable struct {
9-
table Table
10-
tableStruct reflect.Value
11-
tableStructType reflect.Type
12-
ormFields map[any]string
13-
joinType JoinType //(left|right) join
14-
joinCondition where
15-
alias string
16-
rawSql string
17-
bindings []any
9+
table Table
10+
tableStruct reflect.Value
11+
tableStructType reflect.Type
12+
ormFields map[any]string
13+
joinType JoinType //(left|right) join
14+
joinCondition where
15+
alias string
16+
overrideTableName string //override table name
17+
rawSql string
18+
bindings []any
1819
}
1920

2021
func (q queryTable) getAlias() string {
@@ -42,6 +43,9 @@ func (q queryTable) getTableNameAndAlias() string {
4243
}
4344

4445
func (q queryTable) getTableName() string {
46+
if q.overrideTableName != "" {
47+
return q.overrideTableName
48+
}
4549
if q.table.TableName() != "" {
4650
if q.table.DatabaseName() != "" {
4751
return q.table.DatabaseName() + "." + q.table.TableName()

orm/reflect.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,18 +219,42 @@ func getStructFieldWithDefaultTime(obj any) (map[int]any, error) {
219219
return nil, ErrParamElemKindMustBeStruct
220220
}
221221
ret := make(map[int]any)
222-
222+
now := time.Now()
223223
for i := 0; i < tableStruct.NumField(); i++ {
224-
defaultVar := tableStructType.Field(i).Tag.Get("default")
225-
if defaultVar == "" {
226-
continue
227-
}
228-
229224
v := tableStruct.Field(i)
230225
if v.CanInterface() {
226+
defaultVar := tableStructType.Field(i).Tag.Get("default")
227+
name := tableStructType.Field(i).Tag.Get("json")
228+
ormName := tableStructType.Field(i).Tag.Get("orm")
229+
ormName = strings.Split(ormName, ",")[0]
230+
if ormName != "" {
231+
name = ormName
232+
}
233+
231234
if _, ok := v.Interface().(time.Time); ok {
232-
if strings.Contains(strings.ToLower(defaultVar), "current_timestamp") {
233-
ret[i] = time.Now()
235+
lowerDefault := strings.ToLower(defaultVar)
236+
if strings.Contains(lowerDefault, "current_timestamp") {
237+
ret[i] = now
238+
} else if lowerDefault == "" {
239+
if name != deletedAtColumn {
240+
ret[i] = now
241+
}
242+
} else if lowerDefault != "null" {
243+
ret[i] = defaultVar
244+
}
245+
} else if v.Kind() == reflect.Struct {
246+
realVal := reflect.New(v.Type())
247+
if _, ok := realVal.Elem().Field(0).Interface().(time.Time); ok {
248+
lowerDefault := strings.ToLower(defaultVar)
249+
if strings.Contains(lowerDefault, "current_timestamp") {
250+
ret[i] = now
251+
} else if lowerDefault == "" {
252+
if name != deletedAtColumn {
253+
ret[i] = now
254+
}
255+
} else if lowerDefault != "null" {
256+
ret[i] = defaultVar
257+
}
234258
}
235259
}
236260
}

0 commit comments

Comments
 (0)