Skip to content

Commit 9f9cf9b

Browse files
committed
just tests missing now
1 parent 896bfb1 commit 9f9cf9b

File tree

8 files changed

+403
-230
lines changed

8 files changed

+403
-230
lines changed

crates/pgt_completions/src/context.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -100,25 +100,23 @@ impl<'a> CompletionContext<'a> {
100100
executor.add_query_results::<queries::RelationMatch>();
101101

102102
for relation_match in executor.get_iter(stmt_range) {
103-
match relation_match {
104-
QueryResult::Relation(r) => {
105-
let schema_name = r.get_schema(sql);
106-
let table_name = r.get_table(sql);
103+
if let QueryResult::Relation(r) = relation_match {
104+
let schema_name = r.get_schema(sql);
105+
let table_name = r.get_table(sql);
107106

108-
let current = self.mentioned_relations.get_mut(&schema_name);
107+
let current = self.mentioned_relations.get_mut(&schema_name);
109108

110-
match current {
111-
Some(c) => {
112-
c.insert(table_name);
113-
}
114-
None => {
115-
let mut new = HashSet::new();
116-
new.insert(table_name);
117-
self.mentioned_relations.insert(schema_name, new);
118-
}
119-
};
120-
}
121-
};
109+
match current {
110+
Some(c) => {
111+
c.insert(table_name);
112+
}
113+
None => {
114+
let mut new = HashSet::new();
115+
new.insert(table_name);
116+
self.mentioned_relations.insert(schema_name, new);
117+
}
118+
};
119+
}
122120
}
123121
}
124122

crates/pgt_schema_cache/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ pub use functions::{Behavior, Function, FunctionArg, FunctionArgs};
1515
pub use schema_cache::SchemaCache;
1616
pub use schemas::Schema;
1717
pub use tables::{ReplicaIdentity, Table};
18+
pub use types::{PostgresType, PostgresTypeAttribute};

crates/pgt_schema_cache/src/types.rs

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -56,58 +56,3 @@ impl SchemaCacheItem for PostgresType {
5656
.await
5757
}
5858
}
59-
60-
#[cfg(test)]
61-
mod tests {
62-
use pgt_test_utils::test_database::get_new_test_db;
63-
use sqlx::Executor;
64-
65-
use crate::{schema_cache::SchemaCacheItem, types::PostgresType};
66-
67-
#[tokio::test]
68-
async fn test_types() {
69-
let setup = r#"
70-
CREATE TYPE "public"."priority" AS ENUM (
71-
'critical',
72-
'high',
73-
'default',
74-
'low',
75-
'very_low'
76-
);
77-
78-
CREATE TYPE complex AS (
79-
r double precision,
80-
i double precision
81-
);
82-
"#;
83-
84-
let test_db = get_new_test_db().await;
85-
86-
test_db
87-
.execute(setup)
88-
.await
89-
.expect("Failed to setup test database");
90-
91-
let types = PostgresType::load(&test_db).await.unwrap();
92-
93-
let enum_type = types.iter().find(|t| t.name == "priority");
94-
let comp_type = types.iter().find(|t| t.name == "complex");
95-
96-
println!("{:?}", enum_type);
97-
// search for type id
98-
println!("{:?}", comp_type);
99-
100-
comp_type.and_then(|t| {
101-
t.attributes.attrs.iter().for_each(|a| {
102-
let typ = types.iter().find(|t| t.id == a.type_id);
103-
println!(
104-
"{}: {} - {:?}",
105-
a.name,
106-
a.type_id,
107-
typ.as_ref().map(|t| t.name.clone())
108-
);
109-
});
110-
Some(())
111-
});
112-
}
113-
}

crates/pgt_typecheck/src/diagnostics.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,26 @@ impl Advices for TypecheckAdvices {
9797
pub(crate) fn create_type_error(
9898
pg_err: &PgDatabaseError,
9999
ts: &tree_sitter::Tree,
100+
positions_valid: bool,
100101
) -> TypecheckDiagnostic {
101102
let position = pg_err.position().and_then(|pos| match pos {
102103
sqlx::postgres::PgErrorPosition::Original(pos) => Some(pos - 1),
103104
_ => None,
104105
});
105106

106107
let range = position.and_then(|pos| {
107-
ts.root_node()
108-
.named_descendant_for_byte_range(pos, pos)
109-
.map(|node| {
110-
TextRange::new(
111-
node.start_byte().try_into().unwrap(),
112-
node.end_byte().try_into().unwrap(),
113-
)
114-
})
108+
if positions_valid {
109+
ts.root_node()
110+
.named_descendant_for_byte_range(pos, pos)
111+
.map(|node| {
112+
TextRange::new(
113+
node.start_byte().try_into().unwrap(),
114+
node.end_byte().try_into().unwrap(),
115+
)
116+
})
117+
} else {
118+
None
119+
}
115120
});
116121

117122
let severity = match pg_err.severity() {

crates/pgt_typecheck/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use pgt_text_size::TextRange;
77
use sqlx::postgres::PgDatabaseError;
88
pub use sqlx::postgres::PgSeverity;
99
use sqlx::{Executor, PgPool};
10-
pub use typed_identifier::TypedIdentifier;
1110
use typed_identifier::apply_identifiers;
11+
pub use typed_identifier::{IdentifierType, TypedIdentifier};
1212

1313
#[derive(Debug)]
1414
pub struct TypecheckParams<'a> {
@@ -56,20 +56,24 @@ pub async fn check_sql(
5656
// each typecheck operation.
5757
conn.close_on_drop();
5858

59-
let prepared = apply_identifiers(
59+
let (prepared, positions_valid) = apply_identifiers(
6060
params.identifiers,
6161
params.schema_cache,
6262
params.tree,
6363
params.sql,
6464
);
6565

66-
let res = conn.prepare(prepared).await;
66+
let res = conn.prepare(&prepared).await;
6767

6868
match res {
6969
Ok(_) => Ok(None),
7070
Err(sqlx::Error::Database(err)) => {
7171
let pg_err = err.downcast_ref::<PgDatabaseError>();
72-
Ok(Some(create_type_error(pg_err, params.tree)))
72+
Ok(Some(create_type_error(
73+
pg_err,
74+
params.tree,
75+
positions_valid,
76+
)))
7377
}
7478
Err(err) => Err(err),
7579
}

0 commit comments

Comments
 (0)