forked from sqls-server/sqls
/
database.go
220 lines (201 loc) · 5.49 KB
/
database.go
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package database
import (
"bytes"
"context"
"database/sql"
"errors"
"fmt"
"strings"
"github.com/mikeismert/sqls/dialect"
"github.com/mikeismert/sqls/parser/parseutil"
)
var (
ErrNotImplementation error = errors.New("not implementation")
)
const (
DefaultMaxIdleConns = 10
DefaultMaxOpenConns = 5
)
type DBRepository interface {
Driver() dialect.DatabaseDriver
CurrentDatabase(ctx context.Context) (string, error)
Databases(ctx context.Context) ([]string, error)
CurrentSchema(ctx context.Context) (string, error)
Schemas(ctx context.Context) ([]string, error)
SchemaTables(ctx context.Context) (map[string][]string, error)
DescribeDatabaseTable(ctx context.Context) ([]*ColumnDesc, error)
DescribeDatabaseTableBySchema(ctx context.Context, schemaName string) ([]*ColumnDesc, error)
Exec(ctx context.Context, query string) (sql.Result, error)
Query(ctx context.Context, query string) (*sql.Rows, error)
DescribeForeignKeysBySchema(ctx context.Context, schemaName string) ([]*ForeignKey, error)
}
type DBOption struct {
MaxIdleConns int
MaxOpenConns int
}
type ColumnBase struct {
Schema string
Table string
Name string
}
type ColumnDesc struct {
ColumnBase
Type string
Null string
Key string
Default sql.NullString
Extra string
}
type ForeignKey [][2]*ColumnBase
type fkItemDesc struct {
fkId string
schema string
table string
column string
refTable string
refColumn string
}
func (cd *ColumnDesc) OnelineDesc() string {
items := []string{}
if cd.Type != "" {
items = append(items, "`"+cd.Type+"`")
}
if cd.Key == "YES" {
items = append(items, "PRIMARY KEY")
} else if cd.Key != "" && cd.Key != "NO" {
items = append(items, cd.Key)
}
if cd.Extra != "" {
items = append(items, cd.Extra)
}
return strings.Join(items, " ")
}
func ColumnDoc(tableName string, colDesc *ColumnDesc) string {
buf := new(bytes.Buffer)
fmt.Fprintf(buf, "`%s`.`%s` column", tableName, colDesc.Name)
fmt.Fprintln(buf)
fmt.Fprintln(buf)
fmt.Fprintln(buf, colDesc.OnelineDesc())
return buf.String()
}
func Coalesce(str ...string) string {
for _, s := range str {
if s != "" {
return s
}
}
return ""
}
func TableDoc(tableName string, cols []*ColumnDesc) string {
buf := new(bytes.Buffer)
fmt.Fprintf(buf, "# `%s` table", tableName)
fmt.Fprintln(buf)
fmt.Fprintln(buf)
fmt.Fprintln(buf)
fmt.Fprintln(buf, "| Name | Type | Primary key | Default | Extra |")
fmt.Fprintln(buf, "| :--------------- | :--------------- | :---------------------- | :------------------ | :---------------- |")
for _, col := range cols {
fmt.Fprintf(buf, "| `%s` | `%s` | `%s` | `%s` | %s |", col.Name, col.Type, col.Key, Coalesce(col.Default.String, "-"), col.Extra)
fmt.Fprintln(buf)
}
return buf.String()
}
func SubqueryDoc(name string, views []*parseutil.SubQueryView, dbCache *DBCache) string {
buf := new(bytes.Buffer)
fmt.Fprintf(buf, "%s subquery", name)
fmt.Fprintln(buf)
fmt.Fprintln(buf)
for _, view := range views {
for _, colmun := range view.SubQueryColumns {
if colmun.ColumnName == "*" {
tableCols, ok := dbCache.ColumnDescs(colmun.ParentTable.Name)
if !ok {
continue
}
for _, tableCol := range tableCols {
fmt.Fprintf(buf, "- %s(%s.%s): %s", tableCol.Name, colmun.ParentTable.Name, tableCol.Name, tableCol.OnelineDesc())
fmt.Fprintln(buf)
}
} else {
columnDesc, ok := dbCache.Column(colmun.ParentTable.Name, colmun.ColumnName)
if !ok {
continue
}
fmt.Fprintf(buf, "- %s(%s.%s): %s", colmun.DisplayName(), colmun.ParentTable.Name, colmun.ColumnName, columnDesc.OnelineDesc())
fmt.Fprintln(buf)
}
}
}
return buf.String()
}
func SubqueryColumnDoc(identName string, views []*parseutil.SubQueryView, dbCache *DBCache) string {
buf := new(bytes.Buffer)
fmt.Fprintf(buf, "%s subquery column", identName)
fmt.Fprintln(buf)
fmt.Fprintln(buf)
for _, view := range views {
for _, colmun := range view.SubQueryColumns {
if colmun.ColumnName == "*" {
tableCols, ok := dbCache.ColumnDescs(colmun.ParentTable.Name)
if !ok {
continue
}
for _, tableCol := range tableCols {
if identName == tableCol.Name {
fmt.Fprintf(buf, "- %s(%s.%s): %s", identName, colmun.ParentTable.Name, tableCol.Name, tableCol.OnelineDesc())
fmt.Fprintln(buf)
continue
}
}
} else {
if identName != colmun.ColumnName && identName != colmun.AliasName {
continue
}
columnDesc, ok := dbCache.Column(colmun.ParentTable.Name, colmun.ColumnName)
if !ok {
continue
}
fmt.Fprintf(buf, "- %s(%s.%s): %s", identName, colmun.ParentTable.Name, colmun.ColumnName, columnDesc.OnelineDesc())
fmt.Fprintln(buf)
}
}
}
return buf.String()
}
func parseForeignKeys(rows *sql.Rows, schemaName string) ([]*ForeignKey, error) {
var retVal []*ForeignKey
var prevFk string
var cur *ForeignKey
for rows.Next() {
var fkItem fkItemDesc
err := rows.Scan(
&fkItem.fkId,
&fkItem.table,
&fkItem.column,
&fkItem.refTable,
&fkItem.refColumn,
)
if err != nil {
return nil, err
}
var l, r ColumnBase
l.Schema = schemaName
l.Table = fkItem.table
l.Name = fkItem.column
r.Schema = l.Schema
r.Table = fkItem.refTable
r.Name = fkItem.refColumn
if fkItem.fkId != prevFk {
if cur != nil {
retVal = append(retVal, cur)
}
cur = new(ForeignKey)
}
*cur = append(*cur, [2]*ColumnBase{&l, &r})
prevFk = fkItem.fkId
}
if cur != nil {
retVal = append(retVal, cur)
}
return retVal, nil
}