@outsidedata/dolex 0.1.13 → 1.0.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/README.md +8 -8
- package/dist/src/analysis/rules.d.ts.map +1 -1
- package/dist/src/analysis/rules.js +8 -2
- package/dist/src/analysis/rules.js.map +1 -1
- package/dist/src/connectors/csv.d.ts.map +1 -1
- package/dist/src/connectors/csv.js +31 -1
- package/dist/src/connectors/csv.js.map +1 -1
- package/dist/src/connectors/dsl-compiler.d.ts +7 -6
- package/dist/src/connectors/dsl-compiler.d.ts.map +1 -1
- package/dist/src/connectors/dsl-compiler.js +72 -79
- package/dist/src/connectors/dsl-compiler.js.map +1 -1
- package/dist/src/connectors/dsl-validator.js +6 -1
- package/dist/src/connectors/dsl-validator.js.map +1 -1
- package/dist/src/connectors/index.d.ts +0 -3
- package/dist/src/connectors/index.d.ts.map +1 -1
- package/dist/src/connectors/index.js +0 -3
- package/dist/src/connectors/index.js.map +1 -1
- package/dist/src/connectors/js-aggregation.d.ts +4 -4
- package/dist/src/connectors/js-aggregation.d.ts.map +1 -1
- package/dist/src/connectors/js-aggregation.js +22 -16
- package/dist/src/connectors/js-aggregation.js.map +1 -1
- package/dist/src/connectors/manager.d.ts.map +1 -1
- package/dist/src/connectors/manager.js +4 -21
- package/dist/src/connectors/manager.js.map +1 -1
- package/dist/src/mcp/app-shell.js +1 -1
- package/dist/src/mcp/index.d.ts +8 -8
- package/dist/src/mcp/index.js +87 -53
- package/dist/src/mcp/index.js.map +1 -1
- package/dist/src/mcp/prompts.js +4 -4
- package/dist/src/mcp/prompts.js.map +1 -1
- package/dist/src/mcp/spec-store.d.ts +2 -1
- package/dist/src/mcp/spec-store.d.ts.map +1 -1
- package/dist/src/mcp/spec-store.js +4 -2
- package/dist/src/mcp/spec-store.js.map +1 -1
- package/dist/src/mcp/tools/analyze.d.ts +1 -1
- package/dist/src/mcp/tools/analyze.js +2 -2
- package/dist/src/mcp/tools/analyze.js.map +1 -1
- package/dist/src/mcp/tools/bug-report.d.ts.map +1 -1
- package/dist/src/mcp/tools/bug-report.js +2 -10
- package/dist/src/mcp/tools/bug-report.js.map +1 -1
- package/dist/src/mcp/tools/dashboard.js +1 -1
- package/dist/src/mcp/tools/dashboard.js.map +1 -1
- package/dist/src/mcp/tools/dsl-schemas.d.ts +53 -52
- package/dist/src/mcp/tools/dsl-schemas.d.ts.map +1 -1
- package/dist/src/mcp/tools/dsl-schemas.js +3 -3
- package/dist/src/mcp/tools/dsl-schemas.js.map +1 -1
- package/dist/src/mcp/tools/export-html.d.ts +1 -1
- package/dist/src/mcp/tools/export-html.js +2 -2
- package/dist/src/mcp/tools/export-html.js.map +1 -1
- package/dist/src/mcp/tools/list-patterns.js +2 -2
- package/dist/src/mcp/tools/list-patterns.js.map +1 -1
- package/dist/src/mcp/tools/query-source.d.ts +17 -17
- package/dist/src/mcp/tools/query-source.js +4 -4
- package/dist/src/mcp/tools/query-source.js.map +1 -1
- package/dist/src/mcp/tools/refine.d.ts +124 -15
- package/dist/src/mcp/tools/refine.d.ts.map +1 -1
- package/dist/src/mcp/tools/refine.js +335 -253
- package/dist/src/mcp/tools/refine.js.map +1 -1
- package/dist/src/mcp/tools/result-cache.d.ts +1 -1
- package/dist/src/mcp/tools/result-cache.js +1 -1
- package/dist/src/mcp/tools/screenshot.js +1 -1
- package/dist/src/mcp/tools/screenshot.js.map +1 -1
- package/dist/src/mcp/tools/server-privacy.d.ts.map +1 -1
- package/dist/src/mcp/tools/server-privacy.js +6 -7
- package/dist/src/mcp/tools/server-privacy.js.map +1 -1
- package/dist/src/mcp/tools/shared.d.ts +15 -2
- package/dist/src/mcp/tools/shared.d.ts.map +1 -1
- package/dist/src/mcp/tools/shared.js +64 -6
- package/dist/src/mcp/tools/shared.js.map +1 -1
- package/dist/src/mcp/tools/sources.d.ts +5 -113
- package/dist/src/mcp/tools/sources.d.ts.map +1 -1
- package/dist/src/mcp/tools/sources.js +15 -44
- package/dist/src/mcp/tools/sources.js.map +1 -1
- package/dist/src/mcp/tools/visualize-from-source.d.ts +53 -77
- package/dist/src/mcp/tools/visualize-from-source.d.ts.map +1 -1
- package/dist/src/mcp/tools/visualize-from-source.js +24 -9
- package/dist/src/mcp/tools/visualize-from-source.js.map +1 -1
- package/dist/src/mcp/tools/visualize.d.ts +47 -102
- package/dist/src/mcp/tools/visualize.d.ts.map +1 -1
- package/dist/src/mcp/tools/visualize.js +24 -24
- package/dist/src/mcp/tools/visualize.js.map +1 -1
- package/dist/src/patterns/definitions/comparison/grouped-bar.js +5 -5
- package/dist/src/patterns/definitions/comparison/grouped-bar.js.map +1 -1
- package/dist/src/patterns/definitions/composition/waffle.d.ts.map +1 -1
- package/dist/src/patterns/definitions/composition/waffle.js +17 -3
- package/dist/src/patterns/definitions/composition/waffle.js.map +1 -1
- package/dist/src/patterns/definitions/distribution/strip-plot.d.ts.map +1 -1
- package/dist/src/patterns/definitions/distribution/strip-plot.js +12 -3
- package/dist/src/patterns/definitions/distribution/strip-plot.js.map +1 -1
- package/dist/src/patterns/definitions/time/sparkline-grid.js +2 -2
- package/dist/src/patterns/definitions/time/sparkline-grid.js.map +1 -1
- package/dist/src/patterns/selector.d.ts.map +1 -1
- package/dist/src/patterns/selector.js +46 -1
- package/dist/src/patterns/selector.js.map +1 -1
- package/dist/src/renderers/d3/comparison/bar.d.ts.map +1 -1
- package/dist/src/renderers/d3/comparison/bar.js +24 -1
- package/dist/src/renderers/d3/comparison/bar.js.map +1 -1
- package/dist/src/renderers/d3/comparison/grouped-bar.d.ts.map +1 -1
- package/dist/src/renderers/d3/comparison/grouped-bar.js +9 -4
- package/dist/src/renderers/d3/comparison/grouped-bar.js.map +1 -1
- package/dist/src/renderers/d3/composition/donut.d.ts.map +1 -1
- package/dist/src/renderers/d3/composition/donut.js +12 -0
- package/dist/src/renderers/d3/composition/donut.js.map +1 -1
- package/dist/src/renderers/d3/composition/stacked-bar.d.ts.map +1 -1
- package/dist/src/renderers/d3/composition/stacked-bar.js +4 -0
- package/dist/src/renderers/d3/composition/stacked-bar.js.map +1 -1
- package/dist/src/renderers/d3/composition/treemap.js +2 -1
- package/dist/src/renderers/d3/composition/treemap.js.map +1 -1
- package/dist/src/renderers/d3/distribution/beeswarm.d.ts.map +1 -1
- package/dist/src/renderers/d3/distribution/beeswarm.js +8 -0
- package/dist/src/renderers/d3/distribution/beeswarm.js.map +1 -1
- package/dist/src/renderers/d3/distribution/histogram.js +36 -2
- package/dist/src/renderers/d3/distribution/histogram.js.map +1 -1
- package/dist/src/renderers/d3/flow/funnel.d.ts.map +1 -1
- package/dist/src/renderers/d3/flow/funnel.js +4 -0
- package/dist/src/renderers/d3/flow/funnel.js.map +1 -1
- package/dist/src/renderers/d3/flow/sankey-layout.js +20 -3
- package/dist/src/renderers/d3/flow/sankey-layout.js.map +1 -1
- package/dist/src/renderers/d3/heatmap.d.ts.map +1 -1
- package/dist/src/renderers/d3/heatmap.js +18 -3
- package/dist/src/renderers/d3/heatmap.js.map +1 -1
- package/dist/src/renderers/d3/radar.d.ts.map +1 -1
- package/dist/src/renderers/d3/radar.js +3 -1
- package/dist/src/renderers/d3/radar.js.map +1 -1
- package/dist/src/renderers/d3/relationship.d.ts.map +1 -1
- package/dist/src/renderers/d3/relationship.js +29 -7
- package/dist/src/renderers/d3/relationship.js.map +1 -1
- package/dist/src/renderers/d3/shared.js +7 -7
- package/dist/src/renderers/d3/shared.js.map +1 -1
- package/dist/src/renderers/d3/time/area.js +21 -7
- package/dist/src/renderers/d3/time/area.js.map +1 -1
- package/dist/src/renderers/d3/time/calendar-heatmap.js +6 -0
- package/dist/src/renderers/d3/time/calendar-heatmap.js.map +1 -1
- package/dist/src/renderers/d3/time/horizon-chart.d.ts.map +1 -1
- package/dist/src/renderers/d3/time/horizon-chart.js +10 -0
- package/dist/src/renderers/d3/time/horizon-chart.js.map +1 -1
- package/dist/src/renderers/d3/time/line.d.ts.map +1 -1
- package/dist/src/renderers/d3/time/line.js +27 -7
- package/dist/src/renderers/d3/time/line.js.map +1 -1
- package/dist/src/renderers/d3/time/small-multiples.js +8 -0
- package/dist/src/renderers/d3/time/small-multiples.js.map +1 -1
- package/dist/src/renderers/d3/time/sparkline-grid.d.ts.map +1 -1
- package/dist/src/renderers/d3/time/sparkline-grid.js +5 -2
- package/dist/src/renderers/d3/time/sparkline-grid.js.map +1 -1
- package/dist/src/renderers/d3/time/stream-graph.js +6 -0
- package/dist/src/renderers/d3/time/stream-graph.js.map +1 -1
- package/dist/src/renderers/html/_generated/bundles.d.ts +39 -39
- package/dist/src/renderers/html/_generated/bundles.d.ts.map +1 -1
- package/dist/src/renderers/html/_generated/bundles.js +39 -39
- package/dist/src/renderers/html/_generated/bundles.js.map +1 -1
- package/dist/src/renderers/html/builders/dashboard.js +2 -2
- package/dist/src/renderers/html/template.js +2 -2
- package/dist/src/renderers/html/template.js.map +1 -1
- package/dist/src/types.d.ts +33 -26
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/types.js.map +1 -1
- package/package.json +2 -4
- package/src/analysis/rules.ts +10 -2
- package/src/connectors/csv.ts +31 -1
- package/src/connectors/dsl-compiler.ts +76 -82
- package/src/connectors/dsl-validator.ts +6 -1
- package/src/connectors/index.ts +0 -3
- package/src/connectors/js-aggregation.ts +20 -15
- package/src/connectors/manager.ts +4 -24
- package/src/mcp/app-shell.ts +1 -1
- package/src/mcp/index.ts +87 -53
- package/src/mcp/prompts.ts +4 -4
- package/src/mcp/spec-store.ts +5 -1
- package/src/mcp/tools/analyze.ts +2 -2
- package/src/mcp/tools/bug-report.ts +2 -10
- package/src/mcp/tools/dashboard.ts +1 -1
- package/src/mcp/tools/dsl-schemas.ts +3 -3
- package/src/mcp/tools/export-html.ts +2 -2
- package/src/mcp/tools/list-patterns.ts +2 -2
- package/src/mcp/tools/query-source.ts +4 -4
- package/src/mcp/tools/refine.ts +333 -258
- package/src/mcp/tools/result-cache.ts +1 -1
- package/src/mcp/tools/screenshot.ts +1 -1
- package/src/mcp/tools/server-privacy.ts +6 -7
- package/src/mcp/tools/shared.ts +70 -7
- package/src/mcp/tools/sources.ts +18 -46
- package/src/mcp/tools/visualize-from-source.ts +24 -9
- package/src/mcp/tools/visualize.ts +27 -27
- package/src/patterns/definitions/comparison/grouped-bar.ts +5 -5
- package/src/patterns/definitions/composition/waffle.ts +17 -3
- package/src/patterns/definitions/distribution/strip-plot.ts +12 -3
- package/src/patterns/definitions/time/sparkline-grid.ts +2 -2
- package/src/patterns/selector.ts +47 -1
- package/src/renderers/d3/comparison/bar.ts +24 -1
- package/src/renderers/d3/comparison/grouped-bar.ts +9 -4
- package/src/renderers/d3/composition/donut.ts +14 -0
- package/src/renderers/d3/composition/stacked-bar.ts +5 -0
- package/src/renderers/d3/composition/treemap.ts +2 -1
- package/src/renderers/d3/distribution/beeswarm.ts +12 -0
- package/src/renderers/d3/distribution/histogram.ts +44 -2
- package/src/renderers/d3/flow/funnel.ts +8 -0
- package/src/renderers/d3/flow/sankey-layout.ts +23 -3
- package/src/renderers/d3/heatmap.ts +20 -2
- package/src/renderers/d3/radar.ts +4 -1
- package/src/renderers/d3/relationship.ts +31 -6
- package/src/renderers/d3/shared.ts +7 -7
- package/src/renderers/d3/time/area.ts +22 -7
- package/src/renderers/d3/time/calendar-heatmap.ts +5 -0
- package/src/renderers/d3/time/horizon-chart.ts +11 -0
- package/src/renderers/d3/time/line.ts +24 -9
- package/src/renderers/d3/time/small-multiples.ts +6 -0
- package/src/renderers/d3/time/sparkline-grid.ts +5 -2
- package/src/renderers/d3/time/stream-graph.ts +5 -0
- package/src/renderers/html/_generated/bundles.ts +39 -39
- package/src/renderers/html/builders/dashboard.ts +2 -2
- package/src/renderers/html/template.ts +2 -2
- package/src/types.ts +21 -33
- package/src/connectors/mysql.ts +0 -378
- package/src/connectors/postgres.ts +0 -367
- package/src/connectors/sqlite.ts +0 -364
package/src/connectors/sqlite.ts
DELETED
|
@@ -1,364 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SQLite Connector
|
|
3
|
-
*
|
|
4
|
-
* Opens an existing SQLite database file in read-only mode.
|
|
5
|
-
* Introspects schema via PRAGMA table_info / PRAGMA foreign_key_list.
|
|
6
|
-
* Analyzes columns with sample values and type inference from actual data.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import Database from 'better-sqlite3';
|
|
10
|
-
import * as fs from 'fs';
|
|
11
|
-
import * as path from 'path';
|
|
12
|
-
import type {
|
|
13
|
-
DataSourceType,
|
|
14
|
-
DataSourceConfig,
|
|
15
|
-
SqliteSourceConfig,
|
|
16
|
-
DataSchema,
|
|
17
|
-
DataTable,
|
|
18
|
-
DataColumn,
|
|
19
|
-
ForeignKey,
|
|
20
|
-
} from '../types.js';
|
|
21
|
-
import type { DataConnector, ConnectedSource, QueryExecutionResult } from './types.js';
|
|
22
|
-
|
|
23
|
-
const SAMPLE_LIMIT = 30;
|
|
24
|
-
const SAMPLE_DISPLAY_LIMIT = 20;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Map SQLite declared type to our column type system.
|
|
28
|
-
* Falls back to inspecting actual data if the declared type is ambiguous.
|
|
29
|
-
*/
|
|
30
|
-
function mapSqliteType(
|
|
31
|
-
declaredType: string | null,
|
|
32
|
-
columnName: string,
|
|
33
|
-
samples: string[],
|
|
34
|
-
uniqueCount: number,
|
|
35
|
-
totalRows: number
|
|
36
|
-
): DataColumn['type'] {
|
|
37
|
-
const lower = columnName.toLowerCase();
|
|
38
|
-
|
|
39
|
-
// ID detection by name
|
|
40
|
-
if (lower === 'id' || lower === 'rowid') return 'id';
|
|
41
|
-
if (lower.endsWith('_id') || (lower.endsWith('id') && uniqueCount > totalRows * 0.5)) {
|
|
42
|
-
return 'id';
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Check declared type
|
|
46
|
-
const dt = (declaredType || '').toUpperCase();
|
|
47
|
-
|
|
48
|
-
if (dt.includes('INT') || dt.includes('REAL') || dt.includes('FLOAT') || dt.includes('DOUBLE') || dt.includes('NUMERIC') || dt.includes('DECIMAL')) {
|
|
49
|
-
// But if it's named like an ID, keep it as ID
|
|
50
|
-
if (lower.endsWith('_id')) return 'id';
|
|
51
|
-
return 'numeric';
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (dt.includes('DATE') || dt.includes('TIME') || dt.includes('TIMESTAMP')) {
|
|
55
|
-
return 'date';
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// Date detection by name
|
|
59
|
-
if (lower.includes('date') || lower.includes('time') || lower.includes('year') || lower.includes('timestamp')) {
|
|
60
|
-
return 'date';
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// TEXT/VARCHAR/BLOB — infer from actual samples
|
|
64
|
-
if (dt.includes('TEXT') || dt.includes('VARCHAR') || dt.includes('CHAR') || dt.includes('CLOB') || dt === '' || dt === 'BLOB') {
|
|
65
|
-
// Numeric detection from samples
|
|
66
|
-
const numericSamples = samples.filter((s) => s !== '' && s !== null && !isNaN(Number(s)));
|
|
67
|
-
if (samples.length > 0 && numericSamples.length > samples.length * 0.7) {
|
|
68
|
-
return 'numeric';
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// Text detection: long values
|
|
72
|
-
const avgLen = samples.reduce((sum, s) => sum + (s?.length || 0), 0) / (samples.length || 1);
|
|
73
|
-
if (avgLen > 100 || (uniqueCount > totalRows * 0.9 && avgLen > 50)) {
|
|
74
|
-
return 'text';
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return 'categorical';
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Fallback: try numeric detection from samples
|
|
81
|
-
const numericSamples = samples.filter((s) => s !== '' && s !== null && !isNaN(Number(s)));
|
|
82
|
-
if (samples.length > 0 && numericSamples.length > samples.length * 0.7) {
|
|
83
|
-
return 'numeric';
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return 'categorical';
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Get all user tables from a SQLite database (excludes internal tables).
|
|
91
|
-
*/
|
|
92
|
-
function getUserTables(db: Database.Database): string[] {
|
|
93
|
-
const rows = db
|
|
94
|
-
.prepare(
|
|
95
|
-
`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name`
|
|
96
|
-
)
|
|
97
|
-
.all() as { name: string }[];
|
|
98
|
-
return rows.map((r) => r.name);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
class SqliteConnectedSource implements ConnectedSource {
|
|
102
|
-
id: string;
|
|
103
|
-
name: string;
|
|
104
|
-
readonly type: DataSourceType = 'sqlite';
|
|
105
|
-
private db: Database.Database;
|
|
106
|
-
private dbPath: string;
|
|
107
|
-
private cachedSchema: DataSchema | null = null;
|
|
108
|
-
|
|
109
|
-
constructor(id: string, name: string, db: Database.Database, dbPath: string) {
|
|
110
|
-
this.id = id;
|
|
111
|
-
this.name = name;
|
|
112
|
-
this.db = db;
|
|
113
|
-
this.dbPath = dbPath;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
async getSchema(): Promise<DataSchema> {
|
|
117
|
-
if (this.cachedSchema) return this.cachedSchema;
|
|
118
|
-
|
|
119
|
-
const tableNames = getUserTables(this.db);
|
|
120
|
-
const tables: DataTable[] = [];
|
|
121
|
-
const foreignKeys: ForeignKey[] = [];
|
|
122
|
-
|
|
123
|
-
for (const tableName of tableNames) {
|
|
124
|
-
// Get row count
|
|
125
|
-
const countRow = this.db
|
|
126
|
-
.prepare(`SELECT COUNT(*) as cnt FROM "${tableName}"`)
|
|
127
|
-
.get() as { cnt: number };
|
|
128
|
-
const rowCount = countRow.cnt;
|
|
129
|
-
|
|
130
|
-
// Get column info via PRAGMA
|
|
131
|
-
const pragmaRows = this.db
|
|
132
|
-
.prepare(`PRAGMA table_info("${tableName}")`)
|
|
133
|
-
.all() as { cid: number; name: string; type: string; notnull: number; dflt_value: any; pk: number }[];
|
|
134
|
-
|
|
135
|
-
const columns: DataColumn[] = [];
|
|
136
|
-
for (const pragma of pragmaRows) {
|
|
137
|
-
// Sample values
|
|
138
|
-
const sampleStmt = this.db.prepare(
|
|
139
|
-
`SELECT DISTINCT "${pragma.name}" FROM "${tableName}" WHERE "${pragma.name}" IS NOT NULL LIMIT ${SAMPLE_LIMIT}`
|
|
140
|
-
);
|
|
141
|
-
const samples = sampleStmt
|
|
142
|
-
.all()
|
|
143
|
-
.map((r: any) => String(r[pragma.name]));
|
|
144
|
-
|
|
145
|
-
// Stats
|
|
146
|
-
const statsStmt = this.db.prepare(
|
|
147
|
-
`SELECT COUNT(DISTINCT "${pragma.name}") as cnt, COUNT(*) - COUNT("${pragma.name}") as nulls, COUNT(*) as total FROM "${tableName}"`
|
|
148
|
-
);
|
|
149
|
-
const stats = statsStmt.get() as { cnt: number; nulls: number; total: number };
|
|
150
|
-
|
|
151
|
-
const type = mapSqliteType(
|
|
152
|
-
pragma.type,
|
|
153
|
-
pragma.name,
|
|
154
|
-
samples,
|
|
155
|
-
stats.cnt,
|
|
156
|
-
rowCount
|
|
157
|
-
);
|
|
158
|
-
|
|
159
|
-
// Rich profiling
|
|
160
|
-
let columnStats: DataColumn['stats'] = undefined;
|
|
161
|
-
let topValues: DataColumn['topValues'] = undefined;
|
|
162
|
-
|
|
163
|
-
if (type === 'numeric') {
|
|
164
|
-
try {
|
|
165
|
-
const numStmt = this.db.prepare(`
|
|
166
|
-
SELECT
|
|
167
|
-
MIN(CAST("${pragma.name}" AS REAL)) as min_val,
|
|
168
|
-
MAX(CAST("${pragma.name}" AS REAL)) as max_val,
|
|
169
|
-
AVG(CAST("${pragma.name}" AS REAL)) as mean_val
|
|
170
|
-
FROM "${tableName}"
|
|
171
|
-
WHERE "${pragma.name}" IS NOT NULL
|
|
172
|
-
`);
|
|
173
|
-
const numStats = numStmt.get() as any;
|
|
174
|
-
|
|
175
|
-
const sortedStmt = this.db.prepare(`
|
|
176
|
-
SELECT CAST("${pragma.name}" AS REAL) as val
|
|
177
|
-
FROM "${tableName}"
|
|
178
|
-
WHERE "${pragma.name}" IS NOT NULL
|
|
179
|
-
ORDER BY CAST("${pragma.name}" AS REAL)
|
|
180
|
-
`);
|
|
181
|
-
const sorted = sortedStmt.all().map((r: any) => r.val as number);
|
|
182
|
-
const n = sorted.length;
|
|
183
|
-
|
|
184
|
-
if (n > 0) {
|
|
185
|
-
const percentile = (arr: number[], p: number) => {
|
|
186
|
-
const idx = (p / 100) * (arr.length - 1);
|
|
187
|
-
const lo = Math.floor(idx);
|
|
188
|
-
const hi = Math.ceil(idx);
|
|
189
|
-
return lo === hi ? arr[lo] : arr[lo] + (arr[hi] - arr[lo]) * (idx - lo);
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
const mean = numStats.mean_val ?? 0;
|
|
193
|
-
const variance = sorted.reduce((sum: number, v: number) => sum + (v - mean) ** 2, 0) / n;
|
|
194
|
-
const stddev = Math.sqrt(variance);
|
|
195
|
-
|
|
196
|
-
columnStats = {
|
|
197
|
-
min: numStats.min_val,
|
|
198
|
-
max: numStats.max_val,
|
|
199
|
-
mean: numStats.mean_val,
|
|
200
|
-
median: percentile(sorted, 50),
|
|
201
|
-
stddev,
|
|
202
|
-
p25: percentile(sorted, 25),
|
|
203
|
-
p75: percentile(sorted, 75),
|
|
204
|
-
};
|
|
205
|
-
}
|
|
206
|
-
} catch {
|
|
207
|
-
// Profiling failure is non-fatal
|
|
208
|
-
}
|
|
209
|
-
} else if (type === 'categorical' || type === 'date') {
|
|
210
|
-
try {
|
|
211
|
-
const topStmt = this.db.prepare(`
|
|
212
|
-
SELECT "${pragma.name}" as value, COUNT(*) as count
|
|
213
|
-
FROM "${tableName}"
|
|
214
|
-
WHERE "${pragma.name}" IS NOT NULL
|
|
215
|
-
GROUP BY "${pragma.name}"
|
|
216
|
-
ORDER BY COUNT(*) DESC
|
|
217
|
-
LIMIT 10
|
|
218
|
-
`);
|
|
219
|
-
topValues = topStmt.all() as { value: string; count: number }[];
|
|
220
|
-
} catch {
|
|
221
|
-
// Profiling failure is non-fatal
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
columns.push({
|
|
226
|
-
name: pragma.name,
|
|
227
|
-
type,
|
|
228
|
-
sampleValues: samples.slice(0, SAMPLE_DISPLAY_LIMIT),
|
|
229
|
-
uniqueCount: stats.cnt,
|
|
230
|
-
nullCount: stats.nulls,
|
|
231
|
-
totalCount: stats.total,
|
|
232
|
-
stats: columnStats,
|
|
233
|
-
topValues,
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
tables.push({ name: tableName, columns, rowCount });
|
|
238
|
-
|
|
239
|
-
// Get foreign keys via PRAGMA
|
|
240
|
-
const fkRows = this.db
|
|
241
|
-
.prepare(`PRAGMA foreign_key_list("${tableName}")`)
|
|
242
|
-
.all() as { id: number; seq: number; table: string; from: string; to: string }[];
|
|
243
|
-
|
|
244
|
-
for (const fk of fkRows) {
|
|
245
|
-
foreignKeys.push({
|
|
246
|
-
fromTable: tableName,
|
|
247
|
-
fromColumn: fk.from,
|
|
248
|
-
toTable: fk.table,
|
|
249
|
-
toColumn: fk.to,
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
this.cachedSchema = {
|
|
255
|
-
tables,
|
|
256
|
-
foreignKeys,
|
|
257
|
-
source: {
|
|
258
|
-
id: this.id,
|
|
259
|
-
type: 'sqlite',
|
|
260
|
-
name: this.name,
|
|
261
|
-
config: { type: 'sqlite', path: this.dbPath },
|
|
262
|
-
},
|
|
263
|
-
};
|
|
264
|
-
|
|
265
|
-
return this.cachedSchema;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
async getSampleRows(tableName: string, count: number = 5): Promise<Record<string, any>[]> {
|
|
269
|
-
try {
|
|
270
|
-
const countRow = this.db.prepare(`SELECT COUNT(*) as cnt FROM "${tableName}"`).get() as { cnt: number };
|
|
271
|
-
const total = countRow.cnt;
|
|
272
|
-
if (total === 0) return [];
|
|
273
|
-
|
|
274
|
-
if (total <= count) {
|
|
275
|
-
return this.db.prepare(`SELECT * FROM "${tableName}"`).all() as Record<string, any>[];
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
const stmt = this.db.prepare(`
|
|
279
|
-
SELECT * FROM "${tableName}"
|
|
280
|
-
WHERE rowid IN (
|
|
281
|
-
SELECT rowid FROM (
|
|
282
|
-
SELECT rowid, NTILE(${count}) OVER (ORDER BY rowid) as bucket
|
|
283
|
-
FROM "${tableName}"
|
|
284
|
-
) GROUP BY bucket
|
|
285
|
-
)
|
|
286
|
-
LIMIT ${count}
|
|
287
|
-
`);
|
|
288
|
-
return stmt.all() as Record<string, any>[];
|
|
289
|
-
} catch {
|
|
290
|
-
return [];
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
async executeQuery(sql: string): Promise<QueryExecutionResult> {
|
|
295
|
-
try {
|
|
296
|
-
const stmt = this.db.prepare(sql);
|
|
297
|
-
const rows = stmt.all() as Record<string, any>[];
|
|
298
|
-
const columns = rows.length > 0 ? Object.keys(rows[0]) : [];
|
|
299
|
-
return { columns, rows };
|
|
300
|
-
} catch (err: any) {
|
|
301
|
-
return { columns: [], rows: [{ error: err.message }] };
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
async close(): Promise<void> {
|
|
306
|
-
try {
|
|
307
|
-
this.db.close();
|
|
308
|
-
} catch {
|
|
309
|
-
// Already closed, ignore
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
export const sqliteConnector: DataConnector = {
|
|
315
|
-
type: 'sqlite',
|
|
316
|
-
|
|
317
|
-
async test(config: DataSourceConfig): Promise<{ ok: boolean; error?: string }> {
|
|
318
|
-
const sqliteConfig = config as SqliteSourceConfig;
|
|
319
|
-
if (sqliteConfig.type !== 'sqlite') {
|
|
320
|
-
return { ok: false, error: 'Config type must be "sqlite"' };
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
try {
|
|
324
|
-
if (!fs.existsSync(sqliteConfig.path)) {
|
|
325
|
-
return { ok: false, error: `Database file not found: ${sqliteConfig.path}` };
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
const stat = fs.statSync(sqliteConfig.path);
|
|
329
|
-
if (!stat.isFile()) {
|
|
330
|
-
return { ok: false, error: `Path is not a file: ${sqliteConfig.path}` };
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
// Try opening read-only to verify it's a valid SQLite file
|
|
334
|
-
const db = new Database(sqliteConfig.path, { readonly: true });
|
|
335
|
-
const tables = getUserTables(db);
|
|
336
|
-
db.close();
|
|
337
|
-
|
|
338
|
-
if (tables.length === 0) {
|
|
339
|
-
return { ok: false, error: 'Database contains no tables' };
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
return { ok: true };
|
|
343
|
-
} catch (err: any) {
|
|
344
|
-
return { ok: false, error: `Cannot open SQLite database: ${err.message}` };
|
|
345
|
-
}
|
|
346
|
-
},
|
|
347
|
-
|
|
348
|
-
async connect(config: DataSourceConfig): Promise<ConnectedSource> {
|
|
349
|
-
const sqliteConfig = config as SqliteSourceConfig;
|
|
350
|
-
if (sqliteConfig.type !== 'sqlite') {
|
|
351
|
-
throw new Error('Config type must be "sqlite"');
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
if (!fs.existsSync(sqliteConfig.path)) {
|
|
355
|
-
throw new Error(`Database file not found: ${sqliteConfig.path}`);
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
const db = new Database(sqliteConfig.path, { readonly: true });
|
|
359
|
-
const sourceName = path.basename(sqliteConfig.path, path.extname(sqliteConfig.path));
|
|
360
|
-
const id = `sqlite-${sourceName}-${Date.now()}`;
|
|
361
|
-
|
|
362
|
-
return new SqliteConnectedSource(id, sourceName, db, sqliteConfig.path);
|
|
363
|
-
},
|
|
364
|
-
};
|