From 0e2edcd1950db3b88d306c0311c8eb4e0283c0bd Mon Sep 17 00:00:00 2001 From: Christian Himpe Date: Sun, 11 Aug 2024 17:32:01 +0200 Subject: [PATCH] Add "trimPrefix" and "trimSuffix" SQL Methods (#1686) * Add trimPrefix and trimSuffix SQL methods * add trimXXXfix tests --- .../sql/method/DefaultSQLMethodFactory.java | 4 + .../method/string/SQLMethodTrimPrefix.java | 50 +++++++++++ .../method/string/SQLMethodTrimSuffix.java | 50 +++++++++++ .../string/SQLMethodTrimPrefixTest.java | 88 +++++++++++++++++++ .../string/SQLMethodTrimSuffixTest.java | 88 +++++++++++++++++++ 5 files changed, 280 insertions(+) create mode 100644 engine/src/main/java/com/arcadedb/query/sql/method/string/SQLMethodTrimPrefix.java create mode 100644 engine/src/main/java/com/arcadedb/query/sql/method/string/SQLMethodTrimSuffix.java create mode 100644 engine/src/test/java/com/arcadedb/query/sql/method/string/SQLMethodTrimPrefixTest.java create mode 100644 engine/src/test/java/com/arcadedb/query/sql/method/string/SQLMethodTrimSuffixTest.java diff --git a/engine/src/main/java/com/arcadedb/query/sql/method/DefaultSQLMethodFactory.java b/engine/src/main/java/com/arcadedb/query/sql/method/DefaultSQLMethodFactory.java index 8ca8206e9..b6b8131b3 100644 --- a/engine/src/main/java/com/arcadedb/query/sql/method/DefaultSQLMethodFactory.java +++ b/engine/src/main/java/com/arcadedb/query/sql/method/DefaultSQLMethodFactory.java @@ -68,6 +68,8 @@ import com.arcadedb.query.sql.method.string.SQLMethodLength; import com.arcadedb.query.sql.method.string.SQLMethodNormalize; import com.arcadedb.query.sql.method.string.SQLMethodPrefix; +import com.arcadedb.query.sql.method.string.SQLMethodTrimPrefix; +import com.arcadedb.query.sql.method.string.SQLMethodTrimSuffix; import com.arcadedb.query.sql.method.string.SQLMethodReplace; import com.arcadedb.query.sql.method.string.SQLMethodRight; import com.arcadedb.query.sql.method.string.SQLMethodSplit; @@ -144,6 +146,8 @@ public DefaultSQLMethodFactory() { register(SQLMethodLength.NAME, new SQLMethodLength()); register(SQLMethodNormalize.NAME, new SQLMethodNormalize()); register(SQLMethodPrefix.NAME, new SQLMethodPrefix()); + register(SQLMethodTrimPrefix.NAME, new SQLMethodTrimPrefix()); + register(SQLMethodTrimSuffix.NAME, new SQLMethodTrimSuffix()); register(SQLMethodReplace.NAME, new SQLMethodReplace()); register(SQLMethodRight.NAME, new SQLMethodRight()); register(SQLMethodSplit.NAME, new SQLMethodSplit()); diff --git a/engine/src/main/java/com/arcadedb/query/sql/method/string/SQLMethodTrimPrefix.java b/engine/src/main/java/com/arcadedb/query/sql/method/string/SQLMethodTrimPrefix.java new file mode 100644 index 000000000..22f9f0301 --- /dev/null +++ b/engine/src/main/java/com/arcadedb/query/sql/method/string/SQLMethodTrimPrefix.java @@ -0,0 +1,50 @@ +/* + * Copyright © 2021-present Arcade Data Ltd (info@arcadedata.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-FileCopyrightText: 2021-present Arcade Data Ltd (info@arcadedata.com) + * SPDX-License-Identifier: Apache-2.0 + */ +package com.arcadedb.query.sql.method.string; + +import com.arcadedb.database.Identifiable; +import com.arcadedb.query.sql.executor.CommandContext; +import com.arcadedb.query.sql.method.AbstractSQLMethod; + +/** + * @author Johann Sorel (Geomatys) + * @author Luca Garulli (l.garulli--(at)--gmail.com) + */ +public class SQLMethodTrimPrefix extends AbstractSQLMethod { + + public static final String NAME = "trimprefix"; + + public SQLMethodTrimPrefix() { + super(NAME, 1); + } + + @Override + public Object execute( final Object value, final Identifiable iRecord, final CommandContext iContext, final Object[] iParams) { + if (value == null || null == iParams || null == iParams[0]) + return value; + + final String strval = value.toString(); + final String prefix = iParams[0].toString(); + + if (strval.startsWith(prefix)) + return strval.substring(prefix.length()); + else + return strval; + } +} diff --git a/engine/src/main/java/com/arcadedb/query/sql/method/string/SQLMethodTrimSuffix.java b/engine/src/main/java/com/arcadedb/query/sql/method/string/SQLMethodTrimSuffix.java new file mode 100644 index 000000000..8c0383d97 --- /dev/null +++ b/engine/src/main/java/com/arcadedb/query/sql/method/string/SQLMethodTrimSuffix.java @@ -0,0 +1,50 @@ +/* + * Copyright © 2021-present Arcade Data Ltd (info@arcadedata.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-FileCopyrightText: 2021-present Arcade Data Ltd (info@arcadedata.com) + * SPDX-License-Identifier: Apache-2.0 + */ +package com.arcadedb.query.sql.method.string; + +import com.arcadedb.database.Identifiable; +import com.arcadedb.query.sql.executor.CommandContext; +import com.arcadedb.query.sql.method.AbstractSQLMethod; + +/** + * @author Johann Sorel (Geomatys) + * @author Luca Garulli (l.garulli--(at)--gmail.com) + */ +public class SQLMethodTrimSuffix extends AbstractSQLMethod { + + public static final String NAME = "trimsuffix"; + + public SQLMethodTrimSuffix() { + super(NAME, 1); + } + + @Override + public Object execute( final Object value, final Identifiable iRecord, final CommandContext iContext, final Object[] iParams) { + if (value == null || null == iParams || null == iParams[0]) + return value; + + final String strval = value.toString(); + final String suffix = iParams[0].toString(); + + if (strval.endsWith(suffix)) + return strval.substring(0,strval.length() - suffix.length()); + else + return strval; + } +} diff --git a/engine/src/test/java/com/arcadedb/query/sql/method/string/SQLMethodTrimPrefixTest.java b/engine/src/test/java/com/arcadedb/query/sql/method/string/SQLMethodTrimPrefixTest.java new file mode 100644 index 000000000..b24a53972 --- /dev/null +++ b/engine/src/test/java/com/arcadedb/query/sql/method/string/SQLMethodTrimPrefixTest.java @@ -0,0 +1,88 @@ +/* + * Copyright © 2021-present Arcade Data Ltd (info@arcadedata.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-FileCopyrightText: 2021-present Arcade Data Ltd (info@arcadedata.com) + * SPDX-License-Identifier: Apache-2.0 + */ +package com.arcadedb.query.sql.method.string; + +import com.arcadedb.query.sql.executor.SQLMethod; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class SQLMethodTrimPrefixTest { + private SQLMethod method; + + @BeforeEach + void setUp() { + method = new SQLMethodTrimPrefix(); + } + + @Test + void testAllNullPreservation() { + final Object result = method.execute(null, null, null, null); + assertThat(result).isNull(); + } + + @Test + void testBaseNullPreservation() { + final Object result = method.execute(null, null, null, new Object[] {"Bye"}); + assertThat(result).isNull(); + } + + @Test + void testArgNullPreservation() { + final Object result = method.execute("Hello World", null, null, null); + assertThat(result).isEqualTo("Hello World"); + } + + @Test + void testIdentity() { + final Object result = method.execute("Hello World", null, null, new Object[] {"Bye"}); + assertThat(result).isEqualTo("Hello World"); + } + + @Test + void testTrim() { + final Object result = method.execute("Hello World", null, null, new Object[] {"Hello"}); + assertThat(result).isEqualTo(" World"); + } + + @Test + void testFull() { + final Object result = method.execute("Hello World", null, null, new Object[] {"Hello World"}); + assertThat(result).isEqualTo(""); + } + + @Test + void testEmptyArg() { + final Object result = method.execute("Hello World", null, null, new Object[] {""}); + assertThat(result).isEqualTo("Hello World"); + } + + @Test + void testEmptyBase() { + final Object result = method.execute("", null, null, new Object[] {"Bye"}); + assertThat(result).isEqualTo(""); + } + + @Test + void testNonString() { + final Object result = method.execute(123, null, null, new Object[] {"Bye"}); + assertThat(result).isEqualTo("123"); + } +} diff --git a/engine/src/test/java/com/arcadedb/query/sql/method/string/SQLMethodTrimSuffixTest.java b/engine/src/test/java/com/arcadedb/query/sql/method/string/SQLMethodTrimSuffixTest.java new file mode 100644 index 000000000..eef1e2d7a --- /dev/null +++ b/engine/src/test/java/com/arcadedb/query/sql/method/string/SQLMethodTrimSuffixTest.java @@ -0,0 +1,88 @@ +/* + * Copyright © 2021-present Arcade Data Ltd (info@arcadedata.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-FileCopyrightText: 2021-present Arcade Data Ltd (info@arcadedata.com) + * SPDX-License-Identifier: Apache-2.0 + */ +package com.arcadedb.query.sql.method.string; + +import com.arcadedb.query.sql.executor.SQLMethod; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class SQLMethodTrimSuffixTest { + private SQLMethod method; + + @BeforeEach + void setUp() { + method = new SQLMethodTrimSuffix(); + } + + @Test + void testAllNullPreservation() { + final Object result = method.execute(null, null, null, null); + assertThat(result).isNull(); + } + + @Test + void testBaseNullPreservation() { + final Object result = method.execute(null, null, null, new Object[] {"Bye"}); + assertThat(result).isNull(); + } + + @Test + void testArgNullPreservation() { + final Object result = method.execute("Hello World", null, null, null); + assertThat(result).isEqualTo("Hello World"); + } + + @Test + void testIdentity() { + final Object result = method.execute("Hello World", null, null, new Object[] {"Bye"}); + assertThat(result).isEqualTo("Hello World"); + } + + @Test + void testTrim() { + final Object result = method.execute("Hello World", null, null, new Object[] {"World"}); + assertThat(result).isEqualTo("Hello "); + } + + @Test + void testFull() { + final Object result = method.execute("Hello World", null, null, new Object[] {"Hello World"}); + assertThat(result).isEqualTo(""); + } + + @Test + void testEmptyArg() { + final Object result = method.execute("Hello World", null, null, new Object[] {""}); + assertThat(result).isEqualTo("Hello World"); + } + + @Test + void testEmptyBase() { + final Object result = method.execute("", null, null, new Object[] {"Bye"}); + assertThat(result).isEqualTo(""); + } + + @Test + void testNonString() { + final Object result = method.execute(123, null, null, new Object[] {"Bye"}); + assertThat(result).isEqualTo("123"); + } +}