Skip to content

Commit

Permalink
Merge pull request #7367 from nvanbenschoten/nvanbenschoten/deallocate
Browse files Browse the repository at this point in the history
sql: Add support for the DEALLOCATE statement
  • Loading branch information
nvanbenschoten authored Jun 21, 2016
2 parents 2eba7fd + 8ac7f92 commit 6d60fb6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
11 changes: 11 additions & 0 deletions sql/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,17 @@ func (e *Executor) execStmtInOpenTxn(
}
txnState.updateStateAndCleanupOnErr(err, e)
return Result{Err: err}, err
case *parser.Deallocate:
if s.Name == "" {
planMaker.session.PreparedStatements.DeleteAll()
} else {
if found := planMaker.session.PreparedStatements.Delete(string(s.Name)); !found {
err := fmt.Errorf("prepared statement %s does not exist", s.Name)
txnState.updateStateAndCleanupOnErr(err, e)
return Result{Err: err}, err
}
}
return Result{PGTag: s.StatementTag()}, nil
}

if txnState.tr != nil {
Expand Down
19 changes: 16 additions & 3 deletions sql/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,24 @@ func (ps PreparedStatements) New(
return stmt, nil
}

// Delete removes the PreparedStatements with the provided name from the PreparedStatements.
func (ps PreparedStatements) Delete(name string) {
// Delete removes the PreparedStatement with the provided name from the PreparedStatements.
// The method returns whether a statement with that name was found and removed.
func (ps PreparedStatements) Delete(name string) bool {
if stmt, ok := ps.Get(name); ok {
for portalName := range stmt.portalNames {
delete(ps.session.PreparedPortals.portals, portalName)
}
delete(ps.stmts, name)
return true
}
return false
}

// DeleteAll removes all PreparedStatements from the PreparedStatements. This will in turn
// remove all PreparedPortals from the session's PreparedPortals.
func (ps PreparedStatements) DeleteAll() {
ps.stmts = make(map[string]*PreparedStatement)
ps.session.PreparedPortals.portals = make(map[string]*PreparedPortal)
}

// PreparedPortal is a PreparedStatement that has been bound with query arguments.
Expand Down Expand Up @@ -141,9 +151,12 @@ func (pp PreparedPortals) New(name string, stmt *PreparedStatement, qargs parser
}

// Delete removes the PreparedPortal with the provided name from the PreparedPortals.
func (pp PreparedPortals) Delete(name string) {
// The method returns whether a portal with that name was found and removed.
func (pp PreparedPortals) Delete(name string) bool {
if portal, ok := pp.Get(name); ok {
delete(portal.Stmt.portalNames, name)
delete(pp.portals, name)
return true
}
return false
}
7 changes: 6 additions & 1 deletion sql/testdata/prepare
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@ PREPARE a AS SELECT 1
statement error unknown statement type: \*parser.Execute
EXECUTE a

statement error unknown statement type: \*parser.Deallocate
# TODO(nvanbenschoten) This deserves more robust testing, but that is hard
# to add until we support the PREPARE statement.
statement error prepared statement a does not exist
DEALLOCATE a

statement ok
DEALLOCATE ALL

0 comments on commit 6d60fb6

Please sign in to comment.