Skip to content

Instantly share code, notes, and snippets.

@BohuTANG
Created March 2, 2017 01:16
Show Gist options
  • Save BohuTANG/c64d253bb715c96ee50142b7d02d434a to your computer and use it in GitHub Desktop.
Save BohuTANG/c64d253bb715c96ee50142b7d02d434a to your computer and use it in GitHub Desktop.
ddl.diff
diff --git a/src/vendor/github.com/XeLabs/go-mysqlstack/sqlparser/ast.go b/src/vendor/github.com/XeLabs/go-mysqlstack/sqlparser/ast.go
index 5a68968..14e60a8 100644
--- a/src/vendor/github.com/XeLabs/go-mysqlstack/sqlparser/ast.go
+++ b/src/vendor/github.com/XeLabs/go-mysqlstack/sqlparser/ast.go
@@ -381,6 +381,35 @@ func (node *Set) WalkSubtree(visit Visit) error {
)
}
+/*********************************************/
+type DDLType int
+
+const (
+ DDLTypeTable DDLType = iota
+ DDLTypeDatabase
+ DDLTypeIndex
+)
+
+type DataType int
+
+const (
+ DataTypeInt DataType = iota
+ DataTypeVarchar
+)
+
+type ColumnDef struct {
+ Name string
+ NotNull bool
+ Primary bool
+ Unique bool
+ Type DataType
+ VarLen ValExpr
+}
+
+type ColumnDefs []ColumnDef
+
+/*********************************************/
+
// DDL represents a CREATE, ALTER, DROP or RENAME statement.
// Database is set for new database name
// Table is set for AlterStr, DropStr, RenameStr.
@@ -391,6 +420,10 @@ type DDL struct {
Table TableIdent
NewName TableIdent
IfExists bool
+
+ // add
+ Type DDLType
+ Columns ColumnDefs
}
// DDL strings.
diff --git a/src/vendor/github.com/XeLabs/go-mysqlstack/sqlparser/sql.y b/src/vendor/github.com/XeLabs/go-mysqlstack/sqlparser/sql.y
index 7e6d4fa..a64a833 100644
--- a/src/vendor/github.com/XeLabs/go-mysqlstack/sqlparser/sql.y
+++ b/src/vendor/github.com/XeLabs/go-mysqlstack/sqlparser/sql.y
@@ -42,6 +42,8 @@ func forceEOF(yylex interface{}) {
selectExprs SelectExprs
selectExpr SelectExpr
columns Columns
+ columnDef ColumnDef
+ columnDefs ColumnDefs
colName *ColName
tableExprs TableExprs
tableExpr TableExpr
@@ -68,6 +70,8 @@ func forceEOF(yylex interface{}) {
colIdent ColIdent
colIdents []ColIdent
tableIdent TableIdent
+ uval uint
+ bval bool
}
%token LEX_ERROR
@@ -110,6 +114,10 @@ func forceEOF(yylex interface{}) {
%token <empty> CREATE ALTER DROP RENAME ANALYZE
%token <empty> TABLE INDEX VIEW TO IGNORE IF UNIQUE USING
%token <empty> SHOW DESCRIBE EXPLAIN XA
+/*****************************************************/
+%token <uval> INT VARCHAR
+%token <empty> PRIMARY
+/*****************************************************/
// Functions
%token <empty> CURRENT_TIMESTAMP DATABASE
@@ -172,6 +180,14 @@ func forceEOF(yylex interface{}) {
%type <empty> as_opt
%type <empty> force_eof
+/**************************************/
+%type <uval> column_type
+%type <columnDef> column_def
+%type <columnDefs> column_def_commalist
+%type <valExpr> opt_column_width
+%type <bval> opt_notnull opt_primary opt_unique
+/**************************************/
+
%start any_command
%%
@@ -248,9 +264,9 @@ set_statement:
}
create_statement:
- CREATE TABLE not_exists_opt table_id force_eof
+ CREATE TABLE not_exists_opt table_id openb column_def_commalist closeb force_eof
{
- $$ = &DDL{Action: CreateStr, Table: $4, NewName: $4}
+ $$ = &DDL{Type: DDLTypeTable, Table: $4, Columns:$6}
}
| CREATE DATABASE table_id
@@ -267,6 +283,60 @@ create_statement:
$$ = &DDL{Action: CreateStr, NewName: NewTableIdent($3.Lowered())}
}
+/****************************************************************/
+column_def_commalist:
+ column_def
+ {
+ $$ = ColumnDefs{$1}
+ }
+| column_def_commalist ',' column_def
+ {
+ $$ = append($$, $3)
+ }
+
+column_def:
+ ID column_type opt_column_width opt_notnull opt_primary opt_unique
+ {
+ $$ = ColumnDef{Name: string($1), Type: DataType($2), VarLen: $3, NotNull: $4, Primary: $5, Unique: $6}
+ }
+
+column_type:
+ INT
+ {
+ $$ = uint(DataTypeInt)
+ }
+| VARCHAR
+ {
+ $$ = uint(DataTypeVarchar)
+ }
+
+opt_column_width:
+ '(' INTEGRAL ')'
+ {
+ $$ = NewIntVal($2)
+ }
+ | /* empty */
+ {
+ $$ = nil
+ }
+
+opt_notnull:
+ NOT NULL { $$ = true; }
+ | /* empty */ { $$ = false; }
+ ;
+
+opt_primary:
+ PRIMARY KEY { $$ = true; }
+ | /* empty */ { $$ = false; }
+ ;
+
+opt_unique:
+ UNIQUE { $$ = true; }
+ | /* empty */ { $$ = false; }
+ ;
+/****************************************************************/
+
+
alter_statement:
ALTER ignore_opt TABLE table_id non_rename_operation force_eof
{
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment