/
analyze_tables_in_queries.go
56 lines (46 loc) · 1.32 KB
/
analyze_tables_in_queries.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
package repo
import (
"fmt"
"strings"
"github.com/brianbroderick/lantern/pkg/sql/extractor"
)
// addTablesInQueries adds the tables in a query to the Queries struct
func (q *Queries) addTablesInQueries(qu *Query, ext *extractor.Extractor) {
for _, table := range ext.TablesInQueries {
uid := UuidV5(fmt.Sprintf("%s|%s", qu.UID, table.TableUID))
uidStr := uid.String()
if _, ok := q.TablesInQueries[uidStr]; !ok {
q.TablesInQueries[uidStr] = &extractor.TablesInQueries{
UID: uid,
TableUID: table.TableUID,
QueryUID: qu.UID,
Schema: table.Schema,
Name: table.Name,
}
}
}
}
func (q *Queries) UpsertTablesInQueries() {
if len(q.TablesInQueries) == 0 {
return
}
rows := q.insValuesTablesInQueries()
query := fmt.Sprintf(q.insTablesInQueries(), strings.Join(rows, ",\n"))
db := Conn()
defer db.Close()
ExecuteQuery(db, query)
}
func (q *Queries) insTablesInQueries() string {
return `INSERT INTO tables_in_queries (uid, table_uid, query_uid, schema_name, table_name)
VALUES %s
ON CONFLICT (uid) DO NOTHING;`
}
func (q *Queries) insValuesTablesInQueries() []string {
var rows []string
for uid, query := range q.TablesInQueries {
rows = append(rows,
fmt.Sprintf("('%s', '%s', '%s', '%s', '%s')",
uid, query.TableUID, query.QueryUID, query.Schema, query.Name))
}
return rows
}