-
Notifications
You must be signed in to change notification settings - Fork 29
Adding Struct Support #73
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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,98 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
|
||
package org.opensearch.jdbc; | ||
|
||
import java.sql.SQLException; | ||
import java.sql.Struct; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.List; | ||
|
||
|
||
/** | ||
* This class implements the {@link java.sql.Struct} interface. | ||
* <p> | ||
* {@code StructImpl} provides a simple implementation of a struct data type. | ||
* </p> | ||
*/ | ||
public class StructImpl implements Struct { | ||
private final String typeName; | ||
private final Object[] attributes; | ||
|
||
/** | ||
* Constructs a new {@code StructImpl} object with the specified parameter values. | ||
* | ||
* @param typeName the SQL type name of the struct | ||
* @param attributes the attributes of the struct, each attribute is a {@code Map.Entry<K, V>}(key-value pair) | ||
*/ | ||
public StructImpl(String typeName, Object[] attributes) { | ||
this.typeName = typeName; | ||
this.attributes = attributes; | ||
} | ||
|
||
/** | ||
* Returns the SQL type name of the struct. | ||
* | ||
* @return the SQL type name of the struct | ||
* @throws SQLException if a database access error occurs | ||
*/ | ||
@Override | ||
public String getSQLTypeName() throws SQLException { | ||
return this.typeName; | ||
} | ||
|
||
/** | ||
* Returns an array containing the attributes of the struct. | ||
* | ||
* @return an array containing the attribute values of the struct | ||
* @throws SQLException if a database access error occurs | ||
*/ | ||
@Override | ||
public Object[] getAttributes() throws SQLException { | ||
return attributes; | ||
} | ||
|
||
/** | ||
* @throws java.lang.UnsupportedOperationException because functionality is not supported yet | ||
*/ | ||
@Override | ||
public Object[] getAttributes(Map<String,Class<?>> map) throws SQLException { | ||
throw new java.lang.UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
/** | ||
* Compares this StructImpl object with the specified object for equality. | ||
* | ||
* <p> | ||
* Two StructImpl objects are considered equal if they have the same typeName, same number of attributes, | ||
* and contain the same attributes. | ||
* </p> | ||
* | ||
* @param obj the object to compare with this StructImpl object for equality. | ||
* @return {@code true} if the specified object is equal to this StructImpl object, {@code false} otherwise. | ||
*/ | ||
@Override | ||
public boolean equals(Object obj) { | ||
if (!(obj instanceof Struct)) { | ||
return false; | ||
} | ||
if (obj == this) { | ||
return true; | ||
} | ||
Struct other = (Struct) obj; | ||
try { | ||
if (!typeName.equals(other.getSQLTypeName()) || attributes.length != other.getAttributes().length) { | ||
return false; | ||
} | ||
List otherAttributes = Arrays.asList(other.getAttributes()); | ||
return otherAttributes.containsAll(Arrays.asList(attributes)); | ||
} | ||
catch (SQLException e) { | ||
return false; | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,40 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.jdbc.types; | ||
|
||
import java.sql.Struct; | ||
import java.util.Map; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.LinkedHashMap; | ||
import java.util.Collections; | ||
|
||
|
||
|
||
import org.opensearch.jdbc.StructImpl; | ||
|
||
public class StructType implements TypeHelper<Struct> { | ||
|
||
public static final StructType INSTANCE = new StructType(); | ||
|
||
private StructType() { | ||
|
||
} | ||
|
||
@Override | ||
public String getTypeName() { | ||
return "Struct"; | ||
} | ||
|
||
@Override | ||
public Struct fromValue(Object value, Map<String, Object> conversionParams) { | ||
akuzin1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (value == null || !(value instanceof Map<?, ?>)) { | ||
return null; | ||
} | ||
Map<String, Object> structKeyValues = (Map<String, Object>) value; | ||
akuzin1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return new StructImpl(getTypeName(), structKeyValues.entrySet().toArray()); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
57 changes: 57 additions & 0 deletions
57
src/test/java/org/opensearch/jdbc/types/StructTypeTests.java
This file contains hidden or 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,57 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
|
||
package org.opensearch.jdbc.types; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import org.junit.jupiter.api.Test; | ||
import org.opensearch.jdbc.types.StructType; | ||
import org.opensearch.jdbc.StructImpl; | ||
|
||
import java.sql.Struct; | ||
import java.sql.SQLException; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.HashMap; | ||
|
||
public class StructTypeTests { | ||
|
||
@Test | ||
public void testStructTypeFromValue() throws SQLException { | ||
Map<String, Object> attributes = new HashMap<String, Object>() {{ | ||
put("attribute1", "value1"); | ||
put("attribute2", 2); | ||
put("attribute3", 15.0); | ||
}}; | ||
|
||
Map<String, Object> attributes2 = new HashMap<String, Object>() {{ | ||
put("attribute1", "value1"); | ||
put("attribute2", 2); | ||
put("attribute3", 15.0); | ||
put("attribute4", "value4"); | ||
}}; | ||
|
||
Struct actualStruct = StructType.INSTANCE.fromValue(attributes, null); | ||
Struct actualStruct2 = StructType.INSTANCE.fromValue(attributes2, null); | ||
|
||
assertTrue(Arrays.equals(actualStruct.getAttributes(), attributes.entrySet().toArray())); | ||
assertEquals(actualStruct.getAttributes().length, 3); | ||
assertEquals(actualStruct2.getAttributes().length, 4); | ||
assertEquals(actualStruct, new StructImpl(StructType.INSTANCE.getTypeName(), attributes.entrySet().toArray())); | ||
assertNotEquals(actualStruct, actualStruct2); | ||
|
||
Map<String, Object> nestedAttributes = new HashMap<String, Object>() {{ | ||
put("struct", attributes); | ||
put("string", "hello"); | ||
put("int", 1); | ||
}}; | ||
|
||
Struct actualNestedStruct = StructType.INSTANCE.fromValue(nestedAttributes, null); | ||
assertTrue(Arrays.equals(actualNestedStruct.getAttributes(), nestedAttributes.entrySet().toArray())); | ||
assertEquals(actualNestedStruct, new StructImpl(StructType.INSTANCE.getTypeName(), nestedAttributes.entrySet().toArray())); | ||
assertNotEquals(actualStruct, actualNestedStruct); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.