twowaysql

package module
Version: v0.0.0-...-f1a5671 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

README

This repository moved to https://github.com/future-architect/go-twowaysql

twowaysql

2-Way-SQL Go implementation

Installation

go get gitlab.com/osaki-lab/twowaysql

Usage

TODO Below is an example which shows some common use cases for twowaysql.

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/jmoiron/sqlx"
	_ "github.com/lib/pq"
	"gitlab.com/osaki-lab/twowaysql"
)

type Person struct {
	EmpNo     int    `db:"employee_no"`
	DeptNo    int    `db:"dept_no"`
	FirstName string `db:"first_name"`
	LastName  string `db:"last_name"`
	Email     string `db:"email"`
}

type Params struct {
	Name     string `twowaysql:"name"`
	EmpNo    int    `twowaysql:"EmpNo"`
	MaxEmpNo int    `twowaysql:"maxEmpNo"`
	DeptNo   int    `twowaysql:"deptNo"`
}

func main() {
	ctx := context.Background()

	db, err := sqlx.Open("postgres", "user=postgres password=postgres dbname=postgres sslmode=disable")

	defer db.Close()
	if err != nil {
		log.Fatal(err)
	}

	tw := twowaysql.New(db)

	var people []Person
	var params = Params{
		MaxEmpNo: 2000,
		DeptNo:   15,
	}

	err = tw.Select(ctx, &people, `SELECT * FROM persons WHERE employee_no < /*maxEmpNo*/1000 /* IF deptNo */ AND dept_no < /*deptNo*/1 /* END */`, &params)
	if err != nil {
		log.Fatalf("select failed: %v", err)
	}

	fmt.Printf("%#v\n%#v\n%#v", people[0], people[1], people[2])
	//Person{EmpNo:1, DeptNo:10, FirstName:"Evan", LastName:"MacMans", Email:"evanmacmans@example.com"}
	//Person{EmpNo:3, DeptNo:12, FirstName:"Jimmie", LastName:"Bruce", Email:"jimmiebruce@example.com"}
	//Person{EmpNo:2, DeptNo:11, FirstName:"Malvina", LastName:"FitzSimons", Email:"malvinafitzsimons@example.com"}

}

License

Apache License Version 2.0

Documentation

Overview

Package twowaysql provides an implementation of 2WaySQL.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Eval

func Eval(inputQuery string, inputParams interface{}) (string, []interface{}, error)

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
package main

import (
	"fmt"

	"gitlab.com/osaki-lab/twowaysql"
)

func main() {

	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, &params)

	fmt.Println(after)
	fmt.Println(afterParams)

}
Output:

SELECT * FROM person WHERE employee_no = ?/*maxEmpNo*/ AND person.gender in (?, ?, ?)/*int_list*/
[3 1 2 3]

Types

type Twowaysql

type Twowaysql struct {
	// contains filtered or unexported fields
}

Twowaysql is a struct for issuing 2WaySQL query

func New

func New(db *sqlx.DB) *Twowaysql

New returns instance of Twowaysql

func (*Twowaysql) Exec

func (t *Twowaysql) 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"`.

Example
package main

import (
	"context"
	"log"

	"gitlab.com/osaki-lab/twowaysql"
)

var (
	tw  *twowaysql.Twowaysql
	ctx context.Context
)

func main() {

	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`, &params)
	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
package main

import (
	"context"
	"log"

	"gitlab.com/osaki-lab/twowaysql"
)

var (
	tw  *twowaysql.Twowaysql
	ctx context.Context
)

func main() {
	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 */`, &params)
	if err != nil {
		log.Fatal(err)
	}
}
Output:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL