forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostgresGlobalState.java
More file actions
101 lines (84 loc) · 2.79 KB
/
PostgresGlobalState.java
File metadata and controls
101 lines (84 loc) · 2.79 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
package sqlancer.postgres;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import sqlancer.GlobalState;
import sqlancer.Randomly;
public class PostgresGlobalState extends GlobalState<PostgresOptions> {
private List<String> operators;
private List<String> collates;
private List<String> opClasses;
private PostgresSchema schema;
@Override
public void setConnection(Connection con) {
super.setConnection(con);
try {
this.opClasses = getOpclasses(getConnection());
this.operators = getOperators(getConnection());
this.collates = getCollnames(getConnection());
} catch (SQLException e) {
throw new AssertionError(e);
}
}
public void setSchema(PostgresSchema schema) {
this.schema = schema;
}
public PostgresSchema getSchema() {
return schema;
}
private List<String> getCollnames(Connection 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(Connection 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(Connection 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);
}
}