-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Array Support to driver as well as Unit Tests for new functiona…
…lity Signed-off-by: Andrey Kuzin <akuzin@amazon.com>
- Loading branch information
Showing
8 changed files
with
260 additions
and
14 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
|
||
package org.opensearch.jdbc; | ||
|
||
import java.sql.JDBCType; | ||
import java.sql.SQLException; | ||
import java.sql.SQLFeatureNotSupportedException; | ||
import java.sql.ResultSet; | ||
import java.sql.Struct; | ||
import java.sql.Array; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
|
||
|
||
/** | ||
* This class implements the {@link java.sql.Struct} interface. | ||
* <p> | ||
* {@code StructImpl} provides a simple implementation of a struct data type. | ||
* </p> | ||
*/ | ||
public class ArrayImpl implements Array { | ||
private ArrayList<Object> arrayData; | ||
private JDBCType baseTypeName; | ||
|
||
public ArrayImpl(ArrayList<Object> arrayData, JDBCType baseTypeName) { | ||
this.arrayData = arrayData; | ||
this.baseTypeName = baseTypeName; | ||
} | ||
|
||
@Override | ||
public String getBaseTypeName() throws SQLException { | ||
return this.baseTypeName.toString(); | ||
} | ||
|
||
@Override | ||
public int getBaseType() throws SQLException { | ||
throw new SQLFeatureNotSupportedException("getBaseType() is not supported"); | ||
} | ||
|
||
@Override | ||
public Object getArray() throws SQLException { | ||
return arrayData.toArray(); | ||
} | ||
|
||
@Override | ||
public Object getArray(Map<String, Class<?>> map) throws SQLException { | ||
throw new SQLFeatureNotSupportedException("getArray(map) is not supported"); | ||
} | ||
|
||
@Override | ||
public Object getArray(long index, int count) throws SQLException { | ||
if (index < 1 || index > arrayData.size() || index + count - 1 > arrayData.size() || count < 0) { | ||
throw new SQLException("Invalid index or count"); | ||
} | ||
int fromIndex = (int) index - 1; | ||
int toIndex = fromIndex + count; | ||
return arrayData.subList(fromIndex, toIndex).toArray(); | ||
} | ||
|
||
@Override | ||
public Object getArray(long index, int count, Map<String, Class<?>> map) throws SQLException { | ||
throw new SQLFeatureNotSupportedException("getArray(index, count, map) is not supported"); | ||
} | ||
|
||
@Override | ||
public ResultSet getResultSet() throws SQLException { | ||
throw new SQLFeatureNotSupportedException("getResultSet() is not supported"); | ||
} | ||
|
||
@Override | ||
public ResultSet getResultSet(Map<String, Class<?>> map) throws SQLException { | ||
throw new SQLFeatureNotSupportedException("getResultSet(map) is not supported"); | ||
} | ||
|
||
@Override | ||
public ResultSet getResultSet(long index, int count) throws SQLException { | ||
throw new SQLFeatureNotSupportedException("getResultSet(index, count) is not supported"); | ||
} | ||
|
||
@Override | ||
public ResultSet getResultSet(long index, int count, Map<String, Class<?>> map) throws SQLException { | ||
throw new SQLFeatureNotSupportedException("getResultSet(index, count, map) is not supported"); | ||
} | ||
|
||
@Override | ||
public void free() throws SQLException { | ||
arrayData = null; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (!(obj instanceof Array)) { | ||
return false; | ||
} | ||
if (obj == this) { | ||
return true; | ||
} | ||
Array other = (Array) obj; | ||
try { | ||
Object[] myArray = (Object[]) this.getArray(); | ||
Object[] otherArray = (Object[]) this.getArray(); | ||
|
||
if (!(this.getBaseTypeName().equals(other.getBaseTypeName())) || myArray.length != otherArray.length) { | ||
return false; | ||
} | ||
return Arrays.equals(myArray, otherArray); | ||
} | ||
catch (SQLException e) { | ||
return false; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.jdbc.types; | ||
|
||
import java.sql.Array; | ||
import java.sql.JDBCType; | ||
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.ArrayImpl; | ||
|
||
public class ArrayType implements TypeHelper<Array> { | ||
|
||
public static final ArrayType INSTANCE = new ArrayType(); | ||
|
||
private ArrayType() { | ||
|
||
} | ||
|
||
@Override | ||
public String getTypeName() { | ||
return "Array"; | ||
} | ||
|
||
@Override | ||
public Array fromValue(Object value, Map<String, Object> conversionParams) { | ||
if (value == null || !(value instanceof ArrayList)) { | ||
return null; | ||
} | ||
|
||
JDBCType baseType = conversionParams != null ? (JDBCType) conversionParams.get("baseType") : JDBCType.OTHER; | ||
|
||
return new ArrayImpl((ArrayList) value, baseType); | ||
|
||
} | ||
} | ||
|
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
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
Oops, something went wrong.