ormdantic_dialects/
reflection.rs1#[derive(Debug, Clone, Default, PartialEq, Eq)]
2pub struct ReflectionScope {
3 schema: Option<String>,
4 tables: Vec<String>,
5}
6
7impl ReflectionScope {
8 pub fn new() -> Self {
9 Self::default()
10 }
11
12 pub fn schema(mut self, schema: impl Into<String>) -> Self {
13 self.schema = Some(schema.into());
14 self
15 }
16
17 pub fn tables(mut self, tables: Vec<String>) -> Self {
18 self.tables = tables;
19 self
20 }
21
22 pub fn schema_name(&self) -> Option<&str> {
23 self.schema.as_deref()
24 }
25
26 pub fn table_names(&self) -> &[String] {
27 &self.tables
28 }
29}
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub enum ReflectionQueryKind {
33 Tables,
34 Columns,
35 Constraints,
36 Indexes,
37 ForeignKeys,
38}
39
40#[derive(Debug, Clone, PartialEq, Eq)]
41pub struct ReflectionQuery {
42 kind: ReflectionQueryKind,
43 sql: String,
44}
45
46impl ReflectionQuery {
47 pub fn new(kind: ReflectionQueryKind, sql: impl Into<String>) -> Self {
48 Self {
49 kind,
50 sql: sql.into(),
51 }
52 }
53
54 pub fn kind(&self) -> ReflectionQueryKind {
55 self.kind
56 }
57
58 pub fn sql(&self) -> &str {
59 &self.sql
60 }
61}
62
63pub(crate) fn scope_predicate(scope: &ReflectionScope) -> String {
64 let mut predicates = Vec::new();
65 if let Some(schema) = scope.schema_name() {
66 predicates.push(format!("table_schema = '{}'", schema.replace('\'', "''")));
67 }
68 if !scope.table_names().is_empty() {
69 predicates.push(format!(
70 "table_name IN ({})",
71 scope
72 .table_names()
73 .iter()
74 .map(|table| format!("'{}'", table.replace('\'', "''")))
75 .collect::<Vec<_>>()
76 .join(", ")
77 ));
78 }
79 if predicates.is_empty() {
80 String::new()
81 } else {
82 format!(" WHERE {}", predicates.join(" AND "))
83 }
84}