1
1
// Copyright (c) Microsoft. All rights reserved.
2
2
package com .microsoft .semantickernel .connectors .data .jdbc ;
3
3
4
+ import com .microsoft .semantickernel .data .VectorStoreRecordMapper ;
4
5
import com .microsoft .semantickernel .data .recorddefinition .VectorStoreRecordDefinition ;
5
6
import com .microsoft .semantickernel .exceptions .SKException ;
6
7
import com .microsoft .semantickernel .data .recorddefinition .VectorStoreRecordField ;
24
25
import java .util .List ;
25
26
import java .util .Map ;
26
27
import java .util .stream .Collectors ;
28
+ import java .util .stream .Stream ;
27
29
28
30
public class JDBCVectorStoreDefaultQueryProvider
29
31
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 ;
33
32
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
+
35
49
supportedKeyTypes = new HashMap <>();
36
50
supportedKeyTypes .put (String .class , "VARCHAR(255)" );
37
51
@@ -54,19 +68,6 @@ public class JDBCVectorStoreDefaultQueryProvider
54
68
supportedVectorTypes .put (List .class , "TEXT" );
55
69
supportedVectorTypes .put (Collection .class , "TEXT" );
56
70
}
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
- }
70
71
71
72
/**
72
73
* Creates a new builder.
@@ -82,14 +83,9 @@ public static Builder builder() {
82
83
* @return the formatted wildcard string
83
84
*/
84
85
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 (", " ));
93
89
}
94
90
95
91
/**
@@ -102,6 +98,12 @@ protected String getQueryColumnsFromFields(List<VectorStoreRecordField> fields)
102
98
.collect (Collectors .joining (", " ));
103
99
}
104
100
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
+ */
105
107
protected String getColumnNamesAndTypes (List <Field > fields , Map <Class <?>, String > types ) {
106
108
List <String > columns = fields .stream ()
107
109
.map (field -> field .getName () + " " + types .get (field .getType ()))
@@ -114,6 +116,36 @@ protected String getCollectionTableName(String collectionName) {
114
116
return validateSQLidentifier (prefixForCollectionTables + collectionName );
115
117
}
116
118
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
+
117
149
/**
118
150
* Prepares the vector store.
119
151
* Executes any necessary setup steps for the vector store.
@@ -146,11 +178,12 @@ public void validateSupportedTypes(Class<?> recordClass,
146
178
VectorStoreRecordDefinition recordDefinition ) {
147
179
VectorStoreRecordDefinition .validateSupportedTypes (
148
180
Collections .singletonList (recordDefinition .getKeyDeclaredField (recordClass )),
149
- supportedKeyTypes .keySet ());
181
+ getSupportedKeyTypes () .keySet ());
150
182
VectorStoreRecordDefinition .validateSupportedTypes (
151
- recordDefinition .getDataDeclaredFields (recordClass ), supportedDataTypes .keySet ());
183
+ recordDefinition .getDataDeclaredFields (recordClass ), getSupportedDataTypes () .keySet ());
152
184
VectorStoreRecordDefinition .validateSupportedTypes (
153
- recordDefinition .getVectorDeclaredFields (recordClass ), supportedVectorTypes .keySet ());
185
+ recordDefinition .getVectorDeclaredFields (recordClass ),
186
+ getSupportedVectorTypes ().keySet ());
154
187
}
155
188
156
189
/**
@@ -194,8 +227,8 @@ public void createCollection(String collectionName, Class<?> recordClass,
194
227
String createStorageTable = "CREATE TABLE IF NOT EXISTS "
195
228
+ getCollectionTableName (collectionName )
196
229
+ " (" + keyDeclaredField .getName () + " VARCHAR(255) PRIMARY KEY, "
197
- + getColumnNamesAndTypes (dataDeclaredFields , supportedDataTypes ) + ", "
198
- + getColumnNamesAndTypes (vectorDeclaredFields , supportedVectorTypes ) + ");" ;
230
+ + getColumnNamesAndTypes (dataDeclaredFields , getSupportedDataTypes () ) + ", "
231
+ + getColumnNamesAndTypes (vectorDeclaredFields , getSupportedVectorTypes () ) + ");" ;
199
232
200
233
String insertCollectionQuery = "INSERT INTO " + validateSQLidentifier (collectionsTable )
201
234
+ " (collectionId) VALUES (?)" ;
@@ -284,7 +317,8 @@ public List<String> getCollectionNames() {
284
317
*/
285
318
@ Override
286
319
public <Record > List <Record > getRecords (String collectionName , List <String > keys ,
287
- VectorStoreRecordDefinition recordDefinition , JDBCVectorStoreRecordMapper <Record > mapper ,
320
+ VectorStoreRecordDefinition recordDefinition ,
321
+ VectorStoreRecordMapper <Record , ResultSet > mapper ,
288
322
GetRecordOptions options ) {
289
323
List <VectorStoreRecordField > fields ;
290
324
if (options == null || options .includeVectors ()) {
0 commit comments