001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2003 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.lucane.applications.sqlnavigator;
020:
021: import java.sql.*;
022: import java.util.*;
023:
024: import org.lucane.common.*;
025: import org.lucane.common.net.ObjectConnection;
026: import org.lucane.server.*;
027: import org.lucane.server.database.*;
028:
029: public class SqlService extends Service {
030: private DatabaseAbstractionLayer layer;
031:
032: public SqlService() {
033: }
034:
035: public void init(Server parent) {
036: this .layer = parent.getDBLayer();
037: }
038:
039: public void process(ObjectConnection oc, Message message) {
040: SqlAction sa = (SqlAction) message.getData();
041: switch (sa.action) {
042: case SqlAction.GET_DRIVER_INFO:
043: getDriverInfo(oc);
044: break;
045: case SqlAction.GET_TABLE_NAMES:
046: getTableNames(oc);
047: break;
048: case SqlAction.EXECUTE_QUERY:
049: executeQuery(oc, sa.params);
050: break;
051: }
052: }
053:
054: private void executeQuery(ObjectConnection oc, Object object) {
055: try {
056: SqlResult result = new SqlResult();
057:
058: Connection c = layer.getConnection();
059: Statement stm = c.createStatement();
060: ResultSet rs = stm.executeQuery((String) object);
061:
062: try {
063: //-- column names
064: ResultSetMetaData meta = rs.getMetaData();
065: int cols = meta.getColumnCount();
066: for (int i = 0; i < cols; i++)
067: result.addColumn(meta.getColumnLabel(i + 1));
068:
069: //-- data
070: while (rs.next()) {
071: Vector line = new Vector();
072:
073: for (int i = 0; i < cols; i++)
074: line.add(rs.getString(i + 1));
075:
076: result.addLine(line);
077: }
078: } catch (SQLException sqle) {
079: //probably not a select statement
080: }
081:
082: oc.write(result);
083: c.close();
084: } catch (Exception e) {
085: e.printStackTrace();
086: }
087: }
088:
089: private void getTableNames(ObjectConnection oc) {
090: Vector data = new Vector();
091:
092: try {
093: Connection connection = layer.getConnection();
094: DatabaseMetaData dbmd = connection.getMetaData();
095: String[] types = { "TABLE", "VIEW" };
096: ResultSet rs = dbmd.getTables(null, null, null, types);
097:
098: //field n°3 is the table name
099: while (rs.next())
100: data.addElement(rs.getString(3));
101:
102: rs.close();
103: connection.close();
104: oc.write(data);
105: } catch (Exception e) {
106: e.printStackTrace();
107: }
108: }
109:
110: private void getDriverInfo(ObjectConnection oc) {
111: try {
112: Connection connection = layer.getConnection();
113: DatabaseMetaData dbmd = connection.getMetaData();
114: String info = dbmd.getDriverName() + " "
115: + dbmd.getDriverVersion();
116: connection.close();
117:
118: oc.write(info);
119: } catch (Exception e) {
120: e.printStackTrace();
121: }
122: }
123: }
|