|
| 1 | +package tech.ydb.jdbc; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.DriverManager; |
| 5 | +import java.sql.PreparedStatement; |
| 6 | +import java.sql.ResultSet; |
| 7 | +import java.sql.SQLException; |
| 8 | + |
| 9 | +import org.junit.jupiter.api.Assertions; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 12 | + |
| 13 | +import tech.ydb.test.junit5.YdbHelperExtention; |
| 14 | + |
| 15 | +/** |
| 16 | + * |
| 17 | + * @author Aleksandr Gorshenin |
| 18 | + */ |
| 19 | +public class YdbDriverExampleTest { |
| 20 | + @RegisterExtension |
| 21 | + private static final YdbHelperExtention ydb = new YdbHelperExtention(); |
| 22 | + |
| 23 | + private static String jdbcURL() { |
| 24 | + StringBuilder jdbc = new StringBuilder("jdbc:ydb:") |
| 25 | + .append(ydb.useTls() ? "grpcs://" : "grpc://") |
| 26 | + .append(ydb.endpoint()) |
| 27 | + .append(ydb.database()) |
| 28 | + .append("?"); |
| 29 | + |
| 30 | + if (ydb.authToken() != null) { |
| 31 | + jdbc.append("token=").append(ydb.authToken()).append("&"); |
| 32 | + } |
| 33 | + |
| 34 | + return jdbc.toString(); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testYdb() throws SQLException { |
| 39 | + String url = jdbcURL(); // "jdbc:ydb:localhost:2135/local" |
| 40 | + try (Connection connection = DriverManager.getConnection(url)) { |
| 41 | + try { |
| 42 | + connection.createStatement() |
| 43 | + .execute("--jdbc:SCHEME\n" + |
| 44 | + "drop table table_sample"); |
| 45 | + } catch (SQLException e) { |
| 46 | + // |
| 47 | + } |
| 48 | + connection.createStatement() |
| 49 | + .execute("--jdbc:SCHEME\n" + |
| 50 | + "create table table_sample(id Int32, value Text, primary key (id))"); |
| 51 | + |
| 52 | + try (PreparedStatement ps = connection.prepareStatement("" + |
| 53 | + "upsert into table_sample (id, value) values (?, ?)")) { |
| 54 | + |
| 55 | + ps.setInt(1, 1); |
| 56 | + ps.setString(2, "value-1"); |
| 57 | + ps.executeUpdate(); |
| 58 | + |
| 59 | + ps.setInt(1, 2); |
| 60 | + ps.setString(2, "value-2"); |
| 61 | + ps.executeUpdate(); |
| 62 | + } |
| 63 | + |
| 64 | + try (PreparedStatement ps = connection.prepareStatement("" + |
| 65 | + "declare $p1 as Int32;\n" + |
| 66 | + "declare $p2 as Text;\n" + |
| 67 | + "upsert into table_sample (id, value) values ($p1, $p2)")) { |
| 68 | + |
| 69 | + ps.setInt(1, 3); |
| 70 | + ps.setString(2, "value-3"); |
| 71 | + ps.executeUpdate(); |
| 72 | + |
| 73 | + ps.setInt(1, 4); |
| 74 | + ps.setString(2, "value-4"); |
| 75 | + ps.executeUpdate(); |
| 76 | + } |
| 77 | + |
| 78 | + try (PreparedStatement select = connection |
| 79 | + .prepareStatement("select count(1) as cnt from table_sample")) { |
| 80 | + ResultSet rs = select.executeQuery(); |
| 81 | + rs.next(); |
| 82 | + Assertions.assertEquals(4, rs.getLong("cnt")); |
| 83 | + } |
| 84 | + |
| 85 | + try (YdbPreparedStatement psBatch = connection |
| 86 | + .prepareStatement("" + |
| 87 | + "declare $values as List<Struct<id:Int32,value:Text>>;\n" + |
| 88 | + "upsert into table_sample select * from as_table($values)") |
| 89 | + .unwrap(YdbPreparedStatement.class)) { |
| 90 | + |
| 91 | + psBatch.setInt("id", 5); |
| 92 | + psBatch.setString("value", "value-5"); |
| 93 | + psBatch.addBatch(); |
| 94 | + |
| 95 | + psBatch.setInt("id", 6); |
| 96 | + psBatch.setString("value", "value-6"); |
| 97 | + psBatch.addBatch(); |
| 98 | + |
| 99 | + psBatch.executeBatch(); |
| 100 | + } |
| 101 | + |
| 102 | + try (PreparedStatement select = connection |
| 103 | + .prepareStatement("select count(1) as cnt from table_sample")) { |
| 104 | + ResultSet rs = select.executeQuery(); |
| 105 | + rs.next(); |
| 106 | + Assertions.assertEquals(6, rs.getLong("cnt")); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments