Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@

// String
import com.arcadedb.query.sql.method.string.SQLMethodAppend;
import com.arcadedb.query.sql.method.string.SQLMethodCapitalize;
import com.arcadedb.query.sql.method.string.SQLMethodCharAt;
import com.arcadedb.query.sql.method.string.SQLMethodFormat;
import com.arcadedb.query.sql.method.string.SQLMethodIndexOf;
Expand Down Expand Up @@ -137,6 +138,7 @@ public DefaultSQLMethodFactory() {

// String
register(SQLMethodAppend.NAME, new SQLMethodAppend());
register(SQLMethodCapitalize.NAME, new SQLMethodCapitalize());
register(SQLMethodCharAt.NAME, new SQLMethodCharAt());
register(SQLMethodFormat.NAME, new SQLMethodFormat());
register(SQLMethodIndexOf.NAME, new SQLMethodIndexOf());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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;

import java.util.*;
import java.util.regex.Pattern;

/**
* @author Christian Himpe (gramian)
* @author Luca Garulli (l.garulli--(at)--gmail.com)
*/
public class SQLMethodCapitalize extends AbstractSQLMethod {

public static final String NAME = "capitalize";

public SQLMethodCapitalize() {
super(NAME);
}

@Override
public Object execute(final Object iThis, final Identifiable iCurrentRecord, final CommandContext iContext, final Object ioResult, final Object[] iParams) {
return ioResult != null ? Pattern.compile("\\b(.)(.*?)\\b")
.matcher(ioResult.toString())
.replaceAll(match -> match.group(1).toUpperCase(Locale.ENGLISH) +
match.group(2).toLowerCase())
: null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public SQLMethodToLowerCase() {
}

@Override
public Object execute(final Object iThis, final Identifiable iCurrentRecord, final CommandContext iContext, Object ioResult, final Object[] iParams) {
ioResult = ioResult != null ? ioResult.toString().toLowerCase() : null;
return ioResult;
public Object execute(final Object iThis, final Identifiable iCurrentRecord, final CommandContext iContext, final Object ioResult, final Object[] iParams) {
return ioResult != null ? ioResult.toString().toLowerCase() : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public SQLMethodToUpperCase() {
}

@Override
public Object execute(final Object iThis, final Identifiable iCurrentRecord, final CommandContext iContext, Object ioResult, final Object[] iParams) {
ioResult = ioResult != null ? ioResult.toString().toUpperCase(Locale.ENGLISH) : null;
return ioResult;
public Object execute(final Object iThis, final Identifiable iCurrentRecord, final CommandContext iContext, final Object ioResult, final Object[] iParams) {
return ioResult != null ? ioResult.toString().toUpperCase(Locale.ENGLISH) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public SQLMethodTrim() {
}

@Override
public Object execute(final Object iThis, final Identifiable iCurrentRecord, final CommandContext iContext, Object ioResult, final Object[] iParams) {
ioResult = ioResult != null ? ioResult.toString().trim() : null;
return ioResult;
public Object execute(final Object iThis, final Identifiable iCurrentRecord, final CommandContext iContext, final Object ioResult, final Object[] iParams) {
return ioResult != null ? ioResult.toString().trim() : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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 SQLMethodCapitalizeTest {

private SQLMethod method;

@BeforeEach
void setUp() {
method = new SQLMethodCapitalize();
}

@Test
void testNullReturnedAsNull() {
final Object result = method.execute(null, null, null, null, null);
assertThat(result).isNull();
}


@Test
void testCapitalizeEmpty() {
final Object result = method.execute(null, null, null, "", null);
assertThat(result).isEqualTo("");

}

@Test
void testCapitalizeIdentity() {
final Object result = method.execute(null, null, null, "Capitalize This", null);
assertThat(result).isEqualTo("Capitalize This");

}

@Test
void testCapitalizeLower() {
final Object result = method.execute(null, null, null, "CAPITALIZE THIS", null);
assertThat(result).isEqualTo("Capitalize This");

}

@Test
void testCapitalizeUpper() {
final Object result = method.execute(null, null, null, "capitalize this", null);
assertThat(result).isEqualTo("Capitalize This");

}

@Test
void testCapitalizeSingle() {
final Object result = method.execute(null, null, null, "c t", null);
assertThat(result).isEqualTo("C T");

}

@Test
void testCapitalizeNumbers() {
final Object result = method.execute(null, null, null, "111 222", null);
assertThat(result).isEqualTo("111 222");

}
}