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 3b73047 commit 0bbf53b
Show file tree
Hide file tree
Showing 27 changed files with 246 additions and 277 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@
<sonar.skip>false</sonar.skip>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
<sonar.organization>spannm</sonar.organization>
<!-- sonar.token>TODO_TOKEN_HERE</sonar.token -->
<!-- sonar.token>YOUR_TOKEN_HERE</sonar.token -->

<basepom.test.skip>true</basepom.test.skip>
<basepom.check.skip-all>true</basepom.check.skip-all>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/ucanaccess/commands/AutoNumberAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public AutoNumberAction(Table _table, Object[] memento, Object[] byAccess) throw
oldAutoValues.put(col.getName(), cnOld);
newAutoValues.put(col.getName(), cnNew);
conn.setFeedbackState(true);
String stmt = "UPDATE " + SQLConverter.escapeIdentifier(_table.getName(), connHsqldb) + " SET " + cn
+ "=? WHERE " + cn + "=?";
try (PreparedStatement ps = connHsqldb.prepareStatement(stmt)) {
String sql = String.format("UPDATE %s SET %s=? WHERE %s=?",
SQLConverter.escapeIdentifier(_table.getName(), connHsqldb), cn, cn);
try (PreparedStatement ps = connHsqldb.prepareStatement(sql)) {
ps.setObject(1, cnNew);
ps.setObject(2, cnOld);
ps.executeUpdate();
Expand Down
26 changes: 12 additions & 14 deletions src/main/java/net/ucanaccess/commands/BlobAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

public class BlobAction implements IFeedbackAction {
private final Table table;
Expand Down Expand Up @@ -55,26 +56,23 @@ public void doAction(ICommand toChange) throws SQLException {
Connection connHsqldb = conn.getHSQLDBConnection();

for (BlobKey bkey : keys) {
String sql = "UPDATE " + SQLConverter.escapeIdentifier(table.getName(), connHsqldb) + " SET "
+ SQLConverter.escapeIdentifier(bkey.getColumnName(), connHsqldb) + "=? WHERE ";
StringBuilder sb = new StringBuilder();
String and = "";
List<Object> values = new ArrayList<>();
for (Map.Entry<String, Object> me : bkey.getKey().entrySet()) {
sb.append(and).append(SQLConverter.escapeIdentifier(me.getKey(), connHsqldb)).append(" = ?");
values.add(me.getValue());
and = " AND ";
}
sql += sb.toString();

List<Object> values = new ArrayList<>(bkey.getKey().values());

String sql = String.format("UPDATE %s SET %s=? WHERE %s",
SQLConverter.escapeIdentifier(table.getName(), connHsqldb),
SQLConverter.escapeIdentifier(bkey.getColumnName(), connHsqldb),
bkey.getKey().keySet().stream().map(k -> SQLConverter.escapeIdentifier(k, connHsqldb) + " = ?").collect(Collectors.joining(" AND ")));

conn.setFeedbackState(true);

conn.setFeedbackState(true);
try (PreparedStatement ps = connHsqldb.prepareStatement(sql)) {
ps.setObject(1, bkey.getBytes());
int j = 2;
int i = 2;
for (Object value : values) {
ps.setObject(j, value);
++j;
ps.setObject(i, value);
i++;
}
ps.executeUpdate();
conn.setFeedbackState(false);
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/net/ucanaccess/commands/DDLCommandEnlist.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,19 +217,18 @@ private String[] checkEscaped(String _ll, String _rl, String[] _colDecls, String
}

private void parseColumnTypes(List<String> _typeList, List<String> _defaultList, List<Boolean> _notNullList, String _tknt) {

String[] colDecls = _tknt.split("[\\s\n\r]+");
String[] colDecls = _tknt.split("\\s+");
colDecls = checkEscaped("[", "]", colDecls, _tknt);
colDecls = checkEscaped("`", "`", colDecls, _tknt);
String escaped = SQLConverter.isListedAsKeyword(colDecls[0].toUpperCase()) ? colDecls[0].toUpperCase()
: SQLConverter.basicEscapingIdentifier(colDecls[0]);
columnMap.put(escaped, colDecls[0]);

boolean reset = false;
if (_tknt.matches("[\\s\n\r]*\\d+[\\s\n\r]*\\).*")) {
if (_tknt.matches("\\s*\\d+\\s*\\).*")) {
reset = true;
_tknt = _tknt.substring(_tknt.indexOf(')') + 1).trim();
colDecls = _tknt.split("[\\s\n\r]+");
colDecls = _tknt.split("\\s+");
}

if (!reset && colDecls.length < 2) {
Expand Down Expand Up @@ -263,16 +262,16 @@ private void parseColumnTypes(List<String> _typeList, List<String> _defaultList,
}

// getting AUTOINCREMENT and GUID
@SuppressWarnings("java:S5852")
private void parseTypesFromCreateStatement(String _sql) throws SQLException {
_sql = _sql.replaceAll("([\\s\n\r]+)((?i)DECIMAL)([\\s\n\r]*\\()", "$1NUMERIC(")
.replaceAll("([\\s\n\r]+)((?i)NUMERIC)([\\s\n\r]*\\()", "$1NUMERIC(");
int startDecl = _sql.indexOf('(');
int endDecl = _sql.lastIndexOf(')');
String sql = _sql.replaceAll("(\\s+)(?:(?i)DECIMAL|(?i)NUMERIC)\\s*\\(", "$1NUMERIC(");
int startDecl = sql.indexOf('(');
int endDecl = sql.lastIndexOf(')');

if (startDecl >= endDecl) {
throw new InvalidCreateStatementException(_sql);
throw new InvalidCreateStatementException(sql);
}
String decl = _sql.substring(startDecl + 1, endDecl);
String decl = sql.substring(startDecl + 1, endDecl);
String[] tokens = decl.split(",");

List<String> typeList = new ArrayList<>();
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/net/ucanaccess/console/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.*;
import java.util.stream.Collectors;

@SuppressWarnings("java:S106") // suppress sonarcloud warning to not use System.out/err
public class Main {
private static final String EXPORT_USAGE = "export [--help] [--bom] [-d <delimiter>] [-t <table>] "
+ "[--big_query_schema <pathToSchemaFile>] " + "[--newlines] <pathToCsv>";
Expand Down Expand Up @@ -277,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 @@ -322,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 Expand Up @@ -429,7 +429,7 @@ static class TableFormat {
static final List<Integer> NUMERIC_JDBC_TYPES =
List.of(Types.BIT, Types.TINYINT, Types.SMALLINT, Types.INTEGER, Types.BIGINT, Types.FLOAT,
Types.REAL, Types.DOUBLE, Types.NUMERIC, Types.DECIMAL, Types.ROWID);
private final int maxColWidth = 50;
private static final int MAX_COL_WIDTH = 50;
private final List<String> colNames;
private final List<Integer> colWidths;
private final List<Integer> colTypes;
Expand All @@ -449,7 +449,7 @@ static class TableFormat {
for (int col = 1; col <= columnCount; ++col) {
String colLabel = meta.getColumnLabel(col);
colNames.add(colLabel);
colWidths.add(Math.min(colLabel.length(), maxColWidth));
colWidths.add(Math.min(colLabel.length(), MAX_COL_WIDTH));
colTypes.add(meta.getColumnType(col));
}

Expand All @@ -467,7 +467,7 @@ static class TableFormat {
rec.add(s);

int colWidth = colWidths.get(col - 1);
if (colWidth < s.length() && colWidth < maxColWidth) {
if (colWidth < s.length() && colWidth < MAX_COL_WIDTH) {
colWidths.set(col - 1, s.length());
}
}
Expand Down
69 changes: 31 additions & 38 deletions 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 @@ -27,7 +29,9 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressWarnings("java:S2447")
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 @@ -110,7 +114,7 @@ public static double sqr(double _number) {

@FunctionType(functionName = "CBool", argumentTypes = {AccessType.NUMERIC}, returnType = AccessType.YESNO)
public static boolean cbool(BigDecimal _value) {
return cbool((Object) _value);
return cboolImpl(_value);
}

/**
Expand All @@ -121,15 +125,15 @@ public static boolean cbool(BigDecimal _value) {
*/
@FunctionType(functionName = "CBool", argumentTypes = {AccessType.YESNO}, returnType = AccessType.YESNO)
public static boolean cbool(Boolean _value) {
return cbool((Object) _value);
return cboolImpl(_value);
}

@FunctionType(functionName = "CBool", argumentTypes = {AccessType.MEMO}, returnType = AccessType.YESNO)
public static boolean cbool(String _value) {
return cbool((Object) _value);
return cboolImpl(_value);
}

private static boolean cbool(Object _obj) {
private static boolean cboolImpl(Object _obj) {
if (_obj == null) {
return false;
} else if (_obj instanceof Boolean) {
Expand Down Expand Up @@ -214,7 +218,6 @@ public static Integer clong(boolean _value) {
return _value ? -1 : 0;
}

// TODO
@FunctionType(functionName = "CSign", argumentTypes = {AccessType.DOUBLE}, returnType = AccessType.SINGLE)
public static double csign(double _value) {
MathContext mc = new MathContext(7);
Expand All @@ -226,7 +229,7 @@ public static double csign(double _value) {
*/
@FunctionType(functionName = "CStr", argumentTypes = {AccessType.YESNO}, returnType = AccessType.MEMO)
public static String cstr(Boolean _value) throws UcanaccessSQLException {
return cstr((Object) _value);
return cstrImpl(_value);
}

@FunctionType(functionName = "CStr", argumentTypes = {AccessType.TEXT}, returnType = AccessType.MEMO)
Expand All @@ -236,20 +239,20 @@ public static String cstr(String _value) {

@FunctionType(functionName = "CStr", argumentTypes = {AccessType.DOUBLE}, returnType = AccessType.MEMO)
public static String cstr(double _value) throws UcanaccessSQLException {
return cstr((Object) _value);
return cstrImpl(_value);
}

@FunctionType(functionName = "CStr", argumentTypes = {AccessType.LONG}, returnType = AccessType.MEMO)
public static String cstr(int _value) throws UcanaccessSQLException {
return cstr((Object) _value);
return cstrImpl(_value);
}

@FunctionType(functionName = "CStr", argumentTypes = {AccessType.DATETIME}, returnType = AccessType.MEMO)
public static String cstr(Timestamp _value) throws UcanaccessSQLException {
return _value == null ? null : format(_value, "general date");
}

private static String cstr(Object _value) throws UcanaccessSQLException {
private static String cstrImpl(Object _value) throws UcanaccessSQLException {
return _value == null ? null : format(_value.toString(), "", true);
}

Expand Down Expand Up @@ -547,7 +550,9 @@ private static Timestamp dateValue(String _dt, boolean _onlyDate) {
t = new Timestamp(cl.getTime().getTime());
}
return t;
} catch (ParseException _ignored) {}
} catch (ParseException _ignored) {
LOGGER.log(Level.DEBUG, "Ignoring {0}", _ignored.toString());
}
}
return null;
}
Expand Down Expand Up @@ -658,8 +663,8 @@ public static String format(Timestamp _t, String _par) throws UcanaccessSQLExcep
return String.valueOf(datePart(_par, _t));
}
return createSimpleDateFormat(_par
.replace("m", "M")
.replace("n", "m")
.replace('m', 'M')
.replace('n', 'm')
.replace("(?i)AM/PM|A/P|AMPM", "a")
.replace("dddd", "EEEE")).format(_t);
}
Expand All @@ -668,33 +673,32 @@ public static String format(Timestamp _t, String _par) throws UcanaccessSQLExcep
* Returns one of two parts, depending on the evaluation of an expression.
*/
@FunctionType(functionName = "IIf", argumentTypes = {AccessType.YESNO, AccessType.MEMO, AccessType.MEMO}, returnType = AccessType.MEMO)
public static String iif(Boolean _b, String _o, String _o1) {
return (String) iif(_b, _o, (Object) _o1);
public static String iif(Boolean _b, String _o1, String _o2) {
return iifImpl(_b, _o1, _o2);
}

@FunctionType(functionName = "IIf", argumentTypes = {AccessType.YESNO, AccessType.LONG, AccessType.LONG}, returnType = AccessType.LONG)
public static Integer iif(Boolean b, Integer o, Integer o1) {
return (Integer) iif(b, o, (Object) o1);
public static Integer iif(Boolean _b, Integer _o1, Integer _o2) {
return iifImpl(_b, _o1, _o2);
}

@FunctionType(functionName = "IIf", argumentTypes = {AccessType.YESNO, AccessType.DOUBLE, AccessType.DOUBLE}, returnType = AccessType.DOUBLE)
public static Double iif(Boolean b, Double o, Double o1) {
return (Double) iif(b, o, (Object) o1);
public static Double iif(Boolean _b, Double _o1, Double _o2) {
return iifImpl(_b, _o1, _o2);
}

@FunctionType(functionName = "IIf", argumentTypes = {AccessType.YESNO, AccessType.YESNO, AccessType.YESNO}, returnType = AccessType.YESNO)
public static Boolean iif(Boolean b, Boolean o, Boolean o1) {

return (Boolean) iif(b, o, (Object) o1);
public static Boolean iif(Boolean _b, Boolean _o1, Boolean _o2) {
return iifImpl(_b, _o1, _o2);
}

@FunctionType(functionName = "IIf", argumentTypes = {AccessType.YESNO, AccessType.DATETIME, AccessType.DATETIME}, returnType = AccessType.DATETIME)
public static Timestamp iif(Boolean b, Timestamp o, Timestamp o1) {
return (Timestamp) iif(b, o, (Object) o1);
public static Timestamp iif(Boolean b, Timestamp _o1, Timestamp _o2) {
return iifImpl(b, _o1, _o2);
}

private static Object iif(Boolean _b, Object _o1, Object _o2) {
return Objects.requireNonNullElse(_b, Boolean.FALSE) ? _o1 : _o2;
private static <T> T iifImpl(Boolean _b, T _o1, T _o2) {
return _b != null && _b ? _o1 : _o2;
}

/**
Expand Down Expand Up @@ -1064,9 +1068,7 @@ public static String weekdayName(int _number, boolean _abbreviate) {
public static String weekdayName(int _number, boolean _abbreviate, int _firstDayOfWeek) {
// DayOfWeek starts with Monday, WeekdayName with Sunday (default)
int firstDayOfWeek = Math.min(Math.max(1, _firstDayOfWeek), 7);
int offset = firstDayOfWeek == 1 ? -1
: firstDayOfWeek == 1 ? -1
: firstDayOfWeek - 2;
int offset = firstDayOfWeek == 1 ? -1 : firstDayOfWeek - 2;
int number = _number;
while (number > 7) {
number -= 7;
Expand All @@ -1075,10 +1077,6 @@ public static String weekdayName(int _number, boolean _abbreviate, int _firstDay
.getDisplayName(_abbreviate ? TextStyle.SHORT : TextStyle.FULL, Locale.getDefault());
}

public static void main(String[] args) {
System.out.println("WeekDayName(3): " + weekdayName(3));
}

/**
* Returns a number representing the day of the week (a number from 1 to 7) given a date value.
*/
Expand Down Expand Up @@ -1485,16 +1483,12 @@ public static Boolean formulaToBoolean(Boolean res, String datatype) {

@FunctionType(functionName = "formulaToBoolean", argumentTypes = {AccessType.DOUBLE, AccessType.MEMO}, returnType = AccessType.YESNO)
public static Boolean formulaToBoolean(Double res, String datatype) {
if (res == null) {
return null;
}
return res != 0d;
return res == null ? null : res != 0d;
}

@FunctionType(functionName = "formulaToBoolean", argumentTypes = {AccessType.DATETIME, AccessType.MEMO}, returnType = AccessType.YESNO)
public static Boolean formulaToBoolean(Timestamp res, String datatype) {
return null;

}

@FunctionType(functionName = "formulaToBoolean", argumentTypes = {AccessType.MEMO, AccessType.MEMO}, returnType = AccessType.YESNO)
Expand All @@ -1507,7 +1501,6 @@ public static Boolean formulaToBoolean(String res, String datatype) {
return false;
}
return null;

}

@FunctionType(functionName = "formulaToText", argumentTypes = {AccessType.MEMO, AccessType.MEMO}, returnType = AccessType.MEMO)
Expand Down
Loading

0 comments on commit 0bbf53b

Please sign in to comment.