Documentation ¶
Index ¶
- Constants
- Variables
- func CaseTitleToCamel(text string) string
- func CaseTitleToSnake(text string) string
- func ExtractPackageNameFromPath(path string) string
- func ExtractTagValue(field *ast.Field, tagkey string) (column string, ok bool)
- type Builder
- type Criteria
- type DynamicCriteria
- type Field
- type File
- type FuncName
- type FuncReturn
- type FunctionBody
- type FunctionStatement
- type ImportStatement
- type LiteralCriteria
- type Mapping
- type MappingStatus
- type ORM
- type PackageStatement
- type RalationalOperator
- type ReceiverStatement
- type StructStatement
- type WhereClause
Constants ¶
const ERMapReady = EntityReady & RepositoryReady
Variables ¶
var ( JORM_ENTITY = "jorm-entity" JORM_TABLE = "jorm-table" JORM_COLUMN = "jorm-column" JORM_REPOSITORY = "jorm-repository" )
Functions ¶
Types ¶
type DynamicCriteria ¶
type DynamicCriteria struct {
// contains filtered or unexported fields
}
only the literal parameter can be quoted
func NewDynamicCriteria ¶
func NewDynamicCriteria(op RalationalOperator, column string, paramName string) DynamicCriteria
func (DynamicCriteria) Build ¶
func (l DynamicCriteria) Build() string
type File ¶
type File struct { PackageStmt PackageStatement ImportStmts ImportStatement StructStmts []StructStatement FunctionStmts []FunctionStatement }
A File represent implement of the repository interface package xxx import (
"xxxx/entity" "error"
)
type bookRepository struct{}
func (b *bookRepository) FindByNameInAndAuthorIn(names []string, authors []string) (books []entity.Book, err error) { var queryParams = make([]any, 0, len(names)) var selectClause = "SELECT id,name,author,version FROM book" var where = "where" var whereClause = jormgen.AddArray("name", names, queryParams) + " and " + jormgen.AddArray("author", authors, queryParams) var exp = selectClause + " " + where + " " + whereClause rows, err := db.Query(exp, queryParams...) for rows.Next() { var book entity.Book rows.Scan(&book.Id, &book.Name, &book.Author, &book.Version) books = append(books, book) } return }
type FuncName ¶
func NewFuncName ¶
type FuncReturn ¶
type FuncReturn struct { LBrace string Fields []Field RBrace string // contains filtered or unexported fields }
func NewFuncReturn ¶
func (*FuncReturn) Build ¶
func (fr *FuncReturn) Build() string
func (*FuncReturn) SingleResult ¶
func (fr *FuncReturn) SingleResult() bool
type FunctionBody ¶
type FunctionBody struct { VarQueryClause string VarSelectClause string VarWhereClause string VarExpression string StmtQuery string ForRowsNext string LBrace string ForVarEntity string ForStmtScan string ForAppend string RBrace string StmtReturn string }
var queryParams = make([]any, 0, len(names)) var selectClause = "SELECT id,name,author,version FROM book" var where = "where" var whereClause = jormgen.AddArray("name", names, queryParams) + " and " + jormgen.AddArray("author", authors, queryParams) var exp = selectClause + " " + where + " " + whereClause rows, err := db.Query(exp, queryParams...)
for rows.Next() { var book entity.Book rows.Scan(&book.Id, &book.Name, &book.Author, &book.Version) books = append(books, book) }
return
func NewFunctionBody ¶
func NewFunctionBody() *FunctionBody
var queryParams = make([]any, 0, len(names))
func (*FunctionBody) Build ¶
func (fb *FunctionBody) Build() string
type FunctionStatement ¶
type FunctionStatement struct { Func string Recv *ReceiverStatement FuncP *FuncName Return *FuncReturn LBrace string Body *FunctionBody RBrace string }
func (b *bookRepository) FindByNameInAndAuthorIn(names []string, authors []string) (books []entity.Book, err error) { var queryParams = make([]any, 0, len(names)) var selectClause = "SELECT id,name,author,version FROM book" var where = "where" var whereClause = jormgen.AddArray("name", names, queryParams) + " and " + jormgen.AddArray("author", authors, queryParams) var exp = selectClause + " " + where + " " + whereClause rows, err := db.Query(exp, queryParams...) for rows.Next() { var book entity.Book rows.Scan(&book.Id, &book.Name, &book.Author, &book.Version) books = append(books, book) } return }
func NewFunctionStatement ¶
func NewFunctionStatement(funcName string, recv string, returnType string) *FunctionStatement
func (*FunctionStatement) Build ¶
func (fs *FunctionStatement) Build() string
type ImportStatement ¶
func NewImportStatement ¶
func NewImportStatement() *ImportStatement
func (*ImportStatement) AddImport ¶
func (imprts *ImportStatement) AddImport(pkg string)
func (*ImportStatement) Build ¶
func (imprts *ImportStatement) Build() string
type LiteralCriteria ¶
type LiteralCriteria struct {
// contains filtered or unexported fields
}
the whole can be quoted
func NewLiteralCriteria ¶
func NewLiteralCriteria(op RalationalOperator, column string) LiteralCriteria
func (LiteralCriteria) Build ¶
func (l LiteralCriteria) Build() string
type Mapping ¶
type Mapping struct { Entity *ast.TypeSpec // entity struct to be parsed Repository *ast.TypeSpec // repository interface to be parsed PrimaryKeys map[string]any // the type of pk, can be int, float64, strign and so on... todo how about struct(??) SelectClause string // generated select clause SqlText map[string]string // generated where clauses based on repository TableName string // table name EntityPath string // entity path, from import xxxx/xxx/xxx/xxx RepositoryPath string // repository path from import xxx/xx/xx/repostoryxxx Status MappingStatus // enumerate mapping status FieldMap map[string]*ast.Field // key is field name, value is field FuncMap map[string]*FunctionStatement // key is field name, value is field FuncMapText map[string]string // key is field name, value is field }
func NewMapping ¶
func NewMapping() *Mapping
func (*Mapping) BuildFuncStmt ¶
func (*Mapping) BuildSqlText ¶
func (m *Mapping) BuildSqlText()
func (*Mapping) OnEntityReady ¶
func (m *Mapping) OnEntityReady()
func (*Mapping) ParseFieldNameAndOperator ¶
func (m *Mapping) ParseFieldNameAndOperator(section string) (field *ast.Field, op RalationalOperator)
type Book struct { Name string NameLessThan string }
current impl is match BookLessThan such a field 1st, then NameLessThan = "some thing"
type MappingStatus ¶
type MappingStatus int
const ( EntityReady MappingStatus = 1 << iota RepositoryReady )
type PackageStatement ¶
type PackageStatement struct{}
func NewPackageStatement ¶
func NewPackageStatement() *PackageStatement
func (*PackageStatement) Build ¶
func (ps *PackageStatement) Build() string
type RalationalOperator ¶
type RalationalOperator interface {
BuildElementExpression(columnName string, elemType ast.Expr, paramName string, paramType ast.Expr) string
}
var ( OP_EQ RalationalOperator = ralationalOperator("Eq") // A == B OP_LT RalationalOperator = ralationalOperator("Lt") // A < B OP_GT RalationalOperator = ralationalOperator("Gt") // A > B OP_IN RalationalOperator = ralationalOperator("In") // A in (strings.join([B,C,D],",")) OP_NOTEQ RalationalOperator = ralationalOperator("NotEq") // A <> B OP_LE RalationalOperator = ralationalOperator("Le") // A <= B OP_GE RalationalOperator = ralationalOperator("Ge") // A >= B OP_NOTIN RalationalOperator = ralationalOperator("NotIn") // A not in B )
func NewRalationalOperator ¶
func NewRalationalOperator(str string) RalationalOperator
type ReceiverStatement ¶
func NewReceiverStatement ¶
func NewReceiverStatement(typ string) *ReceiverStatement
func (*ReceiverStatement) Build ¶
func (rs *ReceiverStatement) Build() string
type StructStatement ¶
type StructStatement struct { Type string Name string Struct string LBrace string Fields []Field RBrace string }
func NewStructStatement ¶
func NewStructStatement(name string) *StructStatement
func (*StructStatement) Build ¶
func (ss *StructStatement) Build() string
type WhereClause ¶
TODO below code is to optimize the performance when: there are only simple paramters there is no need to concate the sql dynamicly. it is enough that use one simple line to do all the things eg: var whereClause = "name = ? and id = ?" rather than var whereClause = "name = ?" + " and " + "id = ?"
func NewWhereClause ¶
func NewWhereClause() WhereClause