codesynapse_core/ts_extract/
sql.rs1use super::{add_contains_edge, add_node_if_missing, make_file_node};
2use crate::error::Result;
3use crate::extract::{make_id, ImportNode, LanguageExtractor};
4use crate::types::{Edge, ExtractionFragment, Node};
5use std::collections::HashMap;
6use std::path::Path;
7
8pub struct TsSqlExtractor;
9
10impl TsSqlExtractor {
11 pub fn extract(source: &[u8], path: &Path) -> Result<ExtractionFragment> {
12 let (file_id, _, file_node) = make_file_node(path);
13 let mut fragment = ExtractionFragment {
14 nodes: vec![file_node],
15 edges: vec![],
16 };
17
18 let content = std::str::from_utf8(source).unwrap_or("");
19 for line in content.lines() {
20 let upper = line.to_uppercase();
21 let trimmed = line.trim();
22
23 if upper.contains("CREATE TABLE") {
24 let after = trimmed
25 .strip_prefix("CREATE TABLE")
26 .or_else(|| trimmed.strip_prefix("create table"))
27 .or_else(|| trimmed.strip_prefix("create table"))
28 .unwrap_or("")
29 .trim();
30 let table_name = after
31 .split_whitespace()
32 .next()
33 .unwrap_or("")
34 .trim_matches('(');
35 let table_id = make_id(&[&file_id, table_name]);
36 fragment.nodes.push(Node {
37 id: table_id.clone(),
38 label: table_name.to_string(),
39 file_type: "data".to_string(),
40 source_file: path.to_string_lossy().to_string(),
41 source_location: None,
42 community: None,
43 rationale: None,
44 docstring: None,
45 metadata: {
46 let mut m = HashMap::new();
47 m.insert("kind".to_string(), "table".to_string());
48 m
49 },
50 });
51 add_contains_edge(&mut fragment, &file_id, table_id, path);
52 }
53
54 if upper.contains("FOREIGN KEY") && upper.contains("REFERENCES") {
56 if let Some(ref_part) = trimmed.to_lowercase().split("references").nth(1) {
57 let target = ref_part
58 .split_whitespace()
59 .next()
60 .unwrap_or("")
61 .trim_matches('(');
62 if !target.is_empty() {
63 let ref_id = make_id(&[&file_id, target]);
64 let ref_node = Node {
65 id: ref_id.clone(),
66 label: target.to_string(),
67 file_type: "data".to_string(),
68 source_file: path.to_string_lossy().to_string(),
69 source_location: None,
70 community: None,
71 rationale: None,
72 docstring: None,
73 metadata: HashMap::new(),
74 };
75 add_node_if_missing(&mut fragment, ref_node);
76 if !fragment
77 .edges
78 .iter()
79 .any(|e| e.target == ref_id && e.relation == "references")
80 {
81 fragment.edges.push(Edge {
82 source: file_id.clone(),
83 target: ref_id.clone(),
84 relation: "references".to_string(),
85 confidence: "EXTRACTED".to_string(),
86 source_file: Some(path.to_string_lossy().to_string()),
87 weight: 1.0,
88 context: None,
89 });
90 }
91 }
92 }
93 }
94 }
95
96 Ok(fragment)
97 }
98}
99
100impl LanguageExtractor for TsSqlExtractor {
102 fn file_extensions(&self) -> Vec<&'static str> {
103 vec!["sql"]
104 }
105 fn extract(&self, source: &[u8], path: &Path) -> Result<ExtractionFragment> {
106 Self::extract(source, path)
107 }
108 fn resolve_imports(&self, _imports: &[ImportNode]) -> Vec<Edge> {
109 vec![]
110 }
111 fn collect_type_refs(&self, _fragment: &mut ExtractionFragment) {}
112}