-
Notifications
You must be signed in to change notification settings - Fork 429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updated getIndexInfo() to include Columnstore indexes by using custom query. #2566
Open
Ananya2
wants to merge
9
commits into
main
Choose a base branch
from
users/anagarg/issue#2546-final
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
420b583
Updated getIndexInfo() to include Columnstore indexes by using custom…
Ananya2 162f3e0
Updated changes as per review comments.
Ananya2 a386438
formatted code
Ananya2 57a9e34
Added random identifier to the col names
Ananya2 aeb5ce0
Capture Client Guest OS and architecture in JDBC (#2561)
muskan124947 945cd1f
Driver cuts out the question mark from columns labels (aliases) (#2569)
Ananya2 0483bd4
Fix OffsetDateTime conversion for pre-Gregorian dates (#2568)
machavan 8d4f2c6
Handled failure in getApplicationName() (#2571)
muskan124947 3f98b61
Fixed test failure (#2574)
muskan124947 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
.../java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetadataGetIndexInfoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
package com.microsoft.sqlserver.jdbc.databasemetadata; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DatabaseMetaData; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import com.microsoft.sqlserver.testframework.AbstractTest; | ||
import com.microsoft.sqlserver.jdbc.RandomUtil; | ||
import com.microsoft.sqlserver.jdbc.SQLServerException; | ||
import com.microsoft.sqlserver.jdbc.TestResource; | ||
import com.microsoft.sqlserver.jdbc.TestUtils; | ||
import com.microsoft.sqlserver.testframework.AbstractSQLGenerator; | ||
|
||
public class DatabaseMetadataGetIndexInfoTest extends AbstractTest { | ||
|
||
private static String tableName = AbstractSQLGenerator.escapeIdentifier("DBMetadataTestTable"); | ||
Ananya2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private static String col1Name = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("col1")); | ||
private static String col2Name = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("col2")); | ||
private static String col3Name = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("col3")); | ||
|
||
Ananya2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@BeforeAll | ||
public static void setupTests() throws Exception { | ||
setConnection(); | ||
} | ||
|
||
@BeforeEach | ||
public void init() throws SQLException { | ||
try (Connection con = getConnection()) { | ||
con.setAutoCommit(false); | ||
try (Statement stmt = con.createStatement()) { | ||
TestUtils.dropTableIfExists(tableName, stmt); | ||
String createTableSQL = "CREATE TABLE " + tableName + " (" + col1Name + " INT, " + col2Name + " INT, " | ||
+ col3Name + " INT)"; | ||
|
||
stmt.executeUpdate(createTableSQL); | ||
assertNull(connection.getWarnings(), TestResource.getResource("R_noSQLWarningsCreateTableConnection")); | ||
assertNull(stmt.getWarnings(), TestResource.getResource("R_noSQLWarningsCreateTableStatement")); | ||
|
||
String createClusteredIndexSQL = "CREATE CLUSTERED INDEX IDX_Clustered ON " + tableName + "(" + col1Name | ||
+ ")"; | ||
stmt.executeUpdate(createClusteredIndexSQL); | ||
assertNull(connection.getWarnings(), TestResource.getResource("R_noSQLWarningsCreateIndexConnection")); | ||
assertNull(stmt.getWarnings(), TestResource.getResource("R_noSQLWarningsCreateIndexStatement")); | ||
|
||
String createNonClusteredIndexSQL = "CREATE NONCLUSTERED INDEX IDX_NonClustered ON " + tableName + "(" | ||
+ col2Name + ")"; | ||
stmt.executeUpdate(createNonClusteredIndexSQL); | ||
assertNull(connection.getWarnings(), TestResource.getResource("R_noSQLWarningsCreateIndexConnection")); | ||
assertNull(stmt.getWarnings(), TestResource.getResource("R_noSQLWarningsCreateIndexStatement")); | ||
|
||
String createColumnstoreIndexSQL = "CREATE COLUMNSTORE INDEX IDX_Columnstore ON " + tableName + "(" | ||
+ col3Name + ")"; | ||
stmt.executeUpdate(createColumnstoreIndexSQL); | ||
assertNull(connection.getWarnings(), TestResource.getResource("R_noSQLWarningsCreateIndexConnection")); | ||
assertNull(stmt.getWarnings(), TestResource.getResource("R_noSQLWarningsCreateIndexStatement")); | ||
} | ||
con.commit(); | ||
} | ||
} | ||
|
||
@AfterEach | ||
public void terminate() throws SQLException { | ||
try (Connection con = getConnection(); Statement stmt = con.createStatement()) { | ||
try { | ||
TestUtils.dropTableIfExists(tableName, stmt); | ||
} catch (SQLException e) { | ||
fail(TestResource.getResource("R_unexpectedException") + e.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void testGetIndexInfo() throws SQLException { | ||
ResultSet rs1, rs2 = null; | ||
try (Connection connection = getConnection(); Statement stmt = connection.createStatement()) { | ||
String catalog = connection.getCatalog(); | ||
String schema = "dbo"; | ||
String table = "DBMetadataTestTable"; | ||
DatabaseMetaData dbMetadata = connection.getMetaData(); | ||
rs1 = dbMetadata.getIndexInfo(catalog, schema, table, false, false); | ||
|
||
boolean hasClusteredIndex = false; | ||
boolean hasNonClusteredIndex = false; | ||
boolean hasColumnstoreIndex = false; | ||
|
||
String query = "SELECT " + " db_name() AS CatalogName, " + " sch.name AS SchemaName, " | ||
+ " t.name AS TableName, " + " i.name AS IndexName, " + " i.type_desc AS IndexType, " | ||
+ " i.is_unique AS IsUnique, " + " c.name AS ColumnName, " | ||
+ " ic.key_ordinal AS ColumnOrder " + "FROM " + " sys.indexes i " + "INNER JOIN " | ||
+ " sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id " | ||
+ "INNER JOIN " + " sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id " | ||
+ "INNER JOIN " + " sys.tables t ON i.object_id = t.object_id " + "INNER JOIN " | ||
+ " sys.schemas sch ON t.schema_id = sch.schema_id " + | ||
|
||
"WHERE t.name = '" + table + "' " + "AND sch.name = '" + schema + "' " + "ORDER BY " | ||
+ " t.name, i.name, ic.key_ordinal;"; | ||
rs2 = stmt.executeQuery(query); | ||
|
||
while (rs1.next() && rs2.next()) { | ||
String indexType = rs1.getString("IndexType"); | ||
String indexName = rs1.getString("IndexName"); | ||
String catalogName = rs1.getString("CatalogName"); | ||
String schemaName = rs1.getString("SchemaName"); | ||
String tableName = rs1.getString("TableName"); | ||
boolean isUnique = rs1.getBoolean("IsUnique"); | ||
String columnName = rs1.getString("ColumnName"); | ||
int columnOrder = rs1.getInt("ColumnOrder"); | ||
|
||
assertEquals(catalogName, rs2.getString("CatalogName")); | ||
assertEquals(schemaName, rs2.getString("SchemaName")); | ||
assertEquals(tableName, rs2.getString("TableName")); | ||
assertEquals(indexName, rs2.getString("IndexName")); | ||
assertEquals(indexType, rs2.getString("IndexType")); | ||
assertEquals(isUnique, rs2.getBoolean("IsUnique")); | ||
assertEquals(columnName, rs2.getString("ColumnName")); | ||
assertEquals(columnOrder, rs2.getInt("ColumnOrder")); | ||
|
||
if (indexType.contains("COLUMNSTORE")) { | ||
hasColumnstoreIndex = true; | ||
} else if (indexType.equals("CLUSTERED")) { | ||
hasClusteredIndex = true; | ||
} else if (indexType.equals("NONCLUSTERED")) { | ||
hasNonClusteredIndex = true; | ||
} | ||
} | ||
|
||
assertTrue(hasColumnstoreIndex, "COLUMNSTORE index not found."); | ||
assertTrue(hasClusteredIndex, "CLUSTERED index not found."); | ||
assertTrue(hasNonClusteredIndex, "NONCLUSTERED index not found."); | ||
} | ||
} | ||
|
||
@Test | ||
public void testGetIndexInfoCaseSensitivity() throws SQLException { | ||
ResultSet rs1, rs2 = null; | ||
try (Connection connection = getConnection()) { | ||
String catalog = connection.getCatalog(); | ||
String schema = "dbo"; | ||
String table = "DBMetadataTestTable"; | ||
|
||
DatabaseMetaData dbMetadata = connection.getMetaData(); | ||
rs1 = dbMetadata.getIndexInfo(catalog, schema, table, false, false); | ||
rs2 = dbMetadata.getIndexInfo(catalog, schema, table.toUpperCase(), false, false); | ||
|
||
while (rs1.next() && rs2.next()) { | ||
String indexType = rs1.getString("IndexType"); | ||
String indexName = rs1.getString("IndexName"); | ||
String catalogName = rs1.getString("CatalogName"); | ||
String schemaName = rs1.getString("SchemaName"); | ||
String tableName = rs1.getString("TableName"); | ||
boolean isUnique = rs1.getBoolean("IsUnique"); | ||
String columnName = rs1.getString("ColumnName"); | ||
int columnOrder = rs1.getInt("ColumnOrder"); | ||
|
||
assertEquals(catalogName, rs2.getString("CatalogName")); | ||
assertEquals(schemaName, rs2.getString("SchemaName")); | ||
assertEquals(tableName, rs2.getString("TableName")); | ||
assertEquals(indexName, rs2.getString("IndexName")); | ||
assertEquals(indexType, rs2.getString("IndexType")); | ||
assertEquals(isUnique, rs2.getBoolean("IsUnique")); | ||
assertEquals(columnName, rs2.getString("ColumnName")); | ||
assertEquals(columnOrder, rs2.getInt("ColumnOrder")); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
table
andschema
are external inputs. Don't append them into the SQL (that's a SQL injection vulnerability). Use a prepared statement with defined parameters.