-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "trimPrefix" and "trimSuffix" SQL Methods (#1686)
* Add trimPrefix and trimSuffix SQL methods * add trimXXXfix tests
- Loading branch information
Showing
5 changed files
with
280 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
engine/src/main/java/com/arcadedb/query/sql/method/string/SQLMethodTrimPrefix.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
engine/src/main/java/com/arcadedb/query/sql/method/string/SQLMethodTrimSuffix.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
engine/src/test/java/com/arcadedb/query/sql/method/string/SQLMethodTrimPrefixTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
engine/src/test/java/com/arcadedb/query/sql/method/string/SQLMethodTrimSuffixTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |