|
| 1 | +package sqlancer.hsqldb; |
| 2 | + |
| 3 | +import sqlancer.Randomly; |
| 4 | +import sqlancer.SQLConnection; |
| 5 | +import sqlancer.common.DBMSCommon; |
| 6 | +import sqlancer.common.schema.AbstractRelationalTable; |
| 7 | +import sqlancer.common.schema.AbstractSchema; |
| 8 | +import sqlancer.common.schema.AbstractTableColumn; |
| 9 | +import sqlancer.common.schema.TableIndex; |
| 10 | + |
| 11 | +import java.sql.ResultSet; |
| 12 | +import java.sql.SQLException; |
| 13 | +import java.sql.Statement; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +public class HSQLDBSchema extends AbstractSchema<HSQLDBProvider.HSQLDBGlobalState, HSQLDBSchema.HSQLDBTable> { |
| 19 | + |
| 20 | + public HSQLDBSchema(List<HSQLDBTable> databaseTables) { |
| 21 | + super(databaseTables); |
| 22 | + } |
| 23 | + |
| 24 | + public static HSQLDBSchema fromConnection(SQLConnection connection, String databaseName) throws SQLException { |
| 25 | + List<HSQLDBSchema.HSQLDBTable> databaseTables = new ArrayList<>(); |
| 26 | + List<String> tableNames = getTableNames(connection); |
| 27 | + for (String tableName : tableNames) { |
| 28 | + if (DBMSCommon.matchesIndexName(tableName)) { |
| 29 | + continue; // TODO: unexpected? |
| 30 | + } |
| 31 | + List<HSQLDBSchema.HSQLDBColumn> databaseColumns = getTableColumns(connection, tableName); |
| 32 | + boolean isView = tableName.startsWith("v"); |
| 33 | + HSQLDBSchema.HSQLDBTable t = new HSQLDBSchema.HSQLDBTable(tableName, databaseColumns, isView); |
| 34 | + for (HSQLDBSchema.HSQLDBColumn c : databaseColumns) { |
| 35 | + c.setTable(t); |
| 36 | + } |
| 37 | + databaseTables.add(t); |
| 38 | + |
| 39 | + } |
| 40 | + return new HSQLDBSchema(databaseTables); |
| 41 | + } |
| 42 | + |
| 43 | + private static List<String> getTableNames(SQLConnection con) throws SQLException { |
| 44 | + List<String> tableNames = new ArrayList<>(); |
| 45 | + try (Statement s = con.createStatement()) { |
| 46 | + try (ResultSet rs = s.executeQuery("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'PUBLIC';")) { |
| 47 | + while (rs.next()) { |
| 48 | + tableNames.add(rs.getString("TABLE_NAME")); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + return tableNames; |
| 53 | + } |
| 54 | + |
| 55 | + private static List<HSQLDBColumn> getTableColumns(SQLConnection con, String tableName) throws SQLException { |
| 56 | + List<HSQLDBColumn> tableNames = new ArrayList<>(); |
| 57 | + try (Statement s = con.createStatement()) { |
| 58 | + String sql = "SELECT COLUMN_NAME, DATA_TYPE, TYPE_NAME, COLUMN_SIZE FROM INFORMATION_SCHEMA.SYSTEM_COLUMNS WHERE TABLE_NAME = '%s';"; |
| 59 | + try (ResultSet rs = s.executeQuery(String.format(sql, tableName))) { |
| 60 | + while (rs.next()) { |
| 61 | + HSQLDBDataType dataType = HSQLDBDataType.from(rs.getString("TYPE_NAME")); |
| 62 | + HSQLDBCompositeDataType compositeDataType = new HSQLDBCompositeDataType(dataType, rs.getInt("COLUMN_SIZE")); |
| 63 | + HSQLDBColumn column = new HSQLDBColumn(rs.getString("COLUMN_NAME"), null, compositeDataType ); |
| 64 | + tableNames.add(column); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + return tableNames; |
| 69 | + } |
| 70 | + |
| 71 | + public static class HSQLDBTable extends AbstractRelationalTable<HSQLDBSchema.HSQLDBColumn, TableIndex, HSQLDBProvider.HSQLDBGlobalState> { |
| 72 | + |
| 73 | + public HSQLDBTable(String tableName, List<HSQLDBSchema.HSQLDBColumn> columns, boolean isView) { |
| 74 | + super(tableName, columns, Collections.emptyList(), isView); |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + public static class HSQLDBColumn extends AbstractTableColumn<HSQLDBSchema.HSQLDBTable, HSQLDBSchema.HSQLDBCompositeDataType> |
| 80 | + { |
| 81 | + |
| 82 | + public HSQLDBColumn(String name, HSQLDBTable table, HSQLDBCompositeDataType type) { |
| 83 | + super(name, table, type); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public enum HSQLDBDataType { |
| 88 | + |
| 89 | + INTEGER, DOUBLE, BOOLEAN, CHAR, VARCHAR, BINARY, TIME, DATE, TIMESTAMP, NULL; |
| 90 | + |
| 91 | + public static HSQLDBSchema.HSQLDBDataType getRandomWithoutNull() { |
| 92 | + HSQLDBSchema.HSQLDBDataType dt; |
| 93 | + do { |
| 94 | + dt = Randomly.fromOptions(values()); |
| 95 | + } while (dt == HSQLDBSchema.HSQLDBDataType.NULL); |
| 96 | + return dt; |
| 97 | + } |
| 98 | + |
| 99 | + public static HSQLDBDataType from(String type_name) { |
| 100 | + for( HSQLDBDataType value : HSQLDBDataType.values()) { |
| 101 | + if(value.name().equals(type_name)) { |
| 102 | + return value; |
| 103 | + } |
| 104 | + } |
| 105 | + return NULL; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public static class HSQLDBCompositeDataType{ |
| 110 | + private final int size; |
| 111 | + private final HSQLDBDataType type; |
| 112 | + |
| 113 | + public HSQLDBCompositeDataType(HSQLDBDataType type, int size) { |
| 114 | + this.type = type; |
| 115 | + this.size = size; |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + public static HSQLDBCompositeDataType getRandomWithoutNull() { |
| 120 | + HSQLDBSchema.HSQLDBDataType type = HSQLDBSchema.HSQLDBDataType.getRandomWithoutNull(); |
| 121 | + int size; |
| 122 | + switch (type) { |
| 123 | + case VARCHAR: |
| 124 | + case CHAR: |
| 125 | + case TIME: |
| 126 | + case BINARY: |
| 127 | + case TIMESTAMP: |
| 128 | + size = Randomly.fromOptions(4,6,8); |
| 129 | + break; |
| 130 | + case BOOLEAN: |
| 131 | + case INTEGER: |
| 132 | + case DOUBLE: |
| 133 | + //case UUID: |
| 134 | + //case OTHER: |
| 135 | + case DATE: |
| 136 | + size = 0; |
| 137 | + break; |
| 138 | + default: |
| 139 | + throw new AssertionError(type); |
| 140 | + } |
| 141 | + |
| 142 | + return new HSQLDBSchema.HSQLDBCompositeDataType(type, size); |
| 143 | + } |
| 144 | + |
| 145 | + public HSQLDBDataType getType() { |
| 146 | + return type; |
| 147 | + } |
| 148 | + |
| 149 | + public int getSize() { |
| 150 | + return size; |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments