Recently I started working with Grails and I am very much impressed with its capabilities and kind of digging deep now.
Being used MyEclipse extensively I started searching for tool to create groovy domain objects from mysql database. But no luck I couldn't able to find any tools or scripts. So I created one which suits my needs presently. Feel free to use it any way you like.
Features
- Converts Plural table names to Singular form
- Removes underscores (_) and capitalizes first letter of every word.
Dependencies
- MySQL Connector/J
- Apache Commons Lang
Features for next Version
- Validation for user input and Error Handling
- Connects to any database ( MySQL / SQL Server / Oracle )
- Processes Relations and create GORM entries in groovy domain classes
- /*
- *
- * 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
- *
- * <a href="http://www.apache.org/licenses/LICENSE-2.0<br />
- " title="http://www.apache.org/licenses/LICENSE-2.0<br />
- ">http://www.apache.org/licenses/LICENSE-2.0<br />
- </a> *
- * 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.
- *
- * Venkat Addanki - <a href="http://www.techgrasp.com/blogs/venkat<br />
- " title="http://www.techgrasp.com/blogs/venkat<br />
- ">http://www.techgrasp.com/blogs/venkat<br />
- </a> *
- */
- package com.techgrasp.getschema;
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DatabaseMetaData;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.HashMap;
- import org.apache.commons.lang.WordUtils;
- public class Main {
- public static final HashMap<String,String> dataTypes = new HashMap<String,String>();
- public static String hostName = null;
- public static String dbPort = null;
- public static String dbName = null;
- public static String dbUser = null;
- public static String dbPass = null;
- public static String location = null;
- public static void main(String[] args) {
- try {
- if(args.length == 0) {
- System.err.println("Invalid number of parameters");
- System.err.println("Usage: GenerateDomain <dbhost> <port> <dbname> <user> <password> <generatedLocation>");
- System.exit(0);
- }
- hostName = args[0];
- dbPort = args[1];
- dbName = args[2];
- dbUser = args[3];
- dbPass = args[4];
- location = args[5];
- initializeHashMap();
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://" + hostName + ":" + dbPort + "/" + dbName;
- Connection con = DriverManager.getConnection(url, dbUser,dbPass);
- final DatabaseMetaData dmd = con.getMetaData();
- String[] types = { "TABLE" };
- ResultSet tables = dmd.getTables(dbName, null,null,types);
- while (tables.next()) {
- final String tableName = tables.getString(3);
- generateDomain(dbName,tableName,dmd);
- }
- tables.close();
- con.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private static void generateDomain(String catalog, String tableName, DatabaseMetaData dmd) throws IOException, SQLException {
- String groovyDomainName = getDomainNameFromTableName(tableName);
- if(groovyDomainName.equals("Config")) {
- groovyDomainName = "AppConfig";
- }
- System.out.println("Generating class " + groovyDomainName);
- BufferedWriter bw = new BufferedWriter( new FileWriter(location + groovyDomainName + ".groovy"));
- bw.write("class " + groovyDomainName + " { \n\n");
- ResultSet columns = dmd.getColumns(catalog, null, tableName, null);
- while(columns.next()) {
- bw.write(" " + dataTypes.get(columns.getString(6)) + " " + columns.getString(4) + "\n");
- }
- columns.close();
- bw.write("\n");
- bw.write(" static mapping = {\n");
- bw.write(" table '" + tableName + "'\n");
- bw.write(" version false\n");
- bw.write(" }\n");
- bw.write("}\n");
- bw.close();
- }
- private static String getDomainNameFromTableName(final String tableName) {
- String temp1 = tableName.replace('_', ' ') + " ";
- String temp2 = temp1.replaceAll("s ", " ");
- String temp5 = temp2.replaceAll("ie ","y ");
- String temp3 = WordUtils.capitalize(temp5);
- return temp3.replaceAll(" ", "");
- }
- private static void initializeHashMap() {
- dataTypes.put("CHAR","String");
- dataTypes.put("VARCHAR","String");
- dataTypes.put("LONGVARCHAR","String");
- dataTypes.put("TEXT","String");
- dataTypes.put("LONGTEXT","String");
- dataTypes.put("NUMERIC","BigDecimal");
- dataTypes.put("DECIMAL","BigDecimal");
- dataTypes.put("BIT","Boolean");
- dataTypes.put("TINYINT","Byte");
- dataTypes.put("SMALLINT","Short");
- dataTypes.put("INTEGER","Integer");
- dataTypes.put("INT","Integer");
- dataTypes.put("INT UNSIGNED","Integer");
- dataTypes.put("BIGINT","Long");
- dataTypes.put("REAL","Float");
- dataTypes.put("FLOAT","Double");
- dataTypes.put("DOUBLE","Double");
- dataTypes.put("BINARY","Byte[]");
- dataTypes.put("BLOB","Byte[]");
- dataTypes.put("VARBINARY","Byte[]");
- dataTypes.put("LONGVARBINARY","Byte[]");
- dataTypes.put("DATE","Date");
- dataTypes.put("TIME","Date");
- dataTypes.put("DATETIME","Date");
- dataTypes.put("TIMESTAMP","Date");
- }
- }













You should contribute this to the project rather than putting it here. Sure would be nice if this was part of the grails commands???
Post new comment