@@ -15,7 +15,7 @@ public enum Cardinality: String {
1515/// This allows the generation to be smart and set the
1616/// return type to an Array only if needed.
1717struct CardinalityInferrer {
18- let schema : Schema
18+ let env : Environment
1919
2020 mutating func cardinality< S: StmtSyntax > ( for syntax: borrowing S ) -> Cardinality {
2121 return syntax. accept ( visitor: & self )
@@ -31,13 +31,6 @@ struct CardinalityInferrer {
3131 let didFilterByPrimaryKey = !table. primaryKey. contains { !filteredPrimaryKeys. contains ( $0) }
3232 return didFilterByPrimaryKey ? . single : . many
3333 }
34-
35- private func qualifiedName(
36- for name: IdentifierSyntax ,
37- in schema: IdentifierSyntax ?
38- ) -> QualifiedName {
39- return QualifiedName ( name: name. value, schema: schema? . value)
40- }
4134}
4235
4336/// Returns `true` if the query/statement will only return one value.
@@ -56,9 +49,10 @@ extension CardinalityInferrer: StmtSyntaxVisitor {
5649 // No filtering, update is to full table
5750 guard let whereExpr = stmt. whereExpr else { return . many }
5851
59- let tableName = qualifiedName ( for: stmt. tableName. tableName. name, in: stmt. tableName. tableName. schema)
60-
61- guard let table = schema [ tableName] else {
52+ guard let table = env. resolve (
53+ table: stmt. tableName. tableName. name. value,
54+ schema: stmt. tableName. tableName. schema? . value
55+ ) . value else {
6256 // Upstream will have emitted diag
6357 return . many
6458 }
@@ -95,11 +89,12 @@ extension CardinalityInferrer: StmtSyntaxVisitor {
9589 // If its not against a table we cannot infer it.
9690 guard case let . table( table) = join. tableOrSubquery. kind else { return . many }
9791
98- let tableName = qualifiedName ( for: table. name, in: table. schema)
99-
10092 // If we cannot find the table something upstream will have already emitted
10193 // diagnositic so just exit
102- guard let t = schema [ tableName] else { return . many }
94+ guard let t = env. resolve (
95+ table: table. name. value,
96+ schema: table. schema? . value
97+ ) . value else { return . many }
10398
10499 // If they had filtering on all primary keys we can assume a single
105100 // result will be returned.
@@ -124,9 +119,10 @@ extension CardinalityInferrer: StmtSyntaxVisitor {
124119 mutating func visit( _ stmt: borrowing DeleteStmtSyntax ) -> Cardinality {
125120 guard let filter = stmt. whereExpr else { return . many }
126121
127- let tableName = qualifiedName ( for: stmt. table. tableName. name, in: stmt. table. tableName. schema)
128-
129- guard let table = schema [ tableName] else {
122+ guard let table = env. resolve (
123+ table: stmt. table. tableName. name. value,
124+ schema: stmt. table. tableName. schema? . value
125+ ) . value else {
130126 // Upstream will have emitted diag
131127 return . many
132128 }
0 commit comments