1919import sqlancer .StateToReproduce .PostgresStateToReproduce ;
2020import sqlancer .postgres .PostgresSchema .PostgresTable .TableType ;
2121import sqlancer .postgres .ast .PostgresConstant ;
22+ import sqlancer .schema .AbstractTable ;
23+ import sqlancer .schema .AbstractTableColumn ;
2224import sqlancer .schema .TableIndex ;
23- import sqlancer .sqlite3 .schema .SQLite3Schema .SQLite3Column ;
2425
2526public class PostgresSchema {
2627
@@ -38,75 +39,12 @@ public static PostgresDataType getRandomType() {
3839 return Randomly .fromList (dataTypes );
3940 }
4041 }
41-
42- public static class PostgresColumn implements Comparable <PostgresColumn > {
43-
44- private final String name ;
45- private final PostgresDataType columnType ;
46- private PostgresTable table ;
47- private int precision ;
48-
42+
43+
44+ public static class PostgresColumn extends AbstractTableColumn <PostgresTable , PostgresDataType > {
4945
5046 public PostgresColumn (String name , PostgresDataType columnType ) {
51- this .name = name ;
52- this .columnType = columnType ;
53- }
54-
55- @ Override
56- public String toString () {
57- return String .format ("%s.%s: %s" , table .getName (), name , columnType );
58- }
59-
60- @ Override
61- public int hashCode () {
62- return name .hashCode () + 11 * columnType .hashCode ();
63- }
64-
65- @ Override
66- public boolean equals (Object obj ) {
67- if (!(obj instanceof SQLite3Column )) {
68- return false ;
69- } else {
70- PostgresColumn c = (PostgresColumn ) obj ;
71- return table .getName ().contentEquals (getName ()) && name .equals (c .name );
72- }
73- }
74-
75- public String getName () {
76- return name ;
77- }
78-
79- public String getFullQualifiedName () {
80- if (table == null ) {
81- return getName ();
82- } else {
83- return table .getName () + "." + getName ();
84- }
85- }
86-
87- public PostgresDataType getColumnType () {
88- return columnType ;
89- }
90-
91- public int getPrecision () {
92- return precision ;
93- }
94-
95- public void setTable (PostgresTable t ) {
96- this .table = t ;
97- }
98-
99- public PostgresTable getTable () {
100- return table ;
101- }
102-
103- @ Override
104- public int compareTo (PostgresColumn o ) {
105- if (o .getTable ().equals (this .getTable ())) {
106- return name .compareTo (o .getName ());
107- } else {
108- return o .getTable ().compareTo (table );
109- }
47+ super (name , null , columnType );
11048 }
11149
11250 }
@@ -164,7 +102,7 @@ public PostgresRowValue getRandomRowValue(Connection con, PostgresStateToReprodu
164102 if (randomRowValues .getString (columnIndex ) == null ) {
165103 constant = PostgresConstant .createNullConstant ();
166104 } else {
167- switch (column .getColumnType ()) {
105+ switch (column .getType ()) {
168106 case INT :
169107 constant = PostgresConstant .createIntConstant (randomRowValues .getLong (columnIndex ));
170108 break ;
@@ -175,7 +113,7 @@ public PostgresRowValue getRandomRowValue(Connection con, PostgresStateToReprodu
175113 constant = PostgresConstant .createTextConstant (randomRowValues .getString (columnIndex ));
176114 break ;
177115 default :
178- throw new AssertionError (column .getColumnType ());
116+ throw new AssertionError (column .getType ());
179117 }
180118 }
181119 values .put (column , constant );
@@ -274,83 +212,25 @@ public String getRowValuesAsString(List<PostgresColumn> columnsToCheck) {
274212 }
275213
276214 }
277-
278- public static class PostgresTable implements Comparable < PostgresTable > {
215+
216+ public static class PostgresTable extends AbstractTable < PostgresColumn , PostgresIndex > {
279217
280218 public enum TableType {
281219 STANDARD , TEMPORARY
282220 }
283221
284- private final String tableName ;
285- private final List <PostgresColumn > columns ;
286- private final List <PostgresIndex > indexes ;
287222 private final TableType tableType ;
288223 private List <PostgresStatisticsObject > statistics ;
289- private final boolean isView ;
290224 private boolean isInsertable ;
291225
292226 public PostgresTable (String tableName , List <PostgresColumn > columns , List <PostgresIndex > indexes ,
293227 TableType tableType , List <PostgresStatisticsObject > statistics , boolean isView , boolean isInsertable ) {
294- this .tableName = tableName ;
295- this .indexes = indexes ;
228+ super (tableName , columns , indexes , isView );
296229 this .statistics = statistics ;
297- this .isView = isView ;
298230 this .isInsertable = isInsertable ;
299- this .columns = Collections .unmodifiableList (columns );
300231 this .tableType = tableType ;
301232 }
302233
303- @ Override
304- public String toString () {
305- StringBuffer sb = new StringBuffer ();
306- sb .append (tableName + "\n " );
307- for (PostgresColumn c : columns ) {
308- sb .append ("\t " + c + "\n " );
309- }
310- return sb .toString ();
311- }
312-
313- public List <PostgresIndex > getIndexes () {
314- return indexes ;
315- }
316-
317- public String getName () {
318- return tableName ;
319- }
320-
321- public List <PostgresColumn > getColumns () {
322- return columns ;
323- }
324-
325- public String getColumnsAsString () {
326- return columns .stream ().map (c -> c .getName ()).collect (Collectors .joining (", " ));
327- }
328-
329- public String getColumnsAsString (Function <PostgresColumn , String > function ) {
330- return columns .stream ().map (function ).collect (Collectors .joining (", " ));
331- }
332-
333- public PostgresColumn getRandomColumn () {
334- return Randomly .fromList (columns );
335- }
336-
337- public boolean hasIndexes () {
338- return !indexes .isEmpty ();
339- }
340-
341- public PostgresIndex getRandomIndex () {
342- return Randomly .fromList (indexes );
343- }
344-
345- @ Override
346- public int compareTo (PostgresTable o ) {
347- return o .getName ().compareTo (tableName );
348- }
349-
350- public List <PostgresColumn > getRandomNonEmptyColumnSubset () {
351- return Randomly .nonEmptySubset (getColumns ());
352- }
353-
354234 public List <PostgresStatisticsObject > getStatistics () {
355235 return statistics ;
356236 }
@@ -369,10 +249,6 @@ public TableType getTableType() {
369249 return tableType ;
370250 }
371251
372- public boolean isView () {
373- return isView ;
374- }
375-
376252 public boolean isInsertable () {
377253 return isInsertable ;
378254 }
0 commit comments