Skip to content

Commit 82ec465

Browse files
authored
Merge pull request #2 from donatj/feature/ShowCreateTable
Adds SHOW CREATE TABLE Support
2 parents 798b0f9 + 2e29816 commit 82ec465

8 files changed

Lines changed: 211 additions & 49 deletions

File tree

cmd/sqlread/main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"os"
99
"path/filepath"
1010

11-
"github.com/avvmoto/buf-readerat"
11+
bufra "github.com/avvmoto/buf-readerat"
1212
"github.com/donatj/sqlread"
1313
"github.com/donatj/sqlread/mapcache"
1414
)
@@ -129,6 +129,12 @@ func interactive(tree sqlread.SummaryTree, buff io.ReaderAt) {
129129
}
130130
}
131131

132+
for _, sctbl := range qp.Tree.ShowCreateTables {
133+
if err := showCreateTable(tree, sctbl, buff, w); err != nil {
134+
log.Println(err)
135+
}
136+
}
137+
132138
if qp.Tree.Quit {
133139
return
134140
}

cmd/sqlread/query.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,31 @@ func showColumns(tree sqlread.SummaryTree, sctbl string, w DataWriter) error {
7777
return fmt.Errorf("table `%s` not found", sctbl)
7878
}
7979
for _, col := range tbl.Cols {
80-
w.Write([]string{col.Name, col.Type})
80+
err := w.Write([]string{col.Name, col.Type})
81+
if err != nil {
82+
return err
83+
}
84+
}
85+
w.Flush()
86+
87+
return nil
88+
}
89+
90+
func showCreateTable(tree sqlread.SummaryTree, sctbl string, buff io.ReaderAt, w DataWriter) error {
91+
tbl, tok := tree[sctbl]
92+
if !tok {
93+
return fmt.Errorf("table `%s` not found", sctbl)
94+
}
95+
96+
data := make([]byte, tbl.End.Pos-tbl.Start.Pos)
97+
_, err := buff.ReadAt(data, tbl.Start.Pos)
98+
if err != nil {
99+
return err
100+
}
101+
102+
err = w.Write([]string{string(data)})
103+
if err != nil {
104+
return err
81105
}
82106
w.Flush()
83107

interpreter.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ func (intp *Intp) StartIntpState(l *lexer) state {
2626
if l.hasPrefixI("COLUMNS") {
2727
return intp.showColumnsIntpState
2828
}
29+
if l.hasPrefixI("CREATE") {
30+
return intp.showCreateTableIntpState
31+
}
2932

3033
l.emit(TIllegal)
3134
return nil
@@ -42,6 +45,50 @@ func (intp *Intp) StartIntpState(l *lexer) state {
4245
return nil
4346
}
4447

48+
func (intp *Intp) showCreateTableIntpState(l *lexer) state {
49+
l.pos += 6
50+
51+
if l.accept(whitespace) < 1 {
52+
l.emit(TIllegal)
53+
return nil
54+
}
55+
56+
l.start = l.pos
57+
58+
if !l.hasPrefixI("TABLE") {
59+
l.emit(TIllegal)
60+
return nil
61+
}
62+
l.pos += 5
63+
l.emit(TIntpShowCreateTable)
64+
// TODO emit non-existing T_TABLE?
65+
66+
if l.accept(whitespace) < 1 {
67+
l.emit(TIllegal)
68+
return nil
69+
}
70+
71+
l.start = l.pos
72+
73+
if eatIdentifier(l) {
74+
l.emit(TIdentifier)
75+
} else {
76+
l.emit(TIllegal)
77+
return nil
78+
}
79+
80+
l.accept(whitespace)
81+
l.start = l.pos
82+
83+
c := l.next()
84+
if c != semi {
85+
l.emit(TIllegal)
86+
return nil
87+
}
88+
89+
return nil
90+
}
91+
4592
func (intp *Intp) showColumnsIntpState(l *lexer) state {
4693
l.pos += 7
4794
l.emit(TIntpShowColumns)

lexitemtype_string.go

Lines changed: 41 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mapcache/cache.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import (
88
"github.com/donatj/sqlread"
99
)
1010

11+
// CacheVersion is incremented when the structure of the cache changes
12+
// such that it is no longer compatible.
13+
const CacheVersion = 1
14+
1115
type MapCache struct {
1216
sqlfile *os.File
1317
}
@@ -19,6 +23,7 @@ func New(sqlfile *os.File) *MapCache {
1923
}
2024

2125
var (
26+
// ErrCacheMiss is an error when a valid cache is not found
2227
ErrCacheMiss = errors.New("cache miss")
2328
)
2429

@@ -43,6 +48,10 @@ func (m *MapCache) Get() (sqlread.SummaryTree, error) {
4348
return nil, ErrCacheMiss
4449
}
4550

51+
if v.Version != CacheVersion {
52+
return nil, ErrCacheMiss
53+
}
54+
4655
return v.Tree, nil
4756
}
4857

@@ -55,6 +64,7 @@ func (m *MapCache) Store(s sqlread.SummaryTree) error {
5564

5665
e := json.NewEncoder(cf)
5766
err = e.Encode(cacheInfo{
67+
Version: CacheVersion,
5868
FileSize: m.getFileSize(),
5969
Tree: s,
6070
})
@@ -80,6 +90,7 @@ func (m *MapCache) getFileSize() int64 {
8090
}
8191

8292
type cacheInfo struct {
93+
Version int
8394
FileSize int64
8495
Tree sqlread.SummaryTree
8596
}

query.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ type Query struct {
77
}
88

99
type QueryTree struct {
10-
Queries []Query
11-
ShowTables uint
12-
ShowColumns []string
13-
Quit bool
10+
Queries []Query
11+
12+
ShowTables uint
13+
ShowColumns []string
14+
ShowCreateTables []string
15+
16+
Quit bool
1417
}
1518

1619
type QueryParser struct {
@@ -47,6 +50,10 @@ func (q *QueryParser) ParseStart(p *Parser) parseState {
4750
return q.parseShowColumns
4851
}
4952

53+
if isOfAny(c, TIntpShowCreateTable) {
54+
return q.parseShowCreateTable
55+
}
56+
5057
if isOfAny(c, TIntpQuit) {
5158
return q.parseQuit
5259
}
@@ -107,7 +114,7 @@ func (q *QueryParser) parseShowColumns(p *Parser) parseState {
107114
return nil
108115
}
109116
if !isOfAny(y, TIdentifier) {
110-
p.errorUnexpectedLex(x, TIdentifier)
117+
p.errorUnexpectedLex(y, TIdentifier)
111118
return nil
112119
}
113120

@@ -116,6 +123,22 @@ func (q *QueryParser) parseShowColumns(p *Parser) parseState {
116123
return q.ParseStart
117124
}
118125

126+
func (q *QueryParser) parseShowCreateTable(p *Parser) parseState {
127+
y, ok := p.scan()
128+
if !ok {
129+
p.errorUnexpectedEOF()
130+
return nil
131+
}
132+
if !isOfAny(y, TIdentifier) {
133+
p.errorUnexpectedLex(y, TIdentifier)
134+
return nil
135+
}
136+
137+
q.Tree.ShowCreateTables = append(q.Tree.ShowCreateTables, identValue(y))
138+
139+
return q.ParseStart
140+
}
141+
119142
func (q *QueryParser) parseSelect(p *Parser) parseState {
120143
qry := &Query{
121144
Columns: make([]string, 0),

scanner.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const (
5151

5252
TIntpShowTables
5353
TIntpShowColumns
54+
TIntpShowCreateTable
5455

5556
TIntpQuit
5657
)

0 commit comments

Comments
 (0)