Skip to content

Commit

Permalink
Continued work on persistent keys
Browse files Browse the repository at this point in the history
  • Loading branch information
safris committed Jul 21, 2023
1 parent e910e10 commit b0b88b2
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 122 deletions.
22 changes: 21 additions & 1 deletion jsql/src/main/java/org/jaxdb/jsql/OnModify.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,32 @@

package org.jaxdb.jsql;

/**
* Interface used define runtime actions to be made on {@link data.Column}s upon modification.
*
* @param <T> The type parameter of the {@link data.Table}.
*/
public interface OnModify<T extends data.Table> {
default void change(T table) {
/**
* Called when a change is made to the "cur" value of the {@link data.Column} in context.
*
* @param table A reference to {@code Table.this} for the {@link data.Column} in context of the caller.
*/
default void changeCur(T table) {
}

/**
* Called when a change is made to the "old" value of the {@link data.Column} in context.
*
* @param table A reference to {@code Table.this} for the {@link data.Column} in context of the caller.
*/
default void changeOld(T table) {
}

/**
* Called when an UPDATE to the {@link data.Column} in context is committed in lieu of NOTIFY.
*
* @param table A reference to {@code Table.this} for the {@link data.Column} in context of the caller.
*/
void update(T table);
}
10 changes: 5 additions & 5 deletions jsql/src/main/java/org/jaxdb/jsql/generator/ForeignRelation.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ abstract class ForeignRelation extends Relation {
final String cacheMapFieldNameForeign;
final String declarationNameForeign;

ForeignRelation(final String schemaClassName, final TableModel sourceTable, final TableModel tableModel, final ColumnModels columns, final TableModel referenceTable, final ColumnModels referenceColumns, final IndexType indexType, final IndexType indexTypeForeign, final KeyModels keyModels) {
super(schemaClassName, sourceTable, tableModel, columns, indexType, keyModels);
ForeignRelation(final String schemaClassName, final TableModel sourceTable, final TableModel tableModel, final ColumnModels columns, final TableModel referenceTable, final ColumnModels referenceColumns, final IndexType indexType, final IndexType indexTypeForeign) {
super(schemaClassName, sourceTable, tableModel, columns, indexType, tableModel.keyModels);
this.indexTypeForeign = indexTypeForeign;
this.referenceTable = referenceTable;
this.referenceColumns = referenceColumns;
Expand Down Expand Up @@ -65,16 +65,16 @@ String writeDeclaration(final String classSimpleName, final HashSet<String> decl
}

final String writeDeclaration(final String classSimpleName, final String typeName, final String declaredName, final String suffix, final HashSet<String> declared, final String comment) {
final String keyClause = keyModel.keyClause(referenceTable.singletonInstanceName, foreignName, referenceTable.classCase, classSimpleName, CurOld.Old, false, declared, comment);
final String keyClause = keyModel.keyRefArgsInternal(referenceTable.singletonInstanceName, foreignName, referenceTable.classCase, classSimpleName, CurOld.Old, false, declared, comment);
if (keyClause == null)
return null;

final StringBuilder out = new StringBuilder();
out.append("\n public final ").append(typeName).append(' ').append(fieldName).append("_CACHED() { // ForeignRelation.writeDeclaration(String,String,String,String)");
out.append("\n public final ").append(typeName).append(' ').append(fieldName).append("_CACHED() {");
out.append("\n final ").append(CacheMap.class.getName()).append('<').append(declaredName).append("> cache = ").append(referenceTable.singletonInstanceName).append('.').append(cacheMapFieldNameForeign).append(';');
out.append("\n return cache == null ? null : cache.get").append(suffix).append("(").append(keyClause).append(");");
out.append("\n }\n");
out.append("\n public final ").append(typeName).append(' ').append(fieldName).append("_SELECT() throws ").append(IOException.class.getName()).append(", ").append(SQLException.class.getName()).append(" { // ForeignRelation.writeDeclaration(String,String,String,String)");
out.append("\n public final ").append(typeName).append(' ').append(fieldName).append("_SELECT() throws ").append(IOException.class.getName()).append(", ").append(SQLException.class.getName()).append(" {");
out.append("\n final ").append(CacheMap.class.getName()).append('<').append(declaredName).append("> cache = ").append(referenceTable.singletonInstanceName).append('.').append(cacheMapFieldNameForeign).append(';');
out.append("\n return cache == null ? null : cache.select").append(suffix).append("(").append(keyClause).append(");");
out.append("\n }");
Expand Down
85 changes: 29 additions & 56 deletions jsql/src/main/java/org/jaxdb/jsql/generator/KeyModels.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class KeyModel {
private final String fromArgs;
private final String toArgs;

private final LinkedHashSet<String>[] resets = new LinkedHashSet[] {new LinkedHashSet<>(), new LinkedHashSet<>()};

KeyModel(final boolean isForeign, final String singletonInstanceName, final String cacheMethodName, final String toTableRefName, final ColumnModels columns, final IndexType indexType) {
this.isForeign = isForeign;
this.cacheMethodName = cacheMethodName;
Expand Down Expand Up @@ -87,19 +89,9 @@ class KeyModel {
}
}

private final String[] declare = new String[2];
private final boolean[] declareCalled = new boolean[2];

private final String[] reset = new String[2];
private final boolean[] resetCalled = new boolean[2];

String getReset(final CurOld curlOld) {
final String reset = this.reset[curlOld.ordinal()];
if (reset == null)
return null;

resetCalled[curlOld.ordinal()] = true;
return reset;
void writeReset(final StringBuilder b, final CurOld curlOld) {
for (final String reset : this.resets[curlOld.ordinal()])
b.append("\n ").append(reset);
}

void audit() {
Expand All @@ -109,74 +101,55 @@ void audit() {
// if (this.declare[1] != null && !this.declareCalled[1])
// throw new IllegalStateException();

if (this.reset[0] != null && !this.resetCalled[0])
throw new IllegalStateException();
// if (this.reset[0] != null && !this.resetCalled[0])
// throw new IllegalStateException();

// if (this.reset[1] != null && !this.resetCalled[1])
// throw new IllegalStateException();
}

private String include(final String fieldName, final String name, final String args, final CurOld curlOld) {
final String declare = " private " + data.Key.class.getCanonicalName() + " " + name + ";\n\n " + data.Key.class.getCanonicalName() + " " + name + "() {\n return " + name + " == null ? " + name + " = " + data.Key.class.getCanonicalName() + ".with(" + fieldName + ", " + args + ") : " + name + ";\n }\n";
if (curlOld == null) { // Means that resetting the key is not supported (i.e. it is a MutableKey)
declarations.put(declare, () -> {});
}
else {
if (this.declare[curlOld.ordinal()] == null) {
this.declare[curlOld.ordinal()] = declare;
if (this.declareCalled[curlOld.ordinal()])
throw new IllegalStateException();

declarations.put(declare, () -> declareCalled[curlOld.ordinal()] = true);
}
else if (!this.declare[curlOld.ordinal()].equals(declare)) {
// throw new IllegalStateException(this.declare[curlOld.ordinal()] + "\n" + declare);
}

final String reset = "self." + name + " = null;";
if (this.reset[curlOld.ordinal()] == null) {
this.reset[curlOld.ordinal()] = reset;
if (this.resetCalled[curlOld.ordinal()])
throw new IllegalStateException();
}
else if (!this.reset[curlOld.ordinal()].equals(reset)) {
// throw new IllegalStateException(this.reset[curlOld.ordinal()] + "\n" + reset);
}
}

return name + "()";
}

String keyArgs(final HashSet<String> declared) {
String keyArgsExternal(final HashSet<String> declared) {
if (keyArgs == null)
return null;

if (!declared.add(indexFieldName))
return null;

// if (isForeign)
return data.Key.class.getCanonicalName() + ".with(" + indexFieldName + ", " + keyArgs + ")";

// return include(indexFieldName, "_" + ColumnModels.getInstanceNameForCache(cacheMethodName, toTableRefName) + "_Key$", keyArgs, null);
return data.Key.class.getCanonicalName() + ".with(" + indexFieldName + ", " + keyArgs + ")";
}

String rangeArgs() {
String keyArgsRange() {
if (toArgs == null)
return null;

return data.Key.class.getCanonicalName() + ".with(" + indexFieldName + ", " + fromArgs + "), " + data.Key.class.getCanonicalName() + ".with(" + indexFieldName + ", " + toArgs + ")";
}

String keyClause(final String cacheSingletonName, final String cacheMethodName, final String toTableRefName, final String replace1, final CurOld curlOld, final boolean addSelfRef, final HashSet<String> declared, final String comment) {
private String include(final String fieldName, final String name, final String args, final CurOld curOld) {
final String declare = " private " + data.Key.class.getCanonicalName() + " " + name + ";\n\n " + data.Key.class.getCanonicalName() + " " + name + "() {\n return " + name + " == null ? " + name + " = " + data.Key.class.getCanonicalName() + ".with(" + fieldName + ", " + args + ") : " + name + ";\n }\n";
if (curOld == null) { // Means that resetting the key is not supported (i.e. it is a MutableKey)
declarations.put(declare, () -> {});
}
else {
declarations.put(declare, () -> {});

final String reset = "self." + name + " = null;";
resets[curOld.ordinal()].add(reset);
}

return name + "()";
}

String keyRefArgsInternal(final String cacheSingletonName, final String cacheMethodName, final String toTableRefName, final String replace1, final CurOld curlOld, final boolean addSelfRef, final HashSet<String> declared, final String comment) {
final String args = keyClauseValues.replace("{1}", replace1).replace("{2}", curlOld.toString());
final String cacheColumnsRef = cacheSingletonName + "._" + ColumnModels.getInstanceNameForCache(cacheMethodName, toTableRefName) + "Index$";
if (!declared.add(cacheColumnsRef + ":" + args))
return null;

// if (isForeign) {
// if (isForeign)
return data.Key.class.getCanonicalName() + ".with(" + cacheColumnsRef + ", " + args + ")";
// }

// FIXME: Uncomment to continue work on persistent keys
// final String argsXX = keyClauseValues.replace("{1}.this.", "").replace("{2}", curlOld.toString());
// final String xxx = argsXX.replace(".get" + curlOld, "").replace("()", "_").replace(", ", "");
// final String prefix = "_" + ColumnModels.getInstanceNameForCache(cacheMethodName, xxx) + "ON_" + toTableRefName + curlOld;
Expand Down Expand Up @@ -204,7 +177,7 @@ public int hashCode() {

private final LinkedHashMap<String,Runnable> declarations;

KeyModels(int initialCapacity) {
KeyModels(final int initialCapacity) {
super(initialCapacity);
this.declarations = new LinkedHashMap<>(initialCapacity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
import java.util.HashSet;

class ManyToManyRelation extends ForeignRelation {
ManyToManyRelation(final String schemaClassName, final TableModel sourceTable, final TableModel tableModel, final ColumnModels columns, final TableModel referenceTable, final ColumnModels referenceColumns, final IndexType indexType, final IndexType indexTypeForeign, final KeyModels keyModels) {
super(schemaClassName, sourceTable, tableModel, columns, referenceTable, referenceColumns, indexType, indexTypeForeign, keyModels);
ManyToManyRelation(final String schemaClassName, final TableModel sourceTable, final TableModel tableModel, final ColumnModels columns, final TableModel referenceTable, final ColumnModels referenceColumns, final IndexType indexType, final IndexType indexTypeForeign) {
super(schemaClassName, sourceTable, tableModel, columns, referenceTable, referenceColumns, indexType, indexTypeForeign);
}

@Override
String writeCacheInsert(final String classSimpleName, final CurOld curOld, final boolean addSelfRef, final HashSet<String> declared, final String comment) {
final String keyClause = keyModel.keyClause(referenceTable.singletonInstanceName, foreignName, referenceTable.classCase, classSimpleName, curOld, addSelfRef, declared, comment);
final String keyClause = keyModel.keyRefArgsInternal(referenceTable.singletonInstanceName, foreignName, referenceTable.classCase, classSimpleName, curOld, addSelfRef, declared, comment);
if (keyClause == null)
return null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import java.util.HashSet;

class OneToManyRelation extends ForeignRelation {
OneToManyRelation(final String schemaClassName, final TableModel sourceTable, final TableModel tableModel, final ColumnModels columns, final TableModel referenceTable, final ColumnModels referenceColumns, final IndexType indexType, final IndexType indexTypeForeign, final KeyModels keyModels) {
super(schemaClassName, sourceTable, tableModel, columns, referenceTable, referenceColumns, indexType, indexTypeForeign, keyModels);
OneToManyRelation(final String schemaClassName, final TableModel sourceTable, final TableModel tableModel, final ColumnModels columns, final TableModel referenceTable, final ColumnModels referenceColumns, final IndexType indexType, final IndexType indexTypeForeign) {
super(schemaClassName, sourceTable, tableModel, columns, referenceTable, referenceColumns, indexType, indexTypeForeign);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package org.jaxdb.jsql.generator;

class OneToOneRelation extends ForeignRelation {
OneToOneRelation(final String schemaClassName, final TableModel sourceTable, final TableModel tableModel, final ColumnModels columns, final TableModel referenceTable, final ColumnModels referenceColumns, final IndexType indexType, final IndexType indexTypeForeign, final KeyModels keyModels) {
super(schemaClassName, sourceTable, tableModel, columns, referenceTable, referenceColumns, indexType, indexTypeForeign, keyModels);
OneToOneRelation(final String schemaClassName, final TableModel sourceTable, final TableModel tableModel, final ColumnModels columns, final TableModel referenceTable, final ColumnModels referenceColumns, final IndexType indexType, final IndexType indexTypeForeign) {
super(schemaClassName, sourceTable, tableModel, columns, referenceTable, referenceColumns, indexType, indexTypeForeign);
}

@Override
Expand Down
Loading

0 comments on commit b0b88b2

Please sign in to comment.