001: /*
002: * Copyright (C) 2004 Giuseppe MANNA
003: *
004: * This file is part of FreeReportBuilder
005: *
006: * FreeReportBuilder is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program 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
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package it.frb;
023:
024: import java.awt.Component;
025: import javax.swing.JPanel;
026: import javax.swing.JTextField;
027: import javax.swing.JComponent;
028:
029: public class ComputeField {
030: JComponent obj;
031:
032: String sFrom = "";
033: String sWhere = "";
034: String sOrderBy = "";
035: Object[] args = null;
036:
037: Connessione connessione = null;
038:
039: public ComputeField(JComponent obj, String sFrom) {
040: this .obj = obj;
041: this .sFrom = sFrom;
042: }
043:
044: public ComputeField(JComponent obj, String sWhere, String sOrderBy) {
045: this .obj = obj;
046: this .sWhere = sWhere;
047: this .sOrderBy = sOrderBy;
048: }
049:
050: public ComputeField(JComponent obj, String sFrom, String sWhere,
051: String sOrderBy) {
052: this (obj, sFrom, sWhere, sOrderBy, null);
053: }
054:
055: public ComputeField(JComponent obj, String sFrom, String sWhere,
056: String sOrderBy, Object[] args) {
057: this .obj = obj;
058: this .sFrom = sFrom;
059: this .sWhere = sWhere;
060: this .sOrderBy = sOrderBy;
061: this .args = args;
062: }
063:
064: public String getValue() throws java.sql.SQLException {
065: java.sql.Statement stm = null;
066: java.sql.PreparedStatement stps = null;
067: java.sql.Connection conn = null;
068: String returnValue = "";
069: String sGroupByOrderBy = "";
070:
071: boolean bMltStm = ConfigurationProperties.properties()
072: .getUseMultiTransaction(false);
073:
074: conn = Connessione.getConnessione();
075:
076: String sql = "SELECT "
077: + ((javax.swing.JTextField) obj).getText();
078:
079: if (sql.toUpperCase().indexOf("FROM") == -1) {
080: if (sql.toUpperCase().indexOf("WHERE") == -1) {
081: sql = sql + " " + this .sFrom;
082: } else {
083: String sHead = sql.substring(0, sql.toUpperCase()
084: .indexOf("WHERE"));
085: String swhere = sql.substring(sql.toUpperCase()
086: .indexOf("WHERE"));
087:
088: sql = sHead + " " + this .sFrom + " " + swhere;
089: }
090: }
091:
092: String sqlSearch = sql.toUpperCase();
093:
094: if (!sWhere.equals("")) {
095: if (!sOrderBy.equals("")) {
096: int count = 0;
097: Component[] components = ((JPanel) obj.getParent())
098: .getComponents();
099:
100: for (int i = 0; i < components.length; i++) {
101: if (components[i] instanceof JTextField) {
102: if (components[i].getName() == null
103: || components[i].getName().equals("")) {
104: count = count + 1;
105: }
106: }
107: }
108:
109: if (count > 0) {
110: sGroupByOrderBy = DataEngine.replace(sOrderBy,
111: sOrderBy.indexOf("ORDER BY"), sOrderBy
112: .indexOf("ORDER BY") + 8, " ");
113: sGroupByOrderBy = " GROUP BY " + sGroupByOrderBy
114: + " ORDER BY " + sGroupByOrderBy;
115: } else {
116: //sGroupByOrderBy = " ORDER BY " + sGroupByOrderBy;
117: }
118: }
119:
120: if (sqlSearch.indexOf("WHERE") != -1) {
121: sql = sql + " AND " + sWhere + sGroupByOrderBy;
122: } else {
123: sql = sql + " Where " + sWhere + sGroupByOrderBy;
124: }
125: }
126:
127: java.sql.ResultSet rst = null;
128:
129: if (sql.indexOf("?") != -1) {
130: if (args != null) {
131: stps = conn.prepareStatement(sql);
132:
133: for (int i = 0; i < args.length; i++) {
134: stps.setObject(i + 1, args[i]);
135: }
136:
137: if (stps.execute()) {
138: rst = stps.getResultSet();
139: }
140: } else {
141: stm = conn.createStatement();
142:
143: try {
144: rst = stm.executeQuery(sql);
145:
146: } catch (java.sql.SQLException e) {
147: javax.swing.JOptionPane
148: .showMessageDialog(
149: null,
150: "<html><font color=black><table><P><TR><TD>Not been possible to perform two statements on the same connection.</TD></TR>"
151: + "<TR><TD>Database that been used doesn't support this operation.</TD></TR>"
152: + "<TR><TD>To modify the parameter \"BDatabaseUseMultiTrans\" in the file of configuration and to plan it to \"false\"</TD></TR>"
153: + "<TR><TD>to close and to reopen the application.</TD></TR></table></P><html>");
154: conn.close();
155: throw e;
156: }
157: }
158: } else {
159: stm = conn.createStatement();
160:
161: try {
162: rst = stm.executeQuery(sql);
163:
164: } catch (java.sql.SQLException e) {
165: javax.swing.JOptionPane
166: .showMessageDialog(
167: null,
168: "<html><font color=black><table><P><TR><TD>Not been possible to perform two statements on the same connection.</TD></TR>"
169: + "<TR><TD>Database that been used doesn't support this operation.</TD></TR>"
170: + "<TR><TD>To modify the parameter \"BDatabaseUseMultiTrans\" in the file of configuration and to plan it to \"false\"</TD></TR>"
171: + "<TR><TD>to close and to reopen the application.</TD></TR></table></P><html>");
172:
173: if (!bMltStm) {
174: conn.close();
175: }
176:
177: throw e;
178: }
179: }
180:
181: //while (rst.next())
182: //{
183: if (rst.next()) {
184: returnValue = returnValue + rst.getString(1);
185: }
186: //}
187:
188: rst.close();
189:
190: if (stm != null) {
191: stm.close();
192:
193: } else {
194: stps.close();
195: }
196:
197: if (!bMltStm) {
198: conn.close();
199: }
200:
201: return returnValue;
202: }
203: }
|