Compare commits
No commits in common. "b96ab19bc231849f19c9c833ff653952ddab26e3" and "a0d0461f06936453d8ebe177ab8c2b2117988163" have entirely different histories.
b96ab19bc2
...
a0d0461f06
@ -21,14 +21,6 @@ func GenerateIDType(table schema.Table) *ast.GenDecl {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func fkFieldName(col schema.Column) string {
|
|
||||||
if col.IsNonCodeTableForeignKey() {
|
|
||||||
return textutils.SnakeToCamel(strings.TrimSuffix(col.Name, "_id")) + "ID"
|
|
||||||
} else {
|
|
||||||
return textutils.SnakeToCamel(col.Name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// GenerateModelAST produces an AST for a struct type corresponding to the model.
|
// GenerateModelAST produces an AST for a struct type corresponding to the model.
|
||||||
// TODO: generate the right field types here based on column types.
|
// TODO: generate the right field types here based on column types.
|
||||||
func GenerateModelAST(table schema.Table) *ast.GenDecl {
|
func GenerateModelAST(table schema.Table) *ast.GenDecl {
|
||||||
@ -45,9 +37,9 @@ func GenerateModelAST(table schema.Table) *ast.GenDecl {
|
|||||||
Tag: &ast.BasicLit{Kind: token.STRING, Value: "`db:\"rowid\" json:\"id\"`"},
|
Tag: &ast.BasicLit{Kind: token.STRING, Value: "`db:\"rowid\" json:\"id\"`"},
|
||||||
})
|
})
|
||||||
default:
|
default:
|
||||||
if col.IsNonCodeTableForeignKey() {
|
if col.IsForeignKey && strings.HasSuffix(col.Name, "_id") {
|
||||||
fields = append(fields, &ast.Field{
|
fields = append(fields, &ast.Field{
|
||||||
Names: []*ast.Ident{ast.NewIdent(fkFieldName(col))},
|
Names: []*ast.Ident{ast.NewIdent(textutils.SnakeToCamel(strings.TrimSuffix(col.Name, "_id")) + "ID")},
|
||||||
Type: ast.NewIdent(schema.TypenameFromTablename(col.ForeignKeyTargetTable) + "ID"),
|
Type: ast.NewIdent(schema.TypenameFromTablename(col.ForeignKeyTargetTable) + "ID"),
|
||||||
Tag: &ast.BasicLit{Kind: token.STRING, Value: fmt.Sprintf("`db:\"%s\" json:\"%s\"`", col.Name, col.Name)},
|
Tag: &ast.BasicLit{Kind: token.STRING, Value: fmt.Sprintf("`db:\"%s\" json:\"%s\"`", col.Name, col.Name)},
|
||||||
})
|
})
|
||||||
@ -110,7 +102,7 @@ func GenerateSaveItemFunc(tbl schema.Table) *ast.FuncDecl {
|
|||||||
updatePairs = append(updatePairs, col.Name+"="+val)
|
updatePairs = append(updatePairs, col.Name+"="+val)
|
||||||
}
|
}
|
||||||
|
|
||||||
insertStmt := fmt.Sprintf("\n\t\t insert into %s (%s)\n\t\t values (%s)\n\t\t", tbl.TableName, strings.Join(insertCols, ", "), strings.Join(insertVals, ", "))
|
insertStmt := fmt.Sprintf("\n\t\t insert into %s (%s)\n\t\t values (%s)\n\t\t", tbl.TableName, strings.Join(insertCols, ", "), strings.Join(insertVals, ", "))
|
||||||
updateStmt := fmt.Sprintf("\n\t\t update %s\n\t\t set %s\n\t\t where rowid = :rowid\n\t\t", tbl.TableName, strings.Join(updatePairs, ",\n\t\t "))
|
updateStmt := fmt.Sprintf("\n\t\t update %s\n\t\t set %s\n\t\t where rowid = :rowid\n\t\t", tbl.TableName, strings.Join(updatePairs, ",\n\t\t "))
|
||||||
|
|
||||||
funcBody := &ast.BlockStmt{
|
funcBody := &ast.BlockStmt{
|
||||||
@ -199,13 +191,11 @@ func GenerateSaveItemFunc(tbl schema.Table) *ast.FuncDecl {
|
|||||||
return funcDecl
|
return funcDecl
|
||||||
}
|
}
|
||||||
|
|
||||||
func getByIDFuncName(tblname string) string {
|
|
||||||
return "Get" + schema.TypenameFromTablename(tblname) + "ByID"
|
|
||||||
}
|
|
||||||
|
|
||||||
// GenerateGetItemByIDFunc produces an AST for the `GetXyzByID()` function.
|
// GenerateGetItemByIDFunc produces an AST for the `GetXyzByID()` function.
|
||||||
// E.g., a table with `table.TypeName = "foods"` will produce a "GetFoodByID()" function.
|
// E.g., a table with `table.TypeName = "foods"` will produce a "GetFoodByID()" function.
|
||||||
func GenerateGetItemByIDFunc(tbl schema.Table) *ast.FuncDecl {
|
func GenerateGetItemByIDFunc(tbl schema.Table) *ast.FuncDecl {
|
||||||
|
funcName := "Get" + tbl.GoTypeName + "ByID"
|
||||||
|
|
||||||
recv := &ast.FieldList{List: []*ast.Field{{Names: []*ast.Ident{ast.NewIdent("db")}, Type: ast.NewIdent("DB")}}}
|
recv := &ast.FieldList{List: []*ast.Field{{Names: []*ast.Ident{ast.NewIdent("db")}, Type: ast.NewIdent("DB")}}}
|
||||||
arg := &ast.FieldList{List: []*ast.Field{{Names: []*ast.Ident{ast.NewIdent("id")}, Type: ast.NewIdent(tbl.TypeIDName)}}}
|
arg := &ast.FieldList{List: []*ast.Field{{Names: []*ast.Ident{ast.NewIdent("id")}, Type: ast.NewIdent(tbl.TypeIDName)}}}
|
||||||
result := &ast.FieldList{List: []*ast.Field{{Names: []*ast.Ident{ast.NewIdent("ret")}, Type: ast.NewIdent(tbl.GoTypeName)}, {Names: []*ast.Ident{ast.NewIdent("err")}, Type: ast.NewIdent("error")}}}
|
result := &ast.FieldList{List: []*ast.Field{{Names: []*ast.Ident{ast.NewIdent("ret")}, Type: ast.NewIdent(tbl.GoTypeName)}, {Names: []*ast.Ident{ast.NewIdent("err")}, Type: ast.NewIdent("error")}}}
|
||||||
@ -238,7 +228,7 @@ func GenerateGetItemByIDFunc(tbl schema.Table) *ast.FuncDecl {
|
|||||||
|
|
||||||
funcDecl := &ast.FuncDecl{
|
funcDecl := &ast.FuncDecl{
|
||||||
Recv: recv,
|
Recv: recv,
|
||||||
Name: ast.NewIdent(getByIDFuncName(tbl.TableName)),
|
Name: ast.NewIdent(funcName),
|
||||||
Type: &ast.FuncType{Params: arg, Results: result},
|
Type: &ast.FuncType{Params: arg, Results: result},
|
||||||
Body: funcBody,
|
Body: funcBody,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,5 @@
|
|||||||
package schema
|
package schema
|
||||||
|
|
||||||
import "strings"
|
|
||||||
|
|
||||||
// Column represents a single column in a table.
|
// Column represents a single column in a table.
|
||||||
type Column struct {
|
type Column struct {
|
||||||
TableName string `db:"table_name"`
|
TableName string `db:"table_name"`
|
||||||
@ -22,10 +20,6 @@ func (c Column) IsNullableForeignKey() bool {
|
|||||||
return !c.IsNotNull && !c.IsPrimaryKey && c.IsForeignKey
|
return !c.IsNotNull && !c.IsPrimaryKey && c.IsForeignKey
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Column) IsNonCodeTableForeignKey() bool {
|
|
||||||
return c.IsForeignKey && strings.HasSuffix(c.Name, "_id")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Table is a single SQLite table.
|
// Table is a single SQLite table.
|
||||||
type Table struct {
|
type Table struct {
|
||||||
TableName string `db:"name"`
|
TableName string `db:"name"`
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user