Skip to main content

no_mistakes_postgres_engine/embedded/
projection.rs

1use super::*;
2
3pub fn extract_embedded_sql_from_program_with_projections(
4    path: &Path,
5    program: &Program<'_>,
6    source: &str,
7    options: &EmbeddedSqlOptions,
8    demand: (bool, bool),
9) -> EmbeddedSqlFileFacts {
10    let mut scoped = scoped_bindings::collect(program, options);
11    extract_with_scopes(path, program, source, options, demand, &mut scoped)
12}
13
14pub fn extract_with_scopes(
15    path: &Path,
16    program: &Program<'_>,
17    source: &str,
18    options: &EmbeddedSqlOptions,
19    (head_calls_only, exact_sql_origins): (bool, bool),
20    scoped: &mut scoped_bindings::ScopedCollection,
21) -> EmbeddedSqlFileFacts {
22    extract_with_scope_demand(
23        path,
24        program,
25        source,
26        options,
27        SqlProjectionDemand {
28            head_calls_only,
29            exact_sql_origins,
30            ratchet_member_sql: false,
31        },
32        scoped,
33    )
34}
35
36#[doc(hidden)]
37#[derive(Clone, Copy, Default)]
38pub struct SqlProjectionDemand {
39    pub head_calls_only: bool,
40    pub exact_sql_origins: bool,
41    pub ratchet_member_sql: bool,
42}
43
44#[doc(hidden)]
45pub fn extract_with_scope_demand(
46    path: &Path,
47    program: &Program<'_>,
48    source: &str,
49    options: &EmbeddedSqlOptions,
50    demand: SqlProjectionDemand,
51    scoped: &mut scoped_bindings::ScopedCollection,
52) -> EmbeddedSqlFileFacts {
53    record_embedded_collection!();
54    let bindings = executor_bindings(program, options);
55    let mut executor_bindings: Vec<String> = bindings
56        .iter()
57        .filter(|binding| binding.as_str() != self::bindings::MEMBER_QUERY_OPT_IN)
58        .cloned()
59        .collect();
60    executor_bindings.sort();
61    let mut collected = walk::collect_calls(
62        program,
63        source,
64        &bindings,
65        &scoped.executors,
66        &scoped.spans,
67        options,
68        demand,
69    );
70    #[cfg(feature = "test-instrumentation")]
71    let _ = collected.lock_summary_passes;
72    scoped.prune_unused_diagnostic_candidates(&collected.undeclared_executor_types);
73    let mut fragments = collected.fragments;
74    for fragment in &mut fragments {
75        if let Some(sql_text) = fragment.sql_text.take() {
76            let (sql_text, positions) = placeholders::publish_placeholders_with_positions(sql_text);
77            fragment.sql_text = Some(sql_text);
78            fragment.recovered_placeholder_positions = positions;
79        }
80    }
81    for fragments in collected.call_fragments.values_mut() {
82        walk::call_fragments::publish_call_fragments(fragments);
83    }
84    walk::call_fragments::publish_call_fragments(&mut collected.param_append_fragments);
85    walk::call_fragments::publish_call_fragments(&mut collected.runtime_loop_appends);
86    let (matched_factory_names, matched_type_names) =
87        scoped_bindings::matched_names(program, options);
88    EmbeddedSqlFileFacts {
89        lock_helper_summaries: collected.lock_helper_summaries,
90        imported_lock_helpers: collected.imported_lock_helpers,
91        transaction_callbacks: collected.transaction_callbacks,
92        matched_factory_names,
93        matched_type_names,
94        path: path.to_path_buf(),
95        executor_bindings,
96        calls: collected.calls,
97        undeclared_executor_types: collected.undeclared_executor_types,
98        call_spans: collected.call_spans,
99        exported_helpers: collected.exported_helpers,
100        exported_programs: collected.exported_programs,
101        exported_constants: collected.exported_constants,
102        pending_imported: collected.pending_imported,
103        imported_call_lines: Default::default(),
104        source_comment_spans: program
105            .comments
106            .iter()
107            .map(|comment| (comment.span.start, comment.span.end))
108            .collect(),
109        fragments,
110        fragment_variants: collected.fragment_variants,
111        fragment_sites: collected.fragment_sites,
112        call_fragments: collected.call_fragments,
113        runtime_loop_appends: collected.runtime_loop_appends,
114        param_append_fragments: collected.param_append_fragments,
115        pending_relative: PendingRelativeScope {
116            candidates: std::mem::take(&mut scoped.candidates),
117            spans: std::mem::take(&mut scoped.spans),
118            calls: std::mem::take(&mut collected.pending_calls),
119            diagnostic_member_queries: collected.diagnostic_member_queries,
120            confirmed_order: std::mem::take(&mut collected.confirmed_order),
121            call_spans: collected.pending_spans,
122        },
123        unproven_member_queries: collected.unproven_member_queries,
124        postgres_rule_ids: Vec::new(),
125        record_unproven_spans: true,
126        ratchet_unproven: demand.ratchet_member_sql.then(Vec::new),
127    }
128}