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
Merged

Conversation

abel533
Copy link
Contributor

@abel533 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
Copy link
Contributor Author

abel533 commented Jan 10, 2019

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

@kazuki43zoo
Copy link
Member

kazuki43zoo commented Mar 9, 2019

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

@kazuki43zoo
Copy link
Member

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
Copy link
Member

harawata commented Mar 9, 2019

@kazuki43zoo ,
I'll look into it.

Copy link
Member

@harawata harawata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

abel533 and others added 6 commits March 16, 2019 22:26
Resolved warnings, updated license years and reformatted some lines.
… Configuration

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

@abel533 , Thank you for the updates!

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

Copy link
Member

@kazuki43zoo kazuki43zoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@harawata
I've added trivial comments.

@kazuki43zoo kazuki43zoo added the enhancement Improve a feature or add a new feature label Mar 19, 2019
@harawata
Copy link
Member

@kazuki43zoo , Good catch! Thanks a lot!

@abel533
Copy link
Contributor Author

abel533 commented Aug 11, 2021

Based on current PR, new mybatis-mapper released 1.0.0 🎉🎉🎉
https://github.com/mybatis-mapper/mapper

pulllock pushed a commit to pulllock/mybatis-3 that referenced this pull request Oct 19, 2023
Add LanguageDriver to ProviderSqlSource
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improve a feature or add a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants