github.com/unionj-cloud/go-doudou/v2@v2.3.5/toolkit/sqlext/query/query_test.go (about)

     1  package query
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/stretchr/testify/require"
     6  	"github.com/unionj-cloud/go-doudou/v2/toolkit/sqlext/sortenum"
     7  	"testing"
     8  )
     9  
    10  func ExampleCriteria() {
    11  
    12  	query := C().Col("name").Eq("wubin").Or(C().Col("school").Eq("havard")).And(C().Col("age").Eq(18))
    13  	fmt.Println(query.Sql())
    14  
    15  	query = C().Col("name").Eq("wubin").Or(C().Col("school").Eq("havard")).And(C().Col("delete_at").IsNotNull())
    16  	fmt.Println(query.Sql())
    17  
    18  	query = C().Col("name").Eq("wubin").Or(C().Col("school").In("havard")).And(C().Col("delete_at").IsNotNull())
    19  	fmt.Println(query.Sql())
    20  
    21  	query = C().Col("name").Eq("wubin").Or(C().Col("school").In([]string{"havard", "beijing unv"})).And(C().Col("delete_at").IsNotNull())
    22  	fmt.Println(query.Sql())
    23  
    24  	query = C().Col("name").Eq("wubin").Or(C().Col("age").In([]int{5, 10})).And(C().Col("delete_at").IsNotNull())
    25  	fmt.Println(query.Sql())
    26  
    27  	query = C().Col("name").Ne("wubin").Or(C().Col("create_at").Lt("now()"))
    28  	fmt.Println(query.Sql())
    29  
    30  	page := Page{
    31  		Orders: []Order{
    32  			{
    33  				Col:  "create_at",
    34  				Sort: sortenum.Desc,
    35  			},
    36  		},
    37  		Offset: 20,
    38  		Size:   10,
    39  	}
    40  	page = page.Order(Order{
    41  		Col:  "score",
    42  		Sort: sortenum.Asc,
    43  	})
    44  	page = page.Limit(30, 5)
    45  	fmt.Println(page.Sql())
    46  	pageRet := NewPageRet(page)
    47  	fmt.Println(pageRet.PageNo)
    48  
    49  	fmt.Println(P().Order(Order{
    50  		Col:  "score",
    51  		Sort: sortenum.Asc,
    52  	}).Limit(20, 10).Sql())
    53  
    54  	// Output:
    55  	//((`name` = ? or `school` = ?) and `age` = ?) [wubin havard 18]
    56  	//((`name` = ? or `school` = ?) and `delete_at` is not null) [wubin havard]
    57  	//((`name` = ? or `school` in (?)) and `delete_at` is not null) [wubin havard]
    58  	//((`name` = ? or `school` in (?,?)) and `delete_at` is not null) [wubin havard beijing unv]
    59  	//((`name` = ? or `age` in (?,?)) and `delete_at` is not null) [wubin 5 10]
    60  	//(`name` != ? or `create_at` < ?) [wubin now()]
    61  	//order by `create_at` desc,`score` asc limit ?,? [30 5]
    62  	//7
    63  	//order by `score` asc limit ?,? [20 10]
    64  }
    65  
    66  func TestCriteriaAppend(t *testing.T) {
    67  	sqlStatement := C().Col("name").Eq("wubin").Or(C().Col("school").Eq("havard")).
    68  		And(C().Col("age").Eq(18)).
    69  		Or(C().Col("score").Gte(90).And(C().Col("height").Gt(160).And(C().Col("height").Lte(170).
    70  			Append(String("and favourite = 'Go'")))))
    71  	str, _ := sqlStatement.Sql()
    72  	require.Equal(t, "(((`name` = ? or `school` = ?) and `age` = ?) or (`score` >= ? and (`height` > ? and (`height` <= ? and favourite = 'Go'))))", str)
    73  }
    74  
    75  func TestWhereAppend(t *testing.T) {
    76  	sqlStatement := C().Col("name").Eq("wubin").Or(C().Col("school").Eq("havard")).
    77  		And(C().Col("age").Eq(18)).
    78  		Or(C().Col("score").Gte(90).
    79  			And(C().Col("height").Gt(160).And(C().Col("height").Lte(170))).
    80  			Append(String("and favourite = 'Go'")))
    81  	str, _ := sqlStatement.Sql()
    82  	require.Equal(t, "(((`name` = ? or `school` = ?) and `age` = ?) or ((`score` >= ? and (`height` > ? and `height` <= ?)) and favourite = 'Go'))", str)
    83  }
    84  
    85  func ExampleEnd() {
    86  	page := P().Order(Order{
    87  		Col:  "create_at",
    88  		Sort: sortenum.Desc,
    89  	}).Limit(0, 1)
    90  	var where Q
    91  	where = C().Col("project_id").Eq(1)
    92  	where = where.And(C().Col("delete_at").IsNull())
    93  	where = where.End(page)
    94  	fmt.Println(where.Sql())
    95  
    96  	where = C().Col("project_id").Eq(1)
    97  	where = where.And(C().Col("delete_at").IsNull())
    98  	where = where.End(String("for update"))
    99  	fmt.Println(where.Sql())
   100  
   101  	where = C().Col("cc.project_id").Eq(1)
   102  	where = where.And(C().Col("cc.delete_at").IsNull())
   103  	where = where.End(String("for update"))
   104  	fmt.Println(where.Sql())
   105  
   106  	where = C().Col("cc.survey_id").Eq("abc").
   107  		And(C().Col("cc.year").Eq(2021)).
   108  		And(C().Col("cc.month").Eq(10)).
   109  		And(C().Col("cc.stat_type").Eq(2)).End(String("for update"))
   110  	fmt.Println(where.Sql())
   111  
   112  	where = C().Col("cc.name").Like("%ba%")
   113  	fmt.Println(where.Sql())
   114  
   115  	page = P().Order(Order{
   116  		Col:  "user.create_at",
   117  		Sort: sortenum.Desc,
   118  	}).Limit(0, 1)
   119  	where = C().Col("project_id").Eq(1)
   120  	where = where.And(C().Col("delete_at").IsNull())
   121  	where = where.End(page)
   122  	fmt.Println(where.Sql())
   123  
   124  	where = C().Col("delete_at").IsNull().And(C().Col("op_code").NotIn([]int{1, 2, 3}))
   125  	fmt.Println(where.Sql())
   126  
   127  	// Output:
   128  	//(`project_id` = ? and `delete_at` is null) order by `create_at` desc limit ?,? [1 0 1]
   129  	//(`project_id` = ? and `delete_at` is null) for update [1]
   130  	//(cc.`project_id` = ? and cc.`delete_at` is null) for update [1]
   131  	//(((cc.`survey_id` = ? and cc.`year` = ?) and cc.`month` = ?) and cc.`stat_type` = ?) for update [abc 2021 10 2]
   132  	//cc.`name` like ? [%ba%]
   133  	//(`project_id` = ? and `delete_at` is null) order by user.`create_at` desc limit ?,? [1 0 1]
   134  	//(`delete_at` is null and `op_code` not in (?,?,?)) [1 2 3]
   135  }
   136  
   137  func TestWhereAppend2(t *testing.T) {
   138  	var where Q
   139  	where = C().Col("left_number").Gt(0).Or(C().Col("left_number").Lt(0))
   140  	where = where.And(C().Col("name").Ne("感谢参与"))
   141  	where = where.And(C().Col("delete_at").IsNull())
   142  	page := P().Order(Order{
   143  		Col:  "order",
   144  		Sort: sortenum.Desc,
   145  	}).Limit(0, 10)
   146  	where = where.Append(page)
   147  	str, _ := where.Sql()
   148  	require.Equal(t, "(((`left_number` > ? or `left_number` < ?) and `name` != ?) and `delete_at` is null) order by `order` desc limit ?,?", str)
   149  }