Skip to content

Commit

Permalink
Review and fix Sonarcloud findings
Browse files Browse the repository at this point in the history
  • Loading branch information
spannm committed Mar 21, 2024
1 parent 3616d6f commit 32a3ec4
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 124 deletions.
3 changes: 1 addition & 2 deletions src/main/java/net/ucanaccess/console/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ private void executeExport(String cmd) throws SQLException, IOException {

// Process the command line flags.
int i = 1; // skip the first token which will always be "export"
label:
for (; i < tokens.size(); i++) {
String arg = tokens.get(i);
if (!arg.startsWith("-")) {
Expand Down Expand Up @@ -323,7 +322,7 @@ private void executeExport(String cmd) throws SQLException, IOException {
return;
case "--":
++i;
break label;
break;
default:
prompt("Unknown flag " + arg);
prompt(EXPORT_PROMPT);
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/net/ucanaccess/converters/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import net.ucanaccess.ext.FunctionType;
import net.ucanaccess.util.Try;

import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
Expand All @@ -28,6 +30,7 @@
import java.util.regex.Pattern;

public final class Functions {
private static final Logger LOGGER = System.getLogger(Functions.class.getName());
private static Double rnd;
private static Double lastRnd;
private static final double APPROX = 0.00000001;
Expand Down Expand Up @@ -547,7 +550,7 @@ private static Timestamp dateValue(String _dt, boolean _onlyDate) {
}
return t;
} catch (ParseException _ignored) {
continue;
LOGGER.log(Level.DEBUG, "Ignoring {0}", _ignored.toString());
}
}
return null;
Expand Down
30 changes: 17 additions & 13 deletions src/main/java/net/ucanaccess/converters/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import net.ucanaccess.type.ObjectType;
import net.ucanaccess.util.Try;

import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -15,6 +17,8 @@

public class Metadata {

private static final Logger LOGGER = System.getLogger(Metadata.class.getName());

private static final String SCHEMA = "CREATE SCHEMA UCA_METADATA AUTHORIZATION DBA";

private static final String TABLES =
Expand Down Expand Up @@ -233,7 +237,7 @@ public void newColumn(String _name, String _escaped, String _originalType, Integ
ps.setInt(4, _idTable);
ps.executeUpdate();
} catch (SQLException _ignored) {
return;
LOGGER.log(Level.DEBUG, "Ignoring {0}", _ignored.toString());
}
}

Expand All @@ -246,7 +250,7 @@ public List<String> getColumnNames(String _tableName) throws SQLException {
result.add(rs.getString(COLUMN_NAME));
}
return !SYSTEM_SUBQUERY.equals(_tableName) ? result : null;
}).orThrow();
}).orThrow(ex -> ex);
}

public String getColumnName(String _escapedTableName, String _escapedColumnName) throws SQLException {
Expand All @@ -261,7 +265,7 @@ public String getColumnName(String _escapedTableName, String _escapedColumnName)
}
}
return null;
}).orThrow();
}).orThrow(ex -> ex);
}

public String getEscapedColumnName(String _tableName, String _columnName) throws SQLException {
Expand All @@ -270,15 +274,15 @@ public String getEscapedColumnName(String _tableName, String _columnName) throws
ps.setString(2, _columnName);
ResultSet rs = ps.executeQuery();
return rs.next() ? rs.getString(ESCAPED_COLUMN_NAME) : null;
}).orThrow();
}).orThrow(ex -> ex);
}

public String getEscapedTableName(String _tableName) throws SQLException {
return Try.withResources(() -> conn.prepareStatement(SELECT_TABLE_ESCAPED), ps -> {
ps.setString(1, _tableName);
ResultSet rs = ps.executeQuery();
return rs.next() ? rs.getString(ESCAPED_TABLE_NAME) : null;
}).orThrow();
}).orThrow(ex -> ex);
}

public boolean isAutoIncrement(String _tableName, String _columnName) throws SQLException {
Expand All @@ -287,7 +291,7 @@ public boolean isAutoIncrement(String _tableName, String _columnName) throws SQL
ps.setString(2, _columnName);
ResultSet rs = ps.executeQuery();
return rs.next() && rs.getBoolean(IS_AUTOINCREMENT);
}).orThrow();
}).orThrow(ex -> ex);
}

public boolean isCurrency(String _tableName, String _columnName) throws SQLException {
Expand All @@ -296,30 +300,30 @@ public boolean isCurrency(String _tableName, String _columnName) throws SQLExcep
ps.setString(2, _columnName);
ResultSet rs = ps.executeQuery();
return rs.next() && rs.getBoolean(IS_CURRENCY);
}).orThrow();
}).orThrow(ex -> ex);
}

public Integer getTableId(String _escapedName) throws SQLException {
return Try.withResources(() -> conn.prepareStatement(SELECT_TABLE_METADATA), ps -> {
ps.setString(1, _escapedName);
ResultSet rs = ps.executeQuery();
return rs.next() ? rs.getInt(TABLE_ID) : -1;
}).orThrow();
}).orThrow(ex -> ex);
}

public String getTableName(String _escapedName) throws SQLException {
return Try.withResources(() -> conn.prepareStatement(SELECT_TABLE_METADATA), ps -> {
ps.setString(1, _escapedName);
ResultSet rs = ps.executeQuery();
return rs.next() ? rs.getString(TABLE_NAME) : null;
}).orThrow();
}).orThrow(ex -> ex);
}

public void dropTable(String _tableName) throws SQLException {
Try.withResources(() -> conn.prepareStatement(DROP_TABLE), ps -> {
ps.setString(1, _tableName);
ps.execute();
}).orThrow();
}).orThrow(ex -> ex);
}

public void columnDef(String _tableName, String _columnName, String _def) throws SQLException {
Expand All @@ -328,15 +332,15 @@ public void columnDef(String _tableName, String _columnName, String _def) throws
ps.setString(2, _columnName);
ps.setString(3, _tableName);
ps.execute();
}).orThrow();
}).orThrow(ex -> ex);
}

public void calculatedField(String _tableName, String _columnName) throws SQLException {
Try.withResources(() -> conn.prepareStatement(UPDATE_IS_GENERATEDCOLUMN), ps -> {
ps.setString(1, _columnName);
ps.setString(2, _tableName);
ps.execute();
}).orThrow();
}).orThrow(ex -> ex);
}

public void rename(String _oldTableName, String _newTableName, String _newEscapedTableName) throws SQLException {
Expand All @@ -345,7 +349,7 @@ public void rename(String _oldTableName, String _newTableName, String _newEscape
ps.setString(2, _newEscapedTableName);
ps.setString(3, _oldTableName);
ps.executeUpdate();
}).orThrow();
}).orThrow(ex -> ex);
}

}
20 changes: 10 additions & 10 deletions src/main/java/net/ucanaccess/converters/Pivot.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class Pivot {
private String select;
private String from;
private String expression;
private String pivot;
private String pivotStr;
private List<String> pivotIn;
private String aggregateFun;
private final Connection conn;
Expand Down Expand Up @@ -135,15 +135,15 @@ public boolean parsePivot(String _originalQuery) {
select = mtc.group(2);
from = mtc.group(3);
String pe = mtc.group(4);
Matcher mtcExpr = PIVOT_EXPR_PATT.matcher(pe);
if (mtcExpr.find()) {
if (mtcExpr.groupCount() < 2) {
Matcher matcher = PIVOT_EXPR_PATT.matcher(pe);
if (matcher.find()) {
if (matcher.groupCount() < 2) {
return false;
}
pivot = mtcExpr.group(1);
pivotIn = Arrays.asList(mtcExpr.group(2).split(","));
pivotStr = matcher.group(1);
pivotIn = Arrays.asList(matcher.group(2).split(","));
} else {
pivot = pe;
pivotStr = pe;
}
return true;
} else {
Expand All @@ -159,8 +159,8 @@ private void appendCaseWhen(StringBuilder _sb, String _condition, String _cn) {
public String verifySQL() {
StringBuilder sb = new StringBuilder();
String[] fromS = from.split(PIVOT_GROUP_BY);
sb.append("SELECT DISTINCT ").append(pivot).append(" AS PIVOT ")
.append(" FROM ").append(fromS[0]).append(" GROUP BY ").append(pivot).append(",").append(fromS[1]);
sb.append("SELECT DISTINCT ").append(pivotStr).append(" AS PIVOT ")
.append(" FROM ").append(fromS[0]).append(" GROUP BY ").append(pivotStr).append(",").append(fromS[1]);
return SQLConverter.convertSQL(sb.toString()).getSql();
}

Expand Down Expand Up @@ -230,7 +230,7 @@ public String toSQL(String name) {
.append(select);
for (String s : pivotIn) {
sb.append(",");
appendCaseWhen(sb, pivot + "=" + s, replaceQuotation(s));
appendCaseWhen(sb, pivotStr + "=" + s, replaceQuotation(s));
}
sb.append(" FROM ").append(from);

Expand Down
Loading

0 comments on commit 32a3ec4

Please sign in to comment.