devsql 0.2.2

Unified SQL queries across Claude Code + Git data
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#![cfg(feature = "tree-sitter-ast")]

use crate::Result;
use rusqlite::{params, Connection, Statement};
use std::path::Path;

use super::{detect_language, walk_source_files, LineIndex};
use super::tree_sitter::{import_query, TsLanguageKind, TsParser};
use streaming_iterator::StreamingIterator;
use tree_sitter::{Node, QueryCursor, Tree};

const MAX_FILE_SIZE: u64 = 1_048_576; // 1 MB

const CREATE_TABLE_SQL: &str = r#"
CREATE TABLE IF NOT EXISTS imports (
    file_path TEXT,
    line_number INTEGER,
    module TEXT,
    name TEXT,
    alias TEXT,
    kind TEXT,
    is_default INTEGER,
    is_wildcard INTEGER
)"#;

const INSERT_SQL: &str = r#"
INSERT INTO imports (
    file_path,
    line_number,
    module,
    name,
    alias,
    kind,
    is_default,
    is_wildcard
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)
"#;

pub fn load(conn: &Connection, repo_path: &Path) -> Result<()> {
    ensure_table(conn)?;

    let files = walk_source_files(repo_path);
    conn.execute_batch("BEGIN")?;
    let mut stmt = conn.prepare(INSERT_SQL)?;
    let mut parser = TsParser::new();

    for file in files {
        if file.size > MAX_FILE_SIZE {
            continue;
        }

        let language = detect_language(&file.extension);
        let Some(lang_kind) = TsLanguageKind::from_name(language) else {
            continue;
        };

        // Currently only TypeScript/JavaScript imports are implemented.
        if !matches!(
            lang_kind,
            TsLanguageKind::TypeScript | TsLanguageKind::Tsx | TsLanguageKind::JavaScript | TsLanguageKind::Jsx
        ) {
            continue;
        }

        let abs_path = repo_path.join(&file.path);
        let Ok(contents) = std::fs::read_to_string(&abs_path) else {
            continue;
        };

        let line_index = LineIndex::new(&contents);
        let Some(tree) = parser.parse(language, &contents) else {
            continue;
        };

        let rows = extract_imports_from_tree(
            lang_kind,
            &tree,
            &line_index,
            &contents,
            &file.path,
        );

        for row in rows {
            insert_row(&mut stmt, &row)?;
        }
    }

    drop(stmt);
    conn.execute_batch("COMMIT")?;
    create_indexes(conn)?;
    Ok(())
}

fn ensure_table(conn: &Connection) -> Result<()> {
    conn.execute_batch(CREATE_TABLE_SQL)?;
    Ok(())
}

fn create_indexes(conn: &Connection) -> Result<()> {
    conn.execute_batch(
        "CREATE INDEX IF NOT EXISTS idx_imports_module ON imports(module);
         CREATE INDEX IF NOT EXISTS idx_imports_name ON imports(name);",
    )?;
    Ok(())
}

struct ImportRow {
    file_path: String,
    line_number: i64,
    module: String,
    name: Option<String>,
    alias: Option<String>,
    kind: String,
    is_default: bool,
    is_wildcard: bool,
}

fn insert_row(stmt: &mut Statement<'_>, row: &ImportRow) -> Result<()> {
    stmt.execute(params![
        row.file_path,
        row.line_number,
        row.module,
        row.name.as_deref(),
        row.alias.as_deref(),
        row.kind,
        if row.is_default { 1 } else { 0 },
        if row.is_wildcard { 1 } else { 0 },
    ])?;
    Ok(())
}

fn extract_imports_from_tree(
    language: TsLanguageKind,
    tree: &Tree,
    lines: &LineIndex<'_>,
    source: &str,
    file_path: &str,
) -> Vec<ImportRow> {
    let mut rows = Vec::new();

    match language {
        TsLanguageKind::TypeScript | TsLanguageKind::Tsx => {
            rows.extend(extract_ts_like_imports(
                language,
                tree,
                lines,
                source,
                file_path,
            ));
        }
        TsLanguageKind::JavaScript | TsLanguageKind::Jsx => {
            rows.extend(extract_ts_like_imports(
                language,
                tree,
                lines,
                source,
                file_path,
            ));
        }
        _ => {}
    }

    rows
}

fn extract_ts_like_imports(
    language: TsLanguageKind,
    tree: &Tree,
    lines: &LineIndex<'_>,
    source: &str,
    file_path: &str,
) -> Vec<ImportRow> {
    let Some(query) = import_query(language) else {
        return Vec::new();
    };

    let mut rows = Vec::new();
    let mut cursor = QueryCursor::new();
    let mut matches = cursor.matches(query, tree.root_node(), source.as_bytes());

    while {
        matches.advance();
        matches.get().is_some()
    } {
        let matched = matches.get().unwrap();
        for capture in matched.captures {
            let node = capture.node;
            let capture_name = &query.capture_names()[capture.index as usize];
            match node.kind() {
                "import_statement" if capture_name.starts_with("import") => {
                    parse_ts_import_statement(node, source, lines, file_path, &mut rows);
                }
                "export_statement" if capture_name.starts_with("export") => {
                    parse_ts_export_statement(node, source, lines, file_path, &mut rows);
                }
                _ => {}
            }
        }
    }

    rows
}

fn parse_ts_import_statement(
    node: Node,
    source: &str,
    lines: &LineIndex<'_>,
    file_path: &str,
    rows: &mut Vec<ImportRow>,
) {
    let line_number = lines.line_for_byte(node.start_byte()) as i64;
    let module = node
        .child_by_field_name("source")
        .map(|n| string_literal_value(n, source))
        .unwrap_or_default();

    let mut entries = 0usize;
    for child in named_children(node) {
        match child.kind() {
            "import_clause" => {
                entries += parse_ts_import_clause(
                    child,
                    source,
                    file_path,
                    line_number,
                    &module,
                    rows,
                );
            }
            _ => {}
        }
    }

    if entries == 0 {
        rows.push(ImportRow {
            file_path: file_path.to_string(),
            line_number,
            module: module.clone(),
            name: None,
            alias: None,
            kind: "import".to_string(),
            is_default: false,
            is_wildcard: false,
        });
    }
}

fn parse_ts_import_clause(
    clause: Node,
    source: &str,
    file_path: &str,
    line_number: i64,
    module: &str,
    rows: &mut Vec<ImportRow>,
) -> usize {
    let mut added = 0usize;

    for child in named_children(clause) {
        match child.kind() {
            "identifier" => {
                added += 1;
                rows.push(ImportRow {
                    file_path: file_path.to_string(),
                    line_number,
                    module: module.to_string(),
                    name: None,
                    alias: Some(node_text(child, source)),
                    kind: "import".to_string(),
                    is_default: true,
                    is_wildcard: false,
                });
            }
            "named_imports" => {
                for spec in named_children(child).into_iter().filter(|n| n.kind() == "import_specifier") {
                    let spec_name = spec
                        .child_by_field_name("name")
                        .map(|n| clean_identifier(node_text(n, source)));
                    if spec_name.is_none() {
                        continue;
                    }
                    let alias = spec
                        .child_by_field_name("alias")
                        .map(|n| clean_identifier(node_text(n, source)));

                    added += 1;
                    rows.push(ImportRow {
                        file_path: file_path.to_string(),
                        line_number,
                        module: module.to_string(),
                        name: spec_name,
                        alias,
                        kind: "import".to_string(),
                        is_default: false,
                        is_wildcard: false,
                    });
                }
            }
            "namespace_import" => {
                if let Some(alias_node) = named_children(child)
                    .into_iter()
                    .find(|n| n.kind() == "identifier")
                {
                    added += 1;
                    rows.push(ImportRow {
                        file_path: file_path.to_string(),
                        line_number,
                        module: module.to_string(),
                        name: None,
                        alias: Some(node_text(alias_node, source)),
                        kind: "import".to_string(),
                        is_default: false,
                        is_wildcard: true,
                    });
                }
            }
            _ => {}
        }
    }

    added
}

fn parse_ts_export_statement(
    node: Node,
    source: &str,
    lines: &LineIndex<'_>,
    file_path: &str,
    rows: &mut Vec<ImportRow>,
) {
    let Some(module_node) = node.child_by_field_name("source") else {
        return;
    };
    let module = string_literal_value(module_node, source);
    if module.is_empty() {
        return;
    }

    let line_number = lines.line_for_byte(node.start_byte()) as i64;
    let mut added = 0usize;

    for child in named_children(node) {
        match child.kind() {
            "export_clause" => {
                for spec in named_children(child).into_iter().filter(|n| n.kind() == "export_specifier") {
                    let export_name = spec
                        .child_by_field_name("name")
                        .map(|n| clean_identifier(node_text(n, source)));
                    if export_name.is_none() {
                        continue;
                    }
                    let alias = spec
                        .child_by_field_name("alias")
                        .map(|n| clean_identifier(node_text(n, source)));

                    added += 1;
                    rows.push(ImportRow {
                        file_path: file_path.to_string(),
                        line_number,
                        module: module.clone(),
                        name: export_name,
                        alias,
                        kind: "export".to_string(),
                        is_default: false,
                        is_wildcard: false,
                    });
                }
            }
            "namespace_export" => {
                if let Some(alias_node) = named_children(child)
                    .into_iter()
                    .find(|n| n.kind() == "identifier" || n.kind() == "string")
                {
                    added += 1;
                    rows.push(ImportRow {
                        file_path: file_path.to_string(),
                        line_number,
                        module: module.clone(),
                        name: Some("*".to_string()),
                        alias: Some(clean_identifier(node_text(alias_node, source))),
                        kind: "export".to_string(),
                        is_default: false,
                        is_wildcard: true,
                    });
                }
            }
            _ => {}
        }
    }

    if added == 0 {
        rows.push(ImportRow {
            file_path: file_path.to_string(),
            line_number,
            module: module.clone(),
            name: Some("*".to_string()),
            alias: None,
            kind: "export".to_string(),
            is_default: false,
            is_wildcard: true,
        });
    }
}

fn named_children(node: Node) -> Vec<Node> {
    let mut cursor = node.walk();
    let mut children = Vec::new();
    if cursor.goto_first_child() {
        loop {
            let child = cursor.node();
            if child.is_named() {
                children.push(child);
            }
            if !cursor.goto_next_sibling() {
                break;
            }
        }
    }
    children
}

fn string_literal_value(node: Node, source: &str) -> String {
    clean_identifier(node_text(node, source))
}

fn clean_identifier(text: String) -> String {
    let trimmed = text.trim();
    if (trimmed.starts_with('"') && trimmed.ends_with('"'))
        || (trimmed.starts_with('\'') && trimmed.ends_with('\''))
        || (trimmed.starts_with('`') && trimmed.ends_with('`'))
    {
        trimmed[1..trimmed.len().saturating_sub(1)].to_string()
    } else {
        trimmed.to_string()
    }
}

fn node_text(node: Node, source: &str) -> String {
    let start = node.start_byte();
    let end = node.end_byte();
    source[start..end].trim().to_string()
}