001: /*
002: * SQLeonardo :: java database frontend
003: * Copyright (C) 2004 nickyb@users.sourceforge.net
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or (at your option) any later version.
009: *
010: * This program 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
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: *
019: */
020:
021: package nickyb.sqleonardo.environment.ctrl.content;
022:
023: import java.sql.PreparedStatement;
024: import java.util.Vector;
025:
026: import javax.swing.JOptionPane;
027:
028: import nickyb.sqleonardo.common.jdbc.ConnectionAssistant;
029: import nickyb.sqleonardo.common.jdbc.ConnectionHandler;
030: import nickyb.sqleonardo.common.util.Text;
031: import nickyb.sqleonardo.environment.Application;
032: import nickyb.sqleonardo.environment.ctrl.ContentPane;
033: import nickyb.sqleonardo.querybuilder.syntax.SQLFormatter;
034:
035: public class TaskUpdate implements Runnable {
036: private ContentPane target = null;
037: private PreparedStatement pstmt = null;
038:
039: public TaskUpdate(ContentPane target) {
040: this .target = target;
041: }
042:
043: public void run() {
044: ConnectionHandler ch = ConnectionAssistant.getHandler(target
045: .getHandlerKey());
046: for (int exceptions = 0; target.getView().getChanges().count() > exceptions;) {
047: Vector columns = new Vector();
048: Vector params = new Vector();
049: String sql = new String();
050:
051: ContentChanges.Handler handler = target.getView()
052: .getChanges().getHandlerAt(exceptions);
053: Object[] rowdata = target.getView().getValues(handler.rid);
054:
055: if (handler.type.equals(ContentChanges.INSERT)) {
056: for (int i = 0; i < rowdata.length; i++) {
057: Object cell = rowdata[i];
058: if (rowdata[i] instanceof Object[])
059: cell = ((Object[]) rowdata[i])[0];
060: params.addElement(new Object[] {
061: cell,
062: new Integer(target.getView().getColumnType(
063: i)) });
064:
065: String cname = target.getView().getColumnName(i);
066: columns.addElement(SQLFormatter.ensureQuotes(cname,
067: ch.getObject("$identifierQuoteString")
068: .toString(), true));
069: }
070: sql = target.getUpdateModel().getInsertSyntax(
071: (String[]) columns.toArray(new String[columns
072: .size()]));
073: } else {
074: if (handler.type.equals(ContentChanges.DELETE)) {
075: sql = target.getUpdateModel().getDeleteSyntax();
076: } else if (handler.type.equals(ContentChanges.UPDATE)) {
077: for (int i = 0; i < rowdata.length; i++) {
078: if (rowdata[i] instanceof Object[]) {
079: Object cell = ((Object[]) rowdata[i])[0];
080: params.addElement(new Object[] {
081: cell,
082: new Integer(target.getView()
083: .getColumnType(i)) });
084:
085: String cname = target.getView()
086: .getColumnName(i);
087: columns.addElement(SQLFormatter
088: .ensureQuotes(cname, ch.getObject(
089: "$identifierQuoteString")
090: .toString(), true));
091: }
092: }
093: sql = target.getUpdateModel()
094: .getUpdateSyntax(
095: (String[]) columns
096: .toArray(new String[columns
097: .size()]));
098: }
099:
100: for (int i = 0; i < target.getUpdateModel()
101: .getRowIdentifierCount(); i++) {
102: int col = target.getView().getColumnIndex(
103: target.getUpdateModel().getRowIdentifier(i)
104: .getReference());
105: if (col == -1)
106: col = target.getView().getColumnIndex(
107: target.getUpdateModel()
108: .getRowIdentifier(i).getName());
109:
110: Object cell = rowdata[col];
111: if (cell instanceof Object[])
112: cell = ((Object[]) cell)[1];
113: params.addElement(new Object[] {
114: cell,
115: new Integer(target.getView().getColumnType(
116: i)) });
117: }
118: }
119:
120: try {
121: this .close();
122: } catch (Exception e) {
123: Application.println(e, true);
124: }
125:
126: try {
127: execute(sql.toString(), params);
128:
129: for (int i = 0; i < rowdata.length; i++) {
130: if (rowdata[i] instanceof Object[]) {
131: Object cell = ((Object[]) rowdata[i])[0];
132: rowdata[i] = cell;
133: }
134: }
135:
136: target.getView().getChanges().removeHandlerAt(
137: exceptions);
138: } catch (Exception e) {
139: if (!alert(e))
140: break;
141: exceptions++;
142: }
143: }
144: target.getView().onTableChanged(true);
145: target.doStop();
146: }
147:
148: private boolean alert(Exception e) {
149: String title = e.getClass().getName();
150: String message = Text.wrap(e.toString(), 75);
151: message += "\ndo you want continue?";
152:
153: return JOptionPane.showConfirmDialog(Application.window,
154: message, title, JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
155: }
156:
157: private void close() throws Exception {
158: if (pstmt != null) {
159: pstmt.close();
160: pstmt = null;
161: }
162: }
163:
164: private void execute(String sql, Vector values) throws Exception {
165: ConnectionHandler ch = ConnectionAssistant.getHandler(target
166: .getHandlerKey());
167: pstmt = ch.get().prepareStatement(sql);
168:
169: for (int i = 0; i < values.size(); i++) {
170: Object[] param = (Object[]) values.elementAt(i);
171: pstmt.setObject(i + 1, param[0], ((Integer) param[1])
172: .intValue());
173: }
174: pstmt.executeUpdate();
175: this.close();
176: }
177: }
|