1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
package com.mysql.grt.modules;
import java.sql.*;
import com.mysql.grt.*;
import com.mysql.grt.base.*;
import com.mysql.grt.db.mysql.*;
/**
* GRT Migration Class
*
* @author MikeZ
* @version 1.0, 01/25/05
*
*/
public class TransformationMysqlJdbc {
/**
* Static function to return information about this class to the GRT
* environment
*
* @return returns a GRT XML string containing the infos about this class
*/
public static String getModuleInfo() {
return Grt.getModuleInfoXml(TransformationMysqlJdbc.class, "");
}
/**
* Generates all SQL create statements for the given catalog. The SQL
* statement will be set in each database object, e.g. schemata, tables,
* views, ...
*
* @param catalog
* the catalog object to create the SQL for
*/
public static void generateSqlCreateStatements(Catalog catalog,
GrtHashMap options) {
GrtList args = new GrtList();
args.addObject(catalog);
args.addObject(options);
try {
Grt.getInstance().callGrtFunction("TransformationMysql",
"generateSqlCreateStatements", args);
} catch (RuntimeException e) {
// generate sql for tables
Grt.getInstance().addMsg(
"The following error occured while "
+ "creating the SQL code: " + e.getMessage());
Grt.getInstance().flushMessages();
}
}
/**
* Generates the SQL create code for the given object
*
* @param obj
* the database object to create the SQL for
* @return the SQL create code as String
*/
public static String getSqlScript(Catalog catalog, GrtHashMap options) {
String sql = "";
GrtList args = new GrtList();
args.addObject(catalog);
args.addObject(options);
try {
sql = (String) Grt.getInstance().callGrtFunction(
"TransformationMysql", "getSqlScript", args);
} catch (RuntimeException e) {
Grt.getInstance().addMsg(
"The following error occured while "
+ "creating the SQL code: " + e.getMessage());
Grt.getInstance().flushMessages();
}
return sql;
}
/**
* Generates the SQL create code for the given object
*
* @param obj
* the database object to create the SQL for
* @return the SQL create code as String
*/
public static String getSqlCreate(com.mysql.grt.db.DatabaseObject obj) {
String sql;
try {
sql = (String) Grt.getInstance().callGrtFunction(
"TransformationMysql", "getSqlCreate", obj);
} catch (RuntimeException e) {
sql = "/*" + e.getMessage() + "*/";
}
return sql;
}
/**
* Generates the SQL create code for the given object
*
* @param obj
* the database object to create the SQL for
* @return the SQL create code as String
*/
public static String getSqlDrop(com.mysql.grt.db.DatabaseObject obj) {
String sql;
try {
sql = (String) Grt.getInstance().callGrtFunction(
"TransformationMysql", "getSqlDrop", obj);
} catch (RuntimeException e) {
sql = "/*" + e.getMessage() + "*/";
}
return sql;
}
/**
* Generates the SQL create code for the given object
*
* @param obj
* the database object to create the SQL for
* @return the SQL create code as String
*/
public static String getSqlMerge(com.mysql.grt.db.DatabaseObject obj) {
String sql;
try {
sql = (String) Grt.getInstance().callGrtFunction(
"TransformationMysql", "getSqlMerge", obj);
} catch (RuntimeException e) {
sql = "/*" + e.getMessage() + "*/";
}
return sql;
}
/**
* Execute all SQL statements for all objects in the given catalog. The SQL
* statement is taken from the object's sql member
*
* @param jdbcDriver
* class name of the jdbc driver
* @param jdbcConnectionString
* jdbc connection string to the target database
* @param catalog
* the catalog to process
*/
public static void executeSqlStatements(
com.mysql.grt.db.mgmt.Connection dbConn, Catalog catalog,
ObjectLogList logList) throws Exception {
new TransformationMysqlJdbc().doExecuteSqlStatements(dbConn, catalog,
logList);
}
protected void doExecuteSqlStatements(
com.mysql.grt.db.mgmt.Connection dbConn, Catalog catalog,
ObjectLogList logList) throws Exception {
Connection conn = ReverseEngineeringGeneric.establishConnection(dbConn);
String sql;
// Statement stmt = conn.createStatement();
try {
sql = (String) Grt.getInstance().callGrtFunction(
"TransformationMysql", "getScriptHeader", null);
executeSqlStatements(conn, sql, "Execute script header commands.");
} catch (SQLException e) {
// tollerate exception
}
// Create schemata
for (int i = 0; i < catalog.getSchemata().size(); i++) {
Schema schema = (Schema) catalog.getSchemata().get(i);
// Create schema
executeSqlStatement(conn, schema, "Creating schema "
+ schema.getName() + " ...", logList);
// Create tables
Grt.getInstance().addMsg("Creating tables ...");
for (int j = 0; j < schema.getTables().size(); j++) {
Table table = (Table) schema.getTables().get(j);
Grt.getInstance().addProgress(
"Creating table " + table.getName(),
(j * 100) / schema.getTables().size());
Grt.getInstance().flushMessages();
executeSqlStatement(conn, table, "Creating table "
+ table.getName() + " ...", logList);
}
// Create views
Grt.getInstance().addMsg("Creating views ...");
for (int j = 0; j < schema.getViews().size(); j++) {
View view = (View) schema.getViews().get(j);
Grt.getInstance().addProgress(
"Creating view " + view.getName(),
(j * 100) / schema.getViews().size());
Grt.getInstance().flushMessages();
executeSqlStatement(conn, view, "Creating view "
+ view.getName() + " ...", logList);
}
// Create procedures
Grt.getInstance().addMsg("Creating procedures ...");
for (int j = 0; j < schema.getRoutines().size(); j++) {
Routine proc = (Routine) schema.getRoutines().get(j);
Grt.getInstance().addProgress(
"Creating view " + proc.getName(),
(j * 100) / schema.getRoutines().size());
Grt.getInstance().flushMessages();
executeSqlStatement(conn, proc, "Creating procedure "
+ proc.getName() + " ...", logList);
}
// Hide progress bar
Grt.getInstance().addProgress("", -1);
Grt.getInstance().flushMessages();
}
try {
sql = (String) Grt.getInstance().callGrtFunction(
"TransformationMysql", "getScriptFooter", null);
executeSqlStatements(conn, sql, "Execute script footer commands.");
} catch (SQLException e) {
// tollerate exception
}
conn.close();
}
private void executeSqlStatement(Connection conn,
com.mysql.grt.db.DatabaseObject obj, String action,
ObjectLogList logList) {
// generate a new object log
ObjectLog objectLog = new ObjectLog(logList.getOwner());
objectLog.setLogObject(obj);
try {
executeSqlStatements(conn, obj.getSql(), action);
} catch (SQLException e) {
ObjectLogEntry entry = new ObjectLogEntry(objectLog);
entry.setName(e.getMessage());
entry.setEntryType(2);
objectLog.getEntries().add(entry);
} finally {
logList.add(objectLog);
}
}
protected void executeSqlStatements(Connection conn, String sql,
String action) throws SQLException {
GrtStringList cmds;
cmds = (GrtStringList) Grt.getInstance().callGrtFunction(
"TransformationMysql", "splitSqlCommands", sql);
for (int i = 0; i < cmds.size(); i++)
executeSqlStatement(conn, cmds.get(i), action);
}
private void executeSqlStatement(Connection conn, String sql, String action)
throws SQLException {
// check if there is a SQL statement to execute
if ((sql != null) && (!sql.equals(""))) {
Grt.getInstance().addMsg(action);
Grt.getInstance().addMsgDetail(sql);
PreparedStatement stmt = conn.prepareStatement(sql);
try {
stmt.execute();
} catch (SQLException e) {
Grt.getInstance().addMsg(
"An error occured while executing the SQL statement.");
Grt.getInstance().addMsgDetail(e.getMessage());
throw (e);
} finally {
if (stmt != null)
stmt.close();
}
}
}
public static com.mysql.grt.db.DatabaseObject getScriptHeader(
GrtHashMap options) {
com.mysql.grt.db.DatabaseObject header;
try {
header = (com.mysql.grt.db.DatabaseObject) Grt.getInstance()
.callGrtFunction("TransformationMysql", "getScriptHeader",
options);
} catch (RuntimeException e) {
header = null;
}
return header;
}
public static com.mysql.grt.db.DatabaseObject getScriptFooter(
GrtHashMap options) {
com.mysql.grt.db.DatabaseObject footer;
try {
footer = (com.mysql.grt.db.DatabaseObject) Grt.getInstance()
.callGrtFunction("TransformationMysql", "getScriptFooter",
null);
} catch (RuntimeException e) {
footer = null;
}
return footer;
}
}
|