@@ -166,9 +166,9 @@ public Map<Class<?>, String> getSupportedVectorTypes() {
166166 */
167167 @ Override
168168 public void prepareVectorStore () {
169- String createCollectionsTable = "CREATE TABLE IF NOT EXISTS "
170- + validateSQLidentifier ( collectionsTable )
171- + " (collectionId VARCHAR(255) PRIMARY KEY);" ;
169+ String createCollectionsTable = formatQuery (
170+ "CREATE TABLE IF NOT EXISTS %s (collectionId VARCHAR(255) PRIMARY KEY);" ,
171+ validateSQLidentifier ( collectionsTable )) ;
172172
173173 try (Connection connection = dataSource .getConnection ();
174174 PreparedStatement createTable = connection .prepareStatement (createCollectionsTable )) {
@@ -207,8 +207,8 @@ public void validateSupportedTypes(VectorStoreRecordDefinition recordDefinition)
207207 */
208208 @ Override
209209 public boolean collectionExists (String collectionName ) {
210- String query = "SELECT 1 FROM " + validateSQLidentifier ( collectionsTable )
211- + " WHERE collectionId = ?" ;
210+ String query = formatQuery ( "SELECT 1 FROM %s WHERE collectionId = ?" ,
211+ validateSQLidentifier ( collectionsTable )) ;
212212
213213 try (Connection connection = dataSource .getConnection ();
214214 PreparedStatement statement = connection .prepareStatement (query )) {
@@ -232,18 +232,19 @@ public boolean collectionExists(String collectionName) {
232232 public void createCollection (String collectionName ,
233233 VectorStoreRecordDefinition recordDefinition ) {
234234
235- String createStorageTable = "CREATE TABLE IF NOT EXISTS "
236- + getCollectionTableName (collectionName ) + " ("
237- + getKeyColumnName (recordDefinition .getKeyField ()) + " VARCHAR(255) PRIMARY KEY, "
238- + getColumnNamesAndTypes (new ArrayList <>(recordDefinition .getDataFields ()),
239- getSupportedDataTypes ())
240- + ", "
241- + getColumnNamesAndTypes (new ArrayList <>(recordDefinition .getVectorFields ()),
242- getSupportedVectorTypes ())
243- + ");" ;
235+ String createStorageTable = formatQuery ("CREATE TABLE IF NOT EXISTS %s ("
236+ + "%s VARCHAR(255) PRIMARY KEY, "
237+ + "%s, "
238+ + "%s);" ,
239+ getCollectionTableName (collectionName ),
240+ getKeyColumnName (recordDefinition .getKeyField ()),
241+ getColumnNamesAndTypes (new ArrayList <>(recordDefinition .getDataFields ()),
242+ getSupportedDataTypes ()),
243+ getColumnNamesAndTypes (new ArrayList <>(recordDefinition .getVectorFields ()),
244+ getSupportedVectorTypes ()));
244245
245- String insertCollectionQuery = "INSERT INTO " + validateSQLidentifier ( collectionsTable )
246- + " (collectionId) VALUES (?)" ;
246+ String insertCollectionQuery = formatQuery ( "INSERT INTO %s (collectionId) VALUES (?)" ,
247+ validateSQLidentifier ( collectionsTable )) ;
247248
248249 try (Connection connection = dataSource .getConnection ();
249250 PreparedStatement createTable = connection .prepareStatement (createStorageTable )) {
@@ -269,9 +270,10 @@ public void createCollection(String collectionName,
269270 */
270271 @ Override
271272 public void deleteCollection (String collectionName ) {
272- String deleteCollectionOperation = "DELETE FROM " + validateSQLidentifier (collectionsTable )
273- + " WHERE collectionId = ?" ;
274- String dropTableOperation = "DROP TABLE " + getCollectionTableName (collectionName );
273+ String deleteCollectionOperation = formatQuery ("DELETE FROM %s WHERE collectionId = ?" ,
274+ validateSQLidentifier (collectionsTable ));
275+ String dropTableOperation = formatQuery ("DROP TABLE %s" ,
276+ getCollectionTableName (collectionName ));
275277
276278 try (Connection connection = dataSource .getConnection ();
277279 PreparedStatement deleteCollection = connection
@@ -298,7 +300,8 @@ public void deleteCollection(String collectionName) {
298300 */
299301 @ Override
300302 public List <String > getCollectionNames () {
301- String query = "SELECT collectionId FROM " + validateSQLidentifier (collectionsTable );
303+ String query = formatQuery ("SELECT collectionId FROM %s" ,
304+ validateSQLidentifier (collectionsTable ));
302305
303306 try (Connection connection = dataSource .getConnection ();
304307 PreparedStatement statement = connection .prepareStatement (query )) {
@@ -339,10 +342,11 @@ public <Record> List<Record> getRecords(String collectionName, List<String> keys
339342 fields = recordDefinition .getNonVectorFields ();
340343 }
341344
342- String query = "SELECT " + getQueryColumnsFromFields (fields )
343- + " FROM " + getCollectionTableName (collectionName )
344- + " WHERE " + getKeyColumnName (recordDefinition .getKeyField ())
345- + " IN (" + getWildcardString (keys .size ()) + ")" ;
345+ String query = formatQuery ("SELECT %s FROM %s WHERE %s IN (%s)" ,
346+ getQueryColumnsFromFields (fields ),
347+ getCollectionTableName (collectionName ),
348+ getKeyColumnName (recordDefinition .getKeyField ()),
349+ getWildcardString (keys .size ()));
346350
347351 try (Connection connection = dataSource .getConnection ();
348352 PreparedStatement statement = connection .prepareStatement (query )) {
@@ -382,9 +386,10 @@ public void upsertRecords(String collectionName, List<?> records,
382386 @ Override
383387 public void deleteRecords (String collectionName , List <String > keys ,
384388 VectorStoreRecordDefinition recordDefinition , DeleteRecordOptions options ) {
385- String query = "DELETE FROM " + getCollectionTableName (collectionName )
386- + " WHERE " + getKeyColumnName (recordDefinition .getKeyField ())
387- + " IN (" + getWildcardString (keys .size ()) + ")" ;
389+ String query = formatQuery ("DELETE FROM %s WHERE %s IN (%s)" ,
390+ getCollectionTableName (collectionName ),
391+ getKeyColumnName (recordDefinition .getKeyField ()),
392+ getWildcardString (keys .size ()));
388393
389394 try (Connection connection = dataSource .getConnection ();
390395 PreparedStatement statement = connection .prepareStatement (query )) {
@@ -412,6 +417,17 @@ public static String validateSQLidentifier(String identifier) {
412417 throw new SKException ("Invalid SQL identifier: " + identifier );
413418 }
414419
420+ /**
421+ * Formats a query.
422+ *
423+ * @param query the query
424+ * @param args the arguments
425+ * @return the formatted query
426+ */
427+ public String formatQuery (String query , String ... args ) {
428+ return String .format (query , (Object []) args );
429+ }
430+
415431 /**
416432 * The builder for {@link JDBCVectorStoreDefaultQueryProvider}.
417433 */
0 commit comments