Search in sources :

Example 1 with ValueExpression

use of com.kyj.fx.voeditor.visual.component.grid.EditableTableView.ValueExpression in project Gargoyle by callakrsos.

the class EditableTableView method getStatements.

private STATUS getStatements(DML dml, BiFunction<STATUS, List<String>, STATUS> function) {
    List<String> saveSqlList = new ArrayList<>();
    List<Map<ColumnExpression, ObjectProperty<ValueExpression>>> items = Collections.emptyList();
    switch(dml) {
        case UPDATE:
            items = getItems().stream().filter(m -> {
                return !ColumnExpression.isContainsNewRowMeata(m);
            }).collect(Collectors.toList());
            break;
        case DELETE:
            items = this.removedList;
            break;
        case INSERT:
            items = getItems().stream().filter(m -> {
                return ColumnExpression.isContainsNewRowMeata(m);
            }).collect(Collectors.toList());
            break;
        default:
            break;
    }
    if (items.isEmpty())
        return STATUS.OK;
    int modifiedCnt = 0;
    //키값이 존재하지않는 테이블인경우 where조건은 수정되지않는 컬럼으로 사용.
    boolean isNotContainsPrimaryTable = false;
    //키값도 존재하지않을뿐더러 변경된 컬럼도 없는경우
    boolean isNotContainsModifiedTable = false;
    if (items != null && !items.isEmpty()) {
        long count = items.get(0).values().stream().filter(vo -> vo.getValue().isPrimaryKey).count();
        //키값 존재유무에 따른 플래그 update
        if (count == 0)
            isNotContainsPrimaryTable = true;
        //키값이 없는 상태에서 수정된 컬럼도 없는 경우 상태 update
        if (count == 0) {
            long modifiedCount = items.get(0).values().stream().filter(vo -> vo.getValue().isModified).count();
            if (modifiedCount == 0)
                isNotContainsModifiedTable = true;
        }
    }
    for (Map<ColumnExpression, ObjectProperty<ValueExpression>> m : items) {
        StringBuffer whereStatement = new StringBuffer();
        StringBuffer setStatement = new StringBuffer();
        StringBuffer columnsStatement = new StringBuffer();
        StringBuffer valuesStatement = new StringBuffer();
        Iterator<ColumnExpression> iterator = m.keySet().iterator();
        modifiedCnt = 0;
        while (iterator.hasNext()) {
            ColumnExpression colummExp = iterator.next();
            if (colummExp.isNewRowMeta())
                continue;
            if (colummExp.isMetadata)
                continue;
            //				else {
            ObjectProperty<ValueExpression> value = m.get(colummExp);
            ValueExpression valueExp = value.get();
            if (valueExp.isPrimaryKey) {
                //기본키값이 빈 경우 메세지.
                if (valueExp.getDisplayText() == null) /*|| valueExp.getDisplayText().isEmpty()*/
                {
                    return function.apply(STATUS.PK_VAL_IS_EMPTY, Collections.emptyList());
                }
                whereStatement.append(String.format("and %s  = '%s'", colummExp, valueExp.getRealValue()));
            }
            if (valueExp.isModified) {
                setStatement.append(String.format("%s  = %s,", colummExp, getValue(colummExp, valueExp)));
                modifiedCnt++;
                if (isNotContainsPrimaryTable) {
                    whereStatement.append(String.format("and %s  = '%s'", colummExp, valueExp.getRealValue()));
                }
            }
            if ((!valueExp.isPrimaryKey && !valueExp.isModified) && (isNotContainsPrimaryTable || isNotContainsModifiedTable)) {
                whereStatement.append(String.format("and %s  = '%s'", colummExp, valueExp.getRealValue()));
            }
            columnsStatement.append(String.format("%s,", colummExp));
            valuesStatement.append(String.format("'%s',", valueExp.getDisplayText()));
        //				}
        }
        int setStatementLength = setStatement.length();
        if (setStatementLength != 0) {
            setStatement.setLength(setStatementLength - 1);
        }
        int columnsStatementLength = columnsStatement.length();
        if (columnsStatementLength != 0) {
            columnsStatement.setLength(columnsStatementLength - 1);
        }
        int valuesStatementLength = valuesStatement.length();
        if (valuesStatementLength != 0) {
            valuesStatement.setLength(valuesStatementLength - 1);
        }
        switch(dml) {
            case INSERT:
                saveSqlList.add(String.format("insert into %s (%s) values (%s)", this.tableName.get(), columnsStatement.toString(), valuesStatement.toString()));
                break;
            case UPDATE:
                if (modifiedCnt > 0) {
                    saveSqlList.add(String.format("update %s set %s where 1=1 %s", this.tableName.get(), setStatement.toString(), whereStatement.toString()));
                }
                break;
            case DELETE:
                saveSqlList.add(String.format("delete from %s where 1=1 %s", this.tableName.get(), whereStatement.toString()));
                break;
        }
    }
    return function.apply(STATUS.OK, saveSqlList);
}
Also used : Connection(java.sql.Connection) DbUtil(com.kyj.fx.voeditor.visual.util.DbUtil) SimpleStringProperty(javafx.beans.property.SimpleStringProperty) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) FXCollections(javafx.collections.FXCollections) HashMap(java.util.HashMap) TextFieldTableCell(javafx.scene.control.cell.TextFieldTableCell) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) TableColumn(javafx.scene.control.TableColumn) SQLException(java.sql.SQLException) TableCell(javafx.scene.control.TableCell) ListChangeListener(javafx.collections.ListChangeListener) ResultSet(java.sql.ResultSet) Map(java.util.Map) LinkedList(java.util.LinkedList) Objects(com.google.common.base.Objects) TableView(javafx.scene.control.TableView) Callback(javafx.util.Callback) Strings(com.sun.btrace.BTraceUtils.Strings) ColumnExpression(com.kyj.fx.voeditor.visual.component.grid.EditableTableView.ColumnExpression) ObjectProperty(javafx.beans.property.ObjectProperty) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TablePosition(javafx.scene.control.TablePosition) StringConverter(javafx.util.StringConverter) Collectors(java.util.stream.Collectors) ValueExpression(com.kyj.fx.voeditor.visual.component.grid.EditableTableView.ValueExpression) CellDataFeatures(javafx.scene.control.TableColumn.CellDataFeatures) Consumer(java.util.function.Consumer) List(java.util.List) SimpleObjectProperty(javafx.beans.property.SimpleObjectProperty) ValueUtil(kyj.Fx.dao.wizard.core.util.ValueUtil) ObservableValue(javafx.beans.value.ObservableValue) ObservableList(javafx.collections.ObservableList) StringProperty(javafx.beans.property.StringProperty) Collections(java.util.Collections) ResultSetMetaData(java.sql.ResultSetMetaData) ObjectProperty(javafx.beans.property.ObjectProperty) SimpleObjectProperty(javafx.beans.property.SimpleObjectProperty) ArrayList(java.util.ArrayList) ColumnExpression(com.kyj.fx.voeditor.visual.component.grid.EditableTableView.ColumnExpression) ValueExpression(com.kyj.fx.voeditor.visual.component.grid.EditableTableView.ValueExpression) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with ValueExpression

use of com.kyj.fx.voeditor.visual.component.grid.EditableTableView.ValueExpression in project Gargoyle by callakrsos.

the class EditableTableView method readByTableName.

private void readByTableName(String sql, String tableName, boolean appendHist) throws Exception {
    LOGGER.debug(sql);
    getColumns().clear();
    getItems().clear();
    removedList.clear();
    columnMap.clear();
    try (Connection connection = connectionSupplier.get()) {
        List<String> pks = DbUtil.pks(connection, tableName);
        Map<String, Boolean> columnsToMap = DbUtil.columnsToMap(connection, tableName, rs -> {
            try {
                return rs.getString(4);
            } catch (Exception e) {
                return null;
            }
        }, rs -> {
            try {
                return Boolean.valueOf("YES".equals(rs.getString(18)));
            } catch (Exception e) {
                return false;
            }
        });
        LOGGER.debug("nullable columns ? {} ", columnsToMap);
        DbUtil.select(connection, sql, FETCH_COUNT, LIMIT_ROW_COUNT, new BiFunction<ResultSetMetaData, ResultSet, List<Map<String, Object>>>() {

            @Override
            public List<Map<String, Object>> apply(ResultSetMetaData t, ResultSet u) {
                try {
                    int columnCount = t.getColumnCount();
                    for (int i = 1; i <= columnCount; i++) {
                        String columnName = t.getColumnName(i);
                        ColumnExpression columnExp = new ColumnExpression(columnName);
                        columnExp.isPrimaryColumn = pks.contains(columnName);
                        columnExp.isNullableColumn = columnsToMap.containsKey(columnName) && (columnsToMap.get(columnName) == true);
                        columnExp.setColumnType(t.getColumnType(i));
                        TableColumn<Map<ColumnExpression, ObjectProperty<ValueExpression>>, ValueExpression> e = new TableColumn<>(columnName);
                        e.setUserData(columnExp);
                        e.setCellValueFactory(DynamicCallback.fromTableColumn(columnExp));
                        e.setCellFactory(DEFAULT_CELL_FACTORY);
                        e.setEditable(true);
                        columnMap.put(columnName, columnExp);
                        getColumns().add(e);
                    }
                    while (u.next()) {
                        Map<ColumnExpression, ObjectProperty<ValueExpression>> hashMap = new HashMap<>();
                        for (int i = 1; i <= columnCount; i++) {
                            String columnName = t.getColumnName(i);
                            //new ColumnExpression(columnName);
                            ColumnExpression columnExp = columnMap.get(columnName);
                            ValueExpression valueExp = new ValueExpression();
                            valueExp.displayText.set(u.getString(columnName));
                            valueExp.isPrimaryKey = pks.contains(columnName);
                            valueExp.realValue.set(u.getObject(columnName));
                            valueExp.setColumnExpression(columnExp);
                            hashMap.put(columnExp, new SimpleObjectProperty<>(valueExp));
                        }
                        getItems().removeListener(itemChangeListener);
                        getItems().add(hashMap);
                        getItems().addListener(itemChangeListener);
                    }
                } catch (SQLException e) {
                    LOGGER.error(ValueUtil.toString(e));
                }
                return null;
            }
        });
        if (appendHist) {
            if (history.size() >= HISTORY_LIMITED_SIZE) {
                history.removeFirst();
            }
            history.add(sql);
        }
        this.tableName.set(tableName);
    }
}
Also used : ObjectProperty(javafx.beans.property.ObjectProperty) SimpleObjectProperty(javafx.beans.property.SimpleObjectProperty) SQLException(java.sql.SQLException) Connection(java.sql.Connection) TableColumn(javafx.scene.control.TableColumn) SQLException(java.sql.SQLException) ResultSetMetaData(java.sql.ResultSetMetaData) SimpleObjectProperty(javafx.beans.property.SimpleObjectProperty) ColumnExpression(com.kyj.fx.voeditor.visual.component.grid.EditableTableView.ColumnExpression) ValueExpression(com.kyj.fx.voeditor.visual.component.grid.EditableTableView.ValueExpression) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ObservableList(javafx.collections.ObservableList) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

ColumnExpression (com.kyj.fx.voeditor.visual.component.grid.EditableTableView.ColumnExpression)2 ValueExpression (com.kyj.fx.voeditor.visual.component.grid.EditableTableView.ValueExpression)2 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ObjectProperty (javafx.beans.property.ObjectProperty)2 SimpleObjectProperty (javafx.beans.property.SimpleObjectProperty)2 ObservableList (javafx.collections.ObservableList)2 TableColumn (javafx.scene.control.TableColumn)2 Objects (com.google.common.base.Objects)1 DbUtil (com.kyj.fx.voeditor.visual.util.DbUtil)1 Strings (com.sun.btrace.BTraceUtils.Strings)1 Collections (java.util.Collections)1