2017-11-03 02:09:34 +02:00
|
|
|
const SQLite = require('react-native-sqlite-storage');
|
2017-06-11 23:11:14 +02:00
|
|
|
|
|
|
|
class DatabaseDriverReactNative {
|
|
|
|
|
2017-11-28 00:50:46 +02:00
|
|
|
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) => {
|
2017-11-28 00:50:46 +02:00
|
|
|
if ('insertId' in r) this.lastInsertId_ = r.insertId;
|
2017-06-11 23:11:14 +02:00
|
|
|
resolve(r);
|
|
|
|
}, (error) => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:50:46 +02:00
|
|
|
lastInsertId() {
|
|
|
|
return this.lastInsertId_;
|
|
|
|
}
|
|
|
|
|
2017-06-11 23:11:14 +02:00
|
|
|
}
|
|
|
|
|
2017-11-03 02:13:17 +02:00
|
|
|
module.exports = { DatabaseDriverReactNative };
|