Skip to main content

squawk_syntax/ast/
nodes.rs

1pub use crate::ast::generated::nodes::*;
2use crate::{
3    SyntaxNode,
4    ast::{self, AstNode, support},
5};
6
7impl ast::AliasColumnList {
8    pub fn column_names(self) -> impl Iterator<Item = ast::ColumnName> {
9        self.alias_columns().filter_map(|column| column.name())
10    }
11}
12
13// TODO: Initial attempt to try and unify the CreateTable and
14// CreateForeignTable. Not sure this is the right approach, we may want to be
15// more general, like TableSource, which can be a View, CTE, Table,
16// ForeignTable, Subquery, etc.
17#[derive(Debug, Clone, PartialEq, Eq, Hash)]
18pub struct CreateTableLike {
19    pub(crate) syntax: SyntaxNode,
20}
21impl CreateTableLike {
22    #[inline]
23    pub fn table_name(&self) -> Option<ast::TableName> {
24        support::child(&self.syntax)
25    }
26    #[inline]
27    pub fn table_arg_list(&self) -> Option<ast::TableArgList> {
28        support::child(&self.syntax)
29    }
30    #[inline]
31    pub fn inherits(&self) -> Option<ast::Inherits> {
32        support::child(&self.syntax)
33    }
34    #[inline]
35    pub fn partition_of(&self) -> Option<ast::PartitionOf> {
36        support::child(&self.syntax)
37    }
38}
39impl AstNode for CreateTableLike {
40    #[inline]
41    fn can_cast(kind: ast::SyntaxKind) -> bool {
42        matches!(
43            kind,
44            ast::SyntaxKind::CREATE_TABLE | ast::SyntaxKind::CREATE_FOREIGN_TABLE
45        )
46    }
47    #[inline]
48    fn cast(syntax: SyntaxNode) -> Option<Self> {
49        if Self::can_cast(syntax.kind()) {
50            Some(Self { syntax })
51        } else {
52            None
53        }
54    }
55    #[inline]
56    fn syntax(&self) -> &SyntaxNode {
57        &self.syntax
58    }
59}
60
61#[derive(Debug, Clone, PartialEq, Eq, Hash)]
62pub struct CreateViewLike {
63    pub(crate) syntax: SyntaxNode,
64}
65impl CreateViewLike {
66    #[inline]
67    pub fn column_list(&self) -> Option<ast::ColumnList> {
68        support::child(&self.syntax)
69    }
70    #[inline]
71    pub fn view(&self) -> Option<ast::View> {
72        support::child(&self.syntax)
73    }
74    #[inline]
75    pub fn query(&self) -> Option<ast::SelectVariant> {
76        support::child(&self.syntax)
77    }
78}
79impl AstNode for CreateViewLike {
80    #[inline]
81    fn can_cast(kind: ast::SyntaxKind) -> bool {
82        matches!(
83            kind,
84            ast::SyntaxKind::CREATE_MATERIALIZED_VIEW | ast::SyntaxKind::CREATE_VIEW
85        )
86    }
87    #[inline]
88    fn cast(syntax: SyntaxNode) -> Option<Self> {
89        if Self::can_cast(syntax.kind()) {
90            Some(Self { syntax })
91        } else {
92            None
93        }
94    }
95    #[inline]
96    fn syntax(&self) -> &SyntaxNode {
97        &self.syntax
98    }
99}