Skip to content

Commit

Permalink
Merge pull request #22 from ken39arg/hotfix/table-option-with-quote
Browse files Browse the repository at this point in the history
Hotfix/table option with quote
  • Loading branch information
soh335 committed Feb 15, 2017
2 parents c490cc1 + 81d16c0 commit 7defa95
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 17 deletions.
4 changes: 2 additions & 2 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func TestDiff(t *testing.T) {
// create table
{
Before: "CREATE TABLE `fuga` ( `id` INTEGER NOT NULL );",
After: "CREATE TABLE `hoge` ( `id` INTEGER NOT NULL ) ENGINE=InnoDB; CREATE TABLE `fuga` ( `id` INTEGER NOT NULL );",
Expect: "CREATE TABLE `hoge` (\n`id` INTEGER NOT NULL\n) ENGINE = InnoDB;",
After: "CREATE TABLE `hoge` ( `id` INTEGER NOT NULL ) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COMMENT 'table comment'; CREATE TABLE `fuga` ( `id` INTEGER NOT NULL );",
Expect: "CREATE TABLE `hoge` (\n`id` INTEGER NOT NULL\n) ENGINE = InnoDB, DEFAULT CHARACTER SET = utf8mb4, COMMENT = 'table comment';",
},
// drop column
{
Expand Down
8 changes: 7 additions & 1 deletion format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ func formatTableOption(dst io.Writer, option model.TableOption) error {
var buf bytes.Buffer
buf.WriteString(option.Key())
buf.WriteString(" = ")
buf.WriteString(option.Value())
if option.NeedQuotes() {
buf.WriteByte('\'')
buf.WriteString(option.Value())
buf.WriteByte('\'')
} else {
buf.WriteString(option.Value())
}

if _, err := buf.WriteTo(dst); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion format/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestFormat(t *testing.T) {
col.SetType(model.ColumnTypeInt)
table.AddColumn(col)

opt := model.NewTableOption("ENGINE", "InnoDB")
opt := model.NewTableOption("ENGINE", "InnoDB", false)
table.AddOption(opt)

index := model.NewIndex(model.IndexKindPrimaryKey)
Expand Down
6 changes: 4 additions & 2 deletions model/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ type TableOption interface {
Stmt
Key() string
Value() string
NeedQuotes() bool
}

type table struct {
Expand All @@ -165,8 +166,9 @@ type table struct {
}

type tableopt struct {
key string
value string
key string
value string
needQuotes bool
}

// NullState describes the possible NULL constraint of a column
Expand Down
16 changes: 9 additions & 7 deletions model/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,16 @@ func (t *table) Options() chan TableOption {
return ch
}

// NewTableOption creates a new table option with the given name and value
func NewTableOption(k, v string) TableOption {
// NewTableOption creates a new table option with the given name, value, and a flag indicating if quoting is necessary
func NewTableOption(k, v string, q bool) TableOption {
return &tableopt{
key: k,
value: v,
key: k,
value: v,
needQuotes: q,
}
}

func (t *tableopt) ID() string { return "tableopt#" + t.key }
func (t *tableopt) Key() string { return t.key }
func (t *tableopt) Value() string { return t.value }
func (t *tableopt) ID() string { return "tableopt#" + t.key }
func (t *tableopt) Key() string { return t.key }
func (t *tableopt) Value() string { return t.value }
func (t *tableopt) NeedQuotes() bool { return t.needQuotes }
7 changes: 6 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,12 @@ func (p *Parser) parseCreateTableOptionValue(ctx *parseCtx, table model.Table, n
if typ != t.Type {
continue
}
table.AddOption(model.NewTableOption(name, t.Value))
var quotes bool
switch t.Type {
case SINGLE_QUOTE_IDENT, DOUBLE_QUOTE_IDENT:
quotes = true
}
table.AddOption(model.NewTableOption(name, t.Value, quotes))
return nil
}
return newParseError(ctx, t, "expected %v", follow)
Expand Down
6 changes: 3 additions & 3 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ primary key (id, c)
{
Input: `create table hoge (
id bigint unsigned not null auto_increment
) ENGINE=InnoDB AUTO_INCREMENT 10 DEFAULT CHARACTER SET = utf8;
) ENGINE=InnoDB AUTO_INCREMENT 10 DEFAULT CHARACTER SET = utf8 COMMENT = 'hoge comment';
`,
Error: false,
Expect: "CREATE TABLE `hoge` (\n`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT\n) ENGINE = InnoDB, AUTO_INCREMENT = 10, DEFAULT CHARACTER SET = utf8",
Expect: "CREATE TABLE `hoge` (\n`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT\n) ENGINE = InnoDB, AUTO_INCREMENT = 10, DEFAULT CHARACTER SET = utf8, COMMENT = 'hoge comment'",
},
// with key, index
{
Expand Down Expand Up @@ -185,7 +185,7 @@ id bigint unsigned not null auto_increment
Expect: "CREATE TABLE `fuga` (\n`id` INTEGER NOT NULL AUTO_INCREMENT,\nCONSTRAINT `symbol` UNIQUE INDEX `uniq_id` USING BTREE (`id`)\n)",
},
{
Input: "DROP TABLE IF EXISTS `konboi_bug`; CREATE TABLE foo(`id` INT)",
Input: "DROP TABLE IF EXISTS `konboi_bug`; CREATE TABLE foo(`id` INT)",
Expect: "CREATE TABLE `foo` (\n`id` INT\n)",
},
}
Expand Down

0 comments on commit 7defa95

Please sign in to comment.