com.qwazr.database.TableBuilder Maven / Gradle / Ivy
/*
* Copyright 2015-2018 Emmanuel Keller / QWAZR
*
* Licensed 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 com.qwazr.database;
import com.qwazr.database.model.ColumnDefinition;
import com.qwazr.database.model.TableDefinition;
import com.qwazr.database.store.KeyStore;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
public class TableBuilder {
public final String tableName;
public final KeyStore.Impl implementation;
public final Map columns;
public TableBuilder(final String tableName, final KeyStore.Impl implementation) {
this.tableName = tableName;
this.implementation = implementation;
columns = new LinkedHashMap<>();
}
public TableBuilder setColumn(final String name, final ColumnDefinition.Type type,
final ColumnDefinition.Mode mode) {
columns.put(name, new ColumnDefinition(type, mode));
return this;
}
/**
* The build do the following tasks:
*
* - Create the table if it does not exist.
* - Create the columns if they does not exist.
* - Remove existing columns if they are not defined.
*
*
* @param tableService the service to use
*/
public void build(final TableServiceInterface tableService) {
final Set tables = tableService.list();
if (!tables.contains(tableName))
tableService.createTable(tableName, implementation);
final Map existingColumns = tableService.getColumns(tableName);
columns.forEach((columnName, columnDefinition) -> {
if (!columnName.equals(TableDefinition.ID_COLUMN_NAME) && !existingColumns.containsKey(columnName))
tableService.setColumn(tableName, columnName, columnDefinition);
});
existingColumns.keySet().forEach(columnName -> {
if (!columnName.equals(TableDefinition.ID_COLUMN_NAME) && !columns.containsKey(columnName))
tableService.removeColumn(tableName, columnName);
});
}
}