forked from proullon/ramsql
/
truncate.go
37 lines (28 loc) · 818 Bytes
/
truncate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package engine
import (
"fmt"
"github.com/IllidanTwister/ramsql/engine/log"
"github.com/IllidanTwister/ramsql/engine/parser"
"github.com/IllidanTwister/ramsql/engine/protocol"
)
func truncateExecutor(e *Engine, trDecl *parser.Decl, conn protocol.EngineConn) error {
log.Debug("truncateExecutor")
// get tables to be deleted
table := NewTable(trDecl.Decl[0].Lexeme)
return truncateTable(e, table, conn)
}
func truncateTable(e *Engine, table *Table, conn protocol.EngineConn) error {
var rowsDeleted int64
// get relations and write lock them
r := e.relation(table.name)
if r == nil {
return fmt.Errorf("Table %v not found", table.name)
}
r.Lock()
defer r.Unlock()
if r.rows != nil {
rowsDeleted = int64(len(r.rows))
}
r.rows = make([]*Tuple, 0)
return conn.WriteResult(0, rowsDeleted)
}