Improve to share Jdbc3KeyGenerator and NoKeyGenerator instance #882

Closed
kazuki43zoo opened this Issue Jan 3, 2017 · 0 comments

Comments

Projects
None yet
1 participant
@kazuki43zoo
Member

kazuki43zoo commented Jan 3, 2017

The Jdbc3KeyGenerator and NoKeyGenerator can be share an instance because it hasn't state.

I suggest to share an instance as follow:

e.g) Jdbc3KeyGenerator

public class Jdbc3KeyGenerator implements KeyGenerator {
  public static final Jdbc3KeyGenerator INSTANCE = new Jdbc3KeyGenerator(); // Add constant
  // ...
}

e.g) MapperAnnotationBuilder

if (SqlCommandType.INSERT.equals(sqlCommandType) || SqlCommandType.UPDATE.equals(sqlCommandType)) {
  SelectKey selectKey = method.getAnnotation(SelectKey.class);
  if (selectKey != null) {
    keyGenerator = handleSelectKeyAnnotation(selectKey, mappedStatementId, getParameterType(method), languageDriver);
    keyProperty = selectKey.keyProperty();
  } else if (options == null) {
    keyGenerator = configuration.isUseGeneratedKeys() ? new Jdbc3KeyGenerator() : new NoKeyGenerator();
  } else {
    keyGenerator = options.useGeneratedKeys() ? new Jdbc3KeyGenerator() : new NoKeyGenerator();
    keyProperty = options.keyProperty();
    keyColumn = options.keyColumn();
  }
} else {
  keyGenerator = new NoKeyGenerator();
}

if (SqlCommandType.INSERT.equals(sqlCommandType) || SqlCommandType.UPDATE.equals(sqlCommandType)) {
  SelectKey selectKey = method.getAnnotation(SelectKey.class);
  if (selectKey != null) {
    keyGenerator = handleSelectKeyAnnotation(selectKey, mappedStatementId, getParameterType(method), languageDriver);
    keyProperty = selectKey.keyProperty();
  } else if (options == null) {
    keyGenerator = configuration.isUseGeneratedKeys() ? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
  } else {
    keyGenerator = options.useGeneratedKeys() ? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
    keyProperty = options.keyProperty();
    keyColumn = options.keyColumn();
  }
} else {
  keyGenerator = NoKeyGenerator.INSTANCE;
}

@kazuki43zoo kazuki43zoo self-assigned this Jan 3, 2017

@kazuki43zoo kazuki43zoo added this to the 3.4.3 milestone Jan 3, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment