Elffuss BraidHub

kikocis/java-driver public

Add DROP KEYSPACE statement builder.
Changeset1f2afbabe571e282db45cece4fa3e249191d9c312baedbb35f906d3dcb1335e0
AuthorAlex Petrov <oleksandr.petrov@gmail.com> · 2015-12-13 14:57 +01:00
CommitterAlex Petrov <oleksandr.petrov@gmail.com> · 2016-03-01 12:03 +01:00
Parents0be9be9e8022

Browse the files at this changeset

3 files changed

added driver-core/src/main/java/com/datastax/driver/core/schemabuilder/DropKeyspace.java +54 −0
@@ -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+}
modified driver-core/src/main/java/com/datastax/driver/core/schemabuilder/SchemaBuilder.java +10 −0
@@ -103,6 +103,16 @@
103103 return new Drop(tableName, DroppedItem.TABLE);
104104 }
105105
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+
106116 /**
107117 * Start building a new DROP TABLE statement.
108118 *
added driver-core/src/test/java/com/datastax/driver/core/schemabuilder/DropKeyspaceTest.java +42 −0
@@ -0,0 +1,42 @@
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 static com.datastax.driver.core.schemabuilder.SchemaBuilder.dropKeyspace;
21+import static org.assertj.core.api.Assertions.assertThat;
22+
23+public class DropKeyspaceTest {
24+
25+ @Test(groups = "unit")
26+ public void should_drop_keyspace() throws Exception {
27+ //When
28+ SchemaStatement statement = dropKeyspace("test");
29+
30+ //Then
31+ assertThat(statement.getQueryString()).isEqualTo("DROP KEYSPACE test");
32+ }
33+
34+ @Test(groups = "unit")
35+ public void should_drop_keyspace_if_exists() throws Exception {
36+ //When
37+ SchemaStatement statement = dropKeyspace("test").ifExists();
38+
39+ //Then
40+ assertThat(statement.getQueryString()).isEqualTo("DROP KEYSPACE IF EXISTS test");
41+ }
42+}