11// Copyright (c) Microsoft. All rights reserved.
22package com .microsoft .semantickernel .connectors .data .jdbc ;
33
4+ import com .microsoft .semantickernel .data .VectorStoreRecordMapper ;
45import com .microsoft .semantickernel .data .recorddefinition .VectorStoreRecordDefinition ;
56import com .microsoft .semantickernel .exceptions .SKException ;
67import com .microsoft .semantickernel .data .recorddefinition .VectorStoreRecordField ;
2425import java .util .List ;
2526import java .util .Map ;
2627import java .util .stream .Collectors ;
28+ import java .util .stream .Stream ;
2729
2830public class JDBCVectorStoreDefaultQueryProvider
2931 implements JDBCVectorStoreQueryProvider {
30- private static final Map <Class <?>, String > supportedKeyTypes ;
31- private static final Map <Class <?>, String > supportedDataTypes ;
32- private static final Map <Class <?>, String > supportedVectorTypes ;
3332
34- static {
33+ private Map <Class <?>, String > supportedKeyTypes ;
34+ private Map <Class <?>, String > supportedDataTypes ;
35+ private Map <Class <?>, String > supportedVectorTypes ;
36+ private final DataSource dataSource ;
37+ private final String collectionsTable ;
38+ private final String prefixForCollectionTables ;
39+
40+ @ SuppressFBWarnings ("EI_EXPOSE_REP2" ) // DataSource is not exposed
41+ protected JDBCVectorStoreDefaultQueryProvider (
42+ @ Nonnull DataSource dataSource ,
43+ @ Nonnull String collectionsTable ,
44+ @ Nonnull String prefixForCollectionTables ) {
45+ this .dataSource = dataSource ;
46+ this .collectionsTable = collectionsTable ;
47+ this .prefixForCollectionTables = prefixForCollectionTables ;
48+
3549 supportedKeyTypes = new HashMap <>();
3650 supportedKeyTypes .put (String .class , "VARCHAR(255)" );
3751
@@ -54,19 +68,6 @@ public class JDBCVectorStoreDefaultQueryProvider
5468 supportedVectorTypes .put (List .class , "TEXT" );
5569 supportedVectorTypes .put (Collection .class , "TEXT" );
5670 }
57- private final DataSource dataSource ;
58- private final String collectionsTable ;
59- private final String prefixForCollectionTables ;
60-
61- @ SuppressFBWarnings ("EI_EXPOSE_REP2" ) // DataSource is not exposed
62- protected JDBCVectorStoreDefaultQueryProvider (
63- @ Nonnull DataSource dataSource ,
64- @ Nonnull String collectionsTable ,
65- @ Nonnull String prefixForCollectionTables ) {
66- this .dataSource = dataSource ;
67- this .collectionsTable = collectionsTable ;
68- this .prefixForCollectionTables = prefixForCollectionTables ;
69- }
7071
7172 /**
7273 * Creates a new builder.
@@ -82,14 +83,9 @@ public static Builder builder() {
8283 * @return the formatted wildcard string
8384 */
8485 protected String getWildcardString (int wildcards ) {
85- StringBuilder wildcardString = new StringBuilder ();
86- for (int i = 0 ; i < wildcards ; ++i ) {
87- wildcardString .append ("?" );
88- if (i < wildcards - 1 ) {
89- wildcardString .append (", " );
90- }
91- }
92- return wildcardString .toString ();
86+ return Stream .generate (() -> "?" )
87+ .limit (wildcards )
88+ .collect (Collectors .joining (", " ));
9389 }
9490
9591 /**
@@ -102,6 +98,12 @@ protected String getQueryColumnsFromFields(List<VectorStoreRecordField> fields)
10298 .collect (Collectors .joining (", " ));
10399 }
104100
101+ /**
102+ * Formats the column names and types for a table.
103+ * @param fields the fields
104+ * @param types the types
105+ * @return the formatted column names and types
106+ */
105107 protected String getColumnNamesAndTypes (List <Field > fields , Map <Class <?>, String > types ) {
106108 List <String > columns = fields .stream ()
107109 .map (field -> field .getName () + " " + types .get (field .getType ()))
@@ -114,6 +116,36 @@ protected String getCollectionTableName(String collectionName) {
114116 return validateSQLidentifier (prefixForCollectionTables + collectionName );
115117 }
116118
119+ /**
120+ * Gets the supported key types and their corresponding SQL types.
121+ *
122+ * @return the supported key types
123+ */
124+ @ Override
125+ public Map <Class <?>, String > getSupportedKeyTypes () {
126+ return new HashMap <>(this .supportedKeyTypes );
127+ }
128+
129+ /**
130+ * Gets the supported data types and their corresponding SQL types.
131+ *
132+ * @return the supported data types
133+ */
134+ @ Override
135+ public Map <Class <?>, String > getSupportedDataTypes () {
136+ return new HashMap <>(this .supportedDataTypes );
137+ }
138+
139+ /**
140+ * Gets the supported vector types and their corresponding SQL types.
141+ *
142+ * @return the supported vector types
143+ */
144+ @ Override
145+ public Map <Class <?>, String > getSupportedVectorTypes () {
146+ return new HashMap <>(this .supportedVectorTypes );
147+ }
148+
117149 /**
118150 * Prepares the vector store.
119151 * Executes any necessary setup steps for the vector store.
@@ -146,11 +178,12 @@ public void validateSupportedTypes(Class<?> recordClass,
146178 VectorStoreRecordDefinition recordDefinition ) {
147179 VectorStoreRecordDefinition .validateSupportedTypes (
148180 Collections .singletonList (recordDefinition .getKeyDeclaredField (recordClass )),
149- supportedKeyTypes .keySet ());
181+ getSupportedKeyTypes () .keySet ());
150182 VectorStoreRecordDefinition .validateSupportedTypes (
151- recordDefinition .getDataDeclaredFields (recordClass ), supportedDataTypes .keySet ());
183+ recordDefinition .getDataDeclaredFields (recordClass ), getSupportedDataTypes () .keySet ());
152184 VectorStoreRecordDefinition .validateSupportedTypes (
153- recordDefinition .getVectorDeclaredFields (recordClass ), supportedVectorTypes .keySet ());
185+ recordDefinition .getVectorDeclaredFields (recordClass ),
186+ getSupportedVectorTypes ().keySet ());
154187 }
155188
156189 /**
@@ -194,8 +227,8 @@ public void createCollection(String collectionName, Class<?> recordClass,
194227 String createStorageTable = "CREATE TABLE IF NOT EXISTS "
195228 + getCollectionTableName (collectionName )
196229 + " (" + keyDeclaredField .getName () + " VARCHAR(255) PRIMARY KEY, "
197- + getColumnNamesAndTypes (dataDeclaredFields , supportedDataTypes ) + ", "
198- + getColumnNamesAndTypes (vectorDeclaredFields , supportedVectorTypes ) + ");" ;
230+ + getColumnNamesAndTypes (dataDeclaredFields , getSupportedDataTypes () ) + ", "
231+ + getColumnNamesAndTypes (vectorDeclaredFields , getSupportedVectorTypes () ) + ");" ;
199232
200233 String insertCollectionQuery = "INSERT INTO " + validateSQLidentifier (collectionsTable )
201234 + " (collectionId) VALUES (?)" ;
@@ -284,7 +317,8 @@ public List<String> getCollectionNames() {
284317 */
285318 @ Override
286319 public <Record > List <Record > getRecords (String collectionName , List <String > keys ,
287- VectorStoreRecordDefinition recordDefinition , JDBCVectorStoreRecordMapper <Record > mapper ,
320+ VectorStoreRecordDefinition recordDefinition ,
321+ VectorStoreRecordMapper <Record , ResultSet > mapper ,
288322 GetRecordOptions options ) {
289323 List <VectorStoreRecordField > fields ;
290324 if (options == null || options .includeVectors ()) {
0 commit comments