parsers

package
v0.0.0-debug-20260702 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var HandleSqlForRecord = func(sql string) []string {
	split := SplitSqlBySemicolon(sql)
	for i := range split {
		stripScanner := mysql.NewScanner(dialect.MYSQL, split[i])
		//strip needed comment "/*XXX*/"
		var commentIdx [][]int
		for stripScanner.Pos < len(split[i]) {
			typ, comment := stripScanner.ScanComment()
			if typ == mysql.COMMENT {

				if strings.HasPrefix(comment, "/*") && strings.HasSuffix(comment, "*/") {
					commentContent := strings.ToLower(strings.TrimSpace(comment[2 : len(comment)-2]))
					if _, ok := stripContents[commentContent]; ok {
						commentIdx = append(commentIdx, []int{stripScanner.Pos - len(comment), stripScanner.Pos})
					}
				}
			} else if typ == mysql.EofChar() || typ == mysql.LEX_ERROR {
				break
			}
		}

		if len(commentIdx) > 0 {
			var builder strings.Builder
			for j := 0; j < len(commentIdx); j++ {
				if j == 0 {
					builder.WriteString(split[i][0:commentIdx[j][0]])
				} else {
					builder.WriteString(split[i][commentIdx[j-1][1]:commentIdx[j][0]])
				}
			}

			builder.WriteString(split[i][commentIdx[len(commentIdx)-1][1]:len(split[i])])
			split[i] = strings.TrimSpace(builder.String())
		}

		scanner := mysql.NewScanner(dialect.MYSQL, split[i])
		indexes := []int{-1}
		eq := int('=')
		for scanner.Pos < len(split[i]) {
			typ, s := scanner.Scan()
			if typ == mysql.IDENTIFIED {
				typ, _ = scanner.Scan()
				if typ == mysql.BY || typ == mysql.WITH {
					typ, s = scanner.Scan()
					if typ != mysql.RANDOM {
						indexes = append(indexes, scanner.Pos-len(s)-1, scanner.Pos-2)
					}
				}
			} else if strings.ToLower(s) == "access_key_id" || strings.ToLower(s) == "secret_access_key" {
				typ, _ = scanner.Scan()
				if typ == eq {
					_, s = scanner.Scan()
					indexes = append(indexes, scanner.Pos-len(s)-1, scanner.Pos-2)
				}
			}
		}
		indexes = append(indexes, len(split[i]))

		if len(indexes) > 2 {
			var builder strings.Builder
			for j := 0; j < len(indexes); j += 2 {
				builder.WriteString(split[i][indexes[j]+1 : indexes[j+1]])
				if j < len(indexes)-2 {
					builder.WriteString("******")
				}
			}
			split[i] = builder.String()
		}
		split[i] = strings.TrimSpace(split[i])
	}
	return split
}

Functions

func AddRewriteHints

func AddRewriteHints(ctx context.Context, stmts []tree.Statement, sql string) error

AddRewriteHints The position of the hint at the beginning is different from that in MySQL. Placing it in the parser for parsing will lead to syntax conflicts, so it can only be parsed separately

func Parse

func Parse(ctx context.Context, dialectType dialect.DialectType, sql string, lower int64) ([]tree.Statement, error)

func ParseOne

func ParseOne(ctx context.Context, dialectType dialect.DialectType, sql string, lower int64) (tree.Statement, error)

func SplitSqlBySemicolon added in v0.8.0

func SplitSqlBySemicolon(sql string) []string

Types

type RewriteMap

type RewriteMap struct {
	// RawRewrites carries the raw SQL strings from JSON input.
	RawRewrites map[string]string `json:"rewrites"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL