Skip to content

Commit

Permalink
make MBG pluggable(step 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
ningpp committed Oct 19, 2023
1 parent 863fd08 commit 50de1a8
Show file tree
Hide file tree
Showing 45 changed files with 3,140 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.generator.codegen;

import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.GeneratedKotlinFile;
import org.mybatis.generator.api.GeneratedXmlFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.ProgressCallback;

/**
* Introspected table implementation for generating nothing.
*/
public class IntrospectedTableGenerateNothingImpl extends IntrospectedTable {

public IntrospectedTableGenerateNothingImpl() {
this(TargetRuntime.MYBATIS3_DSQL);
}

public IntrospectedTableGenerateNothingImpl(TargetRuntime targetRuntime) {
super(targetRuntime);
}

@Override
public void calculateGenerators(List<String> warnings, ProgressCallback progressCallback) {
//no generator
}

@Override
public List<GeneratedJavaFile> getGeneratedJavaFiles() {
return new ArrayList<>(0);
}

@Override
public List<GeneratedXmlFile> getGeneratedXmlFiles() {
return new ArrayList<>(0);
}

@Override
public List<GeneratedKotlinFile> getGeneratedKotlinFiles() {
return new ArrayList<>(0);
}

@Override
public int getGenerationSteps() {
return 0;
}

@Override
public boolean requiresXMLGenerator() {
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.generator.codegen;

public class IntrospectedTableGenerateNothingMyBatis3Impl extends IntrospectedTableGenerateNothingImpl {

public IntrospectedTableGenerateNothingMyBatis3Impl() {
super(TargetRuntime.MYBATIS3);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.generator.plugins;

import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.GeneratedKotlinFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.kotlin.KotlinFile;
import org.mybatis.generator.codegen.AbstractKotlinGenerator;
import org.mybatis.generator.config.PropertyRegistry;

public abstract class AbstracKotlinGeneratorPlugin extends AbstractGeneratorPlugin {

@Override
public abstract AbstractKotlinGenerator getGenerator(IntrospectedTable introspectedTable);

@Override
public List<GeneratedKotlinFile> contextGenerateAdditionalKotlinFiles(IntrospectedTable introspectedTable) {
AbstractKotlinGenerator generator = getGenerator(introspectedTable);
if (generator == null) {
return new ArrayList<>(0);
}
initGenerator(generator, introspectedTable);

List<GeneratedKotlinFile> answer = new ArrayList<>();
List<KotlinFile> kotlinFiles = generator.getKotlinFiles();
for (KotlinFile kotlinFile : kotlinFiles) {
GeneratedKotlinFile gjf = new GeneratedKotlinFile(kotlinFile,
getProject(),
context.getProperty(PropertyRegistry.CONTEXT_KOTLIN_FILE_ENCODING),
context.getKotlinFormatter());
answer.add(gjf);
}
return answer;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.generator.plugins;

import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.ProgressCallback;
import org.mybatis.generator.codegen.AbstractGenerator;

public abstract class AbstractGeneratorPlugin extends PluginAdapter {

public static final String TARGET_PROJECT_PROPERTY = "targetProject";

public static final ProgressCallback NULL_PROGRESS_CALLBACK = new ProgressCallback() {};

@Override
public boolean validate(List<String> warnings) {
return true;
}

public abstract AbstractGenerator getGenerator(IntrospectedTable introspectedTable);

protected String getProject() {
return properties.getProperty(TARGET_PROJECT_PROPERTY);
}

protected void initGenerator(AbstractGenerator generator,
IntrospectedTable introspectedTable) {
generator.setContext(context);
generator.setIntrospectedTable(introspectedTable);
generator.setProgressCallback(NULL_PROGRESS_CALLBACK);
generator.setWarnings(new ArrayList<>(0));
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.generator.plugins;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.codegen.AbstractJavaClientGenerator;

public abstract class AbstractJavaClientGeneratorPlugin
extends AbstractJavaGeneratorPlugin {

@Override
public AbstractJavaClientGenerator getGenerator(
IntrospectedTable introspectedTable) {
if (!introspectedTable.getRules().generateJavaClient()
|| context.getJavaClientGeneratorConfiguration() == null) {
return null;
}
return getClientGenerator(introspectedTable);
}

public String getType() {
if (context.getJavaClientGeneratorConfiguration() == null) {
return null;
}
return context.getJavaClientGeneratorConfiguration().getConfigurationType();
}

public abstract AbstractJavaClientGenerator getClientGenerator(IntrospectedTable introspectedTable);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.generator.plugins;

import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.CompilationUnit;
import org.mybatis.generator.codegen.AbstractJavaGenerator;
import org.mybatis.generator.config.PropertyRegistry;

public abstract class AbstractJavaGeneratorPlugin extends AbstractGeneratorPlugin {

@Override
public abstract AbstractJavaGenerator getGenerator(IntrospectedTable introspectedTable);

@Override
public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
AbstractJavaGenerator generator = getGenerator(introspectedTable);
if (generator == null) {
return new ArrayList<>(0);
}
initGenerator(generator, introspectedTable);

List<GeneratedJavaFile> answer = new ArrayList<>();
List<CompilationUnit> compilationUnits = generator.getCompilationUnits();
for (CompilationUnit compilationUnit : compilationUnits) {
GeneratedJavaFile gjf = new GeneratedJavaFile(compilationUnit,
getProject(),
context.getProperty(PropertyRegistry.CONTEXT_JAVA_FILE_ENCODING),
context.getJavaFormatter());
answer.add(gjf);
}
return answer;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.generator.plugins;

import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.GeneratedXmlFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.codegen.AbstractJavaClientGenerator;
import org.mybatis.generator.codegen.AbstractXmlGenerator;

public abstract class AbstractXmlGeneratorPlugin extends AbstractGeneratorPlugin {

private ClientCompositePlugin clientPlugin;

public AbstractXmlGeneratorPlugin(ClientCompositePlugin clientPlugin) {
super();
this.clientPlugin = clientPlugin;
}

public ClientCompositePlugin getClientPlugin() {
return clientPlugin;
}

public void setClientPlugin(MyBatis3ClientCompositePlugin clientPlugin) {
this.clientPlugin = clientPlugin;
}

public abstract AbstractXmlGenerator getXmlGenerator(IntrospectedTable introspectedTable);

@Override
public List<GeneratedXmlFile> contextGenerateAdditionalXmlFiles(IntrospectedTable introspectedTable) {
AbstractXmlGenerator generator = getGenerator(introspectedTable);
if (generator == null) {
return new ArrayList<>(0);
}
initGenerator(generator, introspectedTable);

List<GeneratedXmlFile> answer = new ArrayList<>();
Document document = generator.getDocument();
GeneratedXmlFile gxf = new GeneratedXmlFile(document,
introspectedTable.getMyBatis3XmlMapperFileName(),
introspectedTable.getMyBatis3XmlMapperPackage(),
getProject(), true, context.getXmlFormatter());
if (context.getPlugins().sqlMapGenerated(gxf, introspectedTable)) {
answer.add(gxf);
}
return answer;
}

@Override
public AbstractXmlGenerator getGenerator(
IntrospectedTable introspectedTable) {
AbstractXmlGenerator xmlMapperGenerator;
if (getClientPlugin() != null) {
AbstractJavaClientGenerator javaClientGenerator = getClientPlugin().getClientGenerator(introspectedTable);
if (javaClientGenerator == null) {
if (context.getSqlMapGeneratorConfiguration() != null) {
xmlMapperGenerator = getXmlGenerator(introspectedTable);
} else {
xmlMapperGenerator = null;
}
} else {
xmlMapperGenerator = javaClientGenerator.getMatchedXMLGenerator();
}
} else {
xmlMapperGenerator = getXmlGenerator(introspectedTable);
}
return xmlMapperGenerator;
}

}

0 comments on commit 50de1a8

Please sign in to comment.