Skip to content

Commit

Permalink
dialect/sql: add support for table options (#925)
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m committed Nov 6, 2020
1 parent d6702a4 commit 122ee02
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
10 changes: 10 additions & 0 deletions dialect/sql/builder.go
Expand Up @@ -95,6 +95,7 @@ type TableBuilder struct {
exists bool // check existence.
charset string // table charset.
collation string // table collation.
options string // table options.
columns []Querier // table columns.
primary []string // primary key.
constraints []Querier // foreign keys and indices.
Expand Down Expand Up @@ -172,6 +173,12 @@ func (t *TableBuilder) Collate(s string) *TableBuilder {
return t
}

// Options appends additional options to to the statement (MySQL only).
func (t *TableBuilder) Options(s string) *TableBuilder {
t.options = s
return t
}

// Query returns query representation of a `CREATE TABLE` statement.
//
// CREATE TABLE [IF NOT EXISTS] name
Expand Down Expand Up @@ -202,6 +209,9 @@ func (t *TableBuilder) Query() (string, []interface{}) {
if t.collation != "" {
t.WriteString(" COLLATE " + t.collation)
}
if t.options != "" {
t.WriteString(" " + t.options)
}
return t.String(), t.args
}

Expand Down
5 changes: 3 additions & 2 deletions dialect/sql/builder_test.go
Expand Up @@ -59,8 +59,9 @@ func TestBuilder(t *testing.T) {
).
PrimaryKey("id").
Charset("utf8mb4").
Collate("utf8mb4_general_ci"),
wantQuery: "CREATE TABLE `users`(`id` int auto_increment, `name` varchar(255), PRIMARY KEY(`id`)) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci",
Collate("utf8mb4_general_ci").
Options("ENGINE=InnoDB"),
wantQuery: "CREATE TABLE `users`(`id` int auto_increment, `name` varchar(255), PRIMARY KEY(`id`)) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci ENGINE=InnoDB",
},
{
input: CreateTable("users").
Expand Down

0 comments on commit 122ee02

Please sign in to comment.