use dsl_parse::TableRef;
use dsl_resolve::{Binding, Scope};
use text_size::TextRange;
pub fn cte_names_from_text(src: &str) -> Vec<String> {
let bytes = src.as_bytes();
let n = bytes.len();
let mut out = Vec::new();
let mut i = 0;
loop {
while i < n && bytes[i].is_ascii_whitespace() {
i += 1;
}
if i + 1 < n && bytes[i] == b'-' && bytes[i + 1] == b'-' {
while i < n && bytes[i] != b'\n' {
i += 1;
}
continue;
}
if i + 1 < n && bytes[i] == b'/' && bytes[i + 1] == b'*' {
i += 2;
while i + 1 < n && !(bytes[i] == b'*' && bytes[i + 1] == b'/') {
i += 1;
}
i = (i + 2).min(n);
continue;
}
break;
}
if i + 4 > n || !src[i..i + 4].eq_ignore_ascii_case("WITH") {
return out;
}
i += 4;
while i < n && bytes[i].is_ascii_whitespace() {
i += 1;
}
if i + 9 <= n && src[i..i + 9].eq_ignore_ascii_case("RECURSIVE") {
i += 9;
}
loop {
while i < n && bytes[i].is_ascii_whitespace() {
i += 1;
}
let name_start = i;
let name: String;
if i < n && bytes[i] == b'"' {
i += 1;
let inner = i;
while i < n && bytes[i] != b'"' {
i += 1;
}
if i >= n {
return out;
}
name = src[inner..i].to_string();
i += 1;
} else {
while i < n && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_') {
i += 1;
}
if i == name_start {
return out;
}
name = src[name_start..i].to_string();
}
while i < n && bytes[i].is_ascii_whitespace() {
i += 1;
}
if i < n && bytes[i] == b'(' {
let mut depth = 1i32;
i += 1;
while i < n && depth > 0 {
match bytes[i] {
b'(' => depth += 1,
b')' => depth -= 1,
_ => {},
}
i += 1;
}
while i < n && bytes[i].is_ascii_whitespace() {
i += 1;
}
}
if i + 2 > n || !src[i..i + 2].eq_ignore_ascii_case("AS") {
out.push(name);
return out;
}
i += 2;
while i < n && bytes[i].is_ascii_whitespace() {
i += 1;
}
for kw in ["NOT MATERIALIZED", "MATERIALIZED"] {
if i + kw.len() <= n && src[i..i + kw.len()].eq_ignore_ascii_case(kw) {
i += kw.len();
while i < n && bytes[i].is_ascii_whitespace() {
i += 1;
}
break;
}
}
out.push(name);
if i >= n || bytes[i] != b'(' {
return out;
}
let mut depth = 1i32;
i += 1;
while i < n && depth > 0 {
match bytes[i] {
b'(' => depth += 1,
b')' => depth -= 1,
b'\'' => {
i += 1;
while i < n && bytes[i] != b'\'' {
i += 1;
}
},
_ => {},
}
if i < n {
i += 1;
}
}
while i < n && bytes[i].is_ascii_whitespace() {
i += 1;
}
if i < n && bytes[i] == b',' {
i += 1;
continue;
}
return out;
}
}
pub fn cte_columns_from_text(src: &str, target: &str) -> Option<Vec<String>> {
let bytes = src.as_bytes();
let n = bytes.len();
let mut i = 0;
loop {
while i < n && bytes[i].is_ascii_whitespace() {
i += 1;
}
if i + 1 < n && bytes[i] == b'-' && bytes[i + 1] == b'-' {
while i < n && bytes[i] != b'\n' {
i += 1;
}
continue;
}
if i + 1 < n && bytes[i] == b'/' && bytes[i + 1] == b'*' {
i += 2;
while i + 1 < n && !(bytes[i] == b'*' && bytes[i + 1] == b'/') {
i += 1;
}
i = (i + 2).min(n);
continue;
}
break;
}
if i + 4 > n || !src[i..i + 4].eq_ignore_ascii_case("WITH") {
return None;
}
i += 4;
while i < n && bytes[i].is_ascii_whitespace() {
i += 1;
}
if i + 9 <= n && src[i..i + 9].eq_ignore_ascii_case("RECURSIVE") {
i += 9;
}
loop {
while i < n && bytes[i].is_ascii_whitespace() {
i += 1;
}
let name_start = i;
let name: String;
if i < n && bytes[i] == b'"' {
i += 1;
let inner = i;
while i < n && bytes[i] != b'"' {
i += 1;
}
if i >= n {
return None;
}
name = src[inner..i].to_string();
i += 1;
} else {
while i < n && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_') {
i += 1;
}
if i == name_start {
return None;
}
name = src[name_start..i].to_string();
}
while i < n && bytes[i].is_ascii_whitespace() {
i += 1;
}
let mut explicit_cols: Option<Vec<String>> = None;
if i < n && bytes[i] == b'(' {
let start = i + 1;
let mut depth = 1i32;
i += 1;
while i < n && depth > 0 {
match bytes[i] {
b'(' => depth += 1,
b')' => depth -= 1,
_ => {},
}
i += 1;
}
let end = (i - 1).min(n);
explicit_cols = Some(
src[start..end].split(',').map(|s| s.trim().trim_matches('"').to_string()).filter(|s| !s.is_empty()).collect(),
);
while i < n && bytes[i].is_ascii_whitespace() {
i += 1;
}
}
if i + 2 > n || !src[i..i + 2].eq_ignore_ascii_case("AS") {
return if name.eq_ignore_ascii_case(target) { Some(explicit_cols.unwrap_or_default()) } else { None };
}
i += 2;
while i < n && bytes[i].is_ascii_whitespace() {
i += 1;
}
for kw in ["NOT MATERIALIZED", "MATERIALIZED"] {
if i + kw.len() <= n && src[i..i + kw.len()].eq_ignore_ascii_case(kw) {
i += kw.len();
while i < n && bytes[i].is_ascii_whitespace() {
i += 1;
}
break;
}
}
if i >= n || bytes[i] != b'(' {
return if name.eq_ignore_ascii_case(target) { Some(explicit_cols.unwrap_or_default()) } else { None };
}
let body_start = i + 1;
let mut depth = 1i32;
i += 1;
while i < n && depth > 0 {
match bytes[i] {
b'(' => depth += 1,
b')' => depth -= 1,
b'\'' => {
i += 1;
while i < n && bytes[i] != b'\'' {
i += 1;
}
},
_ => {},
}
if i < n {
i += 1;
}
}
let body_end = (i - 1).min(n);
if name.eq_ignore_ascii_case(target) {
if let Some(cols) = explicit_cols {
return Some(cols);
}
let body = &src[body_start..body_end];
return Some(projection_columns_of(body));
}
while i < n && bytes[i].is_ascii_whitespace() {
i += 1;
}
if i < n && bytes[i] == b',' {
i += 1;
continue;
}
return None;
}
}
fn projection_columns_of(body: &str) -> Vec<String> {
let file = dsl_parse::parse(body, dsl_parse::Dialect::Postgres);
for stmt in &file.statements {
if let dsl_parse::StatementKind::Select(s) = &stmt.kind {
let mut out = Vec::new();
for p in &s.projections {
if let dsl_parse::Projection::Expr { expr, alias } = p {
if let Some(a) = alias {
out.push(a.clone());
} else if let dsl_parse::Expr::Column { name, .. } = expr {
out.push(name.clone());
}
}
}
return out;
}
}
Vec::new()
}
pub fn scope_from_text(src: &str) -> Option<Scope> {
let mut scope = Scope::default();
for (schema, table, alias) in iter_table_bindings(src) {
let table_ref =
TableRef { schema: schema.clone(), name: table.clone(), alias: Some(alias.clone()), range: TextRange::default() };
let binding = Binding { alias: alias.clone(), table: table_ref.clone() };
scope.bindings.insert(alias, binding.clone());
scope.bindings.entry(table).or_insert(Binding { alias: table_ref.name.clone(), table: table_ref });
}
if scope.is_empty() { None } else { Some(scope) }
}
fn iter_table_bindings(src: &str) -> Vec<(Option<String>, String, String)> {
let mut out = Vec::new();
let chars: Vec<char> = src.chars().collect();
let mut i = 0;
while i < chars.len() {
if let Some(consumed) = match_keyword(&chars, i, &["FROM", "JOIN", "UPDATE", "INTO", "USING"]) {
i += consumed;
loop {
if let Some((schema, table, alias, consumed2)) = read_table_with_alias(&chars, i) {
out.push((schema, table, alias));
i += consumed2;
let ws = skip_ws(&chars, i);
if chars.get(i + ws) == Some(&',') {
i += ws + 1;
continue;
}
}
break;
}
continue;
}
i += 1;
}
out
}
fn match_keyword(chars: &[char], pos: usize, keywords: &[&str]) -> Option<usize> {
if pos > 0 && is_word(chars[pos - 1]) {
return None;
}
for &kw in keywords {
if matches_ci(chars, pos, kw) && chars.get(pos + kw.len()).is_none_or(|c| !is_word(*c)) {
return Some(kw.len());
}
}
None
}
fn matches_ci(chars: &[char], pos: usize, target: &str) -> bool {
let tchars: Vec<char> = target.chars().collect();
if chars.len() < pos + tchars.len() {
return false;
}
for (i, t) in tchars.iter().enumerate() {
if !chars[pos + i].eq_ignore_ascii_case(t) {
return false;
}
}
true
}
fn is_word(c: char) -> bool {
c.is_alphanumeric() || c == '_'
}
fn read_table_with_alias(chars: &[char], pos: usize) -> Option<(Option<String>, String, String, usize)> {
let mut i = pos;
let ws = skip_ws(chars, i);
i += ws;
let (first, f_len) = read_ident(chars, i)?;
if first.is_empty() {
return None;
}
i += f_len;
let mut schema: Option<String> = None;
let mut table = first;
if chars.get(i) == Some(&'.')
&& let Some((after, a_len)) = read_ident(chars, i + 1)
&& !after.is_empty()
{
schema = Some(table);
table = after;
i += 1 + a_len;
}
let mut alias = table.clone();
let after_table = i;
i += skip_ws(chars, i);
if matches_ci(chars, i, "AS") && chars.get(i + 2).is_none_or(|c| !is_word(*c)) {
i += 2;
i += skip_ws(chars, i);
if let Some((ident, len)) = read_ident(chars, i) {
alias = ident;
i += len;
}
} else if let Some((ident, len)) = read_ident(chars, i) {
let upper = ident.to_uppercase();
const STOPWORDS: &[&str] = &[
"ON",
"WHERE",
"JOIN",
"INNER",
"LEFT",
"RIGHT",
"FULL",
"OUTER",
"CROSS",
"LATERAL",
"USING",
"GROUP",
"ORDER",
"LIMIT",
"OFFSET",
"HAVING",
"UNION",
"INTERSECT",
"EXCEPT",
"AS",
"SET",
"VALUES",
"RETURNING",
"DEFAULT",
"FROM",
"INTO",
"WITH",
"TABLESAMPLE",
"FETCH",
"FOR",
"WINDOW",
"NATURAL",
];
if !STOPWORDS.contains(&upper.as_str()) {
alias = ident;
i += len;
} else {
i = after_table;
}
} else {
i = after_table;
}
Some((schema, table, alias, i - pos))
}
fn skip_ws(chars: &[char], pos: usize) -> usize {
let mut n = 0;
while pos + n < chars.len() && chars[pos + n].is_whitespace() {
n += 1;
}
n
}
fn read_ident(chars: &[char], pos: usize) -> Option<(String, usize)> {
if chars.get(pos) == Some(&'"') {
let mut n = 1;
while pos + n < chars.len() && chars[pos + n] != '"' {
n += 1;
}
if pos + n >= chars.len() {
return None;
}
let inner: String = chars[pos + 1..pos + n].iter().collect();
if inner.is_empty() {
return None;
}
return Some((inner, n + 1));
}
let mut n = 0;
while pos + n < chars.len() && is_word(chars[pos + n]) {
n += 1;
}
if n == 0 {
return None;
}
Some((chars[pos..pos + n].iter().collect(), n))
}