forked from PingCAP-QE/go-sqlsmith
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdml_sql.go
100 lines (89 loc) · 2.69 KB
/
dml_sql.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package sqlsmith
import (
"errors"
"fmt"
"strings"
"github.com/chaos-mesh/go-sqlsmith/builtin"
"github.com/chaos-mesh/go-sqlsmith/types"
"github.com/chaos-mesh/go-sqlsmith/util"
)
// SelectStmt make random select statement SQL
func (s *SQLSmith) SelectStmt(depth int) (string, string, error) {
tree := s.selectStmt(depth)
return s.Walk(tree)
}
// SelectForUpdateStmt make random select statement SQL with for update lock
func (s *SQLSmith) SelectForUpdateStmt(depth int) (string, string, error) {
tree := s.selectForUpdateStmt(depth)
return s.Walk(tree)
}
// UpdateStmt make random update statement SQL
func (s *SQLSmith) UpdateStmt() (string, string, error) {
tree := s.updateStmt()
return s.Walk(tree)
}
// InsertStmt implement insert statement from AST
func (s *SQLSmith) InsertStmt(fn bool) (string, string, error) {
tree := s.insertStmt()
return s.Walk(tree)
}
// DeleteStmt implement delete statement from AST
func (s *SQLSmith) DeleteStmt() (string, string, error) {
tree := s.deleteStmt()
return s.Walk(tree)
}
// InsertStmtStr make random insert statement SQL
func (s *SQLSmith) InsertStmtStr(fn bool) (string, string, error) {
if s.currDB == "" {
return "", "", errors.New("no table selected")
}
var table *types.Table
rdTableIndex := s.rd(len(s.Databases[s.currDB].Tables))
tableIndex := 0
for _, t := range s.Databases[s.currDB].Tables {
if rdTableIndex == tableIndex {
table = t
break
}
tableIndex++
}
var columns []*types.Column
var columnNames []string
for _, column := range table.Columns {
if column.Column == "id" {
continue
}
columns = append(columns, column)
columnNames = append(columnNames, fmt.Sprintf("`%s`", column.Column))
}
var vals []string
for _, column := range columns {
if fn && s.rdFloat64() < 0.5 {
builtinFn := builtin.GenerateFuncCallExpr(nil, s.rd(3), s.stable)
builtinStr, err := util.BufferOut(builtinFn)
if err != nil {
return "", "", err
}
vals = append(vals, builtinStr)
} else {
vals = append(vals, s.generateDataItem(column.DataType))
}
}
sql := fmt.Sprintf("INSERT INTO %s(%s) VALUES(%s)",
table.Table,
strings.Join(columnNames, ", "),
strings.Join(vals, ", "))
return sql, table.Table, nil
}