Skip to content

Commit 7112241

Browse files
committed
Merge branch 'DBTOOLS-1745-logs-refactoring' into 'master'
DBTOOLS-1745 analyzer logs refactoring See merge request codekeeper/pgcodekeeper-core!86
2 parents 266b033 + 2c07d82 commit 7112241

39 files changed

Lines changed: 584 additions & 456 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1414

1515
### Changed
1616

17+
- Reduced log level in the analyzer and improved its information content.
1718
- Classes related to ignore lists have been moved to a common package.
1819
- Classes for creating JDBC connections have been rewritten to reflect the new repository structure.
1920

CHANGELOG.ru.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
### Изменено
1616

17+
- Понижен уровень логов в анализаторе и улучшена их информативность.
1718
- Классы, относящиеся к спискам игнорирования, вынесены в общий пакет.
1819
- Классы для создания JDBC-подключений были переписаны в соответствии с новой структурой репозитория.
1920

Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
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+
}

src/main/java/org/pgcodekeeper/core/localizations/Messages.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,6 @@ public final class Messages {
187187

188188
public static String Select_log_aster_qual_not_found;
189189

190-
public static String Select_log_complex_not_found;
191-
192190
public static String Select_log_not_alter_item;
193191

194192
public static String Select_log_not_alter_prim;
@@ -245,6 +243,44 @@ public final class Messages {
245243

246244
public static String DatabaseFactory_errors_found_while_parsing;
247245

246+
public static String AbstractExpr_log_column_not_found_in_complex;
247+
248+
public static String AbstractExpr_log_complex_not_found;
249+
250+
public static String AbstractExpr_log_unknown_column_ref;
251+
252+
public static String AbstractExpr_log_column_not_found_in_relation;
253+
254+
public static String AbstractExpr_log_relation_not_found;
255+
256+
public static String AbstractExpr_log_tableless_column_not_resolved;
257+
258+
public static String ValueExpr_log_no_vex_alternative;
259+
260+
public static String ValueExpr_log_no_primary_alternative;
261+
262+
public static String ValueExpr_log_subselect_empty;
263+
264+
public static String ValueExpr_log_long_indirection;
265+
266+
public static String ValueExpr_log_duplicate_named_arg;
267+
268+
public static String ValueExpr_log_no_function_special_alternative;
269+
270+
public static String ValueExpr_log_no_datetime_alternative;
271+
272+
public static String ValueExpr_log_no_literal_alternative;
273+
274+
public static String MsAbstractExprWithNmspc_log_dupl_unaliased_table;
275+
276+
public static String MsSelect_log_not_alter_selectops;
277+
278+
public static String MsSelect_log_not_alter_item;
279+
280+
public static String ChAbstractExprWithNmspc_log_dupl_unaliased_table;
281+
282+
public static String ChSelect_log_not_alter_selectops;
283+
248284
static {
249285
ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_NAME);
250286
for (String key : bundle.keySet()) {

0 commit comments

Comments
 (0)