|
| 1 | +package io.teaql.core.mssql; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import io.teaql.core.BaseEntity; |
| 7 | +import io.teaql.core.UserContext; |
| 8 | +import io.teaql.core.meta.SimpleEntityMetaFactory; |
| 9 | +import io.teaql.core.sql.GenericSQLProperty; |
| 10 | +import io.teaql.core.sql.SQLEntityDescriptor; |
| 11 | +import io.teaql.provider.jdbc.JdbcSqlExecutor; |
| 12 | +import io.teaql.runtime.DefaultUserContext; |
| 13 | +import io.teaql.runtime.TeaQLRuntime; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.UUID; |
| 17 | +import javax.sql.DataSource; |
| 18 | +import java.io.PrintWriter; |
| 19 | +import java.sql.Connection; |
| 20 | +import java.sql.DriverManager; |
| 21 | +import java.sql.SQLException; |
| 22 | +import java.sql.SQLFeatureNotSupportedException; |
| 23 | +import java.util.logging.Logger; |
| 24 | +import org.junit.Assume; |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +public class MssqlSchemaIntegrationTest { |
| 28 | + |
| 29 | + private record TestDataSource(String url, String user, String password) implements DataSource { |
| 30 | + @Override public Connection getConnection() throws SQLException { |
| 31 | + return DriverManager.getConnection(url, user, password); |
| 32 | + } |
| 33 | + @Override public Connection getConnection(String username, String secret) throws SQLException { |
| 34 | + return DriverManager.getConnection(url, username, secret); |
| 35 | + } |
| 36 | + @Override public PrintWriter getLogWriter() { return null; } |
| 37 | + @Override public void setLogWriter(PrintWriter writer) {} |
| 38 | + @Override public void setLoginTimeout(int seconds) {} |
| 39 | + @Override public int getLoginTimeout() { return 0; } |
| 40 | + @Override public Logger getParentLogger() throws SQLFeatureNotSupportedException { |
| 41 | + throw new SQLFeatureNotSupportedException(); |
| 42 | + } |
| 43 | + @Override public <T> T unwrap(Class<T> type) throws SQLException { |
| 44 | + throw new SQLException("Not a wrapper for " + type.getName()); |
| 45 | + } |
| 46 | + @Override public boolean isWrapperFor(Class<?> type) { return false; } |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void ensureSchemaUsesSelectedDialectForLargeText() { |
| 51 | + String url = System.getenv("TEAQL_TEST_MSSQL_URL"); |
| 52 | + String user = System.getenv("TEAQL_TEST_MSSQL_USER"); |
| 53 | + String password = System.getenv("TEAQL_TEST_MSSQL_PASSWORD"); |
| 54 | + if ("true".equalsIgnoreCase(System.getenv("TEAQL_REQUIRE_LIVE_DB"))) { |
| 55 | + assertTrue("TEAQL_TEST_MSSQL_URL must be set", url != null && !url.isBlank()); |
| 56 | + assertTrue("TEAQL_TEST_MSSQL_USER must be set", user != null && !user.isBlank()); |
| 57 | + assertTrue("TEAQL_TEST_MSSQL_PASSWORD must be set", password != null && !password.isBlank()); |
| 58 | + } |
| 59 | + Assume.assumeTrue("SQL Server live test credentials are not configured", |
| 60 | + url != null && user != null && password != null); |
| 61 | + |
| 62 | + DataSource dataSource = new TestDataSource(url, user, password); |
| 63 | + JdbcSqlExecutor jdbc = new JdbcSqlExecutor(dataSource); |
| 64 | + String table = "mssql_long_text_" + UUID.randomUUID().toString().substring(0, 8); |
| 65 | + |
| 66 | + SQLEntityDescriptor descriptor = new SQLEntityDescriptor(); |
| 67 | + descriptor.setType("MssqlLongTextProbe"); |
| 68 | + descriptor.setTargetType(BaseEntity.class); |
| 69 | + descriptor.setEntitySupplier(BaseEntity::new); |
| 70 | + descriptor.setDataService("mssql"); |
| 71 | + for (String name : List.of("id", "version")) { |
| 72 | + GenericSQLProperty property = (GenericSQLProperty) descriptor.addSimpleProperty(name, Long.class); |
| 73 | + property.setTableName(table); |
| 74 | + property.setColumnName(name); |
| 75 | + property.setColumnType("BIGINT"); |
| 76 | + } |
| 77 | + GenericSQLProperty payload = (GenericSQLProperty) descriptor.addSimpleProperty("payload", String.class); |
| 78 | + payload.setTableName(table); |
| 79 | + payload.setColumnName("payload"); |
| 80 | + payload.setColumnType("LARGE_TEXT"); |
| 81 | + SimpleEntityMetaFactory metadata = new SimpleEntityMetaFactory(); |
| 82 | + metadata.register(descriptor); |
| 83 | + |
| 84 | + TeaQLRuntime runtime = TeaQLRuntime.builder() |
| 85 | + .metadata(metadata) |
| 86 | + .dataService("mssql", new MssqlDataServiceExecutor("mssql", jdbc)) |
| 87 | + .build(); |
| 88 | + UserContext context = new DefaultUserContext(runtime); |
| 89 | + context.ensureSchema(); |
| 90 | + context.ensureSchema(); |
| 91 | + |
| 92 | + List<Map<String, Object>> columns = jdbc.queryForList( |
| 93 | + "SELECT data_type, character_maximum_length FROM information_schema.columns " |
| 94 | + + "WHERE table_schema = SCHEMA_NAME() AND table_name = ? AND column_name = ?", |
| 95 | + new Object[] {table, "payload"}); |
| 96 | + assertEquals(1, columns.size()); |
| 97 | + Map<String, Object> column = columns.get(0); |
| 98 | + assertEquals("nvarchar", String.valueOf(column.get("data_type")).toLowerCase(java.util.Locale.ROOT)); |
| 99 | + assertEquals(-1, ((Number) column.get("character_maximum_length")).intValue()); |
| 100 | + } |
| 101 | +} |
0 commit comments