mirror of
https://github.com/Fabio286/antares.git
synced 2025-06-05 21:59:22 +02:00
feat(SQLite): keys support
This commit is contained in:
@ -286,22 +286,42 @@ export class SQLiteClient extends AntaresCore {
|
|||||||
* @memberof SQLiteClient
|
* @memberof SQLiteClient
|
||||||
*/
|
*/
|
||||||
async getTableIndexes ({ schema, table }) {
|
async getTableIndexes ({ schema, table }) {
|
||||||
const { rows } = await this.raw(`SHOW INDEXES FROM \`${table}\` FROM \`${schema}\``);
|
const remappedIndexes = [];
|
||||||
|
const { rows: primaryKeys } = await this.raw(`SELECT * FROM "${schema}".pragma_table_info('${table}') WHERE pk != 0`);
|
||||||
|
|
||||||
return rows.map(row => {
|
for (const key of primaryKeys) {
|
||||||
return {
|
remappedIndexes.push({
|
||||||
unique: !row.Non_unique,
|
name: 'PRIMARY',
|
||||||
name: row.Key_name,
|
column: key.name,
|
||||||
column: row.Column_name,
|
indexType: null,
|
||||||
indexType: row.Index_type,
|
type: 'PRIMARY',
|
||||||
type: row.Key_name === 'PRIMARY' ? 'PRIMARY' : !row.Non_unique ? 'UNIQUE' : row.Index_type === 'FULLTEXT' ? 'FULLTEXT' : 'INDEX',
|
cardinality: null,
|
||||||
cardinality: row.Cardinality,
|
comment: '',
|
||||||
comment: row.Comment,
|
indexComment: ''
|
||||||
indexComment: row.Index_comment
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { rows: indexes } = await this.raw(`SELECT * FROM "${schema}".pragma_index_list('${table}');`);
|
||||||
|
|
||||||
|
for (const index of indexes) {
|
||||||
|
const { rows: details } = await this.raw(`SELECT * FROM "${schema}".pragma_index_info('${index.name}');`);
|
||||||
|
|
||||||
|
for (const detail of details) {
|
||||||
|
remappedIndexes.push({
|
||||||
|
name: index.name,
|
||||||
|
column: detail.name,
|
||||||
|
indexType: null,
|
||||||
|
type: index.unique === 1 ? 'UNIQUE' : 'INDEX',
|
||||||
|
cardinality: null,
|
||||||
|
comment: '',
|
||||||
|
indexComment: ''
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return remappedIndexes;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Object} params
|
* @param {Object} params
|
||||||
* @param {String} params.schema
|
* @param {String} params.schema
|
||||||
@ -1235,7 +1255,7 @@ export class SQLiteClient extends AntaresCore {
|
|||||||
sql = sql.replace(/(\/\*(.|[\r\n])*?\*\/)|(--(.*|[\r\n]))/gm, '');// Remove comments
|
sql = sql.replace(/(\/\*(.|[\r\n])*?\*\/)|(--(.*|[\r\n]))/gm, '');// Remove comments
|
||||||
|
|
||||||
const resultsArr = [];
|
const resultsArr = [];
|
||||||
// let paramsArr = [];
|
let paramsArr = [];
|
||||||
const queries = args.split
|
const queries = args.split
|
||||||
? sql.split(/((?:[^;'"]*(?:"(?:\\.|[^"])*"|'(?:\\.|[^'])*')[^;'"]*)+)|;/gm)
|
? sql.split(/((?:[^;'"]*(?:"(?:\\.|[^"])*"|'(?:\\.|[^'])*')[^;'"]*)+)|;/gm)
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
@ -1250,6 +1270,7 @@ export class SQLiteClient extends AntaresCore {
|
|||||||
const keysArr = [];
|
const keysArr = [];
|
||||||
|
|
||||||
const { rows, report, fields, keys, duration } = await new Promise((resolve, reject) => {
|
const { rows, report, fields, keys, duration } = await new Promise((resolve, reject) => {
|
||||||
|
(async () => {
|
||||||
let queryResult;
|
let queryResult;
|
||||||
let affectedRows;
|
let affectedRows;
|
||||||
let fields;
|
let fields;
|
||||||
@ -1271,7 +1292,9 @@ export class SQLiteClient extends AntaresCore {
|
|||||||
affectedRows = info.changes;
|
affectedRows = info.changes;
|
||||||
}
|
}
|
||||||
|
|
||||||
const remappedFields = fields
|
timeStop = new Date();
|
||||||
|
|
||||||
|
let remappedFields = fields
|
||||||
? fields.map(field => {
|
? fields.map(field => {
|
||||||
return {
|
return {
|
||||||
name: field.name,
|
name: field.name,
|
||||||
@ -1286,7 +1309,43 @@ export class SQLiteClient extends AntaresCore {
|
|||||||
}).filter(Boolean)
|
}).filter(Boolean)
|
||||||
: [];
|
: [];
|
||||||
|
|
||||||
timeStop = new Date();
|
if (args.details) {
|
||||||
|
paramsArr = remappedFields.map(field => {
|
||||||
|
return {
|
||||||
|
table: field.table,
|
||||||
|
schema: field.schema
|
||||||
|
};
|
||||||
|
}).filter((val, i, arr) => arr.findIndex(el => el.schema === val.schema && el.table === val.table) === i);
|
||||||
|
|
||||||
|
for (const paramObj of paramsArr) {
|
||||||
|
if (!paramObj.table || !paramObj.schema) continue;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const indexes = await this.getTableIndexes(paramObj);
|
||||||
|
|
||||||
|
remappedFields = remappedFields.map(field => {
|
||||||
|
// const detailedField = columns.find(f => f.name === field.name);
|
||||||
|
const fieldIndex = indexes.find(i => i.column === field.name);
|
||||||
|
if (field.table === paramObj.table && field.schema === paramObj.schema) {
|
||||||
|
// if (detailedField) {
|
||||||
|
// const length = detailedField.numPrecision || detailedField.charLength || detailedField.datePrecision || null;
|
||||||
|
// field = { ...field, ...detailedField, length };
|
||||||
|
// }
|
||||||
|
|
||||||
|
if (fieldIndex) {
|
||||||
|
const key = fieldIndex.type === 'PRIMARY' ? 'pri' : fieldIndex.type === 'UNIQUE' ? 'uni' : 'mul';
|
||||||
|
field = { ...field, key };
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return field;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
resolve({
|
resolve({
|
||||||
duration: timeStop - timeStart,
|
duration: timeStop - timeStart,
|
||||||
@ -1295,6 +1354,7 @@ export class SQLiteClient extends AntaresCore {
|
|||||||
fields: remappedFields,
|
fields: remappedFields,
|
||||||
keys: keysArr
|
keys: keysArr
|
||||||
});
|
});
|
||||||
|
})();
|
||||||
});
|
});
|
||||||
|
|
||||||
resultsArr.push({ rows, report, fields, keys, duration });
|
resultsArr.push({ rows, report, fields, keys, duration });
|
||||||
|
Reference in New Issue
Block a user