Skip to content

Commit 7aa2d38

Browse files
committed
partition
1 parent e8d70fa commit 7aa2d38

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

orm/create_table.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ func getTypeAndDefault(val reflect.Value) (string, string) {
414414
types = "datetime"
415415
} else if _, ok := val.Interface().(time.Time); ok {
416416
types = "datetime"
417-
} else {
417+
} else if kind == reflect.Struct {
418418
realVal := reflect.New(typ)
419419
if _, ok := realVal.Elem().Field(0).Interface().(*time.Time); ok {
420420
types = "datetime"
@@ -423,6 +423,8 @@ func getTypeAndDefault(val reflect.Value) (string, string) {
423423
} else {
424424
types = "varchar(255)"
425425
}
426+
} else {
427+
types = "varchar(255)"
426428
}
427429
if types == "datetime" {
428430
defaults = "current_timestamp"

orm/query.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ func (q *Query[T]) OrHaving(column any, vals ...any) *Query[T] {
324324
}
325325
return newQuery
326326
}
327+
func (q *Query[T]) Partition(p string) *Query[T] {
328+
q.tables[0].partition = p
329+
return q
330+
}
327331

328332
func (q *Query[T]) PartitionBy(column any) *Query[T] {
329333
val, err := q.parseColumn(column)

orm/query_insert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func (q *Query[T]) insert(data any) QueryResult {
179179
rawSql += " ignore"
180180
}
181181

182-
rawSql += " into " + q.tables[0].getTableName() + " " + insertSql
182+
rawSql += " into " + q.tables[0].getTableNamePartition() + " " + insertSql
183183

184184
if updateStr != "" {
185185
rawSql += " on duplicate key update " + updateStr

orm/query_table.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type queryTable struct {
1414
joinCondition where
1515
alias string
1616
overrideTableName string //override table name
17+
partition string
1718
rawSql string
1819
bindings []any
1920
}
@@ -31,7 +32,7 @@ func (q queryTable) getAliasOrTableName() string {
3132

3233
func (q queryTable) getTableNameAndAlias() string {
3334
var strs []string
34-
temp := q.getTableName()
35+
temp := q.getTableNamePartition()
3536
if temp != "" {
3637
strs = append(strs, temp)
3738
}
@@ -56,6 +57,27 @@ func (q queryTable) getTableName() string {
5657
return ""
5758
}
5859

60+
func (q queryTable) getTableNamePartition() string {
61+
if q.overrideTableName != "" {
62+
return q.overrideTableName
63+
}
64+
if q.table.TableName() != "" {
65+
if q.table.DatabaseName() != "" {
66+
return q.table.DatabaseName() + "." + q.table.TableName() + q.getPartition()
67+
} else {
68+
return q.table.TableName() + q.getPartition()
69+
}
70+
}
71+
return ""
72+
}
73+
74+
func (q queryTable) getPartition() string {
75+
if q.partition == "" {
76+
return ""
77+
}
78+
return " partition(" + q.partition + ")"
79+
}
80+
5981
func (q queryTable) getTags(index int, tagName string) []string {
6082
tags := strings.Split(q.tableStructType.Field(index).Tag.Get(tagName), ",")
6183
return tags

0 commit comments

Comments
 (0)