From 224b7f1b1900d27010c547c6c93741e4a0e02084 Mon Sep 17 00:00:00 2001 From: vlowingkloude <126703177+vlowingkloude@users.noreply.github.com> Date: Thu, 3 Oct 2024 12:35:35 +0200 Subject: [PATCH] fix string replace function --- .../org/polypheny/db/functions/Functions.java | 7 +++++++ .../db/sql/fun/StringFunctionsTest.java | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/core/src/main/java/org/polypheny/db/functions/Functions.java b/core/src/main/java/org/polypheny/db/functions/Functions.java index 3fb9735e70..c30e4a8380 100644 --- a/core/src/main/java/org/polypheny/db/functions/Functions.java +++ b/core/src/main/java/org/polypheny/db/functions/Functions.java @@ -1985,6 +1985,13 @@ public static String replace( String s, String search, String replacement ) { return s.replace( search, replacement ); } + /** + * SQL {@code REPLACE(string, search, replacement)} function. + */ + public static PolyString replace( PolyString s, PolyString search, PolyString replacement ) { + return PolyString.of( s.value.replace( search.value, replacement.value ) ); + } + /** * Helper for "array element reference". Caller has already ensured that array and index are not null. Index is 1-based, per SQL. diff --git a/dbms/src/test/java/org/polypheny/db/sql/fun/StringFunctionsTest.java b/dbms/src/test/java/org/polypheny/db/sql/fun/StringFunctionsTest.java index 401c40b3cb..314517e17d 100644 --- a/dbms/src/test/java/org/polypheny/db/sql/fun/StringFunctionsTest.java +++ b/dbms/src/test/java/org/polypheny/db/sql/fun/StringFunctionsTest.java @@ -86,6 +86,22 @@ public static void stop() throws SQLException { // --------------- Tests --------------- + @Test + public void stringReplaceTest() throws SQLException { + try ( TestHelper.JdbcConnection polyphenyDbConnection = new TestHelper.JdbcConnection( true ) ) { + Connection connection = polyphenyDbConnection.getConnection(); + try ( Statement statement = connection.createStatement() ) { + List expectedResult = ImmutableList.of( + new Object[]{"Hello"} + ); + TestHelper.checkResultSet( + statement.executeQuery( "SELECT replace('Hi', 'i', 'ello')" ), + expectedResult, + true ); + } + } + } + @Test public void concatenatesTwoString() throws SQLException {