|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright 2017-2025 TAXTELECOM, LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + *******************************************************************************/ |
| 16 | +package org.pgcodekeeper.core.database.base.parser.antlr; |
| 17 | + |
| 18 | +import org.antlr.v4.runtime.ParserRuleContext; |
| 19 | +import org.pgcodekeeper.core.parsers.antlr.base.CodeUnitToken; |
| 20 | +import org.pgcodekeeper.core.schema.GenericColumn; |
| 21 | +import org.pgcodekeeper.core.schema.IRelation; |
| 22 | +import org.pgcodekeeper.core.schema.PgObjLocation; |
| 23 | +import org.pgcodekeeper.core.schema.meta.MetaContainer; |
| 24 | +import org.pgcodekeeper.core.utils.Pair; |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | + |
| 28 | +import java.util.*; |
| 29 | + |
| 30 | +import static org.pgcodekeeper.core.schema.PgObjLocation.Builder; |
| 31 | +import static org.pgcodekeeper.core.schema.PgObjLocation.LocationType; |
| 32 | + |
| 33 | +/** |
| 34 | + * Abstract base class for SQL expression analysis. |
| 35 | + */ |
| 36 | +public abstract class AbstractExpr { |
| 37 | + |
| 38 | + private static final Logger LOG = LoggerFactory.getLogger(AbstractExpr.class); |
| 39 | + |
| 40 | + private final Set<PgObjLocation> dependencies; |
| 41 | + |
| 42 | + // TODO get current version. |
| 43 | + // Need to get version. I already got it from JdbcLoader(READER) |
| 44 | + // and put it to the 'PgDatabase' as currentPostgreSqlVersion, |
| 45 | + // but I couldn't get it from PgDumpLoader(WRITER), that's why for |
| 46 | + // cases with 'PgDumpLoader(WRITER)' the version was hard-coded in 'AbstractDatabase'. |
| 47 | + protected final MetaContainer meta; |
| 48 | + |
| 49 | + protected final AbstractExpr parent; |
| 50 | + |
| 51 | + /** |
| 52 | + * Base constructor |
| 53 | + * |
| 54 | + * @param parent parent scope |
| 55 | + * @param dependencies set for dependencies |
| 56 | + * @param meta meta storage |
| 57 | + */ |
| 58 | + protected AbstractExpr(AbstractExpr parent, Set<PgObjLocation> dependencies, MetaContainer meta) { |
| 59 | + this.parent = parent; |
| 60 | + this.dependencies = dependencies; |
| 61 | + this.meta = meta; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Constructor for child scope |
| 66 | + * |
| 67 | + * @param parent parent scope |
| 68 | + */ |
| 69 | + protected AbstractExpr(AbstractExpr parent) { |
| 70 | + this(parent, parent.dependencies, parent.meta); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Constructor for root scope |
| 75 | + * |
| 76 | + * @param meta meta storage |
| 77 | + */ |
| 78 | + protected AbstractExpr(MetaContainer meta) { |
| 79 | + this(null, new LinkedHashSet<>(), meta); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Gets an unmodifiable set of database object dependencies found in this expression. |
| 84 | + * |
| 85 | + * @return set of {@link PgObjLocation} representing dependencies |
| 86 | + */ |
| 87 | + public Set<PgObjLocation> getDependencies() { |
| 88 | + return Collections.unmodifiableSet(dependencies); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @return true if this is a child element |
| 93 | + */ |
| 94 | + protected final boolean hasParent() { |
| 95 | + return parent != null; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @param cteName cte name |
| 100 | + * @return true if a cte with the given name exists in the current scope |
| 101 | + */ |
| 102 | + protected boolean hasCte(String cteName) { |
| 103 | + return hasParent() && parent.hasCte(cteName); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @param cteName cte name |
| 108 | + * @return cte for the given name if it exists in the current scope |
| 109 | + */ |
| 110 | + protected List<Pair<String, String>> findCte(String cteName) { |
| 111 | + return hasParent() ? parent.findCte(cteName) : null; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @param schema optional schema qualification of name, may be null |
| 116 | + * @param name alias of the referenced object |
| 117 | + * @param column optional referenced column alias, may be null |
| 118 | + * @return a pair of (Alias, Dealiased name) where Alias is the given name. |
| 119 | + * Dealiased name can be null if the name is internal to the query |
| 120 | + * and is not a reference to external table.<br> |
| 121 | + * null if the name is not found |
| 122 | + */ |
| 123 | + public Map.Entry<String, GenericColumn> findReference(String schema, String name, String column) { |
| 124 | + return hasParent() ? parent.findReference(schema, name, column) : null; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @param name column name |
| 129 | + * @return columns and their types for the given name, if it exists in the current scope |
| 130 | + */ |
| 131 | + protected Pair<IRelation, Pair<String, String>> findColumn(String name) { |
| 132 | + return hasParent() ? parent.findColumn(name) : null; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Adds a dependency |
| 137 | + * |
| 138 | + * @param dependency dependency |
| 139 | + */ |
| 140 | + public void addDependency(PgObjLocation dependency) { |
| 141 | + dependencies.add(dependency); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Adds a dependency with parser rule context as a reference |
| 146 | + * |
| 147 | + * @param genericColumn object location |
| 148 | + * @param ctx parser rule context |
| 149 | + */ |
| 150 | + public void addReference(GenericColumn genericColumn, ParserRuleContext ctx) { |
| 151 | + addDependency(genericColumn, ctx, LocationType.LOCAL_REF); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Adds a dependency with parser rule context as a variable |
| 156 | + * |
| 157 | + * @param genericColumn object location |
| 158 | + * @param ctx rule context |
| 159 | + */ |
| 160 | + public void addVariable(GenericColumn genericColumn, ParserRuleContext ctx) { |
| 161 | + addDependency(genericColumn, ctx, LocationType.VARIABLE); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Adds a dependency with parser rule context and dependency type |
| 166 | + * |
| 167 | + * @param genericColumn object location |
| 168 | + * @param ctx rule context |
| 169 | + * @param type dependency type |
| 170 | + */ |
| 171 | + public void addDependency(GenericColumn genericColumn, ParserRuleContext ctx, LocationType type) { |
| 172 | + addDependency(new Builder() |
| 173 | + .setObject(genericColumn) |
| 174 | + .setCtx(ctx) |
| 175 | + .setLocationType(type) |
| 176 | + .setAlias(ctx.getText()) |
| 177 | + .build()); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Adds a non-system dependency |
| 182 | + * |
| 183 | + * @param genericColumn object location |
| 184 | + * @param ctx rule context |
| 185 | + */ |
| 186 | + public void addDependency(GenericColumn genericColumn, ParserRuleContext ctx) { |
| 187 | + if (isSystemSchema(genericColumn.schema)) { |
| 188 | + return; |
| 189 | + } |
| 190 | + |
| 191 | + var dep = new Builder() |
| 192 | + .setObject(genericColumn) |
| 193 | + .setCtx(ctx) |
| 194 | + .build(); |
| 195 | + addDependency(dep); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Checks whether the schema is systemic. |
| 200 | + * |
| 201 | + * @param schema schema name |
| 202 | + * @return true if the schema is a system schema. |
| 203 | + */ |
| 204 | + protected abstract boolean isSystemSchema(String schema); |
| 205 | + |
| 206 | + /** |
| 207 | + * Looks up a relation in the meta store. |
| 208 | + * |
| 209 | + * @param schemaName schema name |
| 210 | + * @param relationName relation name |
| 211 | + * @return the found relation or null |
| 212 | + */ |
| 213 | + protected IRelation findRelation(String schemaName, String relationName) { |
| 214 | + return meta.findRelation(getSchemaName(schemaName), relationName); |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * Returns schema name to use to search for objects in the metastore. By default, returns given schema name. |
| 219 | + * Subclasses can override this behavior. |
| 220 | + * |
| 221 | + * @param schemaName schema name |
| 222 | + * @return schema name |
| 223 | + */ |
| 224 | + protected String getSchemaName(String schemaName) { |
| 225 | + return schemaName; |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * Logs a message without context |
| 230 | + * |
| 231 | + * @param msg message |
| 232 | + * @param args optional arguments for message |
| 233 | + */ |
| 234 | + protected void log(String msg, Object... args) { |
| 235 | + log(null, msg, args); |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Logs a message with context |
| 240 | + * |
| 241 | + * @param ctx parser rule context |
| 242 | + * @param msg message |
| 243 | + * @param args optional arguments for message |
| 244 | + */ |
| 245 | + protected void log(ParserRuleContext ctx, String msg, Object... args) { |
| 246 | + if (LOG.isDebugEnabled()) { |
| 247 | + String positionInfo = ""; |
| 248 | + if (ctx != null) { |
| 249 | + CodeUnitToken token = (CodeUnitToken) ctx.getStart(); |
| 250 | + positionInfo = "line %d:%d ".formatted(token.getLine(), token.getCodeUnitPositionInLine()); |
| 251 | + } |
| 252 | + |
| 253 | + var logMsg = positionInfo + msg.formatted(args); |
| 254 | + LOG.debug(logMsg); |
| 255 | + } |
| 256 | + } |
| 257 | +} |
0 commit comments