01: /*
02: * EnumReader.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.db.mysql;
13:
14: import java.sql.ResultSet;
15: import java.sql.Statement;
16: import java.util.HashMap;
17: import workbench.db.DbMetadata;
18: import workbench.db.TableIdentifier;
19: import workbench.db.WbConnection;
20: import workbench.log.LogMgr;
21: import workbench.storage.DataStore;
22: import workbench.util.SqlUtil;
23:
24: /**
25: *
26: * @author support@sql-workbench.net
27: */
28: public class EnumReader {
29:
30: public static void updateEnumDefinition(TableIdentifier tbl,
31: DataStore tableDefinition, WbConnection connection) {
32: Statement stmt = null;
33: ResultSet rs = null;
34: HashMap<String, String> defs = new HashMap<String, String>(17);
35:
36: try {
37: stmt = connection.createStatement();
38: rs = stmt.executeQuery("SHOW COLUMNS FROM "
39: + tbl.getTableExpression(connection));
40: int colCount = 0;
41: while (rs.next()) {
42: String column = rs.getString(1);
43: if (column == null)
44: continue;
45:
46: String type = rs.getString(2);
47: if (type == null)
48: continue;
49:
50: String ltype = type.toLowerCase();
51: if (ltype.startsWith("enum") || ltype.startsWith("set")) {
52: colCount++;
53: defs.put(column, type);
54: }
55: }
56: int count = tableDefinition.getRowCount();
57: for (int row = 0; row < count; row++) {
58: String column = tableDefinition
59: .getValueAsString(
60: row,
61: DbMetadata.COLUMN_IDX_TABLE_DEFINITION_COL_NAME);
62: String type = defs.get(column);
63: if (type != null) {
64: tableDefinition
65: .setValue(
66: row,
67: DbMetadata.COLUMN_IDX_TABLE_DEFINITION_DATA_TYPE,
68: type);
69: }
70: }
71: } catch (Exception e) {
72: LogMgr.logError("EnumReader.updateEnumDefinition()",
73: "Could not read enum definition", e);
74: } finally {
75: SqlUtil.closeAll(rs, stmt);
76: }
77: }
78: }
|