|
| 1 | +package tech.ydb.jdbc; |
| 2 | + |
| 3 | + |
| 4 | +import java.sql.Connection; |
| 5 | +import java.sql.Date; |
| 6 | +import java.sql.DriverManager; |
| 7 | +import java.sql.PreparedStatement; |
| 8 | +import java.sql.ResultSet; |
| 9 | +import java.sql.SQLException; |
| 10 | +import java.sql.Statement; |
| 11 | +import java.time.LocalDate; |
| 12 | + |
| 13 | +import org.junit.jupiter.api.Assertions; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 16 | + |
| 17 | +import tech.ydb.jdbc.impl.helper.ExceptionAssert; |
| 18 | +import tech.ydb.jdbc.impl.helper.JdbcUrlHelper; |
| 19 | +import tech.ydb.test.junit5.YdbHelperExtension; |
| 20 | + |
| 21 | +/** |
| 22 | + * |
| 23 | + * @author Aleksandr Gorshenin |
| 24 | + */ |
| 25 | +public class YdbDriverTablesTest { |
| 26 | + @RegisterExtension |
| 27 | + private static final YdbHelperExtension ydb = new YdbHelperExtension(); |
| 28 | + |
| 29 | + private static final JdbcUrlHelper jdbcURL = new JdbcUrlHelper(ydb); |
| 30 | + |
| 31 | + private final static String ERROR_SCAN_QUERY = |
| 32 | + "Scan query should have a single result set. (S_ERROR)"; |
| 33 | + |
| 34 | + private final static String ERROR_BULK_UNSUPPORTED = |
| 35 | + "BULK mode is available only for prepared statement with one UPSERT"; |
| 36 | + |
| 37 | + private final static String CREATE_TABLE = "" |
| 38 | + + "CREATE TABLE simple_table(" |
| 39 | + + " id Int32 NOT NULL," |
| 40 | + + " value Text," |
| 41 | + + " date Date," |
| 42 | + + " PRIMARY KEY (id)" |
| 43 | + + ")"; |
| 44 | + |
| 45 | + private final static String DROP_TABLE = "DROP TABLE simple_table"; |
| 46 | + private final static String UPSERT_ROW = "UPSERT INTO simple_table (id, value, date) VALUES (?, ?, ?)"; |
| 47 | + private final static String INSERT_ROW = "INSERT INTO simple_table (id, value, date) VALUES (?, ?, ?)"; |
| 48 | + private final static String SELECT_ALL = "SELECT * FROM simple_table ORDER BY id"; |
| 49 | + private final static String UPDATE_ROW = "UPDATE simple_table SET value = ? WHERE id = ?"; |
| 50 | + private final static String DELETE_ROW = "DELETE FROM simple_table WHERE id = ?"; |
| 51 | + |
| 52 | + @Test |
| 53 | + public void defaultModeTest() throws SQLException { |
| 54 | + try (Connection connection = DriverManager.getConnection(jdbcURL.build())) { |
| 55 | + try { |
| 56 | + connection.createStatement().execute(DROP_TABLE); |
| 57 | + } catch (SQLException e) { |
| 58 | + // ignore |
| 59 | + } |
| 60 | + |
| 61 | + connection.createStatement().execute(CREATE_TABLE); |
| 62 | + |
| 63 | + LocalDate ld = LocalDate.of(2017, 12, 3); |
| 64 | + String prefix = "text-value-"; |
| 65 | + int idx = 0; |
| 66 | + |
| 67 | + // single upsert |
| 68 | + try (PreparedStatement ps = connection.prepareStatement(UPSERT_ROW)) { |
| 69 | + ps.setInt(1, ++idx); |
| 70 | + ps.setString(2, prefix + idx); |
| 71 | + ps.setDate(3, Date.valueOf(ld.plusDays(idx))); |
| 72 | + ps.executeUpdate(); |
| 73 | + } |
| 74 | + |
| 75 | + // single insert |
| 76 | + try (PreparedStatement ps = connection.prepareStatement(INSERT_ROW)) { |
| 77 | + ps.setInt(1, ++idx); |
| 78 | + ps.setString(2, prefix + idx); |
| 79 | + ps.setDate(3, Date.valueOf(ld.plusDays(idx))); |
| 80 | + ps.executeUpdate(); |
| 81 | + } |
| 82 | + |
| 83 | + // batch upsert |
| 84 | + try (PreparedStatement ps = connection.prepareStatement(UPSERT_ROW)) { |
| 85 | + for (int j = 0; j < 1000; j++) { |
| 86 | + ps.setInt(1, ++idx); |
| 87 | + ps.setString(2, prefix + idx); |
| 88 | + ps.setDate(3, Date.valueOf(ld.plusDays(idx))); |
| 89 | + ps.addBatch(); |
| 90 | + } |
| 91 | + ps.executeBatch(); |
| 92 | + } |
| 93 | + |
| 94 | + // batch insert |
| 95 | + try (PreparedStatement ps = connection.prepareStatement(INSERT_ROW)) { |
| 96 | + for (int j = 0; j < 1000; j++) { |
| 97 | + ps.setInt(1, ++idx); |
| 98 | + ps.setString(2, prefix + idx); |
| 99 | + ps.setDate(3, Date.valueOf(ld.plusDays(idx))); |
| 100 | + ps.addBatch(); |
| 101 | + } |
| 102 | + ps.executeBatch(); |
| 103 | + } |
| 104 | + |
| 105 | + // read all |
| 106 | + try (Statement st = connection.createStatement()) { |
| 107 | + int readed = 0; |
| 108 | + try (ResultSet rs = st.executeQuery(SELECT_ALL)) { |
| 109 | + while (rs.next()) { |
| 110 | + readed++; |
| 111 | + Assertions.assertEquals(readed, rs.getInt("id")); |
| 112 | + Assertions.assertEquals(prefix + readed, rs.getString("value")); |
| 113 | + Assertions.assertEquals(Date.valueOf(ld.plusDays(readed)), rs.getDate("date")); |
| 114 | + } |
| 115 | + } |
| 116 | + Assertions.assertEquals(1000, readed); |
| 117 | + } |
| 118 | + |
| 119 | + // single update |
| 120 | + try (PreparedStatement ps = connection.prepareStatement(UPDATE_ROW)) { |
| 121 | + ps.setString(1, "updated-value"); |
| 122 | + ps.setInt(2, 1); |
| 123 | + ps.executeUpdate(); |
| 124 | + } |
| 125 | + |
| 126 | + // single delete |
| 127 | + try (PreparedStatement ps = connection.prepareStatement(DELETE_ROW)) { |
| 128 | + ps.setInt(1, 2); |
| 129 | + ps.executeUpdate(); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void customQueriesTest() throws SQLException { |
| 136 | + try (Connection conn = DriverManager.getConnection(jdbcURL.build())) { |
| 137 | + try { |
| 138 | + conn.createStatement().execute(DROP_TABLE); |
| 139 | + } catch (SQLException e) { |
| 140 | + // ignore |
| 141 | + } |
| 142 | + |
| 143 | + conn.createStatement().execute(CREATE_TABLE); |
| 144 | + |
| 145 | + LocalDate ld = LocalDate.of(2017, 12, 3); |
| 146 | + String prefix = "text-value-"; |
| 147 | + int idx = 0; |
| 148 | + |
| 149 | + // single bulk upsert |
| 150 | + try (PreparedStatement ps = conn.prepareStatement("BULK " + UPSERT_ROW)) { |
| 151 | + ps.setInt(1, ++idx); |
| 152 | + ps.setString(2, prefix + idx); |
| 153 | + ps.setDate(3, Date.valueOf(ld.plusDays(idx))); |
| 154 | + ps.executeUpdate(); |
| 155 | + } |
| 156 | + |
| 157 | + // single bulk insert |
| 158 | + ExceptionAssert.sqlException(ERROR_BULK_UNSUPPORTED, () -> conn.prepareStatement("BULK " + INSERT_ROW)); |
| 159 | + |
| 160 | + // batch bulk upsert |
| 161 | + try (PreparedStatement ps = conn.prepareStatement("BULK " + UPSERT_ROW)) { |
| 162 | + for (int j = 0; j < 2000; j++) { |
| 163 | + ps.setInt(1, ++idx); |
| 164 | + ps.setString(2, prefix + idx); |
| 165 | + ps.setDate(3, Date.valueOf(ld.plusDays(idx))); |
| 166 | + ps.addBatch(); |
| 167 | + } |
| 168 | + ps.executeBatch(); |
| 169 | + |
| 170 | + for (int j = 0; j < 2000; j++) { |
| 171 | + ps.setInt(1, ++idx); |
| 172 | + ps.setString(2, prefix + idx); |
| 173 | + ps.setDate(3, Date.valueOf(ld.plusDays(idx))); |
| 174 | + ps.addBatch(); |
| 175 | + } |
| 176 | + ps.executeBatch(); |
| 177 | + |
| 178 | + // single row insert |
| 179 | + ps.setInt(1, ++idx); |
| 180 | + ps.setString(2, prefix + idx); |
| 181 | + ps.setDate(3, Date.valueOf(ld.plusDays(idx))); |
| 182 | + ps.execute(); |
| 183 | + } |
| 184 | + |
| 185 | + // read all |
| 186 | + try (Statement st = conn.createStatement()) { |
| 187 | + int readed = 0; |
| 188 | + try (ResultSet rs = st.executeQuery("SCAN " + SELECT_ALL)) { |
| 189 | + while (rs.next()) { |
| 190 | + readed++; |
| 191 | + Assertions.assertEquals(readed, rs.getInt("id")); |
| 192 | + Assertions.assertEquals(prefix + readed, rs.getString("value")); |
| 193 | + Assertions.assertEquals(Date.valueOf(ld.plusDays(readed)), rs.getDate("date")); |
| 194 | + } |
| 195 | + } |
| 196 | + Assertions.assertEquals(4002, readed); |
| 197 | + } |
| 198 | + |
| 199 | + // single update |
| 200 | + ExceptionAssert.sqlException(ERROR_BULK_UNSUPPORTED, () -> conn.prepareStatement("BULK " + UPDATE_ROW)); |
| 201 | + |
| 202 | + // single delete |
| 203 | + ExceptionAssert.sqlException(ERROR_BULK_UNSUPPORTED, () -> conn.prepareStatement("BULK " + DELETE_ROW)); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + @Test |
| 208 | + public void forceScanAndBulkTest() throws SQLException { |
| 209 | + try (Connection conn = DriverManager.getConnection(jdbcURL.withArg("forceScanAndBulk", "true").build())) { |
| 210 | + try { |
| 211 | + conn.createStatement().execute(DROP_TABLE); |
| 212 | + } catch (SQLException e) { |
| 213 | + // ignore |
| 214 | + } |
| 215 | + |
| 216 | + conn.createStatement().execute(CREATE_TABLE); |
| 217 | + |
| 218 | + LocalDate ld = LocalDate.of(2017, 12, 3); |
| 219 | + String prefix = "text-value-"; |
| 220 | + int idx = 0; |
| 221 | + |
| 222 | + // single bulk upsert |
| 223 | + try (PreparedStatement ps = conn.prepareStatement(UPSERT_ROW)) { |
| 224 | + ps.setInt(1, ++idx); |
| 225 | + ps.setString(2, prefix + idx); |
| 226 | + ps.setDate(3, Date.valueOf(ld.plusDays(idx))); |
| 227 | + ps.executeUpdate(); |
| 228 | + } |
| 229 | + |
| 230 | + // single bulk insert |
| 231 | + try (PreparedStatement ps = conn.prepareStatement(INSERT_ROW)) { |
| 232 | + ps.setInt(1, ++idx); |
| 233 | + ps.setString(2, prefix + idx); |
| 234 | + ps.setDate(3, Date.valueOf(ld.plusDays(idx))); |
| 235 | + ps.executeUpdate(); |
| 236 | + } |
| 237 | + |
| 238 | + // scan read |
| 239 | + try (Statement st = conn.createStatement()) { |
| 240 | + int readed = 0; |
| 241 | + try (ResultSet rs = st.executeQuery(SELECT_ALL)) { |
| 242 | + while (rs.next()) { |
| 243 | + readed++; |
| 244 | + Assertions.assertEquals(readed, rs.getInt("id")); |
| 245 | + Assertions.assertEquals(prefix + readed, rs.getString("value")); |
| 246 | + Assertions.assertEquals(Date.valueOf(ld.plusDays(readed)), rs.getDate("date")); |
| 247 | + } |
| 248 | + } |
| 249 | + Assertions.assertEquals(2, readed); |
| 250 | + } |
| 251 | + |
| 252 | + // batch bulk upsert |
| 253 | + try (PreparedStatement ps = conn.prepareStatement(UPSERT_ROW)) { |
| 254 | + for (int j = 0; j < 2000; j++) { |
| 255 | + ps.setInt(1, ++idx); |
| 256 | + ps.setString(2, prefix + idx); |
| 257 | + ps.setDate(3, Date.valueOf(ld.plusDays(idx))); |
| 258 | + ps.addBatch(); |
| 259 | + } |
| 260 | + ps.executeBatch(); |
| 261 | + |
| 262 | + // single row upsert |
| 263 | + ps.setInt(1, ++idx); |
| 264 | + ps.setString(2, prefix + idx); |
| 265 | + ps.setDate(3, Date.valueOf(ld.plusDays(idx))); |
| 266 | + ps.execute(); |
| 267 | + } |
| 268 | + |
| 269 | + // batch bulk inserts |
| 270 | + try (PreparedStatement ps = conn.prepareStatement(INSERT_ROW)) { |
| 271 | + for (int j = 0; j < 2000; j++) { |
| 272 | + ps.setInt(1, ++idx); |
| 273 | + ps.setString(2, prefix + idx); |
| 274 | + ps.setDate(3, Date.valueOf(ld.plusDays(idx))); |
| 275 | + ps.addBatch(); |
| 276 | + } |
| 277 | + ps.executeBatch(); |
| 278 | + |
| 279 | + // single row insert |
| 280 | + ps.setInt(1, ++idx); |
| 281 | + ps.setString(2, prefix + idx); |
| 282 | + ps.setDate(3, Date.valueOf(ld.plusDays(idx))); |
| 283 | + ps.execute(); |
| 284 | + } |
| 285 | + |
| 286 | + // read all |
| 287 | + try (Statement st = conn.createStatement()) { |
| 288 | + int readed = 0; |
| 289 | + try (ResultSet rs = st.executeQuery(SELECT_ALL)) { |
| 290 | + while (rs.next()) { |
| 291 | + readed++; |
| 292 | + Assertions.assertEquals(readed, rs.getInt("id")); |
| 293 | + Assertions.assertEquals(prefix + readed, rs.getString("value")); |
| 294 | + Assertions.assertEquals(Date.valueOf(ld.plusDays(readed)), rs.getDate("date")); |
| 295 | + } |
| 296 | + } |
| 297 | + Assertions.assertEquals(4004, readed); |
| 298 | + } |
| 299 | + |
| 300 | + // single update |
| 301 | + try (PreparedStatement ps = conn.prepareStatement(UPDATE_ROW)) { |
| 302 | + ps.setString(1, "updated-value"); |
| 303 | + ps.setInt(2, 1); |
| 304 | + ExceptionAssert.ydbException(ERROR_SCAN_QUERY, ps::execute); |
| 305 | + } |
| 306 | + |
| 307 | + // single delete |
| 308 | + try (PreparedStatement ps = conn.prepareStatement(DELETE_ROW)) { |
| 309 | + ps.setInt(1, 2); |
| 310 | + ExceptionAssert.ydbException(ERROR_SCAN_QUERY, ps::execute); |
| 311 | + } |
| 312 | + } |
| 313 | + } |
| 314 | +} |
0 commit comments