Make StatementType a type alias instead of an int

Signed-off-by: Andres Taylor <antaylor@squareup.com>
This commit is contained in:
Andres Taylor 2019-10-02 06:17:29 +02:00
Родитель 909d0c8861
Коммит f0cfd16670
5 изменённых файлов: 14 добавлений и 12 удалений

Просмотреть файл

@ -66,7 +66,7 @@ func NewFromString(inCtx context.Context, parent, label string) (Span, context.C
// AnnotateSQL annotates information about a sql query in the span. This is done in a way
// so as to not leak personally identifying information (PII), or sensitive personal information (SPI)
func AnnotateSQL(span Span, sql string) {
span.Annotate("sql-statement-type", sqlparser.StmtType(sqlparser.Preview(sql)))
span.Annotate("sql-statement-type", sqlparser.Preview(sql).String())
}
// FromContext returns the Span from a Context if present. The bool return

Просмотреть файл

@ -30,10 +30,13 @@ import (
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
)
// StatementType encodes the type of a SQL statement
type StatementType int
// These constants are used to identify the SQL statement type.
// Changing this list will require reviewing all calls to Preview.
const (
StmtSelect = iota
StmtSelect StatementType = iota
StmtStream
StmtInsert
StmtReplace
@ -53,7 +56,7 @@ const (
// Preview analyzes the beginning of the query using a simpler and faster
// textual comparison to identify the statement type.
func Preview(sql string) int {
func Preview(sql string) StatementType {
trimmed := StripLeadingComments(sql)
if strings.Index(trimmed, "/*!") == 0 {
@ -111,9 +114,8 @@ func Preview(sql string) int {
return StmtUnknown
}
// StmtType returns the statement type as a string
func StmtType(stmtType int) string {
switch stmtType {
func (s StatementType) String() string {
switch s {
case StmtSelect:
return "SELECT"
case StmtStream:

Просмотреть файл

@ -27,7 +27,7 @@ import (
func TestPreview(t *testing.T) {
testcases := []struct {
sql string
want int
want StatementType
}{
{"select ...", StmtSelect},
{" select ...", StmtSelect},

Просмотреть файл

@ -167,7 +167,7 @@ func (e *Executor) execute(ctx context.Context, safeSession *SafeSession, sql st
}
stmtType := sqlparser.Preview(sql)
logStats.StmtType = sqlparser.StmtType(stmtType)
logStats.StmtType = stmtType.String()
// Mysql warnings are scoped to the current session, but are
// cleared when a "non-diagnostic statement" is executed:
@ -1126,7 +1126,7 @@ func (e *Executor) handleComment(sql string) (*sqltypes.Result, error) {
// StreamExecute executes a streaming query.
func (e *Executor) StreamExecute(ctx context.Context, method string, safeSession *SafeSession, sql string, bindVars map[string]*querypb.BindVariable, target querypb.Target, callback func(*sqltypes.Result) error) (err error) {
logStats := NewLogStats(ctx, method, sql, bindVars)
logStats.StmtType = sqlparser.StmtType(sqlparser.Preview(sql))
logStats.StmtType = sqlparser.Preview(sql).String()
defer logStats.Send()
if bindVars == nil {
@ -1137,7 +1137,7 @@ func (e *Executor) StreamExecute(ctx context.Context, method string, safeSession
// check if this is a stream statement for messaging
// TODO: support keyRange syntax
if logStats.StmtType == sqlparser.StmtType(sqlparser.StmtStream) {
if logStats.StmtType == sqlparser.StmtStream.String() {
return e.handleMessageStream(ctx, safeSession, sql, target, callback, vcursor, logStats)
}
@ -1521,7 +1521,7 @@ func (e *Executor) prepare(ctx context.Context, safeSession *SafeSession, sql st
}
stmtType := sqlparser.Preview(sql)
logStats.StmtType = sqlparser.StmtType(stmtType)
logStats.StmtType = stmtType.String()
// Mysql warnings are scoped to the current session, but are
// cleared when a "non-diagnostic statement" is executed:

Просмотреть файл

@ -325,7 +325,7 @@ func (vs *vstreamer) parseEvent(ev mysql.BinlogEvent) ([]*binlogdatapb.VEvent, e
case sqlparser.StmtOther:
// These are DBA statements like REPAIR that can be ignored.
default:
return nil, fmt.Errorf("unexpected statement type %s in row-based replication: %q", sqlparser.StmtType(cat), q.SQL)
return nil, fmt.Errorf("unexpected statement type %s in row-based replication: %q", cat, q.SQL)
}
case ev.IsTableMap():
// This is very frequent. It precedes every row event.