From a3122b0074718168c9f8bded2ac37c7ea3504a8a Mon Sep 17 00:00:00 2001 From: Max Chadwick Date: Fri, 30 Oct 2020 22:01:44 -0400 Subject: [PATCH] Parse CREATE TABLE statements --- src/processor.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/processor.go b/src/processor.go index f423824..ce17f3c 100644 --- a/src/processor.go +++ b/src/processor.go @@ -8,10 +8,12 @@ import ( type LineProcessor struct { Config *Config Provider ProviderInterface + nextTable string + currentTable sqlparser.Statement } func NewLineProcessor(c *Config, p ProviderInterface) *LineProcessor { - return &LineProcessor{Config: c, Provider: p} + return &LineProcessor{Config: c, Provider: p, nextTable: ""} } func (p LineProcessor) ProcessLine(s string) string { @@ -20,9 +22,30 @@ func (p LineProcessor) ProcessLine(s string) string { return p.processInsert(s) } + p.findNextTable(s) + return s } +func (p LineProcessor) findNextTable(s string) { + if len(p.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(p.nextTable) + p.currentTable = stmt + p.nextTable = "" + } else { + p.nextTable += s + } + } + + k := strings.Index(s, "CREATE TABLE") + if k == 0 { + p.nextTable += s + } +} + func (p LineProcessor) processInsert(s string) string { stmt, _ := sqlparser.Parse(s) insert := stmt.(*sqlparser.Insert)