|
1 | 1 | package sqlancer; |
2 | 2 |
|
3 | 3 | import java.io.Closeable; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.ObjectInputStream; |
| 6 | +import java.io.ObjectOutputStream; |
| 7 | +import java.io.Serializable; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
4 | 10 | import java.util.ArrayList; |
5 | 11 | import java.util.Collections; |
6 | 12 | import java.util.List; |
7 | 13 |
|
8 | 14 | import sqlancer.common.query.Query; |
9 | 15 |
|
10 | | -public class StateToReproduce { |
| 16 | +public class StateToReproduce implements Serializable { |
| 17 | + private static final long serialVersionUID = 1L; |
11 | 18 |
|
12 | 19 | private List<Query<?>> statements = new ArrayList<>(); |
13 | 20 |
|
14 | 21 | private final String databaseName; |
15 | 22 |
|
16 | | - private final DatabaseProvider<?, ?, ?> databaseProvider; |
| 23 | + private transient DatabaseProvider<?, ?, ?> databaseProvider; |
17 | 24 |
|
18 | 25 | public String databaseVersion; |
19 | 26 |
|
@@ -131,6 +138,45 @@ public OracleRunReproductionState createLocalState() { |
131 | 138 | return new OracleRunReproductionState(); |
132 | 139 | } |
133 | 140 |
|
| 141 | + public void serialize(Path path) { |
| 142 | + try (ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(path))) { |
| 143 | + oos.writeObject(this); |
| 144 | + } catch (IOException e) { |
| 145 | + throw new AssertionError(e); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + public static StateToReproduce deserialize(Path path) { |
| 150 | + try (ObjectInputStream ois = new ObjectInputStream(Files.newInputStream(path))) { |
| 151 | + return (StateToReproduce) ois.readObject(); |
| 152 | + } catch (IOException | ClassNotFoundException e) { |
| 153 | + throw new AssertionError(e); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private void writeObject(ObjectOutputStream out) throws IOException { |
| 158 | + out.defaultWriteObject(); |
| 159 | + |
| 160 | + out.writeObject(this.databaseProvider != null ? this.databaseProvider.getDBMSName() : null); |
| 161 | + } |
| 162 | + |
| 163 | + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { |
| 164 | + in.defaultReadObject(); |
| 165 | + String dbmsName = (String) in.readObject(); |
| 166 | + |
| 167 | + DatabaseProvider<?, ?, ?> provider = null; |
| 168 | + if (dbmsName != null) { |
| 169 | + List<DatabaseProvider<?, ?, ?>> providers = Main.getDBMSProviders(); |
| 170 | + for (DatabaseProvider<?, ?, ?> p : providers) { |
| 171 | + if (p.getDBMSName().equals(dbmsName)) { |
| 172 | + provider = p; |
| 173 | + break; |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + this.databaseProvider = provider; |
| 178 | + } |
| 179 | + |
134 | 180 | public void setStatements(List<Query<?>> statements) { |
135 | 181 | this.statements = statements; |
136 | 182 | } |
|
0 commit comments