mirror of
https://github.com/stfrigerio/sqliteDB.git
synced 2026-07-22 05:38:02 +00:00
multiple filters
This commit is contained in:
parent
c8539fadaf
commit
387a1bb751
5 changed files with 51 additions and 34 deletions
|
|
@ -4,8 +4,6 @@ export function buildSqlQuery(params: SqlParams): { query: string; queryParams:
|
|||
const {
|
||||
table,
|
||||
columns,
|
||||
keyColumn,
|
||||
value,
|
||||
dateColumn,
|
||||
startDate,
|
||||
endDate,
|
||||
|
|
@ -23,10 +21,19 @@ export function buildSqlQuery(params: SqlParams): { query: string; queryParams:
|
|||
const queryParams: any[] = [];
|
||||
const conditions: string[] = [];
|
||||
|
||||
// Add key-value condition if provided
|
||||
if (keyColumn && value !== undefined) {
|
||||
conditions.push(`"${keyColumn}" = ?`);
|
||||
queryParams.push(value);
|
||||
// Handle multiple filters
|
||||
if (filterColumn && filterValue) {
|
||||
const filterColumns = Array.isArray(filterColumn) ? filterColumn : [filterColumn];
|
||||
const filterValues = Array.isArray(filterValue) ? filterValue : [filterValue];
|
||||
|
||||
if (filterColumns.length !== filterValues.length) {
|
||||
throw new Error('Number of filter columns must match number of filter values');
|
||||
}
|
||||
|
||||
filterColumns.forEach((col, index) => {
|
||||
conditions.push(`"${col}" = ?`);
|
||||
queryParams.push(filterValues[index]);
|
||||
});
|
||||
}
|
||||
|
||||
// Add date range conditions if provided
|
||||
|
|
@ -40,13 +47,7 @@ export function buildSqlQuery(params: SqlParams): { query: string; queryParams:
|
|||
queryParams.push(endDate);
|
||||
}
|
||||
}
|
||||
|
||||
// Add filter condition if provided
|
||||
if (filterColumn && filterValue !== undefined) {
|
||||
conditions.push(`"${filterColumn}" = ?`);
|
||||
queryParams.push(filterValue);
|
||||
}
|
||||
|
||||
|
||||
// Build the query
|
||||
let query = `SELECT ${selectCols} FROM "${table}"`;
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,10 @@ export function parseSqlParams(source: string): SqlParams | null {
|
|||
case 'columns':
|
||||
params.columns = val.split(',').map(c => c.trim()).join(', ');
|
||||
break;
|
||||
case 'filterColumn':
|
||||
case 'filterValue':
|
||||
params[key] = val.split(',').map(v => v.trim());
|
||||
break;
|
||||
case 'limit':
|
||||
const limitNum = parseInt(val);
|
||||
if (!isNaN(limitNum)) params.limit = limitNum;
|
||||
|
|
@ -27,15 +31,19 @@ export function parseSqlParams(source: string): SqlParams | null {
|
|||
}
|
||||
break;
|
||||
case 'table':
|
||||
case 'keyColumn':
|
||||
case 'value':
|
||||
params.table = val;
|
||||
break;
|
||||
case 'dateColumn':
|
||||
params.dateColumn = val;
|
||||
break;
|
||||
case 'startDate':
|
||||
params.startDate = val;
|
||||
break;
|
||||
case 'endDate':
|
||||
case 'filterColumn':
|
||||
case 'filterValue':
|
||||
params.endDate = val;
|
||||
break;
|
||||
case 'orderBy':
|
||||
params[key] = val;
|
||||
params.orderBy = val;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,11 +33,19 @@ export async function validateTable(db: any, params: SqlParams) {
|
|||
}
|
||||
|
||||
// Validate filterColumn if specified
|
||||
if (params.filterColumn && !availableColumns.includes(params.filterColumn)) {
|
||||
return {
|
||||
message: `Filter column "${params.filterColumn}" does not exist in table "${params.table}".`,
|
||||
availableColumns
|
||||
};
|
||||
if (params.filterColumn) {
|
||||
const filterColumns = Array.isArray(params.filterColumn)
|
||||
? params.filterColumn
|
||||
: [params.filterColumn];
|
||||
|
||||
for (const col of filterColumns) {
|
||||
if (!availableColumns.includes(col)) {
|
||||
return {
|
||||
message: `Filter column "${col}" does not exist in table "${params.table}".`,
|
||||
availableColumns
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Validate orderBy if specified
|
||||
|
|
|
|||
|
|
@ -16,15 +16,15 @@ export async function processSqlBlock(dbService: DBService, source: string, el:
|
|||
el.createEl("p", { text: "Example usage:" });
|
||||
el.createEl("pre", {
|
||||
text: `table: tasks
|
||||
columns: title, status, dueDate
|
||||
dateColumn: dueDate
|
||||
startDate: 2024-01-01
|
||||
endDate: 2024-12-31
|
||||
filterColumn: status
|
||||
filterValue: active
|
||||
limit: 10
|
||||
orderBy: dueDate
|
||||
orderDirection: asc`
|
||||
columns: title, status
|
||||
filterColumn: status, priority
|
||||
filterValue: active, high
|
||||
dateColumn: dueDate
|
||||
startDate: 2024-01-01
|
||||
endDate: 2024-12-31
|
||||
limit: 10
|
||||
orderBy: dueDate
|
||||
orderDirection: asc`
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,8 +6,8 @@ export interface SqlParams {
|
|||
dateColumn?: string;
|
||||
startDate?: string;
|
||||
endDate?: string;
|
||||
filterColumn?: string;
|
||||
filterValue?: string;
|
||||
filterColumn?: string | string[];
|
||||
filterValue?: string | string[];
|
||||
orderBy?: string;
|
||||
orderDirection?: string;
|
||||
limit?: number;
|
||||
|
|
|
|||
Loading…
Reference in a new issue