Provides the main classes and interfaces used by clients of MyBatis Generator.
7 | 8 | 9 | -------------------------------------------------------------------------------- /src/org/mybatis/generator/codegen/AbstractGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 The Apache Software Foundation 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.mybatis.generator.codegen; 17 | 18 | import java.util.List; 19 | 20 | import org.mybatis.generator.api.IntrospectedTable; 21 | import org.mybatis.generator.api.ProgressCallback; 22 | import org.mybatis.generator.config.Context; 23 | 24 | /** 25 | * 26 | * @author Jeff Butler 27 | * 28 | */ 29 | public abstract class AbstractGenerator { 30 | protected Context context; 31 | protected IntrospectedTable introspectedTable; 32 | protected Listjava.util.regex.Matcher.replaceAll
method for this function. See
51 | * the documentation of that method for example of the regular expression
52 | * language used in Java.
53 | *
54 | * @author Jeff Butler
55 | *
56 | */
57 | public class ColumnRenamingRule {
58 | private String searchString;
59 | private String replaceString;
60 |
61 | public String getReplaceString() {
62 | return replaceString;
63 | }
64 |
65 | public void setReplaceString(String replaceString) {
66 | this.replaceString = replaceString;
67 | }
68 |
69 | public String getSearchString() {
70 | return searchString;
71 | }
72 |
73 | public void setSearchString(String searchString) {
74 | this.searchString = searchString;
75 | }
76 |
77 | public void validate(Listequals
.
9 | *
10 | * Example use case in a class called Car:
11 | *
12 | * 13 | * public boolean equals(Object that) { 14 | * if (this == that) 15 | * return true; 16 | * if (!(that instanceof Car)) 17 | * return false; 18 | * Car thatCar = (Car) that; 19 | * return EqualsUtil.areEqual(this.fName, that.fName) 20 | * && EqualsUtil.areEqual(this.fNumDoors, that.fNumDoors) 21 | * && EqualsUtil.areEqual(this.fGasMileage, that.fGasMileage) 22 | * && EqualsUtil.areEqual(this.fColor, that.fColor) 23 | * && Arrays.equals(this.fMaintenanceChecks, that.fMaintenanceChecks); //array! 24 | * } 25 | *26 | * 27 | * Arrays are not handled by this class. This is because the 28 | *
Arrays.equals
methods should be used for array fields.
29 | */
30 | public final class EqualsUtil {
31 |
32 | static public boolean areEqual(boolean aThis, boolean aThat) {
33 | return aThis == aThat;
34 | }
35 |
36 | static public boolean areEqual(char aThis, char aThat) {
37 | return aThis == aThat;
38 | }
39 |
40 | static public boolean areEqual(long aThis, long aThat) {
41 | /*
42 | * Implementation Note Note that byte, short, and int are handled by
43 | * this method, through implicit conversion.
44 | */
45 | return aThis == aThat;
46 | }
47 |
48 | static public boolean areEqual(float aThis, float aThat) {
49 | return Float.floatToIntBits(aThis) == Float.floatToIntBits(aThat);
50 | }
51 |
52 | static public boolean areEqual(double aThis, double aThat) {
53 | return Double.doubleToLongBits(aThis) == Double.doubleToLongBits(aThat);
54 | }
55 |
56 | /**
57 | * Possibly-null object field.
58 | *
59 | * Includes type-safe enumerations and collections, but does not include
60 | * arrays. See class comment.
61 | */
62 | static public boolean areEqual(Object aThis, Object aThat) {
63 | return aThis == null ? aThat == null : aThis.equals(aThat);
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/src/org/mybatis/generator/internal/util/StringUtility.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2005 The Apache Software Foundation
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.mybatis.generator.internal.util;
17 |
18 | import java.util.StringTokenizer;
19 |
20 | /**
21 | *
22 | * @author Jeff Butler
23 | */
24 | public class StringUtility {
25 |
26 | /**
27 | * Utility class. No instances allowed
28 | */
29 | private StringUtility() {
30 | super();
31 | }
32 |
33 | public static boolean stringHasValue(String s) {
34 | return s != null && s.length() > 0;
35 | }
36 |
37 | public static String composeFullyQualifiedTableName(String catalog,
38 | String schema, String tableName, char separator) {
39 | StringBuilder sb = new StringBuilder();
40 |
41 | if (stringHasValue(catalog)) {
42 | sb.append(catalog);
43 | sb.append(separator);
44 | }
45 |
46 | if (stringHasValue(schema)) {
47 | sb.append(schema);
48 | sb.append(separator);
49 | } else {
50 | if (sb.length() > 0) {
51 | sb.append(separator);
52 | }
53 | }
54 |
55 | sb.append(tableName);
56 |
57 | return sb.toString();
58 | }
59 |
60 | public static boolean stringContainsSpace(String s) {
61 | return s != null && s.indexOf(' ') != -1;
62 | }
63 |
64 | public static String escapeStringForJava(String s) {
65 | StringTokenizer st = new StringTokenizer(s, "\"", true); //$NON-NLS-1$
66 | StringBuilder sb = new StringBuilder();
67 | while (st.hasMoreTokens()) {
68 | String token = st.nextToken();
69 | if ("\"".equals(token)) { //$NON-NLS-1$
70 | sb.append("\\\""); //$NON-NLS-1$
71 | } else {
72 | sb.append(token);
73 | }
74 | }
75 |
76 | return sb.toString();
77 | }
78 |
79 | public static String escapeStringForXml(String s) {
80 | StringTokenizer st = new StringTokenizer(s, "\"", true); //$NON-NLS-1$
81 | StringBuilder sb = new StringBuilder();
82 | while (st.hasMoreTokens()) {
83 | String token = st.nextToken();
84 | if ("\"".equals(token)) { //$NON-NLS-1$
85 | sb.append("""); //$NON-NLS-1$
86 | } else {
87 | sb.append(token);
88 | }
89 | }
90 |
91 | return sb.toString();
92 | }
93 |
94 | public static boolean isTrue(String s) {
95 | return "true".equalsIgnoreCase(s); //$NON-NLS-1$
96 | }
97 |
98 | public static boolean stringContainsSQLWildcard(String s) {
99 | if (s == null) {
100 | return false;
101 | }
102 |
103 | return s.indexOf('%') != -1 || s.indexOf('_') != -1;
104 | }
105 | }
106 |
--------------------------------------------------------------------------------
/src/org/mybatis/generator/internal/util/messages/Messages.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2006 The Apache Software Foundation
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.mybatis.generator.internal.util.messages;
17 |
18 | import java.text.MessageFormat;
19 | import java.util.MissingResourceException;
20 | import java.util.ResourceBundle;
21 |
22 | /**
23 | * @author Jeff Butler
24 | */
25 | public class Messages {
26 | private static final String BUNDLE_NAME = "org.mybatis.generator.internal.util.messages.messages"; //$NON-NLS-1$
27 |
28 | private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
29 | .getBundle(BUNDLE_NAME);
30 |
31 | private Messages() {
32 | }
33 |
34 | public static String getString(String key) {
35 | try {
36 | return RESOURCE_BUNDLE.getString(key);
37 | } catch (MissingResourceException e) {
38 | return '!' + key + '!';
39 | }
40 | }
41 |
42 | public static String getString(String key, String parm1) {
43 | try {
44 | return MessageFormat.format(RESOURCE_BUNDLE.getString(key),
45 | new Object[] { parm1 });
46 | } catch (MissingResourceException e) {
47 | return '!' + key + '!';
48 | }
49 | }
50 |
51 | public static String getString(String key, String parm1, String parm2) {
52 | try {
53 | return MessageFormat.format(RESOURCE_BUNDLE.getString(key),
54 | new Object[] { parm1, parm2 });
55 | } catch (MissingResourceException e) {
56 | return '!' + key + '!';
57 | }
58 | }
59 |
60 | public static String getString(String key, String parm1, String parm2,
61 | String parm3) {
62 | try {
63 | return MessageFormat.format(RESOURCE_BUNDLE.getString(key),
64 | new Object[] { parm1, parm2, parm3 });
65 | } catch (MissingResourceException e) {
66 | return '!' + key + '!';
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/org/mybatis/generator/logging/AbstractLogFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 The Apache Software Foundation
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.mybatis.generator.logging;
18 |
19 | /**
20 | * Defines the interface for creating Log implementations.
21 | *
22 | * @author Jeff Butler
23 | *
24 | */
25 | public interface AbstractLogFactory {
26 | Log getLog(Class> aClass);
27 | }
28 |
--------------------------------------------------------------------------------
/src/org/mybatis/generator/logging/JdkLoggingImpl.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 The Apache Software Foundation
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.mybatis.generator.logging;
17 |
18 | import java.util.logging.Level;
19 | import java.util.logging.LogRecord;
20 | import java.util.logging.Logger;
21 |
22 | /**
23 | *
24 | * @author Clinton Begin
25 | * @author Jeff Butler
26 | *
27 | */
28 | public class JdkLoggingImpl implements Log {
29 |
30 | private Logger log;
31 |
32 | public JdkLoggingImpl(Class> clazz) {
33 | log = Logger.getLogger(clazz.getName());
34 | }
35 |
36 | public boolean isDebugEnabled() {
37 | return log.isLoggable(Level.FINE);
38 | }
39 |
40 | public void error(String s, Throwable e) {
41 | LogRecord lr = new LogRecord(Level.SEVERE, s);
42 | lr.setSourceClassName(log.getName());
43 | lr.setThrown(e);
44 |
45 | log.log(lr);
46 | }
47 |
48 | public void error(String s) {
49 | LogRecord lr = new LogRecord(Level.SEVERE, s);
50 | lr.setSourceClassName(log.getName());
51 |
52 | log.log(lr);
53 | }
54 |
55 | public void debug(String s) {
56 | LogRecord lr = new LogRecord(Level.FINE, s);
57 | lr.setSourceClassName(log.getName());
58 |
59 | log.log(lr);
60 | }
61 |
62 | public void warn(String s) {
63 | LogRecord lr = new LogRecord(Level.WARNING, s);
64 | lr.setSourceClassName(log.getName());
65 |
66 | log.log(lr);
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/src/org/mybatis/generator/logging/Log.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 The Apache Software Foundation
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.mybatis.generator.logging;
17 |
18 | /**
19 | *
20 | * @author Clinton Begin
21 | */
22 | public interface Log {
23 |
24 | boolean isDebugEnabled();
25 |
26 | void error(String s, Throwable e);
27 |
28 | void error(String s);
29 |
30 | public void debug(String s);
31 |
32 | public void warn(String s);
33 | }
34 |
--------------------------------------------------------------------------------
/src/org/mybatis/generator/logging/Log4jImpl.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 The Apache Software Foundation
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.mybatis.generator.logging;
17 |
18 | import org.apache.log4j.Logger;
19 |
20 | /**
21 | *
22 | * @author Clinton Begin
23 | *
24 | */
25 | public class Log4jImpl implements Log {
26 |
27 | private Logger log;
28 |
29 | public Log4jImpl(Class> clazz) {
30 | log = Logger.getLogger(clazz);
31 | }
32 |
33 | public boolean isDebugEnabled() {
34 | return log.isDebugEnabled();
35 | }
36 |
37 | public void error(String s, Throwable e) {
38 | log.error(s, e);
39 | }
40 |
41 | public void error(String s) {
42 | log.error(s);
43 | }
44 |
45 | public void debug(String s) {
46 | log.debug(s);
47 | }
48 |
49 | public void warn(String s) {
50 | log.warn(s);
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/org/mybatis/generator/logging/LogFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2009 The Apache Software Foundation
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.mybatis.generator.logging;
17 |
18 | import static org.mybatis.generator.internal.util.messages.Messages.getString;
19 |
20 | import org.mybatis.generator.internal.ObjectFactory;
21 |
22 | /**
23 | * Factory for creating loggers. Uses runtime introspection to determine the
24 | * AbstractLogFactory implementation.
25 | *
26 | * @author Jeff Butler
27 | *
28 | */
29 | public class LogFactory {
30 | private static AbstractLogFactory logFactory;
31 |
32 | static {
33 | try {
34 | ObjectFactory.internalClassForName("org.apache.log4j.Logger"); //$NON-NLS-1$
35 | logFactory = new Log4jLoggingLogFactory();
36 | } catch (Exception e) {
37 | logFactory = new JdkLoggingLogFactory();
38 | }
39 | }
40 |
41 | public static Log getLog(Class> clazz) {
42 | try {
43 | return logFactory.getLog(clazz);
44 | } catch (Throwable t) {
45 | throw new RuntimeException(getString("RuntimeError.21", //$NON-NLS-1$
46 | clazz.getName(), t.getMessage()), t);
47 | }
48 | }
49 |
50 | /**
51 | * This method will switch the logging implementation to Java native
52 | * logging. This is useful in situations where you want to use Java native
53 | * logging to log activity but Log4J is on the classpath. Note that
54 | * this method is only effective for log classes obtained after calling this
55 | * method. If you intend to use this method you should call it before
56 | * calling any other method.
57 | */
58 | public static synchronized void forceJavaLogging() {
59 | logFactory = new JdkLoggingLogFactory();
60 | }
61 |
62 | private static class JdkLoggingLogFactory implements AbstractLogFactory {
63 | public Log getLog(Class> clazz) {
64 | return new JdkLoggingImpl(clazz);
65 | }
66 | }
67 |
68 | private static class Log4jLoggingLogFactory implements AbstractLogFactory {
69 | public Log getLog(Class> clazz) {
70 | return new Log4jImpl(clazz);
71 | }
72 | }
73 |
74 | public static void setLogFactory(AbstractLogFactory logFactory) {
75 | LogFactory.logFactory = logFactory;
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/src/org/mybatis/generator/plugins/CachePlugin.java:
--------------------------------------------------------------------------------
1 | package org.mybatis.generator.plugins;
2 |
3 | import java.util.List;
4 |
5 | import org.mybatis.generator.api.IntrospectedTable;
6 | import org.mybatis.generator.api.PluginAdapter;
7 | import org.mybatis.generator.api.dom.xml.Attribute;
8 | import org.mybatis.generator.api.dom.xml.Document;
9 | import org.mybatis.generator.api.dom.xml.XmlElement;
10 | import org.mybatis.generator.internal.util.StringUtility;
11 |
12 | /**
13 | * This plugin adds a cache element to generated sqlMaps. This plugin
14 | * is for MyBatis3 targeted runtimes only. The plugin accepts the
15 | * following properties (all are optional):
16 | *
17 | * cache_eviction
18 | * cache_flushInterval
19 | * cache_size
20 | * cache_readOnly
21 | * cache_type
22 | *
23 | * All properties correspond to properties of the MyBatis cache element and
24 | * are passed "as is" to the corresponding properties of the generated cache
25 | * element. All properties can be specified at the table level, or on the
26 | * plugin element. The property on the table element will override any
27 | * property on the plugin element.
28 | *
29 | * @author Jason Bennett
30 | * @author Jeff Butler
31 | */
32 | public class CachePlugin extends PluginAdapter {
33 | public enum CacheProperty {
34 | EVICTION("cache_eviction", "eviction"), //$NON-NLS-1$ //$NON-NLS-2$
35 | FLUSH_INTERVAL("cache_flushInterval", "flushInterval"), //$NON-NLS-1$ //$NON-NLS-2$
36 | READ_ONLY("cache_readOnly", "readOnly"), //$NON-NLS-1$ //$NON-NLS-2$
37 | SIZE("cache_size", "size"), //$NON-NLS-1$ //$NON-NLS-2$
38 | TYPE("cache_type", "type"); //$NON-NLS-1$ //$NON-NLS-2$
39 |
40 | private String propertyName;
41 | private String attributeName;
42 |
43 | CacheProperty(String propertyName, String attributeName) {
44 | this.propertyName = propertyName;
45 | this.attributeName = attributeName;
46 | }
47 |
48 | public String getPropertyName() {
49 | return propertyName;
50 | }
51 |
52 | public String getAttributeName() {
53 | return attributeName;
54 | }
55 | }
56 |
57 | public CachePlugin() {
58 | super();
59 | }
60 |
61 | public boolean validate(List