forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYSQLGlobalState.java
More file actions
127 lines (106 loc) · 3.93 KB
/
YSQLGlobalState.java
File metadata and controls
127 lines (106 loc) · 3.93 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package sqlancer.yugabyte.ysql;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import sqlancer.Randomly;
import sqlancer.SQLConnection;
import sqlancer.SQLGlobalState;
public class YSQLGlobalState extends SQLGlobalState<YSQLOptions, YSQLSchema> {
public static final char IMMUTABLE = 'i';
public static final char STABLE = 's';
public static final char VOLATILE = 'v';
// store and allow filtering by function volatility classifications
private final Map<String, Character> functionsAndTypes = new HashMap<>();
private List<String> operators = Collections.emptyList();
private List<String> collates = Collections.emptyList();
private List<String> opClasses = Collections.emptyList();
private List<Character> allowedFunctionTypes = Arrays.asList(IMMUTABLE, STABLE, VOLATILE);
@Override
public void setConnection(SQLConnection con) {
super.setConnection(con);
try {
this.opClasses = getOpclasses(getConnection());
this.operators = getOperators(getConnection());
this.collates = getCollnames(getConnection());
} catch (SQLException e) {
throw new AssertionError(e);
}
}
@Override
public YSQLSchema readSchema() throws SQLException {
return YSQLSchema.fromConnection(getConnection(), getDatabaseName());
}
private List<String> getCollnames(SQLConnection con) throws SQLException {
List<String> opClasses = new ArrayList<>();
try (Statement s = con.createStatement()) {
try (ResultSet rs = s
.executeQuery("SELECT collname FROM pg_collation WHERE collname LIKE '%utf8' or collname = 'C';")) {
while (rs.next()) {
opClasses.add(rs.getString(1));
}
}
}
return opClasses;
}
private List<String> getOpclasses(SQLConnection con) throws SQLException {
List<String> opClasses = new ArrayList<>();
try (Statement s = con.createStatement()) {
try (ResultSet rs = s.executeQuery("select opcname FROM pg_opclass;")) {
while (rs.next()) {
opClasses.add(rs.getString(1));
}
}
}
return opClasses;
}
private List<String> getOperators(SQLConnection con) throws SQLException {
List<String> opClasses = new ArrayList<>();
try (Statement s = con.createStatement()) {
try (ResultSet rs = s.executeQuery("SELECT oprname FROM pg_operator;")) {
while (rs.next()) {
opClasses.add(rs.getString(1));
}
}
}
return opClasses;
}
public List<String> getOperators() {
return operators;
}
public String getRandomOperator() {
return Randomly.fromList(operators);
}
public List<String> getCollates() {
return collates;
}
public String getRandomCollate() {
return Randomly.fromList(collates);
}
public List<String> getOpClasses() {
return opClasses;
}
public String getRandomOpclass() {
return Randomly.fromList(opClasses);
}
public void addFunctionAndType(String functionName, Character functionType) {
this.functionsAndTypes.put(functionName, functionType);
}
public Map<String, Character> getFunctionsAndTypes() {
return this.functionsAndTypes;
}
public void setDefaultAllowedFunctionTypes() {
this.allowedFunctionTypes = Arrays.asList(IMMUTABLE, STABLE, VOLATILE);
}
public List<Character> getAllowedFunctionTypes() {
return this.allowedFunctionTypes;
}
public void setAllowedFunctionTypes(List<Character> types) {
this.allowedFunctionTypes = types;
}
}