Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LanguageDriver to ProviderSqlSource #1391

Merged
merged 9 commits into from Mar 19, 2019

Conversation

Projects
None yet
3 participants
@abel533
Copy link
Contributor

abel533 commented Dec 3, 2018

First, I closed several similar PRs, such as #1229, #1226, #1120, #1111

The new PR reduces the content of the changes.

The current changes are more in line with the definition of LanguageDriver.

  /**
   * Creates an {@link SqlSource} that will hold the statement read from an annotation.
   * It is called during startup, when the mapped statement is read from a class or an xml file.
   * 
   * @param configuration The MyBatis configuration
   * @param script The content of the annotation
   * @param parameterType input parameter type got from a mapper method or specified in the parameterType xml attribute. Can be null.
   * @return 
   */
  SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType);

in MapperAnnotationBuilder:

  private SqlSource getSqlSourceFromAnnotations(Method method, Class<?> parameterType, LanguageDriver languageDriver) {
    try {
      Class<? extends Annotation> sqlAnnotationType = getSqlAnnotationType(method);
      Class<? extends Annotation> sqlProviderAnnotationType = getSqlProviderAnnotationType(method);
      if (sqlAnnotationType != null) {
        if (sqlProviderAnnotationType != null) {
          throw new BindingException("You cannot supply both a static SQL and SqlProvider to method named " + method.getName());
        }
        Annotation sqlAnnotation = method.getAnnotation(sqlAnnotationType);
        //languageDriver!!!!!!!!!
        final String[] strings = (String[]) sqlAnnotation.getClass().getMethod("value").invoke(sqlAnnotation);
        return buildSqlSourceFromStrings(strings, parameterType, languageDriver);
      } else if (sqlProviderAnnotationType != null) {
        Annotation sqlProviderAnnotation = method.getAnnotation(sqlProviderAnnotationType);
        //languageDriver!!!!!!!!!
        return new ProviderSqlSource(assistant.getConfiguration(), sqlProviderAnnotation, type, method, languageDriver);
      }
      return null;
    } catch (Exception e) {
      throw new BuilderException("Could not find value method on SQL annotation.  Cause: " + e, e);
    }
  }

When other LanguageDriver uses the Provider method, you can add @Lang(XMLLanguageDriver.class) to solve.

This is the last PR for ProviderSqlSource. I hope everyone will consider it carefully.

@abel533

This comment has been minimized.

Copy link
Contributor Author

abel533 commented Jan 10, 2019

This function is very useful. Does everyone have any ideas?

@kazuki43zoo

This comment has been minimized.

Copy link
Member

kazuki43zoo commented Mar 9, 2019

@harawata I am interested in this PR, what do you think?

@kazuki43zoo

This comment has been minimized.

Copy link
Member

kazuki43zoo commented Mar 9, 2019

If this PR will merged, I can support read an SQL template file by rule-based on mybatis-thymeleaf as follow:

  @Lang(ThymeleafLanguageDriver.class)
  @SelectProvider(type = ThymeleafSqlTemplatePathProvider.class, method = "provide")
  Name findById(int id);

  class ThymeleafSqlTemplatePathProvider {
    String provide(ProviderContext context) {
      Class<?> type = context.getMapperType();
      Method method = context.getMapperMethod();
      return "/" + type.getName().replaceAll("\\.", "/") + "/" + method.getName() + ".sql";
    }
  }
@harawata

This comment has been minimized.

Copy link
Member

harawata commented Mar 9, 2019

@kazuki43zoo ,
I'll look into it.

@harawata
Copy link
Member

harawata left a comment

Hi, @abel533 !
This version is a lot simpler than the previous ones. Thanks!

abel533 and others added some commits Mar 16, 2019

Cleanup: no logical changes
Resolved warnings, updated license years and reformatted some lines.
Refactoring: moved getLanguageDriver() from MapperBuilderAssistant to…
… Configuration

- In ProviderSqlSource, it is better not to assume the driver is already registered.
- Replaced Class#newInstance() with Constructor#newInstance().
@harawata

This comment has been minimized.

Copy link
Member

harawata commented Mar 19, 2019

@abel533 , Thank you for the updates!

@kazuki43zoo , Could you do the final check and merge if everything is OK?

@kazuki43zoo
Copy link
Member

kazuki43zoo left a comment

@harawata
I've added trivial comments.

@harawata

This comment has been minimized.

Copy link
Member

harawata commented Mar 19, 2019

@kazuki43zoo , Good catch! Thanks a lot!

@kazuki43zoo kazuki43zoo merged commit f738839 into mybatis:master Mar 19, 2019

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

@kazuki43zoo kazuki43zoo added this to the 3.5.1 milestone Mar 19, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.