jorm

package
v0.0.0-...-c6739eb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const ERMapReady = EntityReady & RepositoryReady

Variables

View Source
var (
	JORM_ENTITY     = "jorm-entity"
	JORM_TABLE      = "jorm-table"
	JORM_COLUMN     = "jorm-column"
	JORM_REPOSITORY = "jorm-repository"
)

Functions

func CaseTitleToCamel

func CaseTitleToCamel(text string) string

Id -> id BookName -> bookName

func CaseTitleToSnake

func CaseTitleToSnake(text string) string

id -> ID BookName -> BOOK_NAME

func ExtractPackageNameFromPath

func ExtractPackageNameFromPath(path string) string

func ExtractTagValue

func ExtractTagValue(field *ast.Field, tagkey string) (column string, ok bool)

Types

type Builder

type Builder interface {
	Build() string
}

type Criteria

type Criteria interface {
	Builder
}

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 Field

type Field struct {
	Name string
	Type string
}

func NewField

func NewField(name string, typ string) Field

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

type FuncName struct {
	Name   string
	LBrace string
	Fields []Field
	RBrace string
}

func NewFuncName

func NewFuncName(name string) *FuncName

func (*FuncName) Build

func (fn *FuncName) Build() string

TODO add parameters consider slice

type FuncReturn

type FuncReturn struct {
	LBrace string
	Fields []Field
	RBrace string
	// contains filtered or unexported fields
}

func NewFuncReturn

func NewFuncReturn(field *ast.Field, entity *ast.TypeSpec, pkg string) *FuncReturn

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

type ImportStatement struct {
	Import string
	LBrace string
	Pkgs   []string
	RBrace string
}

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 (m *Mapping) BuildFuncStmt(method *ast.Field, criteria []string)

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 ORM

type ORM struct {
	RootPath     string
	MappingStore map[string]*Mapping
}

func NewORM

func NewORM() *ORM

func (*ORM) Parse

func (o *ORM) Parse(pkgs []*packages.Package)

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

type ReceiverStatement struct {
	LBrace string
	Alias  string
	Type   string
	RBrace string
}

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

type WhereClause interface {
	Build() string
	AddVar(criteria Criteria)
}

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL