코드 리팩토링 (ExcecuteSQL -> ExecuteDMLCommand로 이름수정)으로 관련 코드 수정.
[Tadpole.git] / com.hangum.tadpole.rdb.core / src / com / hangum / tadpole / rdb / core / dialog / table / mysql / MySQLTaableCreateDialog.java
blobed005d877cdd486bd720dd443a92afb7c9a6b711
1 /*******************************************************************************
2 * Copyright (c) 2016 hangum.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Lesser Public License v2.1
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
7 *
8 * Contributors:
9 * hangum - initial API and implementation
10 ******************************************************************************/
11 package com.hangum.tadpole.rdb.core.dialog.table.mysql;
13 import java.util.Map;
15 import org.apache.commons.lang.StringUtils;
16 import org.apache.log4j.Logger;
17 import org.eclipse.jface.dialogs.IDialogConstants;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.jface.dialogs.TitleAreaDialog;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.graphics.Point;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Combo;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.swt.widgets.Text;
32 import org.eclipse.ui.PlatformUI;
34 import com.hangum.tadpole.commons.google.analytics.AnalyticCaller;
35 import com.hangum.tadpole.commons.libs.core.message.CommonMessages;
36 import com.hangum.tadpole.commons.util.GlobalImageUtils;
37 import com.hangum.tadpole.engine.query.dao.system.UserDBDAO;
38 import com.hangum.tadpole.engine.sql.template.MySQLDMLTemplate;
39 import com.hangum.tadpole.engine.sql.util.ExecuteDDLCommand;
40 import com.hangum.tadpole.engine.sql.util.ExecuteDMLCommand;
41 import com.hangum.tadpole.engine.sql.util.resultset.QueryExecuteResultDTO;
42 import com.hangum.tadpole.engine.sql.util.resultset.TadpoleResultSet;
43 import com.hangum.tadpole.engine.utils.RequestQueryUtil;
44 import com.hangum.tadpole.rdb.core.Messages;
45 import com.hangum.tadpole.rdb.core.dialog.msg.TDBErroDialog;
46 import com.hangum.tadpole.rdb.core.viewers.object.ExplorerViewer;
48 /**
49 * 테이블 생성 다이얼로그
51 * @author hangum
54 public class MySQLTaableCreateDialog extends TitleAreaDialog {
55 private static final Logger logger = Logger.getLogger(MySQLTaableCreateDialog.class);
57 /** 테이블 생성이 정상 되었는지 채크하기 위함 */
58 private boolean isCreated = false;
60 private TableCreateDAO tableCreateDao;
62 private UserDBDAO userDB;
63 private Text textTableName;
64 private Combo comboTableEncoding;
65 private Combo comboTableCollation;
66 private Combo comboTableType;
68 /**
69 * Create the dialog.
70 * @param parentShell
72 public MySQLTaableCreateDialog(Shell parentShell, UserDBDAO userDB) {
73 super(parentShell);
74 setShellStyle(SWT.SHELL_TRIM | SWT.BORDER | SWT.MAX | SWT.RESIZE | SWT.TITLE);
76 this.userDB = userDB;
79 @Override
80 protected void configureShell(Shell newShell) {
81 super.configureShell(newShell);
83 newShell.setText(Messages.get().MySQLTaableCreateDialog);
84 newShell.setImage(GlobalImageUtils.getTadpoleIcon());
87 /**
88 * Create contents of the dialog.
89 * @param parent
91 @Override
92 protected Control createDialogArea(Composite parent) {
93 setTitle(Messages.get().MySQLTaableCreateDialog);
95 Composite area = (Composite) super.createDialogArea(parent);
96 Composite container = new Composite(area, SWT.NONE);
97 container.setLayout(new GridLayout(2, false));
98 container.setLayoutData(new GridData(GridData.FILL_BOTH));
100 Label lblTableName = new Label(container, SWT.NONE);
101 lblTableName.setText(Messages.get().TableName);
103 textTableName = new Text(container, SWT.BORDER);
104 textTableName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
106 Label lblTableEncoding = new Label(container, SWT.NONE);
107 lblTableEncoding.setText(Messages.get().TableEncoding);
109 comboTableEncoding = new Combo(container, SWT.NONE | SWT.READ_ONLY);
110 comboTableEncoding.addSelectionListener(new SelectionAdapter() {
111 @Override
112 public void widgetSelected(SelectionEvent e) {
113 changeEncoding();
116 comboTableEncoding.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
118 Label lblTableCollation = new Label(container, SWT.NONE | SWT.READ_ONLY);
119 lblTableCollation.setText(Messages.get().TableCollation);
121 comboTableCollation = new Combo(container, SWT.NONE | SWT.READ_ONLY);
122 comboTableCollation.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
124 Label lblTableType = new Label(container, SWT.NONE);
125 lblTableType.setText(Messages.get().TableType);
127 comboTableType = new Combo(container, SWT.NONE | SWT.READ_ONLY);
128 comboTableType.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
130 initUI();
132 // google analytic
133 AnalyticCaller.track(this.getClass().getName());
135 return area;
139 * change encoding
141 private void changeEncoding() {
142 comboTableCollation.removeAll();
144 Map<Integer, Object> selColumnData = (Map<Integer, Object>)comboTableEncoding.getData(comboTableEncoding.getText());
145 if(selColumnData != null) {
146 try {
147 // 으로 가져와서 comboTableEncoding 에 설정해 준다.
148 QueryExecuteResultDTO showCharacterSet = ExecuteDMLCommand.executeQuery(userDB,
149 String.format("SELECT * FROM information_schema.collations WHERE character_set_name = '%s' ORDER BY collation_name ASC", selColumnData.get(0)), 0, 100);
150 for (Map<Integer, Object> columnData : showCharacterSet.getDataList().getData()) {
151 comboTableCollation.add(""+columnData.get(0));
152 comboTableCollation.setData(""+columnData.get(0), ""+columnData.get(0));
154 comboTableCollation.select(0);
155 } catch(Exception e) {
156 logger.error("init table encoding ui", e);
163 * initialize UI
165 private void initUI() {
166 try {
168 * default collation
169 SHOW VARIABLES LIKE 'collation_database'
171 String strDefaultCollation = "";
172 QueryExecuteResultDTO showCollationDatabase = ExecuteDMLCommand.executeQuery(userDB, "SHOW VARIABLES LIKE 'collation_database'", 0, 10);
173 for (Map<Integer, Object> columnData : showCollationDatabase.getDataList().getData()) {
174 strDefaultCollation = ""+columnData.get(1);
177 // 으로 가져와서 comboTableEncoding 에 설정해 준다.
178 QueryExecuteResultDTO showCharacterSet = ExecuteDMLCommand.executeQuery(userDB, "SELECT * FROM information_schema.character_sets ORDER BY character_set_name ASC", 0, 100);
179 for (Map<Integer, Object> columnData : showCharacterSet.getDataList().getData()) {
180 String strViewData = String.format("%s (%s)", columnData.get(2), columnData.get(1));
181 if(StringUtils.startsWithIgnoreCase(""+columnData.get(1), strDefaultCollation)) {
182 strDefaultCollation = strViewData;
184 comboTableEncoding.add(strViewData);
185 comboTableEncoding.setData(strViewData, columnData);
187 comboTableEncoding.setText(strDefaultCollation);
189 // default database encoding
190 changeEncoding();
193 * default engine
195 TadpoleResultSet tdbEngine = ExecuteDMLCommand.executeQuery(userDB, "SELECT engine, support, comment FROM information_schema.engines WHERE support IN ('DEFAULT', 'YES')", 0, 20).getDataList();
196 String strDefaultEngine = "";
197 for (Map<Integer, Object> mapColumnData : tdbEngine.getData()) {
198 String strViewData = ""+mapColumnData.get(1);
199 if(StringUtils.startsWithIgnoreCase(strViewData, "default")) {
200 strViewData = String.format("%s (%s)", mapColumnData.get(1), mapColumnData.get(0));
201 strDefaultEngine = strViewData;
202 } else {
203 strViewData = ""+mapColumnData.get(0);
205 comboTableType.add(strViewData);
206 comboTableType.setData(strViewData, ""+mapColumnData.get(0));
208 comboTableType.setText(strDefaultEngine);
210 } catch (Exception e) {
211 logger.error("init table create ui", e);
214 textTableName.setFocus();
217 @Override
218 protected void okPressed() {
219 String strTableName = StringUtils.trimToEmpty(textTableName.getText());
220 if("".equals(strTableName)) {
221 MessageDialog.openWarning(null, CommonMessages.get().Warning, Messages.get().TableCreationNameAlter);
222 textTableName.setFocus();
223 return;
226 tableCreateDao = new TableCreateDAO();
227 tableCreateDao.setName(strTableName);
228 Map<Integer, Object> selEncodingData = (Map<Integer, Object>)comboTableEncoding.getData(comboTableEncoding.getText());
229 tableCreateDao.setEncoding(""+selEncodingData.get(0));
230 tableCreateDao.setCollation(""+comboTableCollation.getData(comboTableCollation.getText()));
231 tableCreateDao.setType(""+comboTableType.getData(comboTableType.getText()));
233 if(MessageDialog.openConfirm(null, CommonMessages.get().Confirm, Messages.get().TableCreationWantToCreate)) {
234 String strSQL = String.format(MySQLDMLTemplate.TMP_DIALOG_CREATE_TABLE,
235 tableCreateDao.getFullName(userDB),
236 tableCreateDao.getEncoding(),
237 tableCreateDao.getCollation(),
238 tableCreateDao.getType());
240 try {
241 ExecuteDDLCommand.executSQL(RequestQueryUtil.simpleRequestQuery(userDB, strSQL));
243 // 테이블이
244 isCreated = true;
246 ExplorerViewer ev = (ExplorerViewer)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(ExplorerViewer.ID);
247 if(ev != null) ev.refreshTable(true, strTableName);
249 super.okPressed();
250 } catch (Exception e) {
251 logger.error("table create exception", e); //$NON-NLS-1$
253 TDBErroDialog errDialog = new TDBErroDialog(null, Messages.get().ObjectDeleteAction_25, Messages.get().TableCreationError + e.getMessage());
254 errDialog.open();
256 textTableName.setFocus();
262 * Create contents of the button bar.
263 * @param parent
265 @Override
266 protected void createButtonsForButtonBar(Composite parent) {
267 createButton(parent, IDialogConstants.OK_ID, CommonMessages.get().Confirm, true);
268 createButton(parent, IDialogConstants.CANCEL_ID, CommonMessages.get().Cancel, false);
272 * Return the initial size of the dialog.
274 @Override
275 protected Point getInitialSize() {
276 return new Point(450, 300);
280 * get table create
281 * @return
283 public TableCreateDAO getTableCreateDao() {
284 return tableCreateDao;
288 * 테이블이 정상생성 되었는지 구분
289 * @return
291 public boolean isCreated() {
292 return isCreated;