Browse Source

undefined checks in database transactions

master
GabrielTrettel 4 years ago
parent
commit
585712a25d
  1. 1
      src/app/database/database-connection.js
  2. 2
      src/app/database/database-init.js
  3. 6
      src/app/hooks/selectFromDB.js

1
src/app/database/database-connection.js

@ -3,6 +3,7 @@ import * as SQLite from "expo-sqlite";
function openDatabase() { function openDatabase() {
const db = SQLite.openDatabase("db.testDb"); const db = SQLite.openDatabase("db.testDb");
// return undefined
return db; return db;
} }

2
src/app/database/database-init.js

@ -28,7 +28,7 @@ function createAllTables(db) {
* *
*/ */
function initDatabase(db) { function initDatabase(db) {
createAllTables(db);
(db != undefined && db !== null) && createAllTables(db);
} }
export default initDatabase; export default initDatabase;

6
src/app/hooks/selectFromDB.js

@ -140,9 +140,14 @@ function parseResult(db_result, parseRow) {
return warnings; return warnings;
} }
function isAvailable(dataBase) {
return dataBase !== undefined && dataBase !== null;
}
function genericSelect(queriesToParsersMapper, dispatch, isFocused) { function genericSelect(queriesToParsersMapper, dispatch, isFocused) {
useEffect(() => { useEffect(() => {
console.log("requesting data"); console.log("requesting data");
if (isAvailable(global.userDataBase)) {
queriesToParsersMapper.forEach(([query, parser]) => { queriesToParsersMapper.forEach(([query, parser]) => {
global.userDataBase.transaction((tx) => { global.userDataBase.transaction((tx) => {
tx.executeSql(query, [], (tx, results) => { tx.executeSql(query, [], (tx, results) => {
@ -150,6 +155,7 @@ function genericSelect(queriesToParsersMapper, dispatch, isFocused) {
}); });
}); });
}); });
}
}, [isFocused]); }, [isFocused]);
} }

Loading…
Cancel
Save