-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathPostgresGlobalState.java
More file actions
153 lines (128 loc) · 4.89 KB
/
PostgresGlobalState.java
File metadata and controls
153 lines (128 loc) · 4.89 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package sqlancer.postgres;
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 PostgresGlobalState extends SQLGlobalState<PostgresOptions, PostgresSchema> {
public static final char IMMUTABLE = 'i';
public static final char STABLE = 's';
public static final char VOLATILE = 'v';
private List<String> operators = Collections.emptyList();
private List<String> collates = Collections.emptyList();
private List<String> opClasses = Collections.emptyList();
private List<String> tableAccessMethods = Collections.emptyList();
// store and allow filtering by function volatility classifications
private final Map<String, Character> functionsAndTypes = new HashMap<>();
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());
this.tableAccessMethods = getTableAccessMethods(getConnection());
} catch (SQLException e) {
throw new AssertionError(e);
}
}
private List<String> getCollnames(SQLConnection con) throws SQLException {
List<String> collNames = 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()) {
collNames.add(rs.getString(1));
}
}
}
return collNames;
}
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> operators = new ArrayList<>();
try (Statement s = con.createStatement()) {
try (ResultSet rs = s.executeQuery("SELECT oprname FROM pg_operator;")) {
while (rs.next()) {
operators.add(rs.getString(1));
}
}
}
return operators;
}
private List<String> getTableAccessMethods(SQLConnection con) throws SQLException {
List<String> tableAccessMethods = new ArrayList<>();
try (Statement s = con.createStatement()) {
/*
* pg_am includes both index and table access methods so we need to filter with amtype = 't'
*/
try (ResultSet rs = s.executeQuery("SELECT amname FROM pg_am WHERE amtype = 't';")) {
while (rs.next()) {
tableAccessMethods.add(rs.getString(1));
}
}
}
return tableAccessMethods;
}
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 List<String> getTableAccessMethods() {
return tableAccessMethods;
}
public String getRandomTableAccessMethod() {
return Randomly.fromList(tableAccessMethods);
}
@Override
public PostgresSchema readSchema() throws SQLException {
return PostgresSchema.fromConnection(getConnection(), getDatabaseName());
}
public void addFunctionAndType(String functionName, Character functionType) {
this.functionsAndTypes.put(functionName, functionType);
}
public Map<String, Character> getFunctionsAndTypes() {
return this.functionsAndTypes;
}
public void setAllowedFunctionTypes(List<Character> types) {
this.allowedFunctionTypes = types;
}
public void setDefaultAllowedFunctionTypes() {
this.allowedFunctionTypes = Arrays.asList(IMMUTABLE, STABLE, VOLATILE);
}
public List<Character> getAllowedFunctionTypes() {
return this.allowedFunctionTypes;
}
}