Skip to content

Commit

Permalink
sql: DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
tamird committed Sep 25, 2015
1 parent 7948995 commit 0d944f6
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 28 deletions.
2 changes: 0 additions & 2 deletions sql/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ func (p *planner) AlterTable(n *parser.AlterTable) (planNode, error) {
}

b.Put(MakeDescMetadataKey(tableDesc.GetID()), tableDesc)
// Mark transaction as operating on the system DB.
p.txn.SetSystemDBTrigger()

if err := p.txn.Run(&b); err != nil {
return nil, convertBatchError(tableDesc, b, err)
Expand Down
2 changes: 0 additions & 2 deletions sql/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ func (p *planner) CreateIndex(n *parser.CreateIndex) (planNode, error) {
}

b.Put(MakeDescMetadataKey(tableDesc.GetID()), tableDesc)
// Mark transaction as operating on the system DB.
p.txn.SetSystemDBTrigger()

if err := p.txn.Run(&b); err != nil {
return nil, convertBatchError(tableDesc, b, err)
Expand Down
3 changes: 1 addition & 2 deletions sql/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ func (p *planner) createDescriptor(plainKey descriptorKey, descriptor descriptor
b := client.Batch{}
b.CPut(key, descriptor.GetID(), nil)
b.CPut(descKey, descriptor, nil)
// Mark transaction as operating on the system DB.
p.txn.SetSystemDBTrigger()

return p.txn.Run(&b)
}

Expand Down
9 changes: 2 additions & 7 deletions sql/drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ func (p *planner) DropDatabase(n *parser.DropDatabase) (planNode, error) {
b.Del(nameKey)
// Delete the zone config entry for this database.
b.Del(MakeZoneKey(desc.ID))
// Mark transaction as operating on the system DB.
p.txn.SetSystemDBTrigger()

if err := p.txn.Run(b); err != nil {
return nil, err
}
Expand Down Expand Up @@ -150,9 +149,6 @@ func (p *planner) DropIndex(n *parser.DropIndex) (planNode, error) {
if err := p.txn.Put(descKey, tableDesc); err != nil {
return nil, err
}

// Mark transaction as operating on the system DB.
p.txn.SetSystemDBTrigger()
}

if err := p.txn.Run(&b); err != nil {
Expand Down Expand Up @@ -218,8 +214,7 @@ func (p *planner) DropTable(n *parser.DropTable) (planNode, error) {
b.Del(nameKey)
// Delete the zone config entry for this table.
b.Del(MakeZoneKey(tableDesc.ID))
// Mark transaction as operating on the system DB.
p.txn.SetSystemDBTrigger()

if err := p.txn.Run(b); err != nil {
return nil, err
}
Expand Down
2 changes: 0 additions & 2 deletions sql/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ func (p *planner) Grant(n *parser.Grant) (planNode, error) {
// TODO(marc): do this inside a transaction. This will be needed
// when modifying multiple descriptors in the same op.
descKey := MakeDescMetadataKey(descriptor.GetID())
// Mark transaction as operating on the system DB.
p.txn.SetSystemDBTrigger()
if err := p.txn.Put(descKey, descriptor); err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions sql/parser/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (*DropTable) StatementType() StatementType { return DDL }
func (*Explain) StatementType() StatementType { return Rows }

// StatementType implements the Statement interface.
func (*Grant) StatementType() StatementType { return Ack }
func (*Grant) StatementType() StatementType { return DDL }

// StatementType implements the Statement interface.
func (*Insert) StatementType() StatementType { return RowsAffected }
Expand All @@ -113,7 +113,7 @@ func (*RenameIndex) StatementType() StatementType { return DDL }
func (*RenameTable) StatementType() StatementType { return DDL }

// StatementType implements the Statement interface.
func (*Revoke) StatementType() StatementType { return Ack }
func (*Revoke) StatementType() StatementType { return DDL }

// StatementType implements the Statement interface.
func (*RollbackTransaction) StatementType() StatementType { return Ack }
Expand Down Expand Up @@ -146,7 +146,7 @@ func (*ShowIndex) StatementType() StatementType { return Rows }
func (*ShowTables) StatementType() StatementType { return Rows }

// StatementType implements the Statement interface.
func (*Truncate) StatementType() StatementType { return DDL }
func (*Truncate) StatementType() StatementType { return Ack }

// StatementType implements the Statement interface.
func (*Update) StatementType() StatementType { return RowsAffected }
Expand Down
9 changes: 9 additions & 0 deletions sql/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ func (p *planner) resetTxn() {
// plan needs to be iterated over using planNode.Next() and planNode.Values()
// in order to retrieve matching rows.
func (p *planner) makePlan(stmt parser.Statement) (planNode, error) {
// This will set the system DB trigger for transactions containing
// DDL statements that have no effect, such as
// `BEGIN; INSERT INTO ...; CREATE TABLE IF NOT EXISTS ...; COMMIT;`
// where the table already exists. This will generate some false
// refreshes, but that's expected to be quite rare in practice.
if stmt.StatementType() == parser.DDL {
p.txn.SetSystemDBTrigger()
}

switch n := stmt.(type) {
case *parser.AlterTable:
return p.AlterTable(n)
Expand Down
9 changes: 1 addition & 8 deletions sql/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ func (p *planner) RenameDatabase(n *parser.RenameDatabase) (planNode, error) {
b.Put(descKey, dbDesc)
b.Del(databaseKey{string(n.Name)}.Key())

// Mark transaction as operating on the system DB.
p.txn.SetSystemDBTrigger()
if err := p.txn.Run(&b); err != nil {
if _, ok := err.(*proto.ConditionFailedError); ok {
return nil, fmt.Errorf("the new database name %q already exists", string(n.NewName))
Expand Down Expand Up @@ -158,8 +156,7 @@ func (p *planner) RenameTable(n *parser.RenameTable) (planNode, error) {
b.Put(descKey, tableDesc)
b.CPut(newTbKey, tableDesc.GetID(), nil)
b.Del(tbKey)
// Mark transaction as operating on the system DB.
p.txn.SetSystemDBTrigger()

if err := p.txn.Run(&b); err != nil {
if _, ok := err.(*proto.ConditionFailedError); ok {
return nil, fmt.Errorf("table name %q already exists", n.NewName.Table())
Expand Down Expand Up @@ -221,8 +218,6 @@ func (p *planner) RenameIndex(n *parser.RenameIndex) (planNode, error) {
if err := p.txn.Put(descKey, tableDesc); err != nil {
return nil, err
}
// Mark transaction as operating on the system DB.
p.txn.SetSystemDBTrigger()

return &valuesNode{}, nil
}
Expand Down Expand Up @@ -302,8 +297,6 @@ func (p *planner) RenameColumn(n *parser.RenameColumn) (planNode, error) {
if err := p.txn.Put(descKey, tableDesc); err != nil {
return nil, err
}
// Mark transaction as operating on the system DB.
p.txn.SetSystemDBTrigger()

return &valuesNode{}, nil
}
2 changes: 0 additions & 2 deletions sql/revoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ func (p *planner) Revoke(n *parser.Revoke) (planNode, error) {
// TODO(marc): do this inside a transaction. This will be needed
// when modifying multiple descriptors in the same op.
descKey := MakeDescMetadataKey(descriptor.GetID())
// Mark transaction as operating on the system DB.
p.txn.SetSystemDBTrigger()
if err := p.txn.Put(descKey, descriptor); err != nil {
return nil, err
}
Expand Down

0 comments on commit 0d944f6

Please sign in to comment.