1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/packages/app-mobile/utils/database-driver-react-native.js

80 lines
1.4 KiB
JavaScript
Raw Normal View History

const SQLite = require('react-native-sqlite-storage');
2017-06-11 23:11:14 +02:00
class DatabaseDriverReactNative {
constructor() {
this.lastInsertId_ = null;
}
2017-06-11 23:11:14 +02:00
open(options) {
2019-10-09 21:35:13 +02:00
// SQLite.DEBUG(true);
2017-06-11 23:11:14 +02:00
return new Promise((resolve, reject) => {
2019-07-29 15:43:53 +02:00
SQLite.openDatabase(
{ name: options.name },
db => {
2019-07-29 15:43:53 +02:00
this.db_ = db;
resolve();
},
error => {
2019-07-29 15:43:53 +02:00
reject(error);
},
2019-07-29 15:43:53 +02:00
);
2017-06-11 23:11:14 +02:00
});
}
sqliteErrorToJsError(error) {
2017-07-05 23:29:00 +02:00
return error;
}
2017-06-11 23:11:14 +02:00
selectOne(sql, params = null) {
return new Promise((resolve, reject) => {
2019-07-29 15:43:53 +02:00
this.db_.executeSql(
sql,
params,
r => {
2019-07-29 15:43:53 +02:00
resolve(r.rows.length ? r.rows.item(0) : null);
},
error => {
2019-07-29 15:43:53 +02:00
reject(error);
},
2019-07-29 15:43:53 +02:00
);
2017-06-11 23:11:14 +02:00
});
}
selectAll(sql, params = null) {
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
return this.exec(sql, params).then(r => {
const output = [];
2017-06-11 23:11:14 +02:00
for (let i = 0; i < r.rows.length; i++) {
output.push(r.rows.item(i));
}
return output;
});
}
2020-09-06 14:07:00 +02:00
loadExtension(path) {
throw new Error(`No extension support for ${path} in react-native-sqlite-storage`);
}
2017-06-11 23:11:14 +02:00
exec(sql, params = null) {
return new Promise((resolve, reject) => {
2019-07-29 15:43:53 +02:00
this.db_.executeSql(
sql,
params,
r => {
2019-07-29 15:43:53 +02:00
if ('insertId' in r) this.lastInsertId_ = r.insertId;
resolve(r);
},
error => {
2019-07-29 15:43:53 +02:00
reject(error);
},
2019-07-29 15:43:53 +02:00
);
2017-06-11 23:11:14 +02:00
});
}
lastInsertId() {
return this.lastInsertId_;
}
2017-06-11 23:11:14 +02:00
}
2019-07-29 15:43:53 +02:00
module.exports = { DatabaseDriverReactNative };