Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions vertx-mssql-client/src/main/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ You can configure the client to retry when a connection fails to be established.

include::queries.adoc[leveloffset=1]

== Working with `identity` columns

You can retrieve the value of an `identity` column after inserting new data using the `OUTPUT` clause:

[source,$lang]
----
{@link examples.MSSQLClientExamples#identityColumn}
----

include::connections.adoc[]

include::transactions.adoc[]
Expand Down
11 changes: 11 additions & 0 deletions vertx-mssql-client/src/main/java/examples/MSSQLClientExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,15 @@ public void explicitNullHandling(SqlClient client) {
// ...
});
}

public void identityColumn(SqlClient client) {
client
.preparedQuery("INSERT INTO movies (title) OUTPUT INSERTED.id VALUES (@p1)")
.execute(Tuple.of("The Man Who Knew Too Much"), res -> {
if (res.succeeded()) {
Row row = res.result().iterator().next();
System.out.println(row.getLong("id"));
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.vertx.ext.unit.junit.Repeat;
import io.vertx.ext.unit.junit.RepeatRule;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.Tuple;
import io.vertx.sqlclient.data.NullValue;
import org.junit.After;
Expand All @@ -34,40 +35,40 @@ public class MSSQLQueriesTest extends MSSQLTestBase {
public RepeatRule rule = new RepeatRule();

Vertx vertx;
MSSQLConnection connnection;
MSSQLConnection connection;

@Before
public void setup(TestContext ctx) {
vertx = Vertx.vertx();
options = new MSSQLConnectOptions(MSSQLTestBase.options);
MSSQLConnection.connect(vertx, options, ctx.asyncAssertSuccess(conn -> this.connnection = conn));
MSSQLConnection.connect(vertx, options, ctx.asyncAssertSuccess(conn -> this.connection = conn));
}

@After
public void tearDown(TestContext ctx) {
if (connnection != null) {
connnection.close(ctx.asyncAssertSuccess());
if (connection != null) {
connection.close(ctx.asyncAssertSuccess());
}
vertx.close(ctx.asyncAssertSuccess());
}

@Test
public void testSimpleQueryOrderBy(TestContext ctx) {
connnection.query("SELECT message FROM immutable ORDER BY message DESC")
connection.query("SELECT message FROM immutable ORDER BY message DESC")
.execute(ctx.asyncAssertSuccess(rs -> ctx.assertTrue(rs.size() > 1)));
}

@Test
public void testPreparedQueryOrderBy(TestContext ctx) {
connnection.preparedQuery("SELECT message FROM immutable WHERE id BETWEEN @p1 AND @p2 ORDER BY message DESC")
connection.preparedQuery("SELECT message FROM immutable WHERE id BETWEEN @p1 AND @p2 ORDER BY message DESC")
.execute(Tuple.of(4, 9), ctx.asyncAssertSuccess(rs -> ctx.assertEquals(6, rs.size())));
}

@Test
@Repeat(50)
public void testQueryCurrentTimestamp(TestContext ctx) {
LocalDateTime start = LocalDateTime.now();
connnection.query("SELECT current_timestamp")
connection.query("SELECT current_timestamp")
.execute(ctx.asyncAssertSuccess(rs -> {
Object value = rs.iterator().next().getValue(0);
ctx.assertTrue(value instanceof LocalDateTime);
Expand All @@ -78,13 +79,24 @@ public void testQueryCurrentTimestamp(TestContext ctx) {

@Test
public void testCreateTable(TestContext ctx) {
connnection.query("drop table if exists Basic")
connection.query("drop table if exists Basic")
.execute(ctx.asyncAssertSuccess(drop -> {
connnection.preparedQuery("create table Basic (id int, dessimal numeric(19,2), primary key (id))")
connection.preparedQuery("create table Basic (id int, dessimal numeric(19,2), primary key (id))")
.execute(ctx.asyncAssertSuccess(create -> {
connnection.preparedQuery("INSERT INTO Basic (id, dessimal) values (3, @p1)")
connection.preparedQuery("INSERT INTO Basic (id, dessimal) values (3, @p1)")
.execute(Tuple.of(NullValue.BigDecimal), ctx.asyncAssertSuccess());
}));
}));
}


@Test
public void testInsertReturning(TestContext ctx) {
connection.preparedQuery("insert into EntityWithIdentity (name) OUTPUT INSERTED.id, INSERTED.name VALUES (@p1)")
.execute(Tuple.of("John"), ctx.asyncAssertSuccess(result -> {
Row row = result.iterator().next();
ctx.assertNotNull(row.getInteger("id"));
ctx.assertEquals("John", row.getString("name"));
}));
}
}
8 changes: 8 additions & 0 deletions vertx-mssql-client/src/test/resources/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,11 @@ INSERT INTO Fortune (id, message)
VALUES (11, '<script>alert("This should not be displayed in a browser alert box.");</script>');
INSERT INTO Fortune (id, message)
VALUES (12, 'フレームワークのベンチマーク');

-- Table for testing OUTPUT
CREATE TABLE EntityWithIdentity
(
id bigint identity NOT NULL,
name varchar(255)
primary key (id)
);