|
|
|
@@ -7,8 +7,6 @@ import (
|
|
|
|
"go/token"
|
|
|
|
"go/token"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/jinzhu/inflection"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/schema"
|
|
|
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/schema"
|
|
|
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/textutils"
|
|
|
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/textutils"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
@@ -40,7 +38,7 @@ func GenerateModelAST(table schema.Table) *ast.GenDecl {
|
|
|
|
if col.IsForeignKey && strings.HasSuffix(col.Name, "_id") {
|
|
|
|
if col.IsForeignKey && strings.HasSuffix(col.Name, "_id") {
|
|
|
|
fields = append(fields, &ast.Field{
|
|
|
|
fields = append(fields, &ast.Field{
|
|
|
|
Names: []*ast.Ident{ast.NewIdent(textutils.SnakeToCamel(strings.TrimSuffix(col.Name, "_id")) + "ID")},
|
|
|
|
Names: []*ast.Ident{ast.NewIdent(textutils.SnakeToCamel(strings.TrimSuffix(col.Name, "_id")) + "ID")},
|
|
|
|
Type: ast.NewIdent(textutils.SnakeToCamel(inflection.Singular(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)},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
@@ -175,18 +173,23 @@ func GenerateGetItemByIDFunc(tbl schema.Table) *ast.FuncDecl {
|
|
|
|
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.TypeName)}, {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.TypeName)}, {Names: []*ast.Ident{ast.NewIdent("err")}, Type: ast.NewIdent("error")}}}
|
|
|
|
|
|
|
|
|
|
|
|
selectCols := make([]string, 0, len(tbl.Columns))
|
|
|
|
// Use the xyzSQLFields constant in the select query
|
|
|
|
for _, col := range tbl.Columns {
|
|
|
|
selectExpr := &ast.BinaryExpr{
|
|
|
|
selectCols = append(selectCols, col.Name)
|
|
|
|
X: &ast.BinaryExpr{
|
|
|
|
|
|
|
|
X: &ast.BasicLit{Kind: token.STRING, Value: "`\n\t select `"},
|
|
|
|
|
|
|
|
Op: token.ADD,
|
|
|
|
|
|
|
|
Y: SQLFieldsConstIdent(tbl),
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
Op: token.ADD,
|
|
|
|
|
|
|
|
Y: &ast.BasicLit{Kind: token.STRING, Value: "`\n\t from " + tbl.TableName + "\n\t where rowid = ?\n\t`"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
selectStmt := fmt.Sprintf("\n\t select %s\n\t from %s\n\t where rowid = ?\n\t", strings.Join(selectCols, ", "), tbl.TableName)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
funcBody := &ast.BlockStmt{
|
|
|
|
funcBody := &ast.BlockStmt{
|
|
|
|
List: []ast.Stmt{
|
|
|
|
List: []ast.Stmt{
|
|
|
|
&ast.AssignStmt{
|
|
|
|
&ast.AssignStmt{
|
|
|
|
Lhs: []ast.Expr{ast.NewIdent("err")},
|
|
|
|
Lhs: []ast.Expr{ast.NewIdent("err")},
|
|
|
|
Tok: token.ASSIGN,
|
|
|
|
Tok: token.ASSIGN,
|
|
|
|
Rhs: []ast.Expr{&ast.CallExpr{Fun: &ast.SelectorExpr{X: ast.NewIdent("db.DB"), Sel: ast.NewIdent("Get")}, Args: []ast.Expr{&ast.UnaryExpr{Op: token.AND, X: ast.NewIdent("ret")}, &ast.BasicLit{Kind: token.STRING, Value: "`" + selectStmt + "`"}, ast.NewIdent("id")}}},
|
|
|
|
Rhs: []ast.Expr{&ast.CallExpr{Fun: &ast.SelectorExpr{X: ast.NewIdent("db.DB"), Sel: ast.NewIdent("Get")}, Args: []ast.Expr{&ast.UnaryExpr{Op: token.AND, X: ast.NewIdent("ret")}, selectExpr, ast.NewIdent("id")}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&ast.IfStmt{
|
|
|
|
&ast.IfStmt{
|
|
|
|
Cond: &ast.CallExpr{Fun: &ast.SelectorExpr{X: ast.NewIdent("errors"), Sel: ast.NewIdent("Is")}, Args: []ast.Expr{ast.NewIdent("err"), &ast.SelectorExpr{X: ast.NewIdent("sql"), Sel: ast.NewIdent("ErrNoRows")}}},
|
|
|
|
Cond: &ast.CallExpr{Fun: &ast.SelectorExpr{X: ast.NewIdent("errors"), Sel: ast.NewIdent("Is")}, Args: []ast.Expr{ast.NewIdent("err"), &ast.SelectorExpr{X: ast.NewIdent("sql"), Sel: ast.NewIdent("ErrNoRows")}}},
|
|
|
|
@@ -241,3 +244,31 @@ func GenerateDeleteItemFunc(tbl schema.Table) *ast.FuncDecl {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return funcDecl
|
|
|
|
return funcDecl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GenerateSQLFieldsConst produces an AST for the `const xyzSQLFields = ...` string.
|
|
|
|
|
|
|
|
func GenerateSQLFieldsConst(tbl schema.Table) *ast.GenDecl {
|
|
|
|
|
|
|
|
columns := make([]string, 0, len(tbl.Columns))
|
|
|
|
|
|
|
|
for _, col := range tbl.Columns {
|
|
|
|
|
|
|
|
columns = append(columns, col.Name)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join with comma and space
|
|
|
|
|
|
|
|
value := "`" + strings.Join(columns, ", ") + "`"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return &ast.GenDecl{
|
|
|
|
|
|
|
|
Tok: token.CONST,
|
|
|
|
|
|
|
|
Specs: []ast.Spec{
|
|
|
|
|
|
|
|
&ast.ValueSpec{
|
|
|
|
|
|
|
|
Names: []*ast.Ident{SQLFieldsConstIdent(tbl)},
|
|
|
|
|
|
|
|
Values: []ast.Expr{&ast.BasicLit{Kind: token.STRING, Value: value}},
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------
|
|
|
|
|
|
|
|
// Helpers
|
|
|
|
|
|
|
|
// ---------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func SQLFieldsConstIdent(tbl schema.Table) *ast.Ident {
|
|
|
|
|
|
|
|
return ast.NewIdent(strings.ToLower(tbl.TypeName) + "SQLFields")
|
|
|
|
|
|
|
|
}
|
|
|
|
|