Skip to content

Commit

Permalink
Extract the max length
Browse files Browse the repository at this point in the history
Also validate the INSERT
  • Loading branch information
mpchadwick committed Dec 27, 2020
1 parent f3549bd commit 4e13946
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
30 changes: 25 additions & 5 deletions src/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package dbanon

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

type Column struct {
Name string
Type string
Name string
Type string
MaxLength int
}

func NewColumn(n string, t string) *Column {
return &Column{Name: n, Type: t}
func NewColumn(n string, t string, i int) *Column {
return &Column{Name: n, Type: t, MaxLength: i}
}

var nextTable = ""
Expand All @@ -26,7 +28,7 @@ func findNextTable(s string) {
currentTable = nil
createTable := stmt.(*sqlparser.CreateTable)
for _, col := range createTable.Columns {
column := NewColumn(col.Name, col.Type)
column := NewColumn(col.Name, col.Type, extractMaxLength(col.Type))
currentTable = append(currentTable, column)
}
nextTable = ""
Expand All @@ -40,3 +42,21 @@ func findNextTable(s string) {
nextTable += s
}
}

func extractMaxLength(s string) int {
s = strings.ToLower(s)
// For now we'll only worry about VARCHAR...I've never heard of
// this being needed in practice anyway...
j := strings.Index(s, "varchar")
if j != 0 {
return -1
}

lenStart := strings.Index(s, "(")
lenEnd := strings.Index(s, ")")

len := s[lenStart+1 : lenEnd]
i, _ := strconv.Atoi(len)

return i
}
9 changes: 8 additions & 1 deletion src/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ func (p LineProcessor) ProcessLine(s string) string {

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

// This _shouldn't happen but the statement might not be an Insert
// For example, it'll be nil if the binary charset introducer is foudn
// https://github.com/blastrain/vitess-sqlparser/issues/25
if !ok {
return s
}

table := insert.Table.Name.String()

Expand Down

0 comments on commit 4e13946

Please sign in to comment.