1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/ReactNativeClient/lib/database-driver-react-native.js

75 lines
1.2 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 => {
this.db_ = db;
resolve();
},
error => {
reject(error);
}
);
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 => {
resolve(r.rows.length ? r.rows.item(0) : null);
},
error => {
reject(error);
}
);
2017-06-11 23:11:14 +02:00
});
}
selectAll(sql, params = null) {
2019-07-29 15:43:53 +02:00
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;
});
}
exec(sql, params = null) {
return new Promise((resolve, reject) => {
2019-07-29 15:43:53 +02:00
this.db_.executeSql(
sql,
params,
r => {
if ('insertId' in r) this.lastInsertId_ = r.insertId;
resolve(r);
},
error => {
reject(error);
}
);
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 };