001: package net.sourceforge.squirrel_sql.plugins.mysql.util;
002:
003: /*
004: * Copyright (C) 2003 Arun Kapilan.P
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: import java.sql.ResultSet;
021: import java.sql.ResultSetMetaData;
022: import java.sql.SQLException;
023: import java.sql.Statement;
024: import java.util.Vector;
025:
026: import net.sourceforge.squirrel_sql.client.session.IObjectTreeAPI;
027: import net.sourceforge.squirrel_sql.client.session.ISession;
028: import net.sourceforge.squirrel_sql.fw.sql.DataTypeInfo;
029: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
030: import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
031: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
032: import net.sourceforge.squirrel_sql.fw.sql.PrimaryKeyInfo;
033: import net.sourceforge.squirrel_sql.fw.sql.SQLDatabaseMetaData;
034: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
035: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
036: import net.sourceforge.squirrel_sql.plugins.mysql.MysqlPlugin;
037:
038: /*
039: * DBUtils.java
040: *
041: * Created on June 14, 2003, 9:07 AM
042: *
043: * @author Arun Kapilan.P
044: */
045: public class DBUtils {
046: /** Logger for this class. */
047: @SuppressWarnings("unused")
048: private final static ILogger s_log = LoggerController
049: .createLogger(DBUtils.class);
050:
051: /** Current session. */
052: private ISession _session;
053:
054: /** Current plugin. */
055: @SuppressWarnings("unused")
056: private final MysqlPlugin _plugin;
057:
058: /**
059: * Ctor specifying the current session.
060: */
061: public DBUtils(ISession session, MysqlPlugin plugin) {
062: super ();
063: _session = session;
064: _plugin = plugin;
065:
066: }
067:
068: //To get the TableInfo for the selected object in the tree
069: public ITableInfo getTableInfo() {
070: IObjectTreeAPI treeAPI = _session.getSessionInternalFrame()
071: .getObjectTreeAPI();
072: IDatabaseObjectInfo[] dbInfo = treeAPI
073: .getSelectedDatabaseObjects();
074:
075: if (dbInfo[0] instanceof ITableInfo) {
076: return (ITableInfo) dbInfo[0];
077: }
078: return null;
079: }
080:
081: public String[] getColumnNames() {
082: String[] columnNames = null;
083: try {
084: final ISQLConnection conn = _session.getSQLConnection();
085: Statement stmt = conn.createStatement();
086: ResultSet rs = stmt.executeQuery("SELECT * FROM "
087: + getTableInfo() + ";");
088: ResultSetMetaData md = rs.getMetaData();
089: columnNames = new String[md.getColumnCount()];
090: for (int i = 0; i < columnNames.length; i++) {
091: columnNames[i] = md.getColumnLabel(i + 1);
092: }
093: } catch (SQLException ex) {
094: _session.showErrorMessage(ex);
095: }
096: return columnNames;
097: }
098:
099: public String[] getFieldDataTypes() {
100: String[] dataTypes = null;
101:
102: try {
103: final ISQLConnection conn = _session.getSQLConnection();
104: Statement stmt = conn.createStatement();
105: ResultSet rs = stmt.executeQuery("SELECT * FROM "
106: + getTableInfo() + ";");
107: ResultSetMetaData md = rs.getMetaData();
108:
109: dataTypes = new String[md.getColumnCount()];
110: for (int i = 0; i < dataTypes.length; i++) {
111: dataTypes[i] = md.getColumnTypeName(i + 1);
112: }
113: } catch (SQLException ex) {
114: _session.showErrorMessage(ex);
115: }
116: return dataTypes;
117: }
118:
119: public void execute(String SQLQuery) {
120: try {
121:
122: ISQLConnection conn = _session.getSQLConnection();
123: Statement stmt = conn.createStatement();
124: stmt.execute(SQLQuery);
125: } catch (SQLException ex) {
126: _session.showErrorMessage(ex);
127: }
128: }
129:
130: //Get all the data types available
131: public Vector<String> getDataTypes() {
132:
133: Vector<String> dataTypes = new Vector<String>();
134: try {
135: final ISQLConnection conn = _session.getSQLConnection();
136: SQLDatabaseMetaData dmd = conn.getSQLMetaData();
137: DataTypeInfo[] infos = dmd.getDataTypes();
138: for (int i = 0; i < infos.length; i++) {
139: dataTypes.add(infos[i].getSimpleName());
140: }
141: } catch (SQLException ex) {
142: _session.showErrorMessage(ex);
143: }
144: return dataTypes;
145: }
146:
147: public String getPrimaryKeyColumn() {
148:
149: String primaryKey = "";
150: try {
151: ISQLConnection con = _session.getSQLConnection();
152: SQLDatabaseMetaData db = con.getSQLMetaData();
153: PrimaryKeyInfo[] infos = db.getPrimaryKey(getTableInfo());
154: for (int i = 0; i < infos.length; i++) {
155: primaryKey = infos[i].getColumnName();
156: }
157: } catch (SQLException ex) {
158: _session.showErrorMessage(ex);
159: }
160:
161: return primaryKey;
162: }
163: }
|