forked from amaniak/gel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.go
More file actions
138 lines (111 loc) · 2.44 KB
/
scanner.go
File metadata and controls
138 lines (111 loc) · 2.44 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package gel
import (
"bufio"
"bytes"
"github.com/amaniak/gel/token"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)
const (
SQL_EXTENSTION = ".sql"
)
type FileInfo struct {
Namespace string
FileName string
Location string
}
type Ast struct {
*FileInfo
Position int
Raw string
Bytes []byte
Source string
}
func EmptyAstNode() *Ast {
buf := new([]byte)
a := &Ast{&FileInfo{}, 0, "", *buf, ""}
return a
}
func (node *Ast) IsImFunc() bool {
match, _ := regexp.MatchString(token.IMFUNC.String(), node.Text())
return match
}
func (node *Ast) IsFunc() bool {
match, _ := regexp.MatchString(token.FUNC.String(), node.Text())
return match
}
func (node *Ast) IsImProc() bool {
match, _ := regexp.MatchString(token.IMPROC.String(), node.Text())
return match
}
func (node *Ast) IsProc() bool {
match, _ := regexp.MatchString(token.PROC.String(), node.Text())
return match
}
func (node *Ast) IsExec() bool {
match, _ := regexp.MatchString(token.EXEC.String(), node.Text())
return match
}
func (node *Ast) IsQuery() bool {
match, _ := regexp.MatchString(token.QUERY.String(), node.Text())
return match
}
func (node *Ast) IsComment() bool {
match, _ := regexp.MatchString(token.COMMENT.String(), node.Text())
return match
}
func (node *Ast) IsEmpty() bool {
check := bytes.TrimSpace(node.Bytes)
if len(check) == 0 {
return true
}
return false
}
func (node *Ast) IsNewLine() bool {
return node.Text() == "\n"
}
func (node *Ast) IsWhiteSpace() bool {
reg, _ := regexp.Compile(`^\s`)
return reg.MatchString(node.Text())
}
func (node *Ast) Line() string {
return strconv.Itoa(node.Position)
}
func (node *Ast) Text() string {
return node.Raw
}
type Scanner struct {
Path string
Buffer []*Ast
Tree map[string]*Ast
}
func (s *Scanner) Load() {
err := filepath.Walk(s.Path, s.findSQLFiles)
if err != nil {
log.Fatal("Something went wrong")
}
}
func (s *Scanner) findSQLFiles(pathToFile string, f os.FileInfo, err error) error {
if filepath.Ext(f.Name()) == SQL_EXTENSTION {
namespace := strings.TrimSuffix(f.Name(), filepath.Ext(f.Name()))
file, err := os.Open(pathToFile)
defer file.Close()
buffer := bufio.NewScanner(file)
if err != nil {
log.Fatal(err)
}
line := 1
for buffer.Scan() {
fileInfo := &FileInfo{namespace, f.Name(), pathToFile}
node := &Ast{fileInfo, line,
buffer.Text(), buffer.Bytes(), ""}
s.Buffer = append(s.Buffer, node)
line = line + 1
}
}
return nil
}