Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dialect/sql: add support for table options #925

Merged
merged 1 commit into from Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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