Skip to content

Commit fae6393

Browse files
author
陈奎
committed
feat(tml): add raw select sql
1 parent 7825690 commit fae6393

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ func cvtDDL2Table(cs *ast.CreateTableStmt) (*schemas.Table, error) {
144144
col.IsAutoIncrement = true
145145
case ast.ColumnOptionPrimaryKey:
146146
col.IsPrimaryKey = true
147+
col.Nullable = false
147148
case ast.ColumnOptionNull:
148149
col.Nullable = true
149150
}

template.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ var (
2626

2727
func init() {
2828
GoXormTmp = `
29-
{{- range .Tables}}
29+
{{- range .Tables -}}
30+
// SELECT: {{SelectRawSql .}}
3031
type {{TableMapper .Name}} struct {
3132
{{$table := .}}
3233
{{range .ColumnsSeq}}{{$col := $table.GetColumn .}} {{ColMapper $col.Name}} {{Type $col}} {{Tag $table $col}}
@@ -186,14 +187,15 @@ func NewGolangTmp(args *convertArgs) *GolangTmp {
186187
}
187188
return &GolangTmp{
188189
funcs: template.FuncMap{
189-
"ColMapper": colMapper.Table2Obj,
190-
"TableMapper": tableMapper.Table2Obj,
191-
"Type": typestring,
192-
"Tag": getTag(colMapper, args.genJson, args.genXorm, otherTags),
193-
"UnTitle": unTitle,
194-
"gt": gt,
195-
"getCol": getCol,
196-
"UpperTitle": upTitle,
190+
"ColMapper": colMapper.Table2Obj,
191+
"TableMapper": tableMapper.Table2Obj,
192+
"Type": typestring,
193+
"Tag": getTag(colMapper, args.genJson, args.genXorm, otherTags),
194+
"UnTitle": unTitle,
195+
"gt": gt,
196+
"getCol": getCol,
197+
"UpperTitle": upTitle,
198+
"SelectRawSql": GetSelectRawSql,
197199
},
198200
formater: formatGo,
199201
args: args,
@@ -501,3 +503,12 @@ func include(source []string, target string) bool {
501503
}
502504
return false
503505
}
506+
507+
func GetSelectRawSql(table *schemas.Table) string {
508+
colNames := make([]string, 0, len(table.Columns()))
509+
for _, v := range table.ColumnsSeq() {
510+
col := table.GetColumn(v)
511+
colNames = append(colNames, "`"+col.Name+"`")
512+
}
513+
return fmt.Sprintf("SELECT %s FROM `%s`", strings.Join(colNames, ", "), table.Name)
514+
}

0 commit comments

Comments
 (0)