This repository was archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
sql: plan: Add SHOW CREATE TABLE [Fix #406] #435
Merged
Merged
Changes from 2 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6a7ffc5
sql: plan: Add SHOW CREATE TABLE [Fix #406]
ntakouris 545c49d
Merge branch 'master' into show-create-table
ntakouris a8b819b
sql: plan: Change some things for SHOW CREATE TABLE as per code review
ntakouris 25e441d
Merge remote-tracking branch 'origin/show-create-table' into show-cre…
ntakouris 1980e77
sql: plan: Change SHOW CREATE TABLE to accept a catalog and a databas…
ntakouris e0be8b0
sql: expression: function: Fix parser and some nil panics
ntakouris 1c3ce54
sql: plan: SHOW CREATE TABLE - Use catalog, without database found
ntakouris addd4ae
sql: plan SHOW CREATE TABLE: Implement correct database usage from ca…
ntakouris c3eee19
sql: plan: SHOW CREATE TABLE: Fix iterator to only run once
ntakouris a7bf5f6
sql: parse/plan: Extract show create parser function, refactor SHOW C…
ntakouris 2978206
sql: plan: SHOW CREATE TABLE: Make query generation a bit more readable
ntakouris b4e04f1
sql: parse/plan: Clean imports
ntakouris 7fc4887
Merge branch 'master' into show-create-table
ntakouris a1fcbc3
sql: plan/parse: Fix CI
ntakouris 549bfad
sql: plan/parse: Change to comply with the new catalog api
ntakouris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package plan | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
import ( | ||
"gopkg.in/src-d/go-errors.v1" | ||
"gopkg.in/src-d/go-mysql-server.v0/sql" | ||
) | ||
|
||
var ErrTableNotFound = errors.NewKind("Table `%s` not found") | ||
|
||
// ShowCreateTable is a node that shows the CREATE TABLE statement for a table. | ||
type ShowCreateTable struct { | ||
Database sql.Database | ||
Table string | ||
Registry *sql.IndexRegistry | ||
} | ||
|
||
// Schema implements the Node interface. | ||
func (n *ShowCreateTable) Schema() sql.Schema { | ||
return sql.Schema{ | ||
&sql.Column{Name: "Table", Type: sql.Text, Nullable: false}, | ||
&sql.Column{Name: "Create Table", Type: sql.Text, Nullable: false}, | ||
} | ||
} | ||
|
||
// TransformExpressionsUp implements the Transformable interface. | ||
func (n *ShowCreateTable) TransformExpressionsUp(f sql.TransformExprFunc) (sql.Node, error) { | ||
return n, nil | ||
} | ||
|
||
// TransformUp implements the Transformable interface. | ||
func (n *ShowCreateTable) TransformUp(f sql.TransformNodeFunc) (sql.Node, error) { | ||
return f(NewShowCreateTable(n.Database, n.Table, n.Registry)) | ||
} | ||
|
||
// RowIter implements the Node interface. | ||
func (n *ShowCreateTable) RowIter(*sql.Context) (sql.RowIter, error) { | ||
return &showCreateTablesIter{ | ||
db: n.Database, | ||
table: n.Table, | ||
}, nil | ||
} | ||
|
||
// String implements the Stringer interface. | ||
func (n *ShowCreateTable) String() string { | ||
return fmt.Sprintf("ShowCreateTable(%s)", n.Table) | ||
ntakouris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
type createTableStmt struct { | ||
colName string | ||
colType sql.Type | ||
} | ||
|
||
type showCreateTablesIter struct { | ||
db sql.Database | ||
table string | ||
|
||
createStmt *createTableStmt | ||
} | ||
|
||
func (i *showCreateTablesIter) Next() (sql.Row, error) { | ||
table := i.db.Tables()[i.table] | ||
|
||
if table == nil { | ||
return nil, ErrTableNotFound.New(table) | ||
} | ||
|
||
schema := table.Schema() | ||
ntakouris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
colCreateStatements := make([]string, len(schema), len(schema)) | ||
// Statement creation parts for each column | ||
for indx, col := range schema { | ||
createStmtPart := fmt.Sprintf("`%s` %s", col.Name, col.Type.Type()) | ||
if col.Default != nil { | ||
createStmtPart = fmt.Sprintf("%s DEFAULT %v", createStmtPart, col.Default) | ||
} | ||
|
||
if !col.Nullable { | ||
createStmtPart = fmt.Sprintf("%sNOT NULL", createStmtPart) | ||
ntakouris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if indx != len(schema)-1 { | ||
ntakouris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
colCreateStatements[indx] = createStmtPart + ",\n" | ||
continue | ||
} | ||
|
||
colCreateStatements[indx] = createStmtPart | ||
} | ||
|
||
prettyColCreateStmts := fmt.Sprintf("%s", stripBrackets(colCreateStatements)) | ||
|
||
composedCreateTableStatement := | ||
fmt.Sprintf("CREATE TABLE `%s` (%s) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", i.table, prettyColCreateStmts) | ||
|
||
return sql.NewRow( | ||
i.table, // "Table" string | ||
composedCreateTableStatement, // "Create Table" string | ||
), nil | ||
} | ||
|
||
func stripBrackets(val interface{}) string { | ||
return strings.Trim(fmt.Sprintf("%s", val), "[]") | ||
} | ||
|
||
func (i *showCreateTablesIter) Close() error { | ||
return nil | ||
} | ||
|
||
// NewShowCreateTable creates a new ShowCreateTable node. | ||
func NewShowCreateTable(db sql.Database, table string, registry *sql.IndexRegistry) sql.Node { | ||
return &ShowCreateTable{db, table, registry} | ||
} | ||
|
||
// Resolved implements the Resolvable interface. | ||
func (n *ShowCreateTable) Resolved() bool { | ||
_, ok := n.Database.(*sql.UnresolvedDatabase) | ||
return !ok | ||
} | ||
|
||
// Children implements the Node interface. | ||
func (n *ShowCreateTable) Children() []sql.Node { return nil } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package plan | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"gopkg.in/src-d/go-mysql-server.v0/mem" | ||
"gopkg.in/src-d/go-mysql-server.v0/sql" | ||
"testing" | ||
) | ||
|
||
func TestShowCreateTable(t *testing.T) { | ||
var require = require.New(t) | ||
|
||
db := mem.NewDatabase("test") | ||
|
||
table := mem.NewTable( | ||
"test-table", | ||
sql.Schema{ | ||
&sql.Column{Name: "baz", Type: sql.Text, Default: "", Nullable: false}, | ||
&sql.Column{Name: "zab", Type: sql.Int32, Default: int32(0), Nullable: true}, | ||
&sql.Column{Name: "bza", Type: sql.Int64, Default: int64(0), Nullable: true}, | ||
}) | ||
|
||
db.AddTable(table.Name(), table) | ||
|
||
showCreateTable := NewShowCreateTable(db, table.Name(), sql.NewIndexRegistry()) | ||
|
||
ctx := sql.NewEmptyContext() | ||
rowIter, _ := showCreateTable.RowIter(ctx) | ||
|
||
row, err := rowIter.Next() | ||
|
||
require.Nil(err) | ||
|
||
expected := sql.NewRow( | ||
table.Name(), | ||
"CREATE TABLE `test-table` (`baz` TEXT DEFAULT NOT NULL,\n"+ | ||
" `zab` INT32 DEFAULT 0,\n"+ | ||
" `bza` INT64 DEFAULT 0) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", | ||
) | ||
|
||
require.Equal(expected, row) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.