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

66 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) {
2017-07-05 23:29:00 +02:00
//SQLite.DEBUG(true);
2017-06-11 23:11:14 +02:00
return new Promise((resolve, reject) => {
SQLite.openDatabase({ name: options.name }, (db) => {
this.db_ = db;
resolve();
}, (error) => {
reject(error);
});
});
}
2017-07-05 23:29:00 +02:00
sqliteErrorToJsError(error, sql = null, params = null) {
return error;
}
2017-06-24 19:40:03 +02:00
setDebugMode(v) {
//SQLite.DEBUG(v);
2017-06-11 23:11:14 +02:00
}
selectOne(sql, params = null) {
return new Promise((resolve, reject) => {
this.db_.executeSql(sql, params, (r) => {
resolve(r.rows.length ? r.rows.item(0) : null);
}, (error) => {
reject(error);
});
});
}
selectAll(sql, params = null) {
return this.exec(sql, params).then((r) => {
let output = []
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) => {
this.db_.executeSql(sql, params, (r) => {
if ('insertId' in r) this.lastInsertId_ = r.insertId;
2017-06-11 23:11:14 +02:00
resolve(r);
}, (error) => {
reject(error);
});
});
}
lastInsertId() {
return this.lastInsertId_;
}
2017-06-11 23:11:14 +02:00
}
2017-11-03 02:13:17 +02:00
module.exports = { DatabaseDriverReactNative };