Implement schema handling methods The basic schema creation/deletion methods are implemented for the Ignite backend. In addition, all the SQL generation code was moved to a separate class.
Project: http://git-wip-us.apache.org/repos/asf/gora/repo Commit: http://git-wip-us.apache.org/repos/asf/gora/commit/b1e2ae4c Tree: http://git-wip-us.apache.org/repos/asf/gora/tree/b1e2ae4c Diff: http://git-wip-us.apache.org/repos/asf/gora/diff/b1e2ae4c Branch: refs/heads/master Commit: b1e2ae4c902691bbce02a90b76c4bee1c6e58455 Parents: 7545170 Author: Carlos M <carlosr...@gmail.com> Authored: Tue Jun 26 23:18:19 2018 -0500 Committer: Carlos M <carlosr...@gmail.com> Committed: Tue Jun 26 23:18:19 2018 -0500 ---------------------------------------------------------------------- .../apache/gora/ignite/store/IgniteStore.java | 37 +++++++++-- .../gora/ignite/utils/IgniteSQLBuilder.java | 69 ++++++++++++++++++++ gora-ignite/src/test/resources/gora.properties | 4 +- 3 files changed, 101 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/gora/blob/b1e2ae4c/gora-ignite/src/main/java/org/apache/gora/ignite/store/IgniteStore.java ---------------------------------------------------------------------- diff --git a/gora-ignite/src/main/java/org/apache/gora/ignite/store/IgniteStore.java b/gora-ignite/src/main/java/org/apache/gora/ignite/store/IgniteStore.java index d52d10e..ebc2943 100644 --- a/gora-ignite/src/main/java/org/apache/gora/ignite/store/IgniteStore.java +++ b/gora-ignite/src/main/java/org/apache/gora/ignite/store/IgniteStore.java @@ -22,11 +22,9 @@ import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import java.text.MessageFormat; import java.util.List; -import java.util.Locale; import java.util.Properties; -import java.util.logging.Level; +import org.apache.gora.ignite.utils.IgniteSQLBuilder; import org.apache.gora.persistency.impl.PersistentBase; import org.apache.gora.query.PartitionQuery; import org.apache.gora.query.Query; @@ -105,20 +103,45 @@ public class IgniteStore<K, T extends PersistentBase> extends DataStoreBase<K, T @Override public void createSchema() throws GoraException { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + if (connection == null) { + throw new GoraException( + "Impossible to create the schema as no connection has been initiated."); + } + if (schemaExists()) { + return; + } + try (Statement stmt = connection.createStatement()) { + String createTableSQL = IgniteSQLBuilder.createTable(igniteMapping); + stmt.executeUpdate(createTableSQL); + LOG.info("Table {} has been created for Ignite instance.", + igniteMapping.getTableName()); + } catch (SQLException ex) { + throw new GoraException(ex); + } } @Override public void deleteSchema() throws GoraException { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + if (connection == null) { + throw new GoraException( + "Impossible to delete the schema as no connection has been initiated."); + } + try (Statement stmt = connection.createStatement()) { + String dropTableSQL = IgniteSQLBuilder.dropTable(igniteMapping.getTableName()); + stmt.executeUpdate(dropTableSQL); + LOG.info("Table {} has been dropped from Ignite instance.", + igniteMapping.getTableName()); + } catch (SQLException ex) { + throw new GoraException(ex); + } } @Override public boolean schemaExists() throws GoraException { boolean exists = false; try (Statement stmt = connection.createStatement()) { - MessageFormat messageFormat = new MessageFormat("select * from {0} limit 0", Locale.getDefault()); - ResultSet executeQuery = stmt.executeQuery(messageFormat.format(igniteMapping.getTableName())); + String tableExistsSQL = IgniteSQLBuilder.tableExists(igniteMapping.getTableName()); + ResultSet executeQuery = stmt.executeQuery(tableExistsSQL); executeQuery.close(); exists = true; } catch (SQLException ex) { http://git-wip-us.apache.org/repos/asf/gora/blob/b1e2ae4c/gora-ignite/src/main/java/org/apache/gora/ignite/utils/IgniteSQLBuilder.java ---------------------------------------------------------------------- diff --git a/gora-ignite/src/main/java/org/apache/gora/ignite/utils/IgniteSQLBuilder.java b/gora-ignite/src/main/java/org/apache/gora/ignite/utils/IgniteSQLBuilder.java new file mode 100644 index 0000000..0480fd4 --- /dev/null +++ b/gora-ignite/src/main/java/org/apache/gora/ignite/utils/IgniteSQLBuilder.java @@ -0,0 +1,69 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.gora.ignite.utils; + +import avro.shaded.com.google.common.collect.Lists; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Locale; +import java.util.Map; +import org.apache.gora.ignite.store.Column; +import org.apache.gora.ignite.store.IgniteMapping; + +/** + * + * SQL Builder utility for Ignite. + */ +public class IgniteSQLBuilder { + + private static String format(String pattern, Object... args) { + MessageFormat messageFormat = new MessageFormat(pattern, Locale.getDefault()); + return messageFormat.format(args); + } + + public static String tableExists(String tableName) { + return format("SELECT * FROM {0} LIMIT 0", tableName); + } + + public static String createTable(IgniteMapping mapping) { + StringBuilder sqlBuilder = new StringBuilder(); + sqlBuilder.append("CREATE TABLE "); + sqlBuilder.append(mapping.getTableName()); + sqlBuilder.append("("); + ArrayList<Map.Entry<String, Column>> fieldsList = Lists.newArrayList(mapping.getFields().entrySet()); + for (Map.Entry<String, Column> aField : fieldsList) { + Column aColumn = aField.getValue(); + String name = aColumn.getName(); + Column.FieldType dataType = aColumn.getDataType(); + sqlBuilder.append(name).append(" ").append(dataType.toString()).append(","); + } + sqlBuilder.append("PRIMARY KEY "); + sqlBuilder.append("("); + for (int i = 0; i < mapping.getPrimaryKey().size(); i++) { + sqlBuilder.append(mapping.getPrimaryKey().get(i)); + sqlBuilder.append(i == mapping.getPrimaryKey().size() - 1 ? "" : ","); + } + sqlBuilder.append(")"); + sqlBuilder.append(");"); + return sqlBuilder.toString(); + } + + public static String dropTable(String tableName) { + return format("DROP TABLE IF EXISTS {0} ;", tableName); + } + +} http://git-wip-us.apache.org/repos/asf/gora/blob/b1e2ae4c/gora-ignite/src/test/resources/gora.properties ---------------------------------------------------------------------- diff --git a/gora-ignite/src/test/resources/gora.properties b/gora-ignite/src/test/resources/gora.properties index e45aab7..e0d1901 100644 --- a/gora-ignite/src/test/resources/gora.properties +++ b/gora-ignite/src/test/resources/gora.properties @@ -17,6 +17,6 @@ gora.datastore.default=org.apache.gora.ignite.store.IgniteStore gora.datastore.ignite.schema=PUBLIC gora.datastore.ignite.host=localhost gora.datastore.ignite.port=10800 -gora.datastore.ignite.user= -gora.datastore.ignite.password= +#gora.datastore.ignite.user= +#gora.datastore.ignite.password= gora.datastore.ignite.additionalConfigurations= \ No newline at end of file