Elffuss BraidHub

kikocis/java-driver public

Add CREATE KEYSPACE statement builder.
Changeset17f0e5d21583d59a821bbd446c5c17a124908f81ee6dd7ce5db7a3ecf1e60f4d
AuthorAlex Petrov <oleksandr.petrov@gmail.com> · 2015-12-13 14:50 +01:00
CommitterAlex Petrov <oleksandr.petrov@gmail.com> · 2016-03-01 12:03 +01:00
Parentsdc23ffd69e7e

Browse the files at this changeset

4 files changed

added driver-core/src/main/java/com/datastax/driver/core/schemabuilder/CreateKeyspace.java +56 −0
@@ -0,0 +1,56 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.core.schemabuilder;
17+
18+/**
19+ * A built CREATE KEYSPACE statement.
20+ */
21+public class CreateKeyspace {
22+
23+ static final String command = "CREATE KEYSPACE";
24+
25+ private final String keyspaceName;
26+ private boolean ifNotExists;
27+
28+ public CreateKeyspace(String keyspaceName) {
29+ this.keyspaceName = keyspaceName;
30+ this.ifNotExists = false;
31+ }
32+
33+ public CreateKeyspace ifNotExists() {
34+ this.ifNotExists = true;
35+ return this;
36+ }
37+
38+ /**
39+ * Add options for this CREATE KEYSPACE statement.
40+ *
41+ * @return the options of this CREATE KEYSPACE statement.
42+ */
43+ public KeyspaceOptions with() {
44+ return new KeyspaceOptions(buildCommand(), keyspaceName);
45+ }
46+
47+ String buildCommand() {
48+ StringBuilder createStatement = new StringBuilder();
49+ createStatement.append(command);
50+ if (ifNotExists) {
51+ createStatement.append(" IF NOT EXISTS");
52+ }
53+ return createStatement.toString();
54+ }
55+
56+}
added driver-core/src/main/java/com/datastax/driver/core/schemabuilder/KeyspaceOptions.java +126 −0
@@ -0,0 +1,126 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.core.schemabuilder;
17+
18+import com.google.common.base.Optional;
19+
20+import java.util.Map;
21+
22+/**
23+ * The keyspace options used in CREATE KEYSPACE or ALTER KEYSPACE statements.
24+ */
25+public class KeyspaceOptions extends SchemaStatement {
26+
27+ private Optional<Map<String, Object>> replication = Optional.absent();
28+ private Optional<Boolean> durableWrites = Optional.absent();
29+
30+ private final String command;
31+ private final String keysapceName;
32+
33+ public KeyspaceOptions(String command, String keyspaceName) {
34+ validateNotEmpty(keyspaceName, "Keyspace name");
35+ validateNotKeyWord(keyspaceName,
36+ String.format("The keyspace name '%s' is not allowed because it is a reserved keyword", keyspaceName));
37+
38+ this.command = command;
39+ this.keysapceName = keyspaceName;
40+ }
41+
42+ /**
43+ * Define the replication options for the statement.
44+ *
45+ * @param replication replication properties map
46+ * @return this {@code KeyspaceOptions} object
47+ */
48+ public KeyspaceOptions replication(Map<String, Object> replication) {
49+ validateReplicationOptions(replication);
50+ this.replication = Optional.fromNullable(replication);
51+ return this;
52+ }
53+
54+ /**
55+ * Define the durable writes option for the statement. If set to false,
56+ * data written to the keyspace will bypass the commit log.
57+ *
58+ * @param durableWrites durable write option
59+ * @return this {@code KeyspaceOptions} object
60+ */
61+ public KeyspaceOptions durableWrites(Boolean durableWrites) {
62+ this.durableWrites = Optional.fromNullable(durableWrites);
63+ return this;
64+ }
65+
66+ @Override
67+ String buildInternal() {
68+ StringBuilder builtStatement = new StringBuilder(STATEMENT_START);
69+ builtStatement.append(command);
70+ builtStatement.append(" ");
71+ builtStatement.append(keysapceName);
72+ builtStatement.append("\n\tWITH\n\t\t");
73+
74+ boolean putSeparator = false;
75+ if (replication.isPresent()) {
76+
77+ builtStatement.append("REPLICATION = {");
78+
79+ int l = replication.get().entrySet().size();
80+ for (Map.Entry<String, Object> e : replication.get().entrySet()) {
81+ builtStatement.append("'")
82+ .append(e.getKey())
83+ .append("'")
84+ .append(": ");
85+
86+ if (e.getValue() instanceof String) {
87+ builtStatement.append("'")
88+ .append(e.getValue())
89+ .append("'");
90+ } else {
91+ builtStatement.append(e.getValue());
92+ }
93+
94+ if (--l > 0) {
95+ builtStatement.append(", ");
96+ }
97+ }
98+
99+ builtStatement.append('}');
100+ builtStatement.append("\n\t\t");
101+ putSeparator = true;
102+ }
103+
104+
105+ if (durableWrites.isPresent()) {
106+ if (putSeparator) {
107+ builtStatement.append("AND ");
108+ }
109+
110+ builtStatement.append("DURABLE_WRITES = " + durableWrites.get().toString());
111+ }
112+
113+
114+ return builtStatement.toString();
115+ }
116+
117+ static void validateReplicationOptions(Map<String, Object> replicationOptions) {
118+ if (replicationOptions != null && !replicationOptions.containsKey("class")) {
119+ throw new IllegalArgumentException("Replication Strategy 'class' should be provided");
120+ }
121+
122+ if (replicationOptions != null && !(replicationOptions.get("class") instanceof String)) {
123+ throw new IllegalArgumentException("Replication Strategy should be of type String");
124+ }
125+ }
126+}
modified driver-core/src/main/java/com/datastax/driver/core/schemabuilder/SchemaBuilder.java +10 −0
@@ -31,6 +31,16 @@
3131 private SchemaBuilder() {
3232 }
3333
34+ /**
35+ * Start building a new CREATE KEYSPACE statement.
36+ *
37+ * @param keyspaceName the name of the keyspace to create.
38+ * @return an in-construction CREATE KEYSPACE statement.
39+ */
40+ public static CreateKeyspace createKeyspace(String keyspaceName) {
41+ return new CreateKeyspace(keyspaceName);
42+ }
43+
3444 /**
3545 * Start building a new CREATE TABLE statement.
3646 *
added driver-core/src/test/java/com/datastax/driver/core/schemabuilder/CreateKeyspaceTest.java +56 −0
@@ -0,0 +1,56 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.core.schemabuilder;
17+
18+import org.testng.annotations.Test;
19+
20+import java.util.HashMap;
21+import java.util.Map;
22+
23+import static com.datastax.driver.core.schemabuilder.SchemaBuilder.createKeyspace;
24+import static org.assertj.core.api.Assertions.assertThat;
25+
26+public class CreateKeyspaceTest {
27+
28+ @Test(groups = "unit")
29+ public void should_create_keyspace_with_options() throws Exception {
30+ Map<String, Object> replicationOptions = new HashMap<String, Object>();
31+ replicationOptions.put("class", "SimpleStrategy");
32+ replicationOptions.put("replication_factor", 1);
33+
34+ //When
35+ SchemaStatement statement = createKeyspace("test").with()
36+ .durableWrites(true)
37+ .replication(replicationOptions);
38+
39+ //Then
40+ assertThat(statement.getQueryString())
41+ .isEqualTo("\n\tCREATE KEYSPACE test" +
42+ "\n\tWITH\n\t\t" +
43+ "REPLICATION = {'replication_factor': 1, 'class': 'SimpleStrategy'}\n\t\t" +
44+ "AND DURABLE_WRITES = true");
45+ }
46+
47+ @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class)
48+ public void incorrect_replication_options() throws Exception {
49+ Map<String, Object> replicationOptions = new HashMap<String, Object>();
50+ replicationOptions.put("class", 5);
51+
52+ //When
53+ createKeyspace("test").with()
54+ .replication(replicationOptions);
55+ }
56+}