diff --git a/src/codeblocks/processSqlBlock/helpers/buildSqlQuery.ts b/src/codeblocks/processSqlBlock/helpers/buildSqlQuery.ts index d9f6b55..228b74c 100644 --- a/src/codeblocks/processSqlBlock/helpers/buildSqlQuery.ts +++ b/src/codeblocks/processSqlBlock/helpers/buildSqlQuery.ts @@ -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}"`; diff --git a/src/codeblocks/processSqlBlock/helpers/parseSqlParams.ts b/src/codeblocks/processSqlBlock/helpers/parseSqlParams.ts index f5acb88..b915fdb 100644 --- a/src/codeblocks/processSqlBlock/helpers/parseSqlParams.ts +++ b/src/codeblocks/processSqlBlock/helpers/parseSqlParams.ts @@ -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; } } diff --git a/src/codeblocks/processSqlBlock/helpers/validateTable.ts b/src/codeblocks/processSqlBlock/helpers/validateTable.ts index 5af7941..1949f0b 100644 --- a/src/codeblocks/processSqlBlock/helpers/validateTable.ts +++ b/src/codeblocks/processSqlBlock/helpers/validateTable.ts @@ -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 diff --git a/src/codeblocks/processSqlBlock/index.ts b/src/codeblocks/processSqlBlock/index.ts index 31d46af..6bd1828 100644 --- a/src/codeblocks/processSqlBlock/index.ts +++ b/src/codeblocks/processSqlBlock/index.ts @@ -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; } diff --git a/src/codeblocks/processSqlBlock/types.ts b/src/codeblocks/processSqlBlock/types.ts index be532c0..28c75b4 100644 --- a/src/codeblocks/processSqlBlock/types.ts +++ b/src/codeblocks/processSqlBlock/types.ts @@ -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;