-
Notifications
You must be signed in to change notification settings - Fork 0
/
MssqlJdbcTestApplicationTests.java
106 lines (84 loc) · 3.87 KB
/
MssqlJdbcTestApplicationTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.skunkworks;
import com.microsoft.sqlserver.jdbc.SQLServerDataTable;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlConfig;
import org.springframework.test.context.junit4.SpringRunner;
import javax.sql.DataSource;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Types;
import java.util.logging.Logger;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestConfig.class)
public class MssqlJdbcTestApplicationTests {
private static final Logger l = Logger.getLogger(MssqlJdbcTestApplicationTests.class.getName());
@Autowired
DataSource dataSource;
@Sql(
scripts = "/database/tvpSpringTest.sql",
config = @SqlConfig(separator = "GO")
)
@Test
public void tvpSpringTest() throws Exception {
NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
final String sql = "{call dbo.TestMsDriverTVP(:inputData)}";
SQLServerDataTable inputDataTable = new SQLServerDataTable();
inputDataTable.setTvpName("[dbo].[MsDriverSqlType]");
inputDataTable.addColumnMetadata("ThisId", Types.INTEGER);
inputDataTable.addColumnMetadata("ThatId", Types.INTEGER);
inputDataTable.addRow(2, 1);
inputDataTable.addRow(2, 2);
final MapSqlParameterSource source = new MapSqlParameterSource();
source.addValue("inputData", inputDataTable);
namedParameterJdbcTemplate.update(sql, source);
l.info("done.");
}
@Sql(
scripts = "/database/tvpSchemaErrorTest.sql",
config = @SqlConfig(separator = "GO")
)
@Test
public void tvpSchemaSpringTest() throws Exception {
NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
final String sql = "{call tableValued.UpdateMsDriverTest(:inputData)}";
SQLServerDataTable inputDataTable = new SQLServerDataTable();
inputDataTable.setTvpName("[tableValued].[TheSqlDataType]");
inputDataTable.addColumnMetadata("ThisId", Types.INTEGER);
inputDataTable.addColumnMetadata("ThatId", Types.INTEGER);
inputDataTable.addRow(2, 1);
inputDataTable.addRow(2, 2);
final MapSqlParameterSource source = new MapSqlParameterSource();
source.addValue("inputData", inputDataTable);
namedParameterJdbcTemplate.update(sql, source);
l.info("done.");
}
@Sql(
scripts = "/database/tvpSchemaErrorTest.sql",
config = @SqlConfig(separator = "GO")
)
@Test
public void tvpSchemaErrorTest() throws Exception {
SQLServerDataTable inputDataTable = new SQLServerDataTable();
//inputDataTable.setTvpName("[tableValued].[TheSqlDataType]");
inputDataTable.addColumnMetadata("ThisId", Types.INTEGER);
inputDataTable.addColumnMetadata("ThatId", Types.SMALLINT);
inputDataTable.addRow(2, 1);
inputDataTable.addRow(2, 2);
Connection connection = dataSource.getConnection();
CallableStatement callableStatement = connection.prepareCall("exec tableValued.UpdateMsDriverTest ?");
callableStatement.setObject(1, inputDataTable);
callableStatement.execute();
// SQLServerCallableStatement pStmt =
// (SQLServerCallableStatement) connection.prepareCall("exec tableValued.UpdateMsDriverTest ?");
//
// pStmt.setStructured(1, "tableValued.TheSqlDataType", inputDataTable);
// pStmt.execute();
l.info("done.");
}
}