You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GenerateModels.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright 2004-2011 H2 Group.
  3. * Copyright 2011 James Moger.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.iciql.util;
  18. import java.io.BufferedWriter;
  19. import java.io.File;
  20. import java.io.FileWriter;
  21. import java.io.IOException;
  22. import java.io.PrintStream;
  23. import java.io.PrintWriter;
  24. import java.io.Writer;
  25. import java.sql.Connection;
  26. import java.sql.DriverManager;
  27. import java.sql.SQLException;
  28. import java.util.List;
  29. import java.util.regex.Matcher;
  30. import java.util.regex.Pattern;
  31. import com.iciql.Db;
  32. import com.iciql.DbInspector;
  33. /**
  34. * Generates iciql models.
  35. */
  36. public class GenerateModels {
  37. /**
  38. * The output stream where this tool writes to.
  39. */
  40. protected PrintStream out = System.out;
  41. public static void main(String... args) {
  42. GenerateModels tool = new GenerateModels();
  43. try {
  44. tool.runTool(args);
  45. } catch (SQLException e) {
  46. tool.out.print("Error: ");
  47. tool.out.println(e.getMessage());
  48. tool.out.println();
  49. tool.showUsage();
  50. }
  51. }
  52. public void runTool(String... args) throws SQLException {
  53. String url = null;
  54. String user = "sa";
  55. String password = "";
  56. String schema = null;
  57. String table = null;
  58. String packageName = "";
  59. String folder = null;
  60. boolean annotateSchema = true;
  61. boolean trimStrings = false;
  62. for (int i = 0; args != null && i < args.length; i++) {
  63. String arg = args[i];
  64. if (arg.equals("-url")) {
  65. url = args[++i];
  66. } else if (arg.equals("-user")) {
  67. user = args[++i];
  68. } else if (arg.equals("-password")) {
  69. password = args[++i];
  70. } else if (arg.equals("-schema")) {
  71. schema = args[++i];
  72. } else if (arg.equals("-table")) {
  73. table = args[++i];
  74. } else if (arg.equals("-package")) {
  75. packageName = args[++i];
  76. } else if (arg.equals("-folder")) {
  77. folder = args[++i];
  78. } else if (arg.equals("-annotateSchema")) {
  79. try {
  80. annotateSchema = Boolean.parseBoolean(args[++i]);
  81. } catch (Throwable t) {
  82. throw new SQLException("Can not parse -annotateSchema value");
  83. }
  84. } else if (arg.equals("-trimStrings")) {
  85. try {
  86. trimStrings = Boolean.parseBoolean(args[++i]);
  87. } catch (Throwable t) {
  88. throw new SQLException("Can not parse -trimStrings value");
  89. }
  90. } else {
  91. throwUnsupportedOption(arg);
  92. }
  93. }
  94. if (url == null) {
  95. throw new SQLException("URL not set");
  96. }
  97. execute(url, user, password, schema, table, packageName, folder, annotateSchema, trimStrings);
  98. }
  99. /**
  100. * Generates models from the database.
  101. *
  102. * @param url
  103. * the database URL
  104. * @param user
  105. * the user name
  106. * @param password
  107. * the password
  108. * @param schema
  109. * the schema to read from. null for all schemas.
  110. * @param table
  111. * the table to model. null for all tables within schema.
  112. * @param packageName
  113. * the package name of the model classes.
  114. * @param folder
  115. * destination folder for model classes (package path not
  116. * included)
  117. * @param annotateSchema
  118. * includes the schema in the table model annotations
  119. * @param trimStrings
  120. * automatically trim strings that exceed maxLength
  121. */
  122. public static void execute(String url, String user, String password, String schema, String table,
  123. String packageName, String folder, boolean annotateSchema, boolean trimStrings) throws SQLException {
  124. Connection conn = null;
  125. try {
  126. conn = DriverManager.getConnection(url, user, password);
  127. Db db = Db.open(url, user, password.toCharArray());
  128. DbInspector inspector = new DbInspector(db);
  129. List<String> models = inspector.generateModel(schema, table, packageName, annotateSchema, trimStrings);
  130. File parentFile;
  131. if (StringUtils.isNullOrEmpty(folder)) {
  132. parentFile = new File(System.getProperty("user.dir"));
  133. } else {
  134. parentFile = new File(folder);
  135. }
  136. parentFile.mkdirs();
  137. Pattern p = Pattern.compile("class ([a-zA-Z0-9]+)");
  138. for (String model : models) {
  139. Matcher m = p.matcher(model);
  140. if (m.find()) {
  141. String className = m.group().substring("class".length()).trim();
  142. File classFile = new File(parentFile, className + ".java");
  143. Writer o = new FileWriter(classFile, false);
  144. PrintWriter writer = new PrintWriter(new BufferedWriter(o));
  145. writer.write(model);
  146. writer.close();
  147. System.out.println("Generated " + classFile.getAbsolutePath());
  148. }
  149. }
  150. } catch (IOException io) {
  151. throw new SQLException("could not generate model", io);
  152. } finally {
  153. JdbcUtils.closeSilently(conn);
  154. }
  155. }
  156. /**
  157. * Throw a SQLException saying this command line option is not supported.
  158. *
  159. * @param option
  160. * the unsupported option
  161. * @return this method never returns normally
  162. */
  163. protected SQLException throwUnsupportedOption(String option) throws SQLException {
  164. showUsage();
  165. throw new SQLException("Unsupported option: " + option);
  166. }
  167. protected void showUsage() {
  168. out.println("GenerateModels");
  169. out.println("Usage:");
  170. out.println();
  171. out.println("(*) -url jdbc:h2:~test");
  172. out.println(" -user <string>");
  173. out.println(" -password <string>");
  174. out.println(" -schema <string>");
  175. out.println(" -table <string>");
  176. out.println(" -package <string>");
  177. out.println(" -folder <string>");
  178. out.println(" -annotateSchema <boolean>");
  179. out.println(" -trimStrings <boolean>");
  180. }
  181. }