diff --git a/package.json b/package.json index de5d0c7..aac4db9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-sqlite-explorer", - "version": "0.1.3", + "version": "0.1.4", "description": "Explorer for sqlite database inside react native app", "source": "./src/index.tsx", "main": "./lib/commonjs/index.js", diff --git a/src/components/Table/TableModal.tsx b/src/components/Table/TableModal.tsx index 976ec57..1f004d7 100644 --- a/src/components/Table/TableModal.tsx +++ b/src/components/Table/TableModal.tsx @@ -12,7 +12,7 @@ type TableModalProps = { tableData: TableSignature; modalType: ModalType; onClose: () => void; - onSubmit: (model: TableSignatureValue) => void; + onSubmit: (model: TableSignatureValue, initialModel: TableSignatureValue,) => void; }; const TableModal = ({ @@ -24,14 +24,13 @@ const TableModal = ({ }: TableModalProps) => { const [model, setModel] = useState({}); const [errors, setErrors] = useState({}); + const initialModel = getInitialModel( + tableData.fields, + modalType === 'update' ? checkedRowValue : null + ); useEffect(() => { - setModel( - getInitialModel( - tableData.fields, - modalType === 'update' ? checkedRowValue : null - ) - ); + setModel(initialModel); setErrors({}); }, [modalType]); @@ -65,7 +64,7 @@ const TableModal = ({ if (!!errorLength) return; - onSubmit(model); + onSubmit(model, initialModel); }; return ( diff --git a/src/components/Table/index.tsx b/src/components/Table/index.tsx index 90933c1..cc8fb35 100644 --- a/src/components/Table/index.tsx +++ b/src/components/Table/index.tsx @@ -64,17 +64,15 @@ const Table = ({ tableData, onActionSuccess }: TableProps) => { await DB.addRecord(tableData.name, model); }; - const onUpdate = async (model: TableSignatureValue) => { + const onUpdate = async (model: TableSignatureValue, initModel: TableSignatureValue) => { if (!checkedRowValue) return; - await DB.editRecord(tableData.name, model) - //await DB.deleteRecord(tableData.name, checkedRowValue); - //await DB.addRecord(tableData.name, model); + await DB.editRecord(tableData.name, model, initModel) }; - const onModalSubmit = async (model: TableSignatureValue) => { + const onModalSubmit = async (model: TableSignatureValue, initModel: TableSignatureValue) => { if (modalType === 'add') await onAdd(model); - if (modalType === 'update') await onUpdate(model); + if (modalType === 'update') await onUpdate(model, initModel); await onActionSuccess(tableData); setModalType(null); diff --git a/src/models/DB.ts b/src/models/DB.ts index 8c64d66..d3caff1 100644 --- a/src/models/DB.ts +++ b/src/models/DB.ts @@ -179,7 +179,7 @@ class Database { addRecord = async ( tableName: string, - model: Record + model: TableSignatureValue ): Promise<[ResultSet] | undefined | null> => { try { return await this.DB?.executeSql( @@ -199,15 +199,33 @@ class Database { editRecord = async ( tableName: string, - model: Record + model: TableSignatureValue, + initModel: Record ): Promise<[ResultSet] | undefined | null> => { try { + const where = Object.keys(initModel).reduce((acc, key, index) => { + const value = initModel[key]; + + if (!value || (typeof value === 'string' && value.length > 255) || typeof value === 'object') + return acc; + + return ( + acc + + `${index !== 0 ? ' AND ' : ''}${key}=${typeof value === 'string' ? "'" + value + "'" : value}` + ); + }, ''); + + const dataToSet = Object.keys(model).reduce((acc, key, index, arr) => { + const rawValue = model[key]; + if (typeof rawValue === 'object' || rawValue === null) return acc; + + const value = typeof rawValue === "string" ? `'${rawValue}'` : rawValue; + + return acc + ` ${key} = ${value} ` + (index + 1 === arr.length ? "" : ","); + }, ""); + return await this.DB?.executeSql( - `INSERT OR REPLACE INTO ${tableName} (${Object.keys(model).join(',')}) - VALUES (${Object.keys(model) - .map(() => '?') - .join(',')})`, - Object.values(model) + `UPDATE ${tableName} SET ${dataToSet} WHERE ${where}`, ); } catch (err) { const message = getErrorText(err);