Skip to content

Commit

Permalink
Use CREATE TABLE for EAV
Browse files Browse the repository at this point in the history
  • Loading branch information
mpchadwick committed Nov 1, 2020
1 parent ce344c6 commit cbb9e24
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
32 changes: 32 additions & 0 deletions src/create_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dbanon

import (
"github.com/blastrain/vitess-sqlparser/sqlparser"
"strings"
)

var nextTable = ""
var currentTable []string

func findNextTable(s string) {
if len(nextTable) > 0 {
// TODO: Are we guaranteed this will delimit the end of the CREATE TABLE?
j := strings.Index(s, "/*!40101")
if j == 0 {
stmt, _ := sqlparser.Parse(nextTable)
currentTable = nil
createTable := stmt.(*sqlparser.CreateTable)
for _, col := range createTable.Columns {
currentTable = append(currentTable, col.Name)
}
nextTable = ""
} else {
nextTable += s
}
}

k := strings.Index(s, "CREATE TABLE")
if k == 0 {
nextTable += s
}
}
6 changes: 4 additions & 2 deletions src/eav.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func (eav Eav) ProcessLine(s string) {
eav.processInsert(s)
return
}

findNextTable(s)
}

func (eav Eav) processInsert (s string) {
Expand All @@ -36,7 +38,7 @@ func (eav Eav) processInsert (s string) {
rows := stmt.Rows.(sqlparser.Values)
for _, vt := range rows {
for i, e := range vt {
column := stmt.Columns[i].String()
column := currentTable[i]
switch v := e.(type) {
case *sqlparser.SQLVal:
if column == "entity_type_id" {
Expand All @@ -55,7 +57,7 @@ func (eav Eav) processInsert (s string) {
rows := stmt.Rows.(sqlparser.Values)
for _, vt := range rows {
for i, e := range vt {
column := stmt.Columns[i].String()
column := currentTable[i]
switch v := e.(type) {
case *sqlparser.SQLVal:
if column == "attribute_id" {
Expand Down
28 changes: 1 addition & 27 deletions src/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ type LineProcessor struct {
Provider ProviderInterface
}

var nextTable = ""
var currentTable []string

func NewLineProcessor(c *Config, p ProviderInterface) *LineProcessor {
return &LineProcessor{Config: c, Provider: p}
}
Expand All @@ -23,34 +20,11 @@ func (p LineProcessor) ProcessLine(s string) string {
return p.processInsert(s)
}

p.findNextTable(s)
findNextTable(s)

return s
}

func (p LineProcessor) findNextTable(s string) {
if len(nextTable) > 0 {
// TODO: Are we guaranteed this will delimit the end of the CREATE TABLE?
j := strings.Index(s, "/*!40101")
if j == 0 {
stmt, _ := sqlparser.Parse(nextTable)
currentTable = nil
createTable := stmt.(*sqlparser.CreateTable)
for _, col := range createTable.Columns {
currentTable = append(currentTable, col.Name)
}
nextTable = ""
} else {
nextTable += s
}
}

k := strings.Index(s, "CREATE TABLE")
if k == 0 {
nextTable += s
}
}

func (p LineProcessor) processInsert(s string) string {
stmt, _ := sqlparser.Parse(s)
insert := stmt.(*sqlparser.Insert)
Expand Down

0 comments on commit cbb9e24

Please sign in to comment.