Skip to content

Commit

Permalink
net-sf-ucanaccess-fork: Code hygiene
Browse files Browse the repository at this point in the history
  • Loading branch information
spannm committed Sep 22, 2023
1 parent 1a5bb47 commit 0759441
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 94 deletions.
5 changes: 2 additions & 3 deletions src/main/java/net/ucanaccess/commands/InsertCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,8 @@ public IFeedbackAction persist() throws SQLException {
BlobAction ba = new BlobAction(table, newRow);
ba.doAction(this);
return ana;
} catch (IOException e) {
e.printStackTrace();
throw new UcanaccessSQLException(e);
} catch (IOException _ex) {
throw new UcanaccessSQLException(_ex);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/ucanaccess/console/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ public static void main(String[] args) throws Exception {
System.out.println(sqlw.getMessage());
sqlw = sqlw.getNextWarning();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
} catch (Exception _ex) {
_ex.printStackTrace();
System.out.println(_ex.getMessage());
System.exit(1);
}
Main main = new Main(conn, input);
Expand Down
127 changes: 67 additions & 60 deletions src/main/java/net/ucanaccess/converters/LoadJet.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,68 +32,77 @@
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class LoadJet {
private static int namingCounter = 0;

private final class FunctionsLoader {
private Set<String> functionsDefinition = new HashSet<>();

private void addAggregates() {
functionsDefinition.add(getAggregate("LONGVARCHAR", "last"));
functionsDefinition.add(getAggregate("DECIMAL(100,10)", "last"));
functionsDefinition.add(getAggregate("BOOLEAN", "last"));
functionsDefinition.add(getAggregate("LONGVARCHAR", "first"));
functionsDefinition.add(getAggregate("DECIMAL(100,10)", "first"));
functionsDefinition.add(getAggregate("BOOLEAN", "first"));
functionsDefinition.add(getLastTimestamp());
functionsDefinition.add(getFirstTimestamp());
private final Set<String> functionDefinitions = new LinkedHashSet<>();

private void addAggregates() {
Stream.of(getAggregate("last", "LONGVARCHAR"),
getAggregate("last", "DECIMAL(100,10)"),
getAggregate("last", "BOOLEAN"),
getAggregate("first", "LONGVARCHAR"),
getAggregate("first", "DECIMAL(100,10)"),
getAggregate("first", "BOOLEAN"),
getLastTimestamp(),
getFirstTimestamp()
).forEach(functionDefinitions::add);
}

private String getLastTimestamp() {
return "CREATE AGGREGATE FUNCTION last(IN val TIMESTAMP, IN flag boolean, INOUT ts TIMESTAMP, INOUT counter INT) "
+ "RETURNS TIMESTAMP " + "CONTAINS SQL " + "BEGIN ATOMIC " + "IF flag THEN " + "RETURN ts; "
+ "ELSE " + "IF counter IS NULL THEN SET counter = 0; END IF; " + "SET counter = counter + 1; "
+ "SET ts = val;" + "RETURN NULL; " + "END IF; " + "END ";
+ "RETURNS TIMESTAMP CONTAINS SQL BEGIN ATOMIC IF flag THEN RETURN ts; "
+ "ELSE IF counter IS NULL THEN SET counter = 0; END IF; SET counter = counter + 1; "
+ "SET ts = val; RETURN NULL; END IF; END";
}

private String getFirstTimestamp() {
return "CREATE AGGREGATE FUNCTION First(IN val TIMESTAMP, IN flag boolean, INOUT ts TIMESTAMP , INOUT counter INT) "
+ "RETURNS TIMESTAMP " + "CONTAINS SQL " + "BEGIN ATOMIC " + "IF flag THEN " + "RETURN ts; "
+ "ELSE " + "IF counter IS NULL THEN SET counter = 0; END IF; " + "SET counter = counter + 1; "
+ " IF counter = 1 THEN " + " SET ts = val; END IF; " + "RETURN NULL; " + "END IF; " + "END ";
+ "RETURNS TIMESTAMP CONTAINS SQL BEGIN ATOMIC IF flag THEN RETURN ts; "
+ "ELSE IF counter IS NULL THEN SET counter = 0; END IF; SET counter = counter + 1; "
+ " IF counter = 1 THEN SET ts = val; END IF; RETURN NULL; END IF; END ";
}

private void addFunction(String functionName, String methodName, String returnType, String... parTypes) {
StringBuilder funDef = new StringBuilder();
private void addFunction(String _functionName, String _javaMethodName, String _returnType, String... _paramTypes) {
if (DBReference.is2xx()) {
funDef.append("CREATE FUNCTION ").append(functionName).append("(");
String comma = "";
for (int i = 0; i < parTypes.length; i++) {
funDef.append(comma).append("par").append(i).append(" ").append(parTypes[i]);
comma = ",";
}
funDef.append(")");
funDef.append(" RETURNS ");
funDef.append(returnType);
funDef.append(" LANGUAGE JAVA DETERMINISTIC NO SQL EXTERNAL NAME 'CLASSPATH:");
funDef.append(methodName).append("'");
functionDefinitions.add(new StringBuilder()
.append("CREATE FUNCTION ")
.append(_functionName)
.append("(")
.append(IntStream.range(0, _paramTypes.length).mapToObj(i -> "par" + i + " " + _paramTypes[i]).collect(Collectors.joining(", ")))
.append(")")
.append(" RETURNS ")
.append(_returnType)
.append(" LANGUAGE JAVA DETERMINISTIC NO SQL EXTERNAL NAME 'CLASSPATH:")
.append(_javaMethodName)
.append("'")
.toString());
} else {
funDef.append("CREATE ALIAS ").append(functionName).append(" FOR \"").append(methodName).append("\"");
functionDefinitions.add(new StringBuilder()
.append("CREATE ALIAS ")
.append(_functionName)
.append(" FOR \"")
.append(_javaMethodName)
.append("\"")
.toString());
}
functionsDefinition.add(funDef.toString());
}

private void addFunctions(Class<?> clazz, boolean cswitch) throws SQLException {
Method[] mths = clazz.getDeclaredMethods();
private void addFunctions(Class<?> _clazz, boolean _cswitch) throws SQLException {
Method[] mths = _clazz.getDeclaredMethods();
Map<String, String> tmap = TypesMap.getAccess2HsqlTypesMap();
for (Method mth : mths) {
Annotation[] ants = mth.getAnnotations();
for (Annotation ant : ants) {
if (ant.annotationType().equals(FunctionType.class)) {
FunctionType ft = (FunctionType) ant;
String methodName = clazz.getName() + "." + mth.getName();
String methodName = _clazz.getName() + "." + mth.getName();
String functionName = ft.functionName();
if (functionName == null) {
functionName = methodName;
Expand Down Expand Up @@ -122,7 +131,7 @@ private void addFunctions(Class<?> clazz, boolean cswitch) throws SQLException {
}
}
createFunctions();
if (cswitch) {
if (_cswitch) {
createSwitch();
}
}
Expand All @@ -148,17 +157,16 @@ private void resetDefault() {
}

private void createFunctions() {
for (String functionDef : functionsDefinition) {
for (String functionDef : functionDefinitions) {

try {
exec(functionDef, true);
} catch (SQLException e) {
e.printStackTrace();
Logger.logWarning(LoggerMessageEnum.FUNCTION_ALREADY_ADDED, functionDef);
} catch (SQLException _ex) {
Logger.logWarning(LoggerMessageEnum.FAILED_TO_CREATE_FUNCTION, functionDef, _ex.toString());
}
}

functionsDefinition.clear();
functionDefinitions.clear();
}

private void createSwitch() {
Expand All @@ -184,18 +192,18 @@ private void createSwitch() {
header.append(") RETURNS").append(type).append(" RETURN").append(body);
try {
exec(header.toString(), true);
} catch (SQLException e) {
Logger.logWarning(LoggerMessageEnum.FUNCTION_ALREADY_ADDED, header.toString());
} catch (SQLException _ex) {
Logger.logWarning(LoggerMessageEnum.FAILED_TO_CREATE_FUNCTION, header.toString(), _ex.toString());
}
}
}

}

private String getAggregate(String type, String fun) {
return "CREATE AGGREGATE FUNCTION " + fun + "(IN val " + type + ", IN flag BOOLEAN, INOUT register "
+ type + ", INOUT counter INT) " + " RETURNS " + type + " NO SQL LANGUAGE JAVA "
+ " EXTERNAL NAME 'CLASSPATH:net.ucanaccess.converters.FunctionsAggregate." + fun + "'";
private String getAggregate(String _functionName, String _type) {
return "CREATE AGGREGATE FUNCTION " + _functionName + "(IN val " + _type + ", IN flag BOOLEAN, INOUT register "
+ _type + ", INOUT counter INT) RETURNS " + _type + " NO SQL LANGUAGE JAVA "
+ "EXTERNAL NAME 'CLASSPATH:net.ucanaccess.converters.FunctionsAggregate." + _functionName + "'";
}

private void loadMappedFunctions() throws SQLException {
Expand Down Expand Up @@ -235,8 +243,8 @@ private final class TablesLoader {
private final List<String> unresolvedTables = new ArrayList<>();
private final List<String> calculatedFieldsTriggers = new ArrayList<>();
private final List<String> loadingOrder = new LinkedList<>();
private final Set<Column> alreadyIndexed = new HashSet<>();
private final Set<String> readOnlyTables = new HashSet<>();
private final Set<Column> alreadyIndexed = new LinkedHashSet<>();
private final Set<String> readOnlyTables = new LinkedHashSet<>();

private String commaSeparated(List<? extends Index.Column> columns, boolean escape) throws SQLException {
String comma = "";
Expand Down Expand Up @@ -322,13 +330,13 @@ private String getCalculatedFieldTrigger(String ntn, Column cl, boolean isCreate

return isCreate
? "CREATE TRIGGER expr%d before insert ON " + ntn + " REFERENCING NEW AS newrow FOR EACH ROW "
+ " BEGIN ATOMIC " + " SET newrow." + ecl + " = " + call + "; END "
+ " BEGIN ATOMIC SET newrow." + ecl + " = " + call + "; END "
: "CREATE TRIGGER expr%d before update ON " + ntn
+ " REFERENCING NEW AS newrow OLD AS OLDROW FOR EACH ROW " + " BEGIN ATOMIC IF %s THEN "
+ " REFERENCING NEW AS newrow OLD AS OLDROW FOR EACH ROW BEGIN ATOMIC IF %s THEN "
+ " SET newrow." + ecl + " = " + call + "; ELSEIF newrow." + ecl + " <> oldrow." + ecl
+ " THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '"
+ Logger.getMessage(LoggerMessageEnum.TRIGGER_UPDATE_CF_ERR) + cl.getName().replace("%", "%%")
+ "'" + "; END IF ; END ";
+ "'; END IF ; END ";
}

private boolean isNumeric(DataType dt) {
Expand Down Expand Up @@ -1464,20 +1472,19 @@ private static boolean hasAutoNumberColumn(Table t) {
return false;
}

public void addFunctions(Class<?> clazz) throws SQLException {
functionsLoader.addFunctions(clazz, false);
public void addFunctions(Class<?> _clazz) throws SQLException {
functionsLoader.addFunctions(_clazz, false);
}

private void exec(String expression, boolean logging) throws SQLException {
private void exec(String _expression, boolean _logging) throws SQLException {
try (Statement st = conn.createStatement()) {

st.executeUpdate(expression);
} catch (SQLException e) {
if (logging && e.getErrorCode() != TablesLoader.HSQL_FK_ALREADY_EXISTS) {
Logger.log("Cannot execute:" + expression + " " + e.getMessage());
st.executeUpdate(_expression);
} catch (SQLException _ex) {
if (_logging && _ex.getErrorCode() != TablesLoader.HSQL_FK_ALREADY_EXISTS) {
Logger.log("Cannot execute:" + _expression + " " + _ex.getMessage());
}

throw e;
throw _ex;
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/net/ucanaccess/jdbc/DBReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ private void setIgnoreCase(Connection conn) {
}
}

private void initHSQLDB(Connection conn) {
try (Statement st = conn.createStatement()) {
private void initHSQLDB(Connection _conn) {
try (Statement st = _conn.createStatement()) {
st.execute("SET DATABASE SQL SYNTAX ora TRUE");
st.execute("SET DATABASE SQL CONCAT NULLS " + concatNulls);
if (lobScale == null && inMemory) {
Expand All @@ -297,8 +297,8 @@ private void initHSQLDB(Connection conn) {
st.execute("SET FILES LOB SCALE " + lobScale);
}

} catch (Exception w) {
w.printStackTrace();
} catch (Exception _ex) {
_ex.printStackTrace();
}
}

Expand Down Expand Up @@ -381,8 +381,8 @@ private String getHsqlUrl(final Session session) throws SQLException {
} else {
finalizeHsqlDb(session);
}
} catch (Exception e) {
e.printStackTrace();
} catch (Exception _ex) {
_ex.printStackTrace();
}
}));
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/net/ucanaccess/jdbc/UcanaccessConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ private void flushIO() throws SQLException {
if (testRollback) {
throw new RuntimeException("PhysicalRollbackTest");
}
} catch (Throwable t) {
t.printStackTrace();
} catch (Throwable _t) {
_t.printStackTrace();
hsqlDBConnection.rollback();
ibal.clear();
Iterator<ICommand> it = executed.descendingIterator();
Expand All @@ -304,10 +304,10 @@ private void flushIO() throws SQLException {
try {
ref.getDbIO().flush();
unloadDB();
} catch (IOException e) {
e.printStackTrace();
} catch (IOException _ex) {
_ex.printStackTrace();
}
throw new UcanaccessSQLException(t);
throw new UcanaccessSQLException(_t);
}
try {
ref.getDbIO().flush();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/ucanaccess/log/LoggerMessageEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public enum LoggerMessageEnum implements LoggerResourceMessage {
STATEMENT_DDL,
CONSTRAINT,
LOBSCALE,
FUNCTION_ALREADY_ADDED,
FAILED_TO_CREATE_FUNCTION,
NO_SELECT
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CONCURRENT_PROCESS_ACCESS=File marked as read only. Notice that only one process
NOT_A_VALID_PASSWORD=Password authentication failed.
INVALID_MONTH_NUMBER=Invalid Month Number
UNPARSABLE_DATE=Unparsable Date.
TABLE_DOES_NOT_EXIST=Table %s does not exist .
TABLE_DOES_NOT_EXIST=Table %s does not exist.
INVALID_JACKCESS_OPENER=Jackess Opener class must implement net.ucanaccess.jdbc.JackcessOpener
ONLY_IN_MEMORY_ALLOWED=If you specify a custom JackcessOpener only the in memory mode is allowed
COMPLEX_TYPE_UNSUPPORTED=Complex Type not supported yet
Expand All @@ -27,6 +27,6 @@ CONSTRAINT=Detected %s constraint breach, table %s, record %s: making the table
LOBSCALE=Lobscale value must equal at least one of the following values:1,2,4,8,16,32. Skipping.
ACCESS_97=Access 97 is supported in read-only.
PARAMETER_NULL=Parameter %s mustn't be null.
FUNCTION_ALREADY_ADDED=Function already added: %s
FAILED_TO_CREATE_FUNCTION=Failed to create function '%s': %s
NO_SELECT=Please, don't use executeQuery method to execute INSERT, DELETE or UPDATE SQL statement(it isn't correct). Use execute or executeUpdate methods instead
DEFAULT_NEEDED=When adding a new column not null(%s), you must specify a default because the table %s already contains one or more records(%d records)
16 changes: 8 additions & 8 deletions src/test/java/net/ucanaccess/test/integration/DropTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@ class DropTableTest extends UcanaccessBaseTest {
@Override
protected void init(AccessVersion _accessVersion) throws SQLException {
super.init(_accessVersion);
executeStatements("CREATE TABLE AAAn ( baaaa TEXT(3) PRIMARY KEY,A INTEGER , C TEXT(4)) ",
"CREATE TABLE [AAA n] ( baaaa TEXT(3) PRIMARY KEY,A INTEGER , C TEXT(4)) ");
executeStatements("CREATE TABLE AAAn (baaaa TEXT(3) PRIMARY KEY, A INTEGER, C TEXT(4))",
"CREATE TABLE [AAA n] (baaaa TEXT(3) PRIMARY KEY, A INTEGER, C TEXT(4))");
}

void createSimple(String _tableName, String a, Object[][] ver) throws SQLException, IOException {
void createSimple(String _tableName, String a, Object[][] ver) throws SQLException {
try (Statement st = ucanaccess.createStatement()) {
st.execute("INSERT INTO " + _tableName + " VALUES ('33A',11,'" + a + "' )");
st.execute("INSERT INTO " + _tableName + " VALUES ('33B',111,'" + a + "' )");
st.execute("INSERT INTO " + _tableName + " VALUES ('33A', 11,'" + a + "')");
st.execute("INSERT INTO " + _tableName + " VALUES ('33B',111,'" + a + "')");
checkQuery("SELECT * FROM " + _tableName + " ORDER BY c", ver);
}
}

@ParameterizedTest(name = "[{index}] {0}")
@EnumSource(value = AccessVersion.class)
void testDrop(AccessVersion _accessVersion) throws SQLException, IOException {
void testDrop(AccessVersion _accessVersion) throws SQLException {
init(_accessVersion);

// ucanaccess.setAutoCommit(false);
createSimple("AAAn", "a", new Object[][] {{"33A", 11, "a"}, {"33B", 111, "a"}});
try (Statement st = ucanaccess.createStatement()) {
st.executeUpdate("DROP TABLE AAAn");
// ucanaccess.commit();
st.execute("CREATE TABLE AAAn ( baaaa TEXT(3) PRIMARY KEY,A INTEGER , C TEXT(4)) ");
st.execute("CREATE TABLE AAAn (baaaa TEXT(3) PRIMARY KEY, A INTEGER, C TEXT(4))");
createSimple("AAAn", "b", new Object[][] {{"33A", 11, "b"}, {"33B", 111, "b"}});
dumpQueryResult("SELECT * FROM AAAn");
ucanaccess.commit();
Expand All @@ -53,7 +53,7 @@ void testDropBlank(AccessVersion _accessVersion) throws SQLException, IOExceptio
try (Statement st = ucanaccess.createStatement()) {
st.executeUpdate("DROP TABLE [AAA n]");
// ucanaccess.commit();
st.execute("CREATE TABLE [AAA n] ( baaaa TEXT(3) PRIMARY KEY,A INTEGER , C TEXT(4)) ");
st.execute("CREATE TABLE [AAA n] (baaaa TEXT(3) PRIMARY KEY, A INTEGER, C TEXT(4))");
createSimple("[AAA n]", "b", new Object[][] {{"33A", 11, "b"}, {"33B", 111, "b"}});
dumpQueryResult("SELECT * FROM [AAA n]");
ucanaccess.commit();
Expand Down
Loading

0 comments on commit 0759441

Please sign in to comment.