11package sqlancer .cockroachdb ;
22
3- import java .io .IOException ;
43import java .sql .Connection ;
54import java .sql .DriverManager ;
65import java .sql .SQLException ;
1514import sqlancer .GlobalState ;
1615import sqlancer .IgnoreMeException ;
1716import sqlancer .Main .QueryManager ;
18- import sqlancer .Main .StateLogger ;
1917import sqlancer .MainOptions ;
2018import sqlancer .ProviderAdapter ;
2119import sqlancer .Query ;
2220import sqlancer .QueryAdapter ;
2321import sqlancer .QueryProvider ;
2422import sqlancer .Randomly ;
25- import sqlancer .StateToReproduce ;
2623import sqlancer .TestOracle ;
2724import sqlancer .cockroachdb .CockroachDBProvider .CockroachDBGlobalState ;
2825import sqlancer .cockroachdb .CockroachDBSchema .CockroachDBTable ;
@@ -119,32 +116,29 @@ public static class CockroachDBGlobalState extends GlobalState<CockroachDBOption
119116
120117 private CockroachDBSchema schema ;
121118
122- public void setSchema (CockroachDBSchema schema ) {
123- this .schema = schema ;
124- }
125-
126119 public CockroachDBSchema getSchema () {
120+ if (schema == null ) {
121+ try {
122+ updateSchema ();
123+ } catch (SQLException e ) {
124+ throw new AssertionError ();
125+ }
126+ }
127127 return schema ;
128128 }
129129
130130 @ Override
131131 protected void updateSchema () throws SQLException {
132- setSchema ( CockroachDBSchema .fromConnection (getConnection (), getDatabaseName () ));
132+ this . schema = CockroachDBSchema .fromConnection (getConnection (), getDatabaseName ());
133133 }
134134
135135 }
136136
137137 @ Override
138138 public void generateAndTestDatabase (CockroachDBGlobalState globalState ) throws SQLException {
139139 Randomly r = new Randomly ();
140- Connection con = globalState .getConnection ();
141- String databaseName = globalState .getDatabaseName ();
142140 QueryManager manager = globalState .getManager ();
143- StateLogger logger = globalState .getLogger ();
144- StateToReproduce state = globalState .getState ();
145141 MainOptions options = globalState .getOptions ();
146- globalState .setSchema (CockroachDBSchema .fromConnection (con , databaseName ));
147-
148142 List <String > standardSettings = new ArrayList <>();
149143 standardSettings .add ("--Don't send automatic bug reports\n "
150144 + "SET CLUSTER SETTING debug.panic_on_failed_assertions = true;" );
@@ -172,22 +166,12 @@ public void generateAndTestDatabase(CockroachDBGlobalState globalState) throws S
172166 do {
173167 try {
174168 Query q = CockroachDBTableGenerator .generate (globalState );
175- success = manager .execute (q );
176- logger .writeCurrent (state );
177- try {
178- logger .getCurrentFileWriter ().close ();
179- } catch (IOException e ) {
180- // TODO Auto-generated catch block
181- e .printStackTrace ();
182- }
183- logger .currentFileWriter = null ;
169+ success = globalState .executeStatement (q );
184170 } catch (IgnoreMeException e ) {
185171 // continue trying
186172 }
187173 } while (!success );
188- globalState .setSchema (CockroachDBSchema .fromConnection (con , databaseName ));
189174 }
190- logger .writeCurrent (state );
191175
192176 int [] nrRemaining = new int [Action .values ().length ];
193177 List <Action > actions = new ArrayList <>();
@@ -263,19 +247,13 @@ public void generateAndTestDatabase(CockroachDBGlobalState globalState) throws S
263247 int nrTries = 0 ;
264248 do {
265249 query = nextAction .getQuery (globalState );
266- if (options .logEachSelect ()) {
267- logger .writeCurrent (query .getQueryString ());
268- }
269- success = manager .execute (query );
250+ success = globalState .executeStatement (query );
270251 } while (!success && nrTries ++ < 1000 );
271252 } catch (IgnoreMeException e ) {
272253
273254 }
274- if (query != null && query .couldAffectSchema ()) {
275- globalState .setSchema (CockroachDBSchema .fromConnection (con , databaseName ));
276- if (globalState .getSchema ().getDatabaseTables ().isEmpty ()) {
277- throw new IgnoreMeException ();
278- }
255+ if (query != null && query .couldAffectSchema () && globalState .getSchema ().getDatabaseTables ().isEmpty ()) {
256+ throw new IgnoreMeException ();
279257 }
280258 total --;
281259 }
@@ -292,16 +270,6 @@ public void generateAndTestDatabase(CockroachDBGlobalState globalState) throws S
292270
293271 }
294272 }
295- try {
296- if (options .logEachSelect ()) {
297- logger .getCurrentFileWriter ().close ();
298- logger .currentFileWriter = null ;
299- }
300- } catch (IOException e ) {
301- // TODO Auto-generated catch block
302- e .printStackTrace ();
303- }
304-
305273 }
306274
307275 @ Override
0 commit comments