Skip to content

Commit 69c6ed0

Browse files
committed
initial commit
0 parents  commit 69c6ed0

16 files changed

+855
-0
lines changed

.gitignore

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
# Dependency directories (remove the comment below to include it)
15+
# vendor/
16+
17+
debug.log
18+
19+
# Output files
20+
21+
/app
22+
/app.exe
23+
/main
24+
/main.exe
25+
/web/*.wasm
26+
/web/wasm_exec.js
27+
/lib.a
28+
/lib.h
29+
30+
__debug_bin
31+
__debug_bin.exe
32+
33+
# PlantUML VSCode extension
34+
/out/
35+
36+
# Temporary files
37+
/_tmp/
38+
/.tmp/
39+
/tmp/

LICENSE.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# MIT License
2+
3+
## Copyright (c) 2023 Shellyl_N and Authors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"),
7+
to deal in the Software without restriction, including without limitation
8+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
9+
and/or sell copies of the Software, and to permit persons to whom
10+
the Software is furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included
13+
in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
17+
A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19+
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20+
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Makefile

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
2+
3+
# GOOS - darwin windows linux ...
4+
# GOARCH - 386 amd64 arm arm64 ...
5+
# GOARM - 5 6 7 ...
6+
7+
DEVNUL := /dev/null
8+
DIRSEP := /
9+
SEP := :
10+
RM_F := rm -f
11+
RM_RF := rm -rf
12+
CP := cp
13+
CP_FLAGS :=
14+
CP_R := cp -RT
15+
CP_R_FLAGS :=
16+
FINDFILE := find . -type f -name
17+
WHICH := which
18+
PRINTENV := printenv
19+
GOXOS := darwin
20+
GOXARCH := arm64
21+
GOXARM :=
22+
GOCMD := go
23+
GOBUILD := $(GOCMD) build
24+
GOTIDY := $(GOCMD) mod tidy
25+
GOCLEAN := $(GOCMD) clean
26+
GOTEST := $(GOCMD) test
27+
GOVET := $(GOCMD) vet
28+
GOSHADOW := $(shell $(GOCMD) env GOPATH)/bin/shadow
29+
GOLINT := $(shell $(GOCMD) env GOPATH)/bin/staticcheck
30+
TINYGOCMD := tinygo
31+
SRCS :=
32+
CLI_NAME := app
33+
LIB_NAME := lib
34+
TARGET_CLI := ./
35+
TARGET_LIB := ./lib
36+
TARGET_WASM := ./wasm
37+
BIN_CLI := $(CLI_NAME)
38+
BIN_LIB := $(LIB_NAME).a
39+
BIN_SO := $(LIB_NAME).so
40+
BIN_WASM := web/go.wasm
41+
42+
43+
ifeq ($(OS),Windows_NT)
44+
BIN_CLI := $(CLI_NAME).exe
45+
BIN_LIB := $(LIB_NAME).a
46+
BIN_SO := $(LIB_NAME).dll
47+
ifeq ($(MSYSTEM),)
48+
SHELL := cmd.exe
49+
DEVNUL := NUL
50+
DIRSEP := \\
51+
SEP := ;
52+
RM_F := del /Q
53+
RM_RF := rmdir /S /Q
54+
CP := copy
55+
CP_FLAGS := /Y
56+
CP_R := xcopy
57+
CP_R_FLAGS := /E /I /Y
58+
FINDFILE := cmd.exe /C "where /r . "
59+
WHICH := where
60+
PRINTENV := set
61+
endif
62+
endif
63+
64+
define normalize_dirsep
65+
$(subst /,$(DIRSEP),$1)
66+
endef
67+
68+
define find_file
69+
$(subst $(subst \,/,$(CURDIR)),.,$(subst \,/,$(shell $(FINDFILE) $1)))
70+
endef
71+
72+
73+
# Usage of cp -R and cp
74+
# $(CP_R) $(call normalize_dirsep,path/to/src) $(call normalize_dirsep,path/to/dest) $(CP_R_FLAGS)
75+
# $(CP) $(call normalize_dirsep,path/to/src) $(call normalize_dirsep,path/to/dest) $(CP_FLAGS)
76+
77+
78+
SRCS := $(call find_file,"*.go")
79+
VERSION := $(shell git describe --tags --abbrev=0 2> $(DEVNUL) || echo "0.0.0-alpha.1")
80+
REVISION := $(shell git rev-parse --short HEAD)
81+
82+
LDFLAGS := -ldflags="-s -w -buildid= -X \"main.Version=$(VERSION)\" -X \"main.Revision=$(REVISION)\" -extldflags \"-static\""
83+
LDFLAGS_SHARED := -ldflags="-s -w -buildid= -X \"main.Version=$(VERSION)\" -X \"main.Revision=$(REVISION)\""
84+
85+
86+
.PHONY: printenv clean cleantest upgrade tidy test testinfo cover lint wasm tinywasm doc
87+
all: clean test build
88+
89+
90+
printenv:
91+
@echo SHELL : $(SHELL)
92+
@echo CURDIR : $(CURDIR)
93+
@echo DEVNUL : $(DEVNUL)
94+
@echo DIRSEP : "$(DIRSEP)"
95+
@echo SEP : "$(SEP)"
96+
@echo WHICH GO : $(shell $(WHICH) $(GOCMD))
97+
@echo GOXOS : $(GOXOS)
98+
@echo GOXARCH : $(GOXARCH)
99+
@echo GOXARM : $(GOXARM)
100+
@echo VERSION : $(VERSION)
101+
@echo REVISION : $(REVISION)
102+
@echo SRCS : $(SRCS)
103+
@echo LDFLAGS : $(LDFLAGS)
104+
@echo TARGET_CLI : $(TARGET_CLI)
105+
@echo BIN_CLI : $(BIN_CLI)
106+
@echo BIN_LIB : $(BIN_LIB)
107+
@echo BIN_SO : $(BIN_SO)
108+
109+
110+
clean:
111+
$(GOCLEAN)
112+
-$(RM_F) $(BIN_WASM)
113+
114+
cleantest:
115+
$(GOCLEAN) -testcache
116+
117+
118+
upgrade:
119+
$(GOCMD) get -u && $(GOTIDY)
120+
121+
tidy:
122+
$(GOTIDY)
123+
124+
125+
test:
126+
$(GOTEST) ./...
127+
128+
test+info:
129+
$(GOTEST) -gcflags=-m ./...
130+
131+
cover:
132+
$(GOTEST) -cover ./...
133+
134+
lint:
135+
@echo "Run go vet..."
136+
$(GOVET) ./...
137+
@echo "Run shadow..."
138+
$(GOVET) -vettool="$(GOSHADOW)" ./...
139+
@echo "Run staticcheck..."
140+
$(GOLINT) ./...
141+
142+
143+
wasm: export GOOS:=js
144+
wasm: export GOARCH:=wasm
145+
wasm:
146+
$(CP) "$(shell $(GOCMD) env GOROOT)/misc/wasm/wasm_exec.js" web/.
147+
$(GOBUILD) \
148+
-a -tags wasm \
149+
-trimpath \
150+
-buildvcs=false \
151+
$(LDFLAGS) \
152+
-o $(BIN_WASM) $(TARGET_WASM)
153+
154+
155+
tinywasm: export GOOS:=js
156+
tinywasm: export GOARCH:=wasm
157+
tinywasm:
158+
$(CP) "$(shell $(TINYGOCMD) env TINYGOROOT)/targets/wasm_exec.js" web/.
159+
$(TINYGOCMD) build \
160+
-tags wasm \
161+
-no-debug \
162+
-stack-size 512kB \
163+
-o $(BIN_WASM) $(TARGET_WASM)
164+
165+
166+
doc:
167+
godoc -http=:6060

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# go-open-soql-visualizer
2+
3+
4+
5+
## ⚖️ License
6+
7+
MIT
8+
Copyright (c) 2023 Shellyl_N and Authors.

go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/shellyln/go-open-soql-visualizer
2+
3+
go 1.18
4+
5+
require github.com/shellyln/go-open-soql-parser v0.0.7
6+
7+
require (
8+
github.com/shellyln/go-nameutil v0.0.2 // indirect
9+
github.com/shellyln/takenoco v0.0.13 // indirect
10+
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
github.com/shellyln/go-nameutil v0.0.2 h1:K5qEhqTlfXs620C3ECdcM6I6KsQQV+jjFiMBonDII3s=
2+
github.com/shellyln/go-nameutil v0.0.2/go.mod h1:pbg084sJdrtGqmzs0pT8fPkitcoY3eSJNi4aZxtA8O0=
3+
github.com/shellyln/go-open-soql-parser v0.0.7 h1:i+1Rtv8E6vTQ4c+bU5ci1HtyE6LDRkMz20ENFgVqpzo=
4+
github.com/shellyln/go-open-soql-parser v0.0.7/go.mod h1:yN6XuGQT9Nh/bAajdADTMph4Kh7t6Umm7OmifuwoN/s=
5+
github.com/shellyln/takenoco v0.0.13 h1:/UQcrcsIlfrnGahhaQ25kj3kQnp7JMJUNuovxM6lZ6E=
6+
github.com/shellyln/takenoco v0.0.13/go.mod h1:SF6jGo3dFtcTgstpXGTJoKXPOldlDwV8R5U1znhEets=

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package main
2+
3+
func main() {
4+
}

soql/visualizer/visualizer.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package visualizer
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/shellyln/go-open-soql-parser/soql/parser/types"
7+
)
8+
9+
func Visualize(q *types.SoqlQuery) string {
10+
relations := "erDiagram\n\n"
11+
12+
for viewId := range q.Meta.ViewGraph {
13+
leaf := q.Meta.ViewGraph[viewId]
14+
15+
if leaf.ParentViewId == 0 {
16+
qLeaf := q.Meta.QueryGraph[leaf.QueryId]
17+
if qLeaf.ParentQueryId != 0 {
18+
parentQLeaf := q.Meta.QueryGraph[qLeaf.ParentQueryId]
19+
parentName := parentQLeaf.Query.From[0].Name
20+
fields := leaf.Object.PerObjectQuery.Fields
21+
22+
colName := ""
23+
if len(fields) > 0 && len(fields[0].Name) > 0 {
24+
colName = fields[0].Name[len(fields[0].Name)-1]
25+
}
26+
27+
relations += fmt.Sprintf("%v ||..o{ %v: \"in %v\"\n", parentName[len(parentName)-1], leaf.Name, colName)
28+
}
29+
continue
30+
}
31+
parentLeaf := q.Meta.ViewGraph[leaf.ParentViewId]
32+
33+
var format string
34+
var lineType string
35+
if leaf.NonResult {
36+
lineType = ".."
37+
} else {
38+
lineType = "--"
39+
}
40+
41+
if leaf.Many {
42+
// one to zero or many (subquery root)
43+
format = "%v ||%vo{ %v: \"subquery\"\n"
44+
} else if leaf.InnerJoin {
45+
// one to one
46+
format = "%v ||%v|| %v: \"\"\n"
47+
} else {
48+
// one to zero or one
49+
format = "%v ||%vo| %v: \"\"\n"
50+
}
51+
relations += fmt.Sprintf(format, parentLeaf.Name, lineType, leaf.Name)
52+
}
53+
54+
relations += "\n"
55+
56+
fieldMap := make(map[string]struct{})
57+
58+
for viewId := range q.Meta.ViewGraph {
59+
leaf := q.Meta.ViewGraph[viewId]
60+
relations += fmt.Sprintf("%v {\n", leaf.Name)
61+
62+
fields := leaf.Object.PerObjectQuery.Fields
63+
for j := range fields {
64+
if _, ok := fieldMap[fields[j].Key]; ok {
65+
continue
66+
}
67+
name := fields[j].Name[len(fields[j].Name)-1]
68+
relations += fmt.Sprintf(" unknown %v\n", name)
69+
fieldMap[fields[j].Key] = struct{}{}
70+
}
71+
72+
relations += "}\n\n"
73+
}
74+
75+
return relations
76+
}

0 commit comments

Comments
 (0)