aboutsummaryrefslogtreecommitdiffhomepage
path: root/cmd/extpo/sql.go
blob: 43bea9c1352a0730b06483202c42965ee0604ef6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main

import (
	"bytes"
	"errors"
	"io"

	"github.com/rqlite/sql"
)

type sqlVisitor struct{}

func processSql(path string) {
	p := sql.NewParser(bytes.NewReader(currentFile))
	for {
		stmt, err := p.ParseStatement()
		switch {
		case errors.Is(err, io.EOF):
			return
		case err != nil:
			die(err)
		}
		sql.Walk(sqlVisitor{}, stmt)
	}
}

func processSqlArgs(msgidExpr, msgctxtExpr sql.Expr, pos sql.Pos) {
	msgid, ok := msgidExpr.(*sql.StringLit)
	if !ok {
		return
	}
	msgctxt, ok := msgctxtExpr.(*sql.StringLit)
	if !ok {
		return
	}
	tl := translation{
		msgid:   msgid.Value,
		msgctxt: msgctxt.Value,
	}
	ti := translations[tl]
	ti.locs = append(ti.locs, loc{currentPath, pos.Line})
	translations[tl] = ti
}

func (v sqlVisitor) Visit(n sql.Node) (sql.Visitor, sql.Node, error) {
	if cn, ok := n.(*sql.Call); ok && sql.IdentName(cn.Name) == "C_" {
		processSqlArgs(cn.Args[0], cn.Args[1], cn.Lparen)
	}
	return v, n, nil
}

func (v sqlVisitor) VisitEnd(n sql.Node) (sql.Node, error) {
	return n, nil
}