rest/plugins/sharding/sharding_test.go

62 lines
1.7 KiB
Go
Raw Normal View History

2024-12-11 17:29:01 +08:00
package sharding
import (
"fmt"
"github.com/uole/sqlparser"
"testing"
)
func TestFormat(t *testing.T) {
t.Log(fmt.Sprintf("%.2f%%", 0.5851888*100))
}
func TestSharding_Query(t *testing.T) {
//sql := "SELECT COUNT(*) FROM aaa"
sql := "SELECT uid,SUM(IF(direction='inbound',1,0)) AS inbound_times,SUM(IF(direction='inbound',IF(answer_duration>0,1,0),0)) AS inbound_answer_times,SUM(IF(direction='outbound',1,0)) AS outbound_times,SUM(IF(direction='outbound',IF(answer_duration>0,1,0),0)) AS outbound_answer_times FROM `cdr_logs` WHERE ((`domain` = 'test.cc.echo.me' OR `domain` = 'default') AND `name` <> '') AND (`create_stamp` BETWEEN 1712505600 AND 1712505608) AND `name` IN ('a','b','c') GROUP BY `uid` having uid != '' ORDER BY create_stamp DESC LIMIT 15"
stmt, err := sqlparser.Parse(sql)
if err != nil {
t.Error(err)
return
}
selectStmt, ok := stmt.(*sqlparser.Select)
if !ok {
t.Error("not select stmt")
return
}
buf := sqlparser.NewTrackedBuffer(nil)
sqlparser.NewTableIdent("test").Format(buf)
buf.Reset()
selectStmt.Format(buf)
t.Log("SQL")
t.Log(buf.String())
buf.Reset()
t.Log("SELECT")
selectStmt.SelectExprs.Format(buf)
t.Log(buf.String())
buf.Reset()
t.Log("FROM")
selectStmt.From.Format(buf)
t.Log(buf.String())
buf.Reset()
t.Log("WHERE")
selectStmt.Where.Format(buf)
t.Log(buf.String())
buf.Reset()
t.Log("ORDER BY")
selectStmt.OrderBy.Format(buf)
t.Log(buf.String())
buf.Reset()
t.Log("GROUP BY")
selectStmt.GroupBy.Format(buf)
t.Log(buf.String())
buf.Reset()
t.Log("LIMIT")
selectStmt.Limit.Format(buf)
t.Log(buf.String())
buf.Reset()
t.Log("Having")
selectStmt.Having.Format(buf)
t.Log(buf.String())
buf.Reset()
}