From 54bf6440de572f53bfd16ea63b76b192ab95eed1 Mon Sep 17 00:00:00 2001 From: Chaiwat Ekkaewnumchai Date: Tue, 2 Jun 2020 01:44:21 -0700 Subject: [PATCH] Allow to Edit only When There Is Primary Key in Table Summary: To be able to edit correct cell, identifiers are needed. Assume that the primary key is unique, this should be safe to allow the query to find exactly one row to edit Reviewed By: jknoxville Differential Revision: D21788239 fbshipit-source-id: b09d1b6da1b46cbc961f08010467e973546acbef --- desktop/plugins/databases/UpdateQueryUtil.tsx | 14 ++++++++++++++ desktop/plugins/databases/index.tsx | 14 ++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/desktop/plugins/databases/UpdateQueryUtil.tsx b/desktop/plugins/databases/UpdateQueryUtil.tsx index 9a0f53028..3df5a0da6 100644 --- a/desktop/plugins/databases/UpdateQueryUtil.tsx +++ b/desktop/plugins/databases/UpdateQueryUtil.tsx @@ -71,3 +71,17 @@ export function constructUpdateQuery( SET ${constructQueryClause(change, ',')} WHERE ${constructQueryClause(where, 'AND')}`; } + +export function isUpdatable( + columnMeta: Array, + columnData: Array>, +): boolean { + const primaryKeyIdx = columnMeta.indexOf('primary_key'); + return ( + primaryKeyIdx >= 0 && + columnData.reduce((acc: boolean, column) => { + const primaryValue = column[primaryKeyIdx]; + return acc || (primaryValue.type === 'boolean' && primaryValue.value); + }, false) + ); +} diff --git a/desktop/plugins/databases/index.tsx b/desktop/plugins/databases/index.tsx index 7e70c991e..389a3315c 100644 --- a/desktop/plugins/databases/index.tsx +++ b/desktop/plugins/databases/index.tsx @@ -37,7 +37,11 @@ import {DatabaseClient} from './ClientProtocol'; import ButtonNavigation from './ButtonNavigation'; import DatabaseDetailSidebar from './DatabaseDetailSidebar'; import DatabaseStructure from './DatabaseStructure'; -import {convertStringToValue, constructUpdateQuery} from './UpdateQueryUtil'; +import { + convertStringToValue, + constructUpdateQuery, + isUpdatable, +} from './UpdateQueryUtil'; import sqlFormatter from 'sql-formatter'; import dateFormat from 'dateformat'; @@ -94,6 +98,7 @@ type Page = { count: number; total: number; highlightedRows: Array; + editable: boolean; }; export type Structure = { @@ -495,6 +500,10 @@ export default class DatabasesPlugin extends FlipperPlugin< indexesColumns: event.indexesColumns, indexesValues: event.indexesValues, }, + currentPage: { + ...state.currentPage!, + editable: isUpdatable(event.columns, event.rows), + }, }; }, ], @@ -812,6 +821,7 @@ export default class DatabasesPlugin extends FlipperPlugin< count: data.count, total: data.total, highlightedRows: [], + editable: false, }); }) .catch((e) => { @@ -1127,7 +1137,7 @@ export default class DatabasesPlugin extends FlipperPlugin< )}