#1467296 patch for bug #1454529
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / php / wizards / PHPInsertRowPage.java
1 package com.quantum.php.wizards;
2
3 import java.text.MessageFormat;
4
5 import org.eclipse.jface.preference.IPreferenceStore;
6 import org.eclipse.swt.SWT;
7 import org.eclipse.swt.dnd.TextTransfer;
8 import org.eclipse.swt.dnd.Transfer;
9 import org.eclipse.swt.events.ModifyEvent;
10 import org.eclipse.swt.events.ModifyListener;
11 import org.eclipse.swt.layout.GridData;
12 import org.eclipse.swt.layout.GridLayout;
13 import org.eclipse.swt.widgets.Composite;
14 import org.eclipse.swt.widgets.Label;
15 import org.eclipse.swt.widgets.Text;
16
17 import com.quantum.QuantumPlugin;
18 import com.quantum.adapters.DatabaseAdapter;
19 import com.quantum.model.Bookmark;
20 import com.quantum.model.Entity;
21 import com.quantum.php.PHPMessages;
22 import com.quantum.wizards.BaseSQLPage;
23 import com.quantum.wizards.SQLPage;
24
25 public class PHPInsertRowPage extends BaseSQLPage implements SQLPage {
26         String[] columnNames;
27         Text[] values;
28         Label query;
29         int numColumns = 0;
30         private IPreferenceStore fStore;
31          
32         public PHPInsertRowPage(String pageName) {
33                 super(pageName);
34         }
35
36         public void createControl(Composite parent) {
37                 System.out.println("page create control"); //$NON-NLS-1$
38                 fStore = QuantumPlugin.getDefault().getPreferenceStore();
39                 Composite container = new Composite(parent, SWT.V_SCROLL);
40                 GridLayout layout = new GridLayout();
41                 container.setLayout(layout);
42                 layout.numColumns = 2;
43
44                 columnNames = this.results.getColumnNames();
45                 values = new Text[columnNames.length];
46                 new Label(container, SWT.NULL).setText(PHPMessages.getString("PHPInsertRowPage.ColumnName")); //$NON-NLS-1$
47                 new Label(container, SWT.NULL).setText(PHPMessages.getString("PHPInsertRowPage.Value")); //$NON-NLS-1$
48                 for (int i = 0; i < columnNames.length; i++) {
49                         Label label = new Label(container, SWT.NULL);
50                         label.setText(columnNames[i]);
51                         values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
52                         GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
53                         gridData.widthHint = 150;
54                         values[i].setLayoutData(gridData);
55
56                         //values[i].setText(data[i]);
57                         values[i].addModifyListener(new ModifyListener() {
58                                 public void modifyText(ModifyEvent e) {
59                                         updateQuery();
60                                 }                               
61                         });
62                 }
63                 query = new Label(container, SWT.WRAP);
64                 GridData gridData = new GridData();
65                 gridData.horizontalSpan = layout.numColumns;
66                 gridData.horizontalAlignment = GridData.FILL;
67                 gridData.verticalAlignment = GridData.FILL;
68                 gridData.grabExcessHorizontalSpace = true;
69                 gridData.grabExcessVerticalSpace = true;
70                 query.setLayoutData(gridData);
71
72                 setControl(container);
73         updateQuery();
74        
75                 setPageComplete(true);
76         }
77         public void updateQuery() {
78                 System.out.println("Updating query"); //$NON-NLS-1$
79                 StringBuffer valuesClause = new StringBuffer();
80                 StringBuffer namesClause = new StringBuffer();
81                 Bookmark bookmark = this.results.getBookmark();
82                 Entity entity = this.results.getEntity();
83                 DatabaseAdapter adapter = bookmark.getAdapter();
84                 
85                 numColumns = 0;
86                 for (int i = 0; i < columnNames.length; i++) {
87                         String name = columnNames[i];
88                         String value = values[i].getText();
89                         if (value.length() > 0) {
90                                 if (numColumns > 0) {
91                                         valuesClause.append(", "); //$NON-NLS-1$
92                                         namesClause.append(", ");
93                                 }
94                 appendColumn(valuesClause, entity, name, adapter, value);
95                                 namesClause.append(name);       
96                                 numColumns++;
97                         }
98                 }
99                 
100 //              String query = "INSERT INTO " + this.results.getEntity().getQuotedTableName(); //$NON-NLS-1$
101 //              if (numColumns > 0) {
102 //                      query += " (" + namesClause + ")";
103 //                      query += " VALUES " + "(" + valuesClause; //$NON-NLS-1$
104 //                      query += " )"; //$NON-NLS-1$
105 //              }
106             String[] arguments = { this.results.getEntity().getQuotedTableName(), namesClause.toString(), valuesClause.toString()};
107             MessageFormat form = new MessageFormat(fStore.getString("phpeclipse.sql.insert.template"));
108         
109             String query = form.format(arguments);
110         
111             //    String query = "$results = mysql_query(\"INSERT INTO " + row.getTable() + " (";
112             //    query += fieldClause.toString() + ") ";
113             //    query += " VALUES (" + valuesClause.toString();
114             //    query += ")\");";
115                 this.query.setText(query);
116         }
117     /* (non-Javadoc)
118      * @see com.quantum.wizards.BaseSQLPage#getQueryText()
119      */
120     protected String getQueryText() {
121         return this.query.getText();
122     }
123     /* (non-Javadoc)
124      * @see com.quantum.wizards.SQLPage#performFinish()
125      */
126     public boolean performFinish() {
127         QuantumPlugin.getDefault().getSysClip().setContents(
128                         new Object[] { query.getText() },
129                         new Transfer[] { TextTransfer.getInstance()});
130       return true;
131     }
132 }
133
134 //package com.quantum.php.wizards;
135 //
136 //import java.text.MessageFormat;
137 //
138 //import org.eclipse.jface.preference.IPreferenceStore;
139 //import org.eclipse.jface.wizard.WizardPage;
140 //import org.eclipse.swt.SWT;
141 //import org.eclipse.swt.dnd.TextTransfer;
142 //import org.eclipse.swt.dnd.Transfer;
143 //import org.eclipse.swt.events.ModifyEvent;
144 //import org.eclipse.swt.events.ModifyListener;
145 //import org.eclipse.swt.layout.GridData;
146 //import org.eclipse.swt.layout.GridLayout;
147 //import org.eclipse.swt.widgets.Composite;
148 //import org.eclipse.swt.widgets.Label;
149 //import org.eclipse.swt.widgets.Text;
150 //
151 //import com.quantum.QuantumPlugin;
152 //import com.quantum.sql.TableRow;
153 //import com.quantum.wizards.BaseSQLPage;
154 //import com.quantum.wizards.SQLPage;
155 ////import com.quantum.view.PHPSourceConsole;
156 ////import com.quantum.view.tableview.TableAdapter;
157 //
158 //public class PHPInsertRowPage extends BaseSQLPage implements SQLPage {
159 //  TableRow row;
160 //  String[] columnNames;
161 //  Text[] values;
162 //  Label query;
163 //  private final static boolean DEBUG = false;
164 //  private IPreferenceStore fStore;
165 //
166 //  public PHPInsertRowPage(String pageName) {
167 //    super(pageName);
168 //  }
169 //
170 ////  public void init(TableRow row, TableAdapter adapter) {
171 ////    this.row = row;
172 ////  }
173 //
174 //  public void createControl(Composite parent) {
175 //    if (DEBUG) {
176 //      System.out.println("page create control");
177 //    }
178 //    fStore = QuantumPlugin.getDefault().getPreferenceStore();
179 //    Composite container = new Composite(parent, SWT.NULL);
180 //    GridLayout layout = new GridLayout();
181 //    container.setLayout(layout);
182 //    int layoutColumns = 2;
183 //    layout.numColumns = layoutColumns;
184 //
185 //    if (DEBUG) {
186 //      if (row == null) {
187 //        System.out.println("Row is null");
188 //      }
189 //      if (row.getColumnNames() == null) {
190 //        System.out.println("Columns are null");
191 //      }
192 //      if (row.getTableData() == null) {
193 //        System.out.println("Data is null");
194 //      }
195 //    }
196 //
197 //    columnNames = row.getColumnNames();
198 //    String[] data = row.getTableData();
199 //    if (DEBUG) {
200 //      for (int i = 0; i < row.getColumnCount(); i++) {
201 //        System.out.println("data = " + i + "=" + data[i]);
202 //        System.out.println("column = " + i + "=" + columnNames[i]);
203 //      }
204 //    }
205 //    values = new Text[row.getColumnCount()];
206 //    Label temp = new Label(container, SWT.NULL);
207 //    temp.setText("Column Name");
208 //    temp = new Label(container, SWT.NULL);
209 //    temp.setText("Value");
210 //    for (int i = 0; i < row.getColumnCount(); i++) {
211 //      Label label = new Label(container, SWT.NULL);
212 //      label.setText(columnNames[i]);
213 //      values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
214 //      GridData fullHorizontal = new GridData();
215 //      fullHorizontal.horizontalAlignment = GridData.FILL;
216 //      values[i].setLayoutData(fullHorizontal);
217 //
218 //      //values[i].setText(data[i]);
219 //      values[i].addModifyListener(new ModifyListener() {
220 //        public void modifyText(ModifyEvent e) {
221 //          updateQuery();
222 //        }
223 //      });
224 //    }
225 //    query = new Label(container, SWT.WRAP);
226 //    GridData gridData = new GridData();
227 //    gridData.horizontalSpan = layoutColumns;
228 //    gridData.horizontalAlignment = GridData.FILL;
229 //    gridData.verticalAlignment = GridData.FILL;
230 //    gridData.grabExcessHorizontalSpace = true;
231 //    gridData.grabExcessVerticalSpace = true;
232 //    query.setLayoutData(gridData);
233 //
234 //    setControl(container);
235 //    updateQuery();
236 //
237 //    setPageComplete(true);
238 //  }
239 //  public void updateQuery() {
240 //    if (DEBUG) {
241 //      System.out.println("Updating insert query");
242 //    }
243 //    StringBuffer fieldClause = new StringBuffer();
244 //
245 //    StringBuffer valuesClause = new StringBuffer();
246 //    String text;
247 //    boolean first = false;
248 //    for (int i = 0; i < columnNames.length; i++) {
249 //      text = values[i].getText();
250 //      if (!text.equals("")) {
251 //        if (first) {
252 //          valuesClause.append(", ");
253 //          fieldClause.append(", ");
254 //        }
255 //        valuesClause.append("'" + values[i].getText() + "'");
256 //        fieldClause.append(columnNames[i]);
257 //        first = true;
258 //      }
259 //    }
260 //    //    if (valuesClause.length() > 1) {
261 //    //      valuesClause.deleteCharAt(valuesClause.length() - 1);
262 //    //      valuesClause.deleteCharAt(valuesClause.length() - 1);
263 //    //    }
264 //    String[] arguments = { row.getTable(), fieldClause.toString(), valuesClause.toString()};
265 //    MessageFormat form = new MessageFormat(fStore.getString("phpeclipse.sql.insert.template"));
266 //
267 //    String query = form.format(arguments);
268 //
269 //    //    String query = "$results = mysql_query(\"INSERT INTO " + row.getTable() + " (";
270 //    //    query += fieldClause.toString() + ") ";
271 //    //    query += " VALUES (" + valuesClause.toString();
272 //    //    query += ")\");";
273 //    this.query.setText(query);
274 //  }
275 //  public boolean performFinish() {
276 ////    PHPSourceConsole console = PHPSourceConsole.getInstance();
277 ////    console.clear();
278 ////    console.print(query.getText());
279 //      QuantumPlugin.getDefault().getSysClip().setContents(
280 //                      new Object[] { query.getText() },
281 //                      new Transfer[] { TextTransfer.getInstance()});
282 //    return true;
283 //  }
284 //  /* (non-Javadoc)
285 //   * @see com.quantum.wizards.BaseSQLPage#getQueryText()
286 //   */
287 //  protected String getQueryText() {
288 //      return this.query.getText();
289 //  }
290 //}