Documentation ¶
Overview ¶
Package twowaysql provides an implementation of 2WaySQL.
Index ¶
- func Eval(inputQuery string, inputParams interface{}) (string, []interface{}, error)
- func GenerateMarkdown(w io.Writer, lang string) error
- func ParseMarkdownFS(fsys fs.FS, pattern ...string) (map[string]*Document, error)
- func ParseMarkdownGlob(pattern ...string) (map[string]*Document, error)
- type CRUDMatrix
- type Document
- type Fixture
- type MatchRule
- type Param
- type ParamType
- type Table
- type TestCase
- type Twowaysql
- func (t *Twowaysql) Begin(ctx context.Context) (*TwowaysqlTx, error)
- func (t *Twowaysql) Close() error
- func (t *Twowaysql) DB() *sqlx.DB
- func (t *Twowaysql) Exec(ctx context.Context, query string, params interface{}) (sql.Result, error)
- func (t *Twowaysql) Select(ctx context.Context, dest interface{}, query string, params interface{}) error
- func (t *Twowaysql) Transaction(ctx context.Context, fn func(tx *TwowaysqlTx) error) error
- type TwowaysqlTx
- func (t *TwowaysqlTx) Commit() error
- func (t *TwowaysqlTx) Exec(ctx context.Context, query string, params interface{}) (sql.Result, error)
- func (t *TwowaysqlTx) Rollback() error
- func (t *TwowaysqlTx) Select(ctx context.Context, dest interface{}, query string, params interface{}) error
- func (t *TwowaysqlTx) Tx() *sqlx.Tx
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Eval ¶
Eval returns converted query and bind value. inputParams takes a tagged struct. Tags must be in the form `map:"tag_name"`. The return value is expected to be used to issue queries to the database
Example ¶
type Info struct { Name string `twowaysql:"name"` EmpNo int `twowaysql:"EmpNo"` MaxEmpNo int `twowaysql:"maxEmpNo"` DeptNo int `twowaysql:"deptNo"` Email string `twowaysql:"email"` GenderList []string `twowaysql:"gender_list"` IntList []int `twowaysql:"int_list"` } var params = Info{ Name: "Jeff", MaxEmpNo: 3, DeptNo: 12, GenderList: []string{"M", "F"}, IntList: []int{1, 2, 3}, } var before = `SELECT * FROM person WHERE employee_no = /*maxEmpNo*/1000 AND /* IF int_list !== null */ person.gender in /*int_list*/(3,5,7) /* END */` after, afterParams, _ := twowaysql.Eval(before, ¶ms) fmt.Println(after) fmt.Println(afterParams)
Output: SELECT * FROM person WHERE employee_no = ?/*maxEmpNo*/ AND person.gender in (?, ?, ?)/*int_list*/ [3 1 2 3]
func GenerateMarkdown ¶ added in v1.7.0
ParseMarkdown parses markdown content
func ParseMarkdownFS ¶ added in v1.7.0
ParseMarkdown parses markdown content
Types ¶
type CRUDMatrix ¶ added in v1.7.0
type CRUDMatrix struct { Table string `json:"table"` C bool `json:"c"` R bool `json:"r"` U bool `json:"u"` D bool `json:"d"` Description string `json:"description,omitempty"` }
CRUDMatrix represents CRUD Matrix
type Document ¶ added in v1.7.0
type Document struct { SQL string `json:"sql"` Title string `json:"title"` Params []Param `json:"params"` CRUDMatrix []CRUDMatrix `json:"crud_matrix,omitempty"` TestCases []TestCase `json:"testcases,omitempty"` CommonTestFixture Fixture `json:"common_test_fixtures,omitempty"` }
Document contains SQL and metadata
func ParseMarkdown ¶ added in v1.7.0
ParseMarkdown parses markdown content
func ParseMarkdownFile ¶ added in v1.7.0
ParseMarkdownFile parses markdown file
func ParseMarkdownString ¶ added in v1.7.0
ParseMarkdown parses markdown content
type MatchRule ¶ added in v1.7.0
type MatchRule int
func (MatchRule) MarshalJSON ¶ added in v1.7.0
func (*MatchRule) UnmarshalJSON ¶ added in v1.7.0
type Param ¶ added in v1.7.0
type Param struct { Name string `json:"name"` Type ParamType `json:"type"` Value string `json:"value"` Description string `json:"description,omitempty"` }
Param is parameter type of 2-Way-SQL
type ParamType ¶ added in v1.7.0
type ParamType int
ParamType is for describing parameters
func (ParamType) MarshalJSON ¶ added in v1.7.0
func (*ParamType) UnmarshalJSON ¶ added in v1.7.0
type Twowaysql ¶
type Twowaysql struct {
// contains filtered or unexported fields
}
Twowaysql is a struct for issuing 2WaySQL query
func (*Twowaysql) Begin ¶
func (t *Twowaysql) Begin(ctx context.Context) (*TwowaysqlTx, error)
Begin is a thin wrapper around db.BeginTxx in the sqlx package.
func (*Twowaysql) Exec ¶
Exec is a thin wrapper around db.Exec in the sqlx package. params takes a tagged struct. The tags format must be `twowaysql:"tag_name"`.
Example ¶
type Info struct { Name string `twowaysql:"name"` EmpNo int `twowaysql:"EmpNo"` MaxEmpNo int `twowaysql:"maxEmpNo"` DeptNo int `twowaysql:"deptNo"` Email string `twowaysql:"email"` GenderList []string `twowaysql:"gender_list"` IntList []int `twowaysql:"int_list"` } var params = Info{ MaxEmpNo: 3, DeptNo: 12, } result, err := tw.Exec(ctx, `UPDATE persons SET dept_no = /*deptNo*/1 WHERE employee_no = /*EmpNo*/1`, ¶ms) if err != nil { log.Fatal(err) } rows, err := result.RowsAffected() if err != nil { log.Fatal(err) } if rows != 1 { log.Fatalf("expected to affect 1 row. affected %d", rows) }
Output:
func (*Twowaysql) Select ¶
func (t *Twowaysql) Select(ctx context.Context, dest interface{}, query string, params interface{}) error
Select is a thin wrapper around db.Select in the sqlx package. params takes a tagged struct. The tags format must be `twowaysql:"tag_name"`. dest takes a pointer to a slice of a struct. The struct tag format must be `db:"tag_name"`.
Example ¶
type Person struct { FirstName string `db:"first_name"` LastName string `db:"last_name"` Email string `db:"email"` } type Info struct { Name string `twowaysql:"name"` EmpNo int `twowaysql:"EmpNo"` MaxEmpNo int `twowaysql:"maxEmpNo"` DeptNo int `twowaysql:"deptNo"` Email string `twowaysql:"email"` GenderList []string `twowaysql:"gender_list"` IntList []int `twowaysql:"int_list"` } var params = Info{ MaxEmpNo: 3, DeptNo: 12, } var people []Person err := tw.Select(ctx, &people, `SELECT first_name, last_name, email FROM persons WHERE employee_no < /*maxEmpNo*/1000 /* IF deptNo */ AND dept_no < /*deptNo*/1 /* END */`, ¶ms) if err != nil { log.Fatal(err) }
Output:
func (*Twowaysql) Transaction ¶
Transaction starts a transaction as a block. arguments function is return error will rollback, otherwise to commit.
type TwowaysqlTx ¶
type TwowaysqlTx struct {
// contains filtered or unexported fields
}
TwowaysqlTx is a structure for issuing 2WaySQL queries within a transaction.
func (*TwowaysqlTx) Commit ¶
func (t *TwowaysqlTx) Commit() error
Commit is a thin wrapper around tx.Commit in the sqlx package.
func (*TwowaysqlTx) Exec ¶
func (t *TwowaysqlTx) Exec(ctx context.Context, query string, params interface{}) (sql.Result, error)
Exec is a thin wrapper around db.Exec in the sqlx package. params takes a tagged struct. The tags format must be `twowaysql:"tag_name"`. It is an equivalent implementation of Twowaysql.Exec
func (*TwowaysqlTx) Rollback ¶
func (t *TwowaysqlTx) Rollback() error
Rollback is a thin wrapper around tx.Rollback in the sqlx package.
func (*TwowaysqlTx) Select ¶
func (t *TwowaysqlTx) Select(ctx context.Context, dest interface{}, query string, params interface{}) error
Select is a thin wrapper around db.Select in the sqlx package. params takes a tagged struct. The tags format must be `twowaysql:"tag_name"`. dest takes a pointer to a slice of a struct. The struct tag format must be `db:"tag_name"`. It is an equivalent implementation of Twowaysql.Select