aboutsummaryrefslogtreecommitdiffhomepage
path: root/cmd/extpo/sql.go
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2025-11-03 15:51:56 +0100
committerThomas Voss <mail@thomasvoss.com> 2025-11-03 15:51:56 +0100
commitef4e140949408a917b45cc253dd38d229e34f46b (patch)
treef0b877c76e94b84b7c55cf2bf54a5bd280ddb358 /cmd/extpo/sql.go
parent15f841ab43fd9b431e93d2e870c23ae7695929cc (diff)
Support extracting translations from SQL scripts
Diffstat (limited to 'cmd/extpo/sql.go')
-rw-r--r--cmd/extpo/sql.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/cmd/extpo/sql.go b/cmd/extpo/sql.go
new file mode 100644
index 0000000..43bea9c
--- /dev/null
+++ b/cmd/extpo/sql.go
@@ -0,0 +1,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
+}