github.com/team-ide/go-dialect@v1.9.20/worker/exec_test.go (about)

     1  package worker
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"github.com/team-ide/go-dialect/dialect"
     7  	"github.com/team-ide/go-driver/db_mysql"
     8  	"github.com/team-ide/go-driver/db_sqlite3"
     9  	"reflect"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  func TestDoQuery(t *testing.T) {
    15  	db, err := db_mysql.Open(db_mysql.GetDSN("root", "123456", "127.0.0.1", 3306, ""))
    16  	if err != nil {
    17  		panic(err)
    18  	}
    19  	var list []*QueryStruct
    20  	err = DoQueryStructs(db, `select user as a from mysql.user`, []interface{}{}, &list)
    21  	if err != nil {
    22  		panic(err)
    23  	}
    24  	for _, one := range list {
    25  		bs, _ := json.Marshal(one)
    26  		fmt.Println(string(bs))
    27  	}
    28  }
    29  func TestDoQueryOne(t *testing.T) {
    30  	db, err := db_mysql.Open(db_mysql.GetDSN("root", "123456", "127.0.0.1", 3306, ""))
    31  	if err != nil {
    32  		panic(err)
    33  	}
    34  	one := &QueryStruct{}
    35  	_, err = DoQueryStruct(db, `select user as a,1 b,0 c,now() deleteTime from mysql.user where user='mysql.sys'`, []interface{}{}, one)
    36  	if err != nil {
    37  		panic(err)
    38  	}
    39  	bs, _ := json.Marshal(one)
    40  	fmt.Println(string(bs))
    41  
    42  	data, err := DoQueryOne(db, `select user as a from mysql.user where user='mysql.sys'`, []interface{}{})
    43  	if err != nil {
    44  		panic(err)
    45  	}
    46  	bs, _ = json.Marshal(data)
    47  	fmt.Println(string(bs))
    48  }
    49  
    50  func TestDoQueryPage(t *testing.T) {
    51  	page := NewPage()
    52  	page.PageNo = 1
    53  	page.PageSize = 3
    54  	db, err := db_mysql.Open(db_mysql.GetDSN("root", "123456", "127.0.0.1", 3306, ""))
    55  	if err != nil {
    56  		panic(err)
    57  	}
    58  	dia, err := dialect.NewDialect("mysql")
    59  	list, err := DoQueryPage(db, dia, `select user as a from mysql.user `, []interface{}{}, page)
    60  	if err != nil {
    61  		panic(err)
    62  	}
    63  	bs, _ := json.Marshal(page)
    64  	fmt.Println(string(bs))
    65  	for _, one := range list {
    66  		bs, _ = json.Marshal(one)
    67  		fmt.Println(string(bs))
    68  	}
    69  	var dataList []*QueryStruct
    70  
    71  	err = DoQueryPageStructs(db, dia, `select user as a from mysql.user `, []interface{}{}, page, &dataList)
    72  	if err != nil {
    73  		panic(err)
    74  	}
    75  	bs, _ = json.Marshal(page)
    76  	fmt.Println(string(bs))
    77  	for _, one := range dataList {
    78  		bs, _ = json.Marshal(one)
    79  		fmt.Println(string(bs))
    80  	}
    81  	err = DoQueryPageStructs(db, dia, `select user as a from mysql.user `, []interface{}{}, page, &dataList)
    82  	if err != nil {
    83  		panic(err)
    84  	}
    85  	bs, _ = json.Marshal(page)
    86  	fmt.Println(string(bs))
    87  	for _, one := range dataList {
    88  		bs, _ = json.Marshal(one)
    89  		fmt.Println(string(bs))
    90  	}
    91  }
    92  
    93  func TestBean(t *testing.T) {
    94  	//var list []*QueryBean
    95  	//
    96  	//listBeanType := GetListBeanType(list)
    97  	//listBeanValue := reflect.New(listBeanType)
    98  	//listBeanValue.Elem().FieldByName("A").Set(reflect.ValueOf("xxx"))
    99  	//newBean := listBeanValue.Interface() // 调用反射创建对象
   100  	//fmt.Println(listBeanType.String())
   101  	//fmt.Println(listBeanValue)
   102  	//fmt.Println(newBean)
   103  	//list = append(list, newBean.(*QueryBean))
   104  	//fmt.Println(list)
   105  
   106  	data := &QueryStruct{}
   107  	reflect.ValueOf(data).Elem().FieldByName("DeleteTime").Set(reflect.ValueOf(time.Time{}))
   108  	bs, _ := json.Marshal(data)
   109  	fmt.Println(string(bs))
   110  }
   111  
   112  type QueryStruct struct {
   113  	A          string    `json:"a"`
   114  	B          int8      `json:"b"`
   115  	C          int8      `json:"c"`
   116  	DeleteTime time.Time `json:"deleteTime,omitempty"`
   117  }
   118  
   119  func TestQueryUser(t *testing.T) {
   120  	db, err := db_sqlite3.Open(db_sqlite3.GetDSN(`C:\Users\ZhuLiang\TeamIDE\data\backups\版本-1.7.7-升级之前备份-数据库`))
   121  	if err != nil {
   122  		panic(err)
   123  	}
   124  	var one int64
   125  	_, err = DoQueryStruct(db, `SELECT value FROM TM_ID WHERE idType=?`, []interface{}{5001}, &one)
   126  	if err != nil {
   127  		panic(err)
   128  	}
   129  	bs, _ := json.Marshal(one)
   130  	fmt.Println(string(bs))
   131  }
   132  
   133  type Model struct {
   134  }