import org.sonar.db.dialect.Dialect;
import org.sonar.db.dialect.Oracle;
-import static org.sonar.db.version.ColumnDefValidation.validateColumnName;
+import static org.sonar.db.version.Validations.validateColumnName;
public class BigIntegerColumnDef extends AbstractColumnDef {
import org.sonar.db.dialect.Oracle;
import org.sonar.db.dialect.PostgreSql;
-import static org.sonar.db.version.ColumnDefValidation.validateColumnName;
+import static org.sonar.db.version.Validations.validateColumnName;
public class BlobColumnDef extends AbstractColumnDef {
public BlobColumnDef(Builder builder) {
import org.sonar.db.dialect.Oracle;
import org.sonar.db.dialect.PostgreSql;
-import static org.sonar.db.version.ColumnDefValidation.validateColumnName;
+import static org.sonar.db.version.Validations.validateColumnName;
/**
* Used to define VARCHAR column
import org.sonar.db.dialect.Oracle;
import org.sonar.db.dialect.PostgreSql;
-import static org.sonar.db.version.ColumnDefValidation.validateColumnName;
+import static org.sonar.db.version.Validations.validateColumnName;
/**
* Used to define CLOB columns
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.db.version;
-
-import com.google.common.base.CharMatcher;
-import javax.annotation.Nullable;
-
-import static com.google.common.base.CharMatcher.JAVA_LOWER_CASE;
-import static com.google.common.base.Preconditions.checkArgument;
-import static java.util.Objects.requireNonNull;
-
-public class ColumnDefValidation {
-
- private ColumnDefValidation() {
- // Only static stuff here
- }
-
- public static String validateColumnName(@Nullable String columnName) {
- String name = requireNonNull(columnName, "Column name cannot be null");
- checkArgument(JAVA_LOWER_CASE.or(CharMatcher.anyOf("_")).or(CharMatcher.DIGIT).matchesAllOf(name),
- String.format("Column name should only contains lowercase and _ characters, got '%s'", columnName));
- return name;
- }
-}
*/
package org.sonar.db.version;
-import com.google.common.base.CharMatcher;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import java.util.ArrayList;
import org.sonar.db.dialect.Oracle;
import org.sonar.db.dialect.PostgreSql;
-import static com.google.common.base.CharMatcher.anyOf;
-import static com.google.common.base.CharMatcher.inRange;
import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Stream.of;
+import static org.sonar.db.version.Validations.CONSTRAINT_NAME_MAX_SIZE;
+import static org.sonar.db.version.Validations.TABLE_NAME_MAX_SIZE;
+import static org.sonar.db.version.Validations.checkDbIdentifier;
public class CreateTableBuilder {
- private static final int TABLE_NAME_MAX_SIZE = 25;
- private static final int CONSTRAINT_NAME_MAX_SIZE = 30;
- private static final CharMatcher DIGIT_CHAR_MATCHER = inRange('0', '9');
- private static final CharMatcher LOWER_CASE_ASCII_LETTERS_CHAR_MATCHER = inRange('a', 'z');
- private static final CharMatcher UNDERSCORE_CHAR_MATCHER = anyOf("_");
private final Dialect dialect;
private final String tableName;
return this;
}
- private static String checkDbIdentifier(String identifier, String identifierDesc, int maxSize) {
- String res = checkNotNull(identifier, "%s can't be null", identifierDesc);
- checkArgument(
- identifier.length() <= maxSize,
- "%s length can't be more than %s", identifierDesc, maxSize);
- checkArgument(
- LOWER_CASE_ASCII_LETTERS_CHAR_MATCHER.or(DIGIT_CHAR_MATCHER).or(anyOf("_")).matchesAllOf(identifier),
- "%s must be lower case and contain only alphanumeric chars or '_', got '%s'", identifierDesc, identifier);
- checkArgument(
- DIGIT_CHAR_MATCHER.or(UNDERSCORE_CHAR_MATCHER).matchesNoneOf(identifier.subSequence(0, 1)),
- "%s must not start by a number or '_', got '%s'", identifierDesc, identifier);
- return res;
- }
-
private String createTableStatement() {
StringBuilder res = new StringBuilder("CREATE TABLE ");
res.append(tableName);
import org.sonar.db.dialect.Oracle;
import org.sonar.db.dialect.PostgreSql;
-import static org.sonar.db.version.ColumnDefValidation.validateColumnName;
+import static org.sonar.db.version.Validations.validateColumnName;
public class DecimalColumnDef extends AbstractColumnDef {
import org.sonar.db.dialect.Oracle;
import org.sonar.db.dialect.PostgreSql;
-import static org.sonar.db.version.ColumnDefValidation.validateColumnName;
+import static org.sonar.db.version.Validations.validateColumnName;
public class IntegerColumnDef extends AbstractColumnDef {
import org.sonar.db.dialect.Oracle;
import org.sonar.db.dialect.PostgreSql;
-import static org.sonar.db.version.ColumnDefValidation.validateColumnName;
+import static org.sonar.db.version.Validations.validateColumnName;
/**
* Integer that supports at least range [0..128]. Full range depends on database vendor.
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.db.version;
+
+import com.google.common.base.CharMatcher;
+import javax.annotation.Nullable;
+
+import static com.google.common.base.CharMatcher.anyOf;
+import static com.google.common.base.CharMatcher.inRange;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static java.util.Objects.requireNonNull;
+
+public class Validations {
+
+ static final int TABLE_NAME_MAX_SIZE = 25;
+ static final int CONSTRAINT_NAME_MAX_SIZE = 30;
+
+ private static final CharMatcher DIGIT_CHAR_MATCHER = inRange('0', '9');
+ private static final CharMatcher LOWER_CASE_ASCII_LETTERS_CHAR_MATCHER = inRange('a', 'z');
+ private static final CharMatcher UNDERSCORE_CHAR_MATCHER = anyOf("_");
+
+ private Validations() {
+ // Only static stuff here
+ }
+
+ static String validateColumnName(@Nullable String columnName) {
+ String name = requireNonNull(columnName, "Column name cannot be null");
+ checkDbIdentifierCharacters(columnName, "Column name");
+ return name;
+ }
+
+ /**
+ * Ensure {@code identifier} is a valid DB identifier.
+ *
+ * @throws NullPointerException if {@code identifier} is {@code null}
+ * @throws IllegalArgumentException if {@code identifier} is empty
+ * @throws IllegalArgumentException if {@code identifier} is longer than {@code maxSize}
+ * @throws IllegalArgumentException if {@code identifier} is not lowercase
+ * @throws IllegalArgumentException if {@code identifier} contains characters others than ASCII letters, ASCII numbers or {@code _}
+ * @throws IllegalArgumentException if {@code identifier} starts with {@code _} or a number
+ */
+ static String checkDbIdentifier(@Nullable String identifier, String identifierDesc, int maxSize) {
+ String res = checkNotNull(identifier, "%s can't be null", identifierDesc);
+ checkArgument(!res.isEmpty(), "%s, can't be empty", identifierDesc);
+ checkArgument(
+ identifier.length() <= maxSize,
+ "%s length can't be more than %s", identifierDesc, maxSize);
+ checkDbIdentifierCharacters(identifier, identifierDesc);
+ return res;
+ }
+
+ private static void checkDbIdentifierCharacters(String identifier, String identifierDesc) {
+ checkArgument(
+ LOWER_CASE_ASCII_LETTERS_CHAR_MATCHER.or(DIGIT_CHAR_MATCHER).or(anyOf("_")).matchesAllOf(identifier),
+ "%s must be lower case and contain only alphanumeric chars or '_', got '%s'", identifierDesc, identifier);
+ checkArgument(
+ DIGIT_CHAR_MATCHER.or(UNDERSCORE_CHAR_MATCHER).matchesNoneOf(identifier.subSequence(0, 1)),
+ "%s must not start by a number or '_', got '%s'", identifierDesc, identifier);
+ }
+}
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
-import static org.sonar.db.version.ColumnDefValidation.validateColumnName;
+import static org.sonar.db.version.Validations.validateColumnName;
/**
* Used to define VARCHAR column
BlobColumnDef.Builder builder = newBlobColumnDefBuilder();
expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Column name should only contains lowercase and _ characters, got 'T'");
+ expectedException.expectMessage("Column name must be lower case and contain only alphanumeric chars or '_', got 'T'");
builder.setColumnName("T");
}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.db.version;
-
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-
-import static org.sonar.db.version.ColumnDefValidation.validateColumnName;
-
-public class ColumnDefValidationTest {
-
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
- @Test
- public void accept_valid_table_name() throws Exception {
- validateColumnName("date_in_ms");
- validateColumnName("date_in_ms_1");
- }
-
- @Test
- public void fail_with_NPE_if_name_is_null() throws Exception {
- thrown.expect(NullPointerException.class);
- thrown.expectMessage("Column name cannot be null");
-
- validateColumnName(null);
- }
-
- @Test
- public void fail_when_column_name_is_in_upper_case() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Column name should only contains lowercase and _ characters, got 'DATE_IN_MS'");
-
- validateColumnName("DATE_IN_MS");
- }
-
- @Test
- public void fail_when_column_name_contains_invalid_character() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Column name should only contains lowercase and _ characters, got 'date-in/ms'");
-
- validateColumnName("date-in/ms");
- }
-
-}
IntegerColumnDef.Builder builder = newIntegerColumnDefBuilder();
expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Column name should only contains lowercase and _ characters, got 'T'");
+ expectedException.expectMessage("Column name must be lower case and contain only alphanumeric chars or '_', got 'T'");
builder.setColumnName("T");
}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.db.version;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static org.sonar.db.version.Validations.validateColumnName;
+
+public class ValidationsTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Test
+ public void accept_valid_table_name() throws Exception {
+ validateColumnName("date_in_ms");
+ validateColumnName("date_in_ms_1");
+ }
+
+ @Test
+ public void fail_with_NPE_if_name_is_null() throws Exception {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("Column name cannot be null");
+
+ validateColumnName(null);
+ }
+
+ @Test
+ public void fail_when_column_name_is_in_upper_case() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Column name must be lower case and contain only alphanumeric chars or '_', got 'DATE_IN_MS'");
+
+ validateColumnName("DATE_IN_MS");
+ }
+
+ @Test
+ public void fail_when_column_name_contains_invalid_character() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Column name must be lower case and contain only alphanumeric chars or '_', got 'date-in/ms'");
+
+ validateColumnName("date-in/ms");
+ }
+
+}