github.com/jtzjtz/kit@v1.0.2/sql/sqlbinding_test.go (about)

     1  package sql
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func Test1EQ1(t *testing.T) {
    10  	// 1=1
    11  	sql := DefaultField
    12  	t.Log(sql)
    13  }
    14  func TestAnd(t *testing.T) {
    15  	// id = '1' AND name LIKE '%zhangsan%' AND age > 19
    16  	sql := DefaultField.And("id").EQ("1").And("name").Like("zhangsan").And("age").GT(19)
    17  	t.Log(sql)
    18  }
    19  
    20  func TestIn(t *testing.T) {
    21  	// id IN (1, 2, 3)
    22  	sql := DefaultField.And("id").In(1, 2, 3)
    23  	t.Log(sql)
    24  }
    25  
    26  func TestInStr(t *testing.T) {
    27  	// id IN ('a', 'b', 'c')
    28  	sql := DefaultField.And("id").In("a", "b", "c")
    29  	t.Log(sql)
    30  }
    31  
    32  func TestSqlInject(t *testing.T) {
    33  	// full_name LIKE '%\'%f\') union select 1,2,3,4,5,6,7,user(),9,10,11,12,13,14#%\')%'
    34  	sql := DefaultField.And("full_name").Like("'%f') union select 1,2,3,4,5,6,7,user(),9,10,11,12,13,14#%')")
    35  	t.Log(sql)
    36  }
    37  
    38  func TestOR(t *testing.T) {
    39  	// (name LIKE 'abc%' AND sex > 20 OR  name LIKE 'bcd%' AND sex < 20) AND job = 'xxx'
    40  	sql := OR(DefaultField.And("name").RLike("abc").And("sex").GT(20), DefaultField.And("name").RLike("bcd").And("sex").LT(20)).And("job").EQ("xxx")
    41  	t.Log(sql)
    42  }
    43  
    44  func TestOR2(t *testing.T) {
    45  	// name = 'zhangsan' AND ( age < 20 OR  age > 25) AND email LIKE '%xxx@qq.com%'
    46  	sql := DefaultField.And("name").EQ("zhangsan").And(fmt.Sprint(OR(DefaultField.And("age").LT(20), DefaultField.And("age").GT(25)).And("email").Like("xxx@qq.com")))
    47  	t.Log(sql)
    48  }
    49  
    50  func TestTime(t *testing.T) {
    51  	// create_time = "2020-12-30 15:35:49"
    52  	sql := DefaultField.And("create_time").EQ(time.Now())
    53  	t.Log(sql)
    54  }