Skip to content

Commit

Permalink
Change how to identify an index
Browse files Browse the repository at this point in the history
  • Loading branch information
lestrrat authored and ken39arg committed Feb 17, 2017
1 parent 3845aae commit 49de041
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion format/format_test.go
Expand Up @@ -22,7 +22,7 @@ func TestFormat(t *testing.T) {
opt := model.NewTableOption("ENGINE", "InnoDB", false)
table.AddOption(opt)

index := model.NewIndex(model.IndexKindPrimaryKey)
index := model.NewIndex(model.IndexKindPrimaryKey, table.ID())
index.SetName("hoge_pk")
index.AddColumns("fuga")
table.AddIndex(index)
Expand Down
20 changes: 15 additions & 5 deletions model/index.go
Expand Up @@ -3,26 +3,36 @@ package model
import (
"crypto/sha256"
"fmt"
"io"
)

// NewIndex creates a new index with the given index kind.
func NewIndex(kind IndexKind) Index {
func NewIndex(kind IndexKind, table string) Index {
return &index{
kind: kind,
kind: kind,
table: table,
}
}

func (stmt *index) ID() string {
// This is tricky. and index may or may not have a name. It would
// have been so much easier if we did, but we don't, so we'll fake
// something
// something.
//
// In case we don't have a name, we need to know the table, the kind,
// the type, // the column(s), and the reference(s).
name := "index"
if stmt.HasName() {
name = name + "#" + stmt.Name()
}
h := sha256.New()
io.WriteString(h, fmt.Sprintf("%v, %v, %v, %v, %v, %v", stmt.symbol, stmt.kind, stmt.name, stmt.typ, stmt.columns, stmt.reference))
fmt.Fprintf(h,
"%s.%s.%s.%v.%s",
stmt.table,
stmt.kind,
stmt.typ,
stmt.columns,
stmt.reference,
)
return fmt.Sprintf("%s#%x", name, h.Sum(nil))
}

Expand Down
1 change: 1 addition & 0 deletions model/interface.go
Expand Up @@ -73,6 +73,7 @@ type index struct {
kind IndexKind
name maybeString
typ IndexType
table string
columns []string
// TODO Options.
reference Reference
Expand Down
18 changes: 9 additions & 9 deletions parser.go
Expand Up @@ -360,17 +360,17 @@ func (p *Parser) parseTableConstraint(ctx *parseCtx, table model.Table) error {
var index model.Index
switch t := ctx.peek(); t.Type {
case PRIMARY:
index = model.NewIndex(model.IndexKindPrimaryKey)
index = model.NewIndex(model.IndexKindPrimaryKey, table.ID())
if err := p.parseColumnIndexPrimaryKey(ctx, index); err != nil {
return err
}
case UNIQUE:
index = model.NewIndex(model.IndexKindUnique)
index = model.NewIndex(model.IndexKindUnique, table.ID())
if err := p.parseColumnIndexUniqueKey(ctx, index); err != nil {
return err
}
case FOREIGN:
index = model.NewIndex(model.IndexKindForeignKey)
index = model.NewIndex(model.IndexKindForeignKey, table.ID())
if err := p.parseColumnIndexForeignKey(ctx, index); err != nil {
return err
}
Expand All @@ -387,7 +387,7 @@ func (p *Parser) parseTableConstraint(ctx *parseCtx, table model.Table) error {
}

func (p *Parser) parseTablePrimaryKey(ctx *parseCtx, table model.Table) error {
index := model.NewIndex(model.IndexKindPrimaryKey)
index := model.NewIndex(model.IndexKindPrimaryKey, table.ID())
if err := p.parseColumnIndexPrimaryKey(ctx, index); err != nil {
return err
}
Expand All @@ -396,7 +396,7 @@ func (p *Parser) parseTablePrimaryKey(ctx *parseCtx, table model.Table) error {
}

func (p *Parser) parseTableUniqueKey(ctx *parseCtx, table model.Table) error {
index := model.NewIndex(model.IndexKindUnique)
index := model.NewIndex(model.IndexKindUnique, table.ID())
if err := p.parseColumnIndexUniqueKey(ctx, index); err != nil {
return err
}
Expand All @@ -405,7 +405,7 @@ func (p *Parser) parseTableUniqueKey(ctx *parseCtx, table model.Table) error {
}

func (p *Parser) parseTableIndex(ctx *parseCtx, table model.Table) error {
index := model.NewIndex(model.IndexKindNormal)
index := model.NewIndex(model.IndexKindNormal, table.ID())
if err := p.parseColumnIndexKey(ctx, index); err != nil {
return err
}
Expand All @@ -414,7 +414,7 @@ func (p *Parser) parseTableIndex(ctx *parseCtx, table model.Table) error {
}

func (p *Parser) parseTableFulltextIndex(ctx *parseCtx, table model.Table) error {
index := model.NewIndex(model.IndexKindFullText)
index := model.NewIndex(model.IndexKindFullText, table.ID())
if err := p.parseColumnIndexFullTextKey(ctx, index); err != nil {
return err
}
Expand All @@ -423,7 +423,7 @@ func (p *Parser) parseTableFulltextIndex(ctx *parseCtx, table model.Table) error
}

func (p *Parser) parseTableSpatialIndex(ctx *parseCtx, table model.Table) error {
index := model.NewIndex(model.IndexKindSpatial)
index := model.NewIndex(model.IndexKindSpatial, table.ID())
if err := p.parseColumnIndexSpatialKey(ctx, index); err != nil {
return err
}
Expand All @@ -432,7 +432,7 @@ func (p *Parser) parseTableSpatialIndex(ctx *parseCtx, table model.Table) error
}

func (p *Parser) parseTableForeignKey(ctx *parseCtx, table model.Table) error {
index := model.NewIndex(model.IndexKindForeignKey)
index := model.NewIndex(model.IndexKindForeignKey, table.ID())
if err := p.parseColumnIndexForeignKey(ctx, index); err != nil {
return err
}
Expand Down

0 comments on commit 49de041

Please sign in to comment.