Skip to content

Commit

Permalink
Add "trimPrefix" and "trimSuffix" SQL Methods (#1686)
Browse files Browse the repository at this point in the history
* Add trimPrefix and trimSuffix SQL methods

* add trimXXXfix tests
  • Loading branch information
gramian authored Aug 11, 2024
1 parent 8f4a748 commit 0e2edcd
Show file tree
Hide file tree
Showing 5 changed files with 280 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright © 2021-present Arcade Data Ltd ([email protected])
*
* 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 ([email protected])
* 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright © 2021-present Arcade Data Ltd ([email protected])
*
* 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 ([email protected])
* 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright © 2021-present Arcade Data Ltd ([email protected])
*
* 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 ([email protected])
* 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");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright © 2021-present Arcade Data Ltd ([email protected])
*
* 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 ([email protected])
* 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");
}
}

0 comments on commit 0e2edcd

Please sign in to comment.