| 
21 | 21 | from .transformations import _filter_order  | 
22 | 22 | 
 
  | 
23 | 23 | LIMITING_FK_EDGES_CLAUSE_1 = (  | 
24 |  | -    """AND second.{fk_field_name}_{pk_name} = %(limiting_fk_edges_instance_pk)s"""  | 
 | 24 | +    """AND second.{fk_field_name}_id = %(limiting_fk_edges_instance_pk)s"""  | 
25 | 25 | )  | 
26 |  | -LIMITING_FK_EDGES_CLAUSE_2 = """AND {relationship_table}.{fk_field_name}_{pk_name} = %(limiting_fk_edges_instance_pk)s"""  | 
 | 26 | +LIMITING_FK_EDGES_CLAUSE_2 = """AND {relationship_table}.{fk_field_name}_id = %(limiting_fk_edges_instance_pk)s"""  | 
27 | 27 | 
 
  | 
28 | 28 | LIMITING_FK_NODES_CLAUSE_1 = """"""  | 
29 | 29 | LIMITING_FK_NODES_CLAUSE_2 = """"""  | 
 | 
34 | 34 | # DISALLOWED_DESCENDANTS_NODES_CLAUSE_1 = """AND second.parent_pk <> ALL(%(disallowed_descendants_node_pks)s)"""  # Used for descendants and downward path  | 
35 | 35 | # DISALLOWED_DESCENDANTS_NODES_CLAUSE_2 = """AND {relationship_table}.parent_pk <> ALL(%(disallowed_descendants_node_pks)s)"""  | 
36 | 36 | 
 
  | 
37 |  | -DISALLOWED_ANCESTORS_NODES_CLAUSE_1 = """AND first.parent_{pk_name} <> ALL(%(disallowed_ancestors_node_pks)s)"""  # Used for ancestors and upward path  | 
38 |  | -DISALLOWED_ANCESTORS_NODES_CLAUSE_2 = """AND {relationship_table}.parent_{pk_name} <> ALL(%(disallowed_ancestors_node_pks)s)"""  | 
 | 37 | +DISALLOWED_ANCESTORS_NODES_CLAUSE_1 = """AND first.parent_id <> ALL(%(disallowed_ancestors_node_pks)s)"""  # Used for ancestors and upward path  | 
 | 38 | +DISALLOWED_ANCESTORS_NODES_CLAUSE_2 = """AND {relationship_table}.parent_id <> ALL(%(disallowed_ancestors_node_pks)s)"""  | 
39 | 39 | 
 
  | 
40 |  | -DISALLOWED_DESCENDANTS_NODES_CLAUSE_1 = """AND first.child_{pk_name} <> ALL(%(disallowed_descendants_node_pks)s)"""  # Used for descendants and downward path  | 
41 |  | -DISALLOWED_DESCENDANTS_NODES_CLAUSE_2 = """AND {relationship_table}.child_{pk_name} <> ALL(%(disallowed_descendants_node_pks)s)"""  | 
 | 40 | +DISALLOWED_DESCENDANTS_NODES_CLAUSE_1 = """AND first.child_id <> ALL(%(disallowed_descendants_node_pks)s)"""  # Used for descendants and downward path  | 
 | 41 | +DISALLOWED_DESCENDANTS_NODES_CLAUSE_2 = """AND {relationship_table}.child_id <> ALL(%(disallowed_descendants_node_pks)s)"""  | 
42 | 42 | 
 
  | 
43 | 43 | 
 
  | 
44 | 44 | ALLOWED_ANCESTORS_NODES_CLAUSE_1 = """AND first.parent_pk = ANY(%(allowed_ancestors_node_pks)s)"""  # Used for ancestors and upward path  | 
45 |  | -ALLOWED_ANCESTORS_NODES_CLAUSE_2 = """AND {relationship_table}.parent_{pk_name} = ANY(%(allowed_ancestors_node_pks)s)"""  | 
 | 45 | +ALLOWED_ANCESTORS_NODES_CLAUSE_2 = """AND {relationship_table}.parent_id = ANY(%(allowed_ancestors_node_pks)s)"""  | 
46 | 46 | 
 
  | 
47 |  | -ALLOWED_DESCENDANTS_NODES_CLAUSE_1 = """AND first.child_{pk_name} = ANY(%(allowed_descendants_node_pks)s)"""  # Used for descendants and downward path  | 
48 |  | -ALLOWED_DESCENDANTS_NODES_CLAUSE_2 = """AND {relationship_table}.child_{pk_name} = ANY(%(allowed_descendants_node_pks)s)"""  | 
 | 47 | +ALLOWED_DESCENDANTS_NODES_CLAUSE_1 = """AND first.child_id = ANY(%(allowed_descendants_node_pks)s)"""  # Used for descendants and downward path  | 
 | 48 | +ALLOWED_DESCENDANTS_NODES_CLAUSE_2 = """AND {relationship_table}.child_id = ANY(%(allowed_descendants_node_pks)s)"""  | 
49 | 49 | 
 
  | 
50 | 50 | ANCESTORS_QUERY = """  | 
51 | 51 | WITH RECURSIVE traverse({pk_name}, depth) AS (  | 
52 |  | -    SELECT first.parent_{pk_name}, 1  | 
 | 52 | +    SELECT first.parent_id, 1  | 
53 | 53 |         FROM {relationship_table} AS first  | 
54 | 54 |         LEFT OUTER JOIN {relationship_table} AS second  | 
55 |  | -        ON first.parent_{pk_name} = second.child_{pk_name}  | 
56 |  | -    WHERE first.child_{pk_name} = %(pk)s  | 
 | 55 | +        ON first.parent_id = second.child_id  | 
 | 56 | +    WHERE first.child_id = %(pk)s  | 
57 | 57 |     -- LIMITING_FK_EDGES_CLAUSE_1  | 
58 | 58 |     -- DISALLOWED_ANCESTORS_NODES_CLAUSE_1  | 
59 | 59 |     -- ALLOWED_ANCESTORS_NODES_CLAUSE_1  | 
60 | 60 |     {ancestors_clauses_1}  | 
61 | 61 | UNION  | 
62 |  | -    SELECT DISTINCT parent_{pk_name}, traverse.depth + 1  | 
 | 62 | +    SELECT DISTINCT parent_id, traverse.depth + 1  | 
63 | 63 |         FROM traverse  | 
64 | 64 |         INNER JOIN {relationship_table}  | 
65 |  | -        ON {relationship_table}.child_{pk_name} = traverse.{pk_name}  | 
 | 65 | +        ON {relationship_table}.child_id = traverse.{pk_name}  | 
66 | 66 |     WHERE 1 = 1  | 
67 | 67 |     -- LIMITING_FK_EDGES_CLAUSE_2  | 
68 | 68 |     -- DISALLOWED_ANCESTORS_NODES_CLAUSE_2  | 
 | 
77 | 77 | 
 
  | 
78 | 78 | DESCENDANTS_QUERY = """  | 
79 | 79 | WITH RECURSIVE traverse({pk_name}, depth) AS (  | 
80 |  | -    SELECT first.child_{pk_name}, 1  | 
 | 80 | +    SELECT first.child_id, 1  | 
81 | 81 |         FROM {relationship_table} AS first  | 
82 | 82 |         LEFT OUTER JOIN {relationship_table} AS second  | 
83 |  | -        ON first.child_{pk_name} = second.parent_{pk_name}  | 
84 |  | -    WHERE first.parent_{pk_name} = %(pk)s  | 
 | 83 | +        ON first.child_id = second.parent_id  | 
 | 84 | +    WHERE first.parent_id = %(pk)s  | 
85 | 85 |     -- LIMITING_FK_EDGES_CLAUSE_1  | 
86 | 86 |     -- DISALLOWED_DESCENDANTS_NODES_CLAUSE_1  | 
87 | 87 |     -- ALLOWED_DESCENDANTS_NODES_CLAUSE_1  | 
88 | 88 |     {descendants_clauses_1}  | 
89 | 89 | UNION  | 
90 |  | -    SELECT DISTINCT child_{pk_name}, traverse.depth + 1  | 
 | 90 | +    SELECT DISTINCT child_id, traverse.depth + 1  | 
91 | 91 |         FROM traverse  | 
92 | 92 |         INNER JOIN {relationship_table}  | 
93 |  | -        ON {relationship_table}.parent_{pk_name} = traverse.{pk_name}  | 
 | 93 | +        ON {relationship_table}.parent_id = traverse.{pk_name}  | 
94 | 94 |     WHERE 1=1  | 
95 | 95 |     -- LIMITING_FK_EDGES_CLAUSE_2  | 
96 | 96 |     -- DISALLOWED_DESCENDANTS_NODES_CLAUSE_2  | 
 | 
104 | 104 | """  | 
105 | 105 | 
 
  | 
106 | 106 | PATH_LIMITING_FK_EDGES_CLAUSE = (  | 
107 |  | -    """AND first.{fk_field_name}_{pk_name} = %(limiting_fk_edges_instance_pk)s"""  | 
 | 107 | +    """AND first.{fk_field_name}_id = %(limiting_fk_edges_instance_pk)s"""  | 
108 | 108 | )  | 
109 | 109 | PATH_LIMITING_FK_NODES_CLAUSE = """"""  | 
110 | 110 | 
 
  | 
111 | 111 | DISALLOWED_UPWARD_PATH_NODES_CLAUSE = (  | 
112 |  | -    """AND second.parent_{pk_name} <> ALL('{disallowed_path_node_pks}')"""  | 
 | 112 | +    """AND second.parent_id <> ALL('{disallowed_path_node_pks}')"""  | 
113 | 113 | )  | 
114 | 114 | DISALLOWED_DOWNWARD_PATH_NODES_CLAUSE = (  | 
115 |  | -    """AND second.child_{pk_name} <> ALL('{disallowed_path_node_pks}')"""  | 
 | 115 | +    """AND second.child_id <> ALL('{disallowed_path_node_pks}')"""  | 
116 | 116 | )  | 
117 | 117 | ALLOWED_UPWARD_PATH_NODES_CLAUSE = (  | 
118 |  | -    """AND second.parent_{pk_name} = ALL('{allowed_path_node_pks}')"""  | 
 | 118 | +    """AND second.parent_id = ALL('{allowed_path_node_pks}')"""  | 
119 | 119 | )  | 
120 | 120 | ALLOWED_DOWNWARD_PATH_NODES_CLAUSE = (  | 
121 |  | -    """AND second.child_{pk_name} = ALL('{allowed_path_node_pks}')"""  | 
 | 121 | +    """AND second.child_id = ALL('{allowed_path_node_pks}')"""  | 
122 | 122 | )  | 
123 | 123 | 
 
  | 
124 | 124 | UPWARD_PATH_QUERY = """  | 
125 |  | -WITH RECURSIVE traverse(child_{pk_name}, parent_{pk_name}, depth, path) AS (  | 
 | 125 | +WITH RECURSIVE traverse(child_id, parent_id, depth, path) AS (  | 
126 | 126 |     SELECT  | 
127 |  | -        first.child_{pk_name},  | 
128 |  | -        first.parent_{pk_name},  | 
 | 127 | +        first.child_id,  | 
 | 128 | +        first.parent_id,  | 
129 | 129 |         1 AS depth,  | 
130 |  | -        ARRAY[first.child_{pk_name}] AS path  | 
 | 130 | +        ARRAY[first.child_id] AS path  | 
131 | 131 |         FROM {relationship_table} AS first  | 
132 |  | -    WHERE child_{pk_name} = %(starting_node)s  | 
 | 132 | +    WHERE child_id = %(starting_node)s  | 
133 | 133 | UNION ALL  | 
134 | 134 |     SELECT  | 
135 |  | -        first.child_{pk_name},  | 
136 |  | -        first.parent_{pk_name},  | 
 | 135 | +        first.child_id,  | 
 | 136 | +        first.parent_id,  | 
137 | 137 |         second.depth + 1 AS depth,  | 
138 |  | -        path || first.child_{pk_name} AS path  | 
 | 138 | +        path || first.child_id AS path  | 
139 | 139 |         FROM {relationship_table} AS first, traverse AS second  | 
140 |  | -    WHERE first.child_{pk_name} = second.parent_{pk_name}  | 
141 |  | -    AND (first.child_{pk_name} <> ALL(second.path))  | 
 | 140 | +    WHERE first.child_id = second.parent_id  | 
 | 141 | +    AND (first.child_id <> ALL(second.path))  | 
142 | 142 |     -- PATH_LIMITING_FK_EDGES_CLAUSE  | 
143 | 143 |     -- DISALLOWED_UPWARD_PATH_NODES_CLAUSE  | 
144 | 144 |     -- ALLOWED_UPWARD_PATH_NODES_CLAUSE  | 
 | 
150 | 150 | FROM   | 
151 | 151 |     (  | 
152 | 152 |     SELECT path || ARRAY[%(ending_node)s], depth FROM traverse  | 
153 |  | -        WHERE parent_{pk_name} = %(ending_node)s  | 
 | 153 | +        WHERE parent_id = %(ending_node)s  | 
154 | 154 |         AND depth <= %(max_depth)s  | 
155 | 155 |         LIMIT 1  | 
156 | 156 | ) AS x({pk_name});  | 
157 | 157 | """  | 
158 | 158 | 
 
  | 
159 | 159 | DOWNWARD_PATH_QUERY = """  | 
160 |  | -WITH RECURSIVE traverse(parent_{pk_name}, child_{pk_name}, depth, path) AS (  | 
 | 160 | +WITH RECURSIVE traverse(parent_id, child_id, depth, path) AS (  | 
161 | 161 |     SELECT  | 
162 |  | -        first.parent_{pk_name},  | 
163 |  | -        first.child_{pk_name},  | 
 | 162 | +        first.parent_id,  | 
 | 163 | +        first.child_id,  | 
164 | 164 |         1 AS depth,  | 
165 |  | -        ARRAY[first.parent_{pk_name}] AS path  | 
 | 165 | +        ARRAY[first.parent_id] AS path  | 
166 | 166 |         FROM {relationship_table} AS first  | 
167 |  | -    WHERE parent_{pk_name} = %(starting_node)s  | 
 | 167 | +    WHERE parent_id = %(starting_node)s  | 
168 | 168 | UNION ALL  | 
169 | 169 |     SELECT  | 
170 |  | -        first.parent_{pk_name},  | 
171 |  | -        first.child_{pk_name},  | 
 | 170 | +        first.parent_id,  | 
 | 171 | +        first.child_id,  | 
172 | 172 |         second.depth + 1 AS depth,  | 
173 |  | -        path || first.parent_{pk_name} AS path  | 
 | 173 | +        path || first.parent_id AS path  | 
174 | 174 |         FROM {relationship_table} AS first, traverse AS second  | 
175 |  | -    WHERE first.parent_{pk_name} = second.child_{pk_name}  | 
176 |  | -    AND (first.parent_{pk_name} <> ALL(second.path))  | 
 | 175 | +    WHERE first.parent_id = second.child_id  | 
 | 176 | +    AND (first.parent_id <> ALL(second.path))  | 
177 | 177 |     -- PATH_LIMITING_FK_EDGES_CLAUSE  | 
178 | 178 |     -- DISALLOWED_DOWNWARD_PATH_NODES_CLAUSE  | 
179 | 179 |     -- ALLOWED_DOWNWARD_PATH_NODES_CLAUSE  | 
 | 
185 | 185 | FROM   | 
186 | 186 |     (  | 
187 | 187 |     SELECT path || ARRAY[%(ending_node)s], depth FROM traverse  | 
188 |  | -        WHERE child_{pk_name} = %(ending_node)s  | 
 | 188 | +        WHERE child_id = %(ending_node)s  | 
189 | 189 |         AND depth <= %(max_depth)s  | 
190 | 190 |         LIMIT 1  | 
191 | 191 | ) AS x({pk_name});  | 
 | 
0 commit comments