csv-sql-engine 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/engine/define-ast-handler.d.ts +3 -3
- package/dist/engine/engine.js +1 -1
- package/dist/engine/handlers/row-delete.handler.js +8 -2
- package/dist/engine/handlers/row-insert.handler.js +12 -9
- package/dist/engine/handlers/row-select.handler.js +11 -8
- package/dist/engine/handlers/row-update.handler.js +8 -2
- package/dist/engine/sort-values.d.ts +10 -4
- package/package.json +1 -1
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { type MaybePromise } from '@augment-vir/common';
|
|
2
2
|
import { type AstHandlerParams } from './params.js';
|
|
3
|
+
import { type SortValuesOutput } from './sort-values.js';
|
|
3
4
|
/**
|
|
4
5
|
* Output from a handler that handled a SQL query.
|
|
5
6
|
*
|
|
6
7
|
* @category Internal
|
|
7
8
|
*/
|
|
8
|
-
export type AstHandlerResult = {
|
|
9
|
-
|
|
10
|
-
values: string[][];
|
|
9
|
+
export type AstHandlerResult = SortValuesOutput & {
|
|
10
|
+
numberOfRowsAffected: number;
|
|
11
11
|
};
|
|
12
12
|
/**
|
|
13
13
|
* An AST / SQL handler.
|
package/dist/engine/engine.js
CHANGED
|
@@ -51,7 +51,7 @@ async function executeIndividualCommand(params) {
|
|
|
51
51
|
for (const handler of allAstHandlers) {
|
|
52
52
|
const output = await handler.handler(params);
|
|
53
53
|
if (output) {
|
|
54
|
-
return output
|
|
54
|
+
return output;
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
/** If nothing handled the query, then we don't support it. */
|
|
@@ -36,12 +36,18 @@ export const rowDeleteHandler = defineAstHandler({
|
|
|
36
36
|
},
|
|
37
37
|
unconsumedInterpolationValues: sql.unconsumedValues,
|
|
38
38
|
})
|
|
39
|
-
:
|
|
39
|
+
: {
|
|
40
|
+
columnNames: [],
|
|
41
|
+
values: [],
|
|
42
|
+
};
|
|
40
43
|
rowIndexesToDelete.forEach((rowIndexToDelete) => {
|
|
41
44
|
csvContents.splice(rowIndexToDelete, 1);
|
|
42
45
|
});
|
|
43
46
|
await writeCsvFile(tableFilePath, csvContents);
|
|
44
|
-
return
|
|
47
|
+
return {
|
|
48
|
+
...result,
|
|
49
|
+
numberOfRowsAffected: rowIndexesToDelete.length,
|
|
50
|
+
};
|
|
45
51
|
});
|
|
46
52
|
return results.flat().filter(check.isTruthy);
|
|
47
53
|
}
|
|
@@ -33,20 +33,23 @@ export const rowInsertHandler = defineAstHandler({
|
|
|
33
33
|
unconsumedInterpolationValues: sql.unconsumedValues,
|
|
34
34
|
}).values[0], 'No sorted row retrieved.');
|
|
35
35
|
await appendCsvRow(newRow, tableFilePath);
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return sortValues({
|
|
36
|
+
const readResult = ast.returning
|
|
37
|
+
? sortValues({
|
|
39
38
|
csvFileHeaderOrder,
|
|
40
|
-
sqlQueryHeaderOrder:
|
|
39
|
+
sqlQueryHeaderOrder: ast.returning.columns.map((column) => column.expr.column),
|
|
41
40
|
from: {
|
|
42
41
|
csvFile: [newRow],
|
|
43
42
|
},
|
|
44
43
|
unconsumedInterpolationValues: undefined,
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
44
|
+
})
|
|
45
|
+
: {
|
|
46
|
+
columnNames: [],
|
|
47
|
+
values: [],
|
|
48
|
+
};
|
|
49
|
+
return {
|
|
50
|
+
...readResult,
|
|
51
|
+
numberOfRowsAffected: 1,
|
|
52
|
+
};
|
|
50
53
|
});
|
|
51
54
|
return results.filter(check.isTruthy);
|
|
52
55
|
}
|
|
@@ -27,14 +27,17 @@ export const rowSelectHandler = defineAstHandler({
|
|
|
27
27
|
});
|
|
28
28
|
const rowIndexesToSelect = findWhereMatches(ast.where, csvContents, tableFilePath);
|
|
29
29
|
const columnNames = ast.columns.map((column) => column.expr.column);
|
|
30
|
-
return
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
30
|
+
return {
|
|
31
|
+
...sortValues({
|
|
32
|
+
csvFileHeaderOrder: csvHeaders,
|
|
33
|
+
sqlQueryHeaderOrder: columnNames,
|
|
34
|
+
from: {
|
|
35
|
+
csvFile: csvContents.filter((row, index) => rowIndexesToSelect.includes(index)),
|
|
36
|
+
},
|
|
37
|
+
unconsumedInterpolationValues: sql.unconsumedValues,
|
|
38
|
+
}),
|
|
39
|
+
numberOfRowsAffected: 0,
|
|
40
|
+
};
|
|
38
41
|
});
|
|
39
42
|
return allSelections.flat().filter(check.isTruthy);
|
|
40
43
|
}
|
|
@@ -50,9 +50,15 @@ export const rowUpdateHandler = defineAstHandler({
|
|
|
50
50
|
},
|
|
51
51
|
unconsumedInterpolationValues: sql.unconsumedValues,
|
|
52
52
|
})
|
|
53
|
-
:
|
|
53
|
+
: {
|
|
54
|
+
columnNames: [],
|
|
55
|
+
values: [],
|
|
56
|
+
};
|
|
54
57
|
await writeCsvFile(tableFilePath, csvContents);
|
|
55
|
-
return
|
|
58
|
+
return {
|
|
59
|
+
...result,
|
|
60
|
+
numberOfRowsAffected: rowIndexesToUpdate.length,
|
|
61
|
+
};
|
|
56
62
|
});
|
|
57
63
|
return results.flat().filter(check.isTruthy);
|
|
58
64
|
}
|
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import { type RequireExactlyOne } from 'type-fest';
|
|
2
2
|
import { type ConsumableValue } from '../sql/sql.js';
|
|
3
|
+
/**
|
|
4
|
+
* Output from {@link sortValues}.
|
|
5
|
+
*
|
|
6
|
+
* @category Internal
|
|
7
|
+
*/
|
|
8
|
+
export type SortValuesOutput = {
|
|
9
|
+
values: string[][];
|
|
10
|
+
columnNames: string[];
|
|
11
|
+
};
|
|
3
12
|
/**
|
|
4
13
|
* Sorts values for CSV insertion or reading and handle interpolated values.
|
|
5
14
|
*
|
|
@@ -15,7 +24,4 @@ export declare function sortValues({ csvFileHeaderOrder, sqlQueryHeaderOrder, fr
|
|
|
15
24
|
sqlQuery: ReadonlyArray<ReadonlyArray<string>>;
|
|
16
25
|
}>;
|
|
17
26
|
unconsumedInterpolationValues: undefined | ConsumableValue[];
|
|
18
|
-
}>):
|
|
19
|
-
values: string[][];
|
|
20
|
-
columnNames: string[];
|
|
21
|
-
};
|
|
27
|
+
}>): SortValuesOutput;
|