@@ -45,9 +45,13 @@ type AlterIndex struct {
4545 // Action states whether it's a CREATE, DROP, or RENAME
4646 Action IndexAction
4747 // ddlNode references to the database that is being operated on
48- ddlNode
48+ Db sql. Database
4949 // Table is the table that is being referenced
5050 Table sql.TableNode
51+ // IfExists indicates if we should error when deleting an index that doesn't exist
52+ IfExists bool
53+ // IfNotExists indicates if we should error when creating a duplicate index
54+ IfNotExists bool
5155 // IndexName is the index name, and in the case of a RENAME it represents the new name
5256 IndexName string
5357 // PreviousIndexName states the old name when renaming an index
@@ -72,32 +76,34 @@ var _ sql.Expressioner = (*AlterIndex)(nil)
7276var _ sql.Node = (* AlterIndex )(nil )
7377var _ sql.CollationCoercible = (* AlterIndex )(nil )
7478
75- func NewAlterCreateIndex (db sql.Database , table sql.TableNode , indexName string , using sql.IndexUsing , constraint sql.IndexConstraint , columns []sql.IndexColumn , comment string ) * AlterIndex {
79+ func NewAlterCreateIndex (db sql.Database , table sql.TableNode , ifNotExists bool , indexName string , using sql.IndexUsing , constraint sql.IndexConstraint , columns []sql.IndexColumn , comment string ) * AlterIndex {
7680 return & AlterIndex {
77- Action : IndexAction_Create ,
78- ddlNode : ddlNode {Db : db },
79- Table : table ,
80- IndexName : indexName ,
81- Using : using ,
82- Constraint : constraint ,
83- Columns : columns ,
84- Comment : comment ,
81+ Action : IndexAction_Create ,
82+ Db : db ,
83+ Table : table ,
84+ IfNotExists : ifNotExists ,
85+ IndexName : indexName ,
86+ Using : using ,
87+ Constraint : constraint ,
88+ Columns : columns ,
89+ Comment : comment ,
8590 }
8691}
8792
88- func NewAlterDropIndex (db sql.Database , table sql.TableNode , indexName string ) * AlterIndex {
93+ func NewAlterDropIndex (db sql.Database , table sql.TableNode , ifExists bool , indexName string ) * AlterIndex {
8994 return & AlterIndex {
9095 Action : IndexAction_Drop ,
91- ddlNode : ddlNode { Db : db } ,
96+ Db : db ,
9297 Table : table ,
98+ IfExists : ifExists ,
9399 IndexName : indexName ,
94100 }
95101}
96102
97103func NewAlterRenameIndex (db sql.Database , table sql.TableNode , fromIndexName , toIndexName string ) * AlterIndex {
98104 return & AlterIndex {
99105 Action : IndexAction_Rename ,
100- ddlNode : ddlNode { Db : db } ,
106+ Db : db ,
101107 Table : table ,
102108 IndexName : toIndexName ,
103109 PreviousIndexName : fromIndexName ,
@@ -107,7 +113,7 @@ func NewAlterRenameIndex(db sql.Database, table sql.TableNode, fromIndexName, to
107113func NewAlterDisableEnableKeys (db sql.Database , table sql.TableNode , disableKeys bool ) * AlterIndex {
108114 return & AlterIndex {
109115 Action : IndexAction_DisableEnableKeys ,
110- ddlNode : ddlNode { Db : db } ,
116+ Db : db ,
111117 Table : table ,
112118 DisableKeys : disableKeys ,
113119 }
@@ -120,7 +126,7 @@ func (p *AlterIndex) Schema() sql.Schema {
120126
121127// WithChildren implements the Node interface. For AlterIndex, the only appropriate input is
122128// a single child - The Table.
123- func (p AlterIndex ) WithChildren (children ... sql.Node ) (sql.Node , error ) {
129+ func (p * AlterIndex ) WithChildren (children ... sql.Node ) (sql.Node , error ) {
124130 if len (children ) != 1 {
125131 return nil , sql .ErrInvalidChildrenNumber .New (p , len (children ), 1 )
126132 }
@@ -131,21 +137,24 @@ func (p AlterIndex) WithChildren(children ...sql.Node) (sql.Node, error) {
131137 }
132138 switch p .Action {
133139 case IndexAction_Create , IndexAction_Drop , IndexAction_Rename , IndexAction_DisableEnableKeys :
134- p .Table = child
135- return & p , nil
140+ np := * p
141+ np .Table = child
142+ return & np , nil
136143 default :
137144 return nil , ErrIndexActionNotImplemented .New (p .Action )
138145 }
139146}
140147
141- func (p AlterIndex ) WithColumns (columns []sql.IndexColumn ) (sql.Node , error ) {
142- p .Columns = columns
143- return & p , nil
148+ func (p * AlterIndex ) WithColumns (columns []sql.IndexColumn ) (sql.Node , error ) {
149+ np := * p
150+ np .Columns = columns
151+ return & np , nil
144152}
145153
146- func (p AlterIndex ) WithTargetSchema (schema sql.Schema ) (sql.Node , error ) {
147- p .targetSchema = schema
148- return & p , nil
154+ func (p * AlterIndex ) WithTargetSchema (schema sql.Schema ) (sql.Node , error ) {
155+ np := * p
156+ np .targetSchema = schema
157+ return & np , nil
149158}
150159
151160func (p * AlterIndex ) TargetSchema () sql.Schema {
@@ -164,7 +173,7 @@ func (p *AlterIndex) Expressions() []sql.Expression {
164173
165174// WithExpressions implements the Node Interface. For AlterIndex, expressions represent column defaults on the
166175// targetSchema instance - required to be the same number of columns on the target schema.
167- func (p AlterIndex ) WithExpressions (expressions ... sql.Expression ) (sql.Node , error ) {
176+ func (p * AlterIndex ) WithExpressions (expressions ... sql.Expression ) (sql.Node , error ) {
168177 columns := p .TargetSchema ().Copy ()
169178
170179 if len (columns ) != len (expressions ) {
@@ -209,7 +218,7 @@ func (p *AlterIndex) WithDatabase(database sql.Database) (sql.Node, error) {
209218 return & np , nil
210219}
211220
212- func (p AlterIndex ) String () string {
221+ func (p * AlterIndex ) String () string {
213222 pr := sql .NewTreePrinter ()
214223 switch p .Action {
215224 case IndexAction_Create :
@@ -259,7 +268,7 @@ func (p AlterIndex) String() string {
259268}
260269
261270func (p * AlterIndex ) Resolved () bool {
262- return p .Table .Resolved () && p .ddlNode . Resolved () && p . targetSchema .Resolved ()
271+ return p .Table .Resolved () && p .targetSchema .Resolved ()
263272}
264273
265274func (p * AlterIndex ) IsReadOnly () bool {
0 commit comments