通过接口上添加注释的方式,Gen 允许从 Raw SQL 生成完全安全的通用 Go 代码, 这些接口可以在代码生成过程中应用于多个model。
不仅支持完整的 SQL,也支持SQL 代码片段生成使用,让我们举一个例子:
Raw SQL
type Querier interface { // SELECT * FROM @@table WHERE id=@id GetByID(id int) (gen.T, error) // GetByID query data by id and return it as *struct*
// GetByRoles query data by roles and return it as *slice of pointer* // (The below blank line is required to comment for the generated method) // // SELECT * FROM @@table WHERE role IN @rolesName GetByRoles(rolesName ...string) ([]*gen.T, error)
// InsertValue insert value // // INSERT INTO @@table (name, age) VALUES (@name, @age) InsertValue(name string, age int) error }
g := gen.NewGenerator(gen.Config{ // ... some config })
// Apply the interface to existing `User` and generated `Employee` g.ApplyInterface(func(Querier) {}, model.User{}, g.GenerateModel("employee"))
type Querier interface { // FindByNameAndAge query data by name and age and return it as map // // where("name=@name AND age=@age") FindByNameAndAge(name string, age int) (gen.M, error) }
g := gen.NewGenerator(gen.Config{ // ... some config })
// Apply the interface to existing `User` and generated `Employee` g.ApplyInterface(func(Querier) {}, model.User{}, g.GenerateModel("employee"))