Add Logic to Edit And Update Database on Both Client And Server
Summary: This logic relies on the logic on client side, specifically `SqliteDatabaseDriver.java` in specifying some keys to retrieve useful data in `currentStructure`. This will be error-prone if developer writes their own data driver which doesn't follow the same naming as the data driver used as model. Also, this diff adds util file to deal with query for updating database on client side. I decided to construct query clause on server side so that the client side change is not needed. Reviewed By: jknoxville Differential Revision: D21788241 fbshipit-source-id: cf9a920c3e5b7b29f619bc3f00e68616b3445cab
This commit is contained in:
committed by
Facebook GitHub Bot
parent
fe17ddce1a
commit
5a2221e6cd
73
desktop/plugins/databases/UpdateQueryUtil.tsx
Normal file
73
desktop/plugins/databases/UpdateQueryUtil.tsx
Normal file
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
|
||||
import {Value} from 'flipper';
|
||||
|
||||
const INT_DATA_TYPE = ['INTEGER', 'LONG', 'INT', 'BIGINT'];
|
||||
const FLOAT_DATA_TYPE = ['REAL', 'DOUBLE'];
|
||||
const BLOB_DATA_TYPE = ['BLOB'];
|
||||
|
||||
export function convertStringToValue(
|
||||
types: {[key: string]: {type: string; nullable: boolean}},
|
||||
key: string,
|
||||
value: string | null,
|
||||
): Value {
|
||||
if (value !== null && types.hasOwnProperty(key)) {
|
||||
const {type, nullable} = types[key];
|
||||
if (value.length <= 0 && nullable) {
|
||||
return {type: 'null', value: null};
|
||||
}
|
||||
if (INT_DATA_TYPE.indexOf(type) >= 0) {
|
||||
return {type: 'integer', value: parseInt(value, 10)};
|
||||
} else if (FLOAT_DATA_TYPE.indexOf(type) >= 0) {
|
||||
return {type: 'float', value: parseFloat(value)};
|
||||
} else if (BLOB_DATA_TYPE.indexOf(type) >= 0) {
|
||||
return {type: 'blob', value};
|
||||
}
|
||||
}
|
||||
// if no type found assume type is nullable string
|
||||
if (value === null) {
|
||||
return {type: 'null', value: null};
|
||||
} else {
|
||||
return {type: 'string', value};
|
||||
}
|
||||
}
|
||||
|
||||
function constructQueryClause(
|
||||
values: {[key: string]: Value},
|
||||
connector: string,
|
||||
): string {
|
||||
return Object.entries(values).reduce(
|
||||
(clauses, [key, val]: [string, Value], idx) => {
|
||||
const {type, value} = val;
|
||||
const valueString =
|
||||
type === 'null'
|
||||
? 'NULL'
|
||||
: type === 'string' || type === 'blob'
|
||||
? `'${value}'`
|
||||
: `${value}`;
|
||||
if (idx <= 0) {
|
||||
return `${key}=${valueString}`;
|
||||
} else {
|
||||
return `${clauses} ${connector} ${key}=${valueString}`;
|
||||
}
|
||||
},
|
||||
'',
|
||||
);
|
||||
}
|
||||
|
||||
export function constructUpdateQuery(
|
||||
table: string,
|
||||
where: {[key: string]: Value},
|
||||
change: {[key: string]: Value},
|
||||
): string {
|
||||
return `UPDATE ${table}
|
||||
SET ${constructQueryClause(change, ',')}
|
||||
WHERE ${constructQueryClause(where, 'AND')}`;
|
||||
}
|
||||
Reference in New Issue
Block a user