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.util.Vector;
024:
025: import javax.swing.JScrollPane;
026: import javax.swing.JTextArea;
027:
028: import nickyb.sqleonardo.common.gui.AbstractDialogModal;
029: import nickyb.sqleonardo.environment.Application;
030: import nickyb.sqleonardo.environment.ctrl.ContentPane;
031: import nickyb.sqleonardo.querybuilder.syntax.SQLFormatter;
032:
033: public class DialogPreview extends AbstractDialogModal {
034: private JTextArea syntaxes;
035: private ContentPane content;
036:
037: public DialogPreview(ContentPane content) {
038: super (Application.window, "changes");
039: this .content = content;
040:
041: getContentPane().add(
042: new JScrollPane(syntaxes = new JTextArea()));
043: syntaxes.setEditable(false);
044: }
045:
046: protected void onOpen() {
047: // ConnectionHandler ch = ConnectionAssistant.getHandler(content.getHandlerKey());
048: for (int i = 0; i < content.getView().getChanges().count(); i++) {
049: Vector columns = new Vector();
050: Vector values = new Vector();
051: String sql = new String();
052:
053: ContentChanges.Handler handler = content.getView()
054: .getChanges().getHandlerAt(i);
055: Object[] rowdata = content.getView().getValues(handler.rid);
056:
057: if (handler.type.equals(ContentChanges.INSERT)) {
058: for (int j = 0; j < rowdata.length; j++) {
059: Object cell = rowdata[j];
060: if (rowdata[j] instanceof Object[])
061: cell = ((Object[]) rowdata[j])[0];
062: values.addElement(toJdbcValue(cell, j));
063:
064: // String cname = content.getView().getColumnName(j);
065: // columns.addElement(SQLFormatter.ensureQuotes(cname,ch.getObject("$identifierQuoteString").toString(),QueryB));
066: columns.addElement(content.getQueryModel()
067: .getQueryExpression()
068: .getQuerySpecification().getSelectList()[j]
069: .toString());
070: }
071: sql = content.getUpdateModel().getInsertSyntax(
072: (String[]) columns.toArray(new String[columns
073: .size()]), values.toArray());
074: } else {
075: Vector whereValues = new Vector();
076: for (int j = 0; j < content.getUpdateModel()
077: .getRowIdentifierCount(); j++) {
078: int col = content.getView()
079: .getColumnIndex(
080: content.getUpdateModel()
081: .getRowIdentifier(j)
082: .getReference());
083: if (col == -1)
084: col = content.getView().getColumnIndex(
085: content.getUpdateModel()
086: .getRowIdentifier(j).getName());
087:
088: Object cell = rowdata[col];
089: if (cell instanceof Object[])
090: cell = ((Object[]) cell)[1];
091: whereValues.addElement(toJdbcValue(cell, j));
092: }
093:
094: if (handler.type.equals(ContentChanges.DELETE)) {
095: sql = content.getUpdateModel().getDeleteSyntax(
096: whereValues.toArray());
097: } else if (handler.type.equals(ContentChanges.UPDATE)) {
098: for (int j = 0; j < rowdata.length; j++) {
099: if (rowdata[j] instanceof Object[]) {
100: Object cell = ((Object[]) rowdata[j])[0];
101: values.addElement(toJdbcValue(cell, j));
102:
103: // String cname = content.getView().getColumnName(j);
104: // columns.addElement(SQLFormatter.ensureQuotes(cname,ch.getObject("$identifierQuoteString").toString(),false));
105: columns.addElement(content.getQueryModel()
106: .getQueryExpression()
107: .getQuerySpecification()
108: .getSelectList()[j].toString());
109: }
110: }
111: sql = content.getUpdateModel()
112: .getUpdateSyntax(
113: (String[]) columns
114: .toArray(new String[columns
115: .size()]),
116: values.toArray(),
117: whereValues.toArray());
118: }
119: }
120: syntaxes.append(sql.trim() + ";\n");
121: }
122: }
123:
124: private String toJdbcValue(Object value, int col) {
125: return SQLFormatter.toJdbcValue(value, content.getView()
126: .getColumnType(col));
127: }
128: }
|