Skip to content

Commit 7149d14

Browse files
committed
Merge branch 'java1019' into 2.1
2 parents cc7dc39 + ca14cf8 commit 7149d14

9 files changed

Lines changed: 462 additions & 0 deletions

File tree

changelog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
- [improvement] JAVA-963: Automatically register PercentileTracker from components that use it.
3131
- [bug] JAVA-1089: Set LWT made from BuiltStatements to non-idempotent.
3232
- [improvement] JAVA-923: Position idempotent flag on object mapper queries.
33+
- [new feature] JAVA-1019: SchemaBuilder support for CREATE/ALTER/DROP KEYSPACE.
3334

3435
Merged from 2.0 branch:
3536

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 ALTER KEYSPACE statement.
20+
*/
21+
public class AlterKeyspace {
22+
23+
static final String COMMAND = "ALTER KEYSPACE";
24+
25+
private final String keyspaceName;
26+
27+
public AlterKeyspace(String keyspaceName) {
28+
this.keyspaceName = keyspaceName;
29+
}
30+
31+
/**
32+
* Add options for this ALTER KEYSPACE statement.
33+
*
34+
* @return the options of this ALTER KEYSPACE statement.
35+
*/
36+
public KeyspaceOptions with() {
37+
return new KeyspaceOptions(COMMAND, keyspaceName);
38+
}
39+
40+
}
41+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 DROP KEYSPACE statement.
20+
*/
21+
public class DropKeyspace extends SchemaStatement {
22+
23+
private final String keyspaceName;
24+
private boolean ifExists;
25+
26+
public DropKeyspace(String keyspaceName) {
27+
this.keyspaceName = keyspaceName;
28+
this.ifExists = false;
29+
validateNotEmpty(keyspaceName, "Keyspace name");
30+
validateNotKeyWord(keyspaceName,
31+
String.format("The keyspace name '%s' is not allowed because it is a reserved keyword", keyspaceName));
32+
}
33+
34+
/**
35+
* Add the 'IF EXISTS' condition to this DROP statement.
36+
*
37+
* @return this statement.
38+
*/
39+
public DropKeyspace ifExists() {
40+
this.ifExists = true;
41+
return this;
42+
}
43+
44+
@Override
45+
public String buildInternal() {
46+
StringBuilder dropStatement = new StringBuilder("DROP KEYSPACE ");
47+
if (ifExists) {
48+
dropStatement.append("IF EXISTS ");
49+
}
50+
dropStatement.append(keyspaceName);
51+
return dropStatement.toString();
52+
}
53+
54+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 keyspaceName;
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.keyspaceName = 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(keyspaceName);
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+
}

driver-core/src/main/java/com/datastax/driver/core/schemabuilder/SchemaBuilder.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ public final class SchemaBuilder {
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
*
@@ -52,6 +62,16 @@ public static Create createTable(String keyspaceName, String tableName) {
5262
return new Create(keyspaceName, tableName);
5363
}
5464

65+
/**
66+
* Start building a new ALTER KEYSPACE statement.
67+
*
68+
* @param keyspaceName the name of the keyspace to be altered.
69+
* @return an in-construction ALTER KEYSPACE statement.
70+
*/
71+
public static AlterKeyspace alterKeyspace(String keyspaceName) {
72+
return new AlterKeyspace(keyspaceName);
73+
}
74+
5575
/**
5676
* Start building a new ALTER TABLE statement.
5777
*
@@ -83,6 +103,16 @@ public static Drop dropTable(String tableName) {
83103
return new Drop(tableName, DroppedItem.TABLE);
84104
}
85105

106+
/**
107+
* Start building a new DROP KEYSPACE statement.
108+
*
109+
* @param keyspaceName the name of the keyspace to be dropped.
110+
* @return an in-construction DROP KEYSPACE statement.
111+
*/
112+
public static DropKeyspace dropKeyspace(String keyspaceName) {
113+
return new DropKeyspace(keyspaceName);
114+
}
115+
86116
/**
87117
* Start building a new DROP TABLE statement.
88118
*
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.alterKeyspace;
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
public class AlterKeyspaceTest {
27+
28+
@Test(groups = "unit")
29+
public void should_alter_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 = alterKeyspace("test").with()
36+
.durableWrites(true)
37+
.replication(replicationOptions);
38+
39+
//Then
40+
assertThat(statement.getQueryString())
41+
.isEqualTo("\n\tALTER 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+
alterKeyspace("test").with()
54+
.replication(replicationOptions);
55+
}
56+
}

0 commit comments

Comments
 (0)