sqlancer.postgres.PostgresGlobalState Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sqlancer Show documentation
Show all versions of sqlancer Show documentation
SQLancer finds logic bugs in Database Management Systems through automatic testing
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 {
public static final char IMMUTABLE = 'i';
public static final char STABLE = 's';
public static final char VOLATILE = 'v';
private List operators = Collections.emptyList();
private List collates = Collections.emptyList();
private List opClasses = Collections.emptyList();
// store and allow filtering by function volatility classifications
private final Map functionsAndTypes = new HashMap<>();
private List 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);
}
}
private List getCollnames(SQLConnection con) throws SQLException {
List 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 getOpclasses(SQLConnection con) throws SQLException {
List 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 getOperators(SQLConnection con) throws SQLException {
List 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 getOperators() {
return operators;
}
public String getRandomOperator() {
return Randomly.fromList(operators);
}
public List getCollates() {
return collates;
}
public String getRandomCollate() {
return Randomly.fromList(collates);
}
public List getOpClasses() {
return opClasses;
}
public String getRandomOpclass() {
return Randomly.fromList(opClasses);
}
@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 getFunctionsAndTypes() {
return this.functionsAndTypes;
}
public void setAllowedFunctionTypes(List types) {
this.allowedFunctionTypes = types;
}
public void setDefaultAllowedFunctionTypes() {
this.allowedFunctionTypes = Arrays.asList(IMMUTABLE, STABLE, VOLATILE);
}
public List getAllowedFunctionTypes() {
return this.allowedFunctionTypes;
}
}