react-native-flipper-databases
Version:
Flipper Databases plugin for React Native
91 lines (78 loc) • 2.66 kB
JavaScript
export class SQLiteStorageDriver {
constructor(databases) {
this.databases = databases;
}
async getDatabases() {
return this.databases;
}
async getTableNames(_ref) {
let {
database
} = _ref;
const [result] = await database.executeSql(`SELECT name FROM sqlite_schema WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%'`);
const tables = [];
for (let index = 0; index < result.rows.length; index++) {
tables.push(result.rows.item(index).name);
}
return tables;
}
async getTableStructure(_ref2, schema) {
let {
database
} = _ref2;
const [result] = await database.executeSql(`PRAGMA table_info(${schema})`);
const cols = [];
for (let index = 0; index < result.rows.length; index++) {
const col = result.rows.item(index);
cols.push([col.name, col.type, Boolean(col.notnull), Boolean(col.pk), col.dflt_value]);
}
return {
structureColumns: ['name', 'type', 'not null', 'primary key', 'default value'],
structureValues: cols,
indexesColumns: ['name', 'type'],
indexesValues: cols.filter(col => col[3]).map(_ref3 => {
let [name, type] = _ref3;
return [name, type];
})
};
}
async getTableData(databaseDescriptor, schema, order, reverse, start, count) {
const {
structureValues
} = await this.getTableStructure(databaseDescriptor, schema);
const columns = structureValues.map(_ref4 => {
let [colName] = _ref4;
return colName;
});
const [countResult] = await databaseDescriptor.database.executeSql(`SELECT COUNT(*) as count FROM ${schema}`);
const totalCount = countResult.rows.item(0).count;
const orderCol = order ?? 'rowid';
const orderSort = reverse ? 'DESC' : 'ASC';
const [result] = await databaseDescriptor.database.executeSql(`SELECT * FROM ${schema} ORDER BY ${orderCol} ${orderSort} LIMIT ${count} OFFSET ${start}`);
const values = [];
for (let index = 0; index < result.rows.length; index++) {
const row = result.rows.item(index);
values.push(columns.map(colName => row[colName]));
}
return {
columns,
values,
start,
count: result.rows.length,
total: totalCount
};
}
async getTableInfo(_ref5, table) {
let {
database
} = _ref5;
const [result] = await database.executeSql(`SELECT sql FROM sqlite_schema WHERE name = '${table}'`);
return {
definition: result.rows.item(0).sql
};
}
async executeSql(_databaseDescriptor, _query) {
return Promise.reject('Unsupported method');
}
}
//# sourceMappingURL=SQLiteStorage.js.map