[Jiemamy-notify:1705] commit [2896] INSERT文の出力に対応。

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2009年 3月 17日 (火) 21:36:50 JST


Revision: 2896
          http://svn.sourceforge.jp/view?root=jiemamy&view=rev&rev=2896
Author:   daisuke_m
Date:     2009-03-17 21:36:50 +0900 (Tue, 17 Mar 2009)

Log Message:
-----------
INSERT文の出力に対応。

Modified Paths:
--------------
    artemis/trunk/jiemamy-composer/src/main/java/org/jiemamy/composer/exporter/DefaultSqlExportConfig.java
    artemis/trunk/jiemamy-composer/src/main/java/org/jiemamy/composer/exporter/SqlExporter.java
    artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/dialect/DefaultSqlEmitter.java
    eros/trunk/maven-jiemamy-plugin/src/main/java/org/jiemamy/maven/JiemamyMojo.java
    vesta/trunk/org.jiemamy.eclipse.ui/src/main/java/org/jiemamy/eclipse/composer/SqlExportWizard.java
    zeus/trunk/jiemamy-spec-core/src/main/java/org/jiemamy/dialect/EmitConfig.java

Added Paths:
-----------
    vesta/trunk/org.jiemamy.eclipse.ui/src/main/java/org/jiemamy/eclipse/composer/SqlExportWizardPage.java


-------------- next part --------------
Modified: artemis/trunk/jiemamy-composer/src/main/java/org/jiemamy/composer/exporter/DefaultSqlExportConfig.java
===================================================================
--- artemis/trunk/jiemamy-composer/src/main/java/org/jiemamy/composer/exporter/DefaultSqlExportConfig.java	2009-03-17 11:56:32 UTC (rev 2895)
+++ artemis/trunk/jiemamy-composer/src/main/java/org/jiemamy/composer/exporter/DefaultSqlExportConfig.java	2009-03-17 12:36:50 UTC (rev 2896)
@@ -28,34 +28,35 @@
 	/** CREATE文の前にDROP文を出力するかどうか */
 	private boolean emitDropStatements;
 	
-	/** INSERT文を出力するかどうか */
-	private boolean emitInsertStatements;
+	/** 出力するデータセットのインデックス */
+	private int dataSetIndex;
 	
 
 	public boolean emitDropStatements() {
 		return emitDropStatements;
 	}
 	
-	public boolean emitInsertStatements() {
-		return emitInsertStatements;
+	public int getDataSetIndex() {
+		return dataSetIndex;
 	}
 	
 	/**
-	 * CREATE文の前にDROP文を出力するかどうかを設定する。
+	 * 出力するデータセットのインデックスを設定する。
 	 * 
-	 * @param emitDropStatements 出力する場合は{@code true}、そうでない場合は{@code false}
+	 * <p>INSERT文を出力しない場合は、負数を設定する。</p>
+	 * 
+	 * @param dataSetIndex 出力するデータセットのインデックス
 	 */
-	public void setEmitDropStatements(boolean emitDropStatements) {
-		this.emitDropStatements = emitDropStatements;
+	public void setDataSetIndex(int dataSetIndex) {
+		this.dataSetIndex = dataSetIndex;
 	}
 	
 	/**
-	 * INSERT文を出力するかどうかを設定する。
+	 * CREATE文の前にDROP文を出力するかどうかを設定する。
 	 * 
-	 * @param emitInsertStatements 出力する場合は{@code true}、そうでない場合は{@code false}
+	 * @param emitDropStatements 出力する場合は{@code true}、そうでない場合は{@code false}
 	 */
-	public void setEmitInsertStatements(boolean emitInsertStatements) {
-		this.emitInsertStatements = emitInsertStatements;
+	public void setEmitDropStatements(boolean emitDropStatements) {
+		this.emitDropStatements = emitDropStatements;
 	}
-	
 }

Modified: artemis/trunk/jiemamy-composer/src/main/java/org/jiemamy/composer/exporter/SqlExporter.java
===================================================================
--- artemis/trunk/jiemamy-composer/src/main/java/org/jiemamy/composer/exporter/SqlExporter.java	2009-03-17 11:56:32 UTC (rev 2895)
+++ artemis/trunk/jiemamy-composer/src/main/java/org/jiemamy/composer/exporter/SqlExporter.java	2009-03-17 12:36:50 UTC (rev 2896)
@@ -52,6 +52,9 @@
 	/** ConfigKey: 出力ファイル名 (String) */
 	public static final String OUTPUT_FILE = "outputFile";
 	
+	/** ConfigKey: 出力データセット番号 (Integer) */
+	public static final String DATA_SET_INDEX = "dataSetIndex";
+	
 
 	public boolean exportModel(RootModel rootModel, SqlExportConfig config) throws ExportException {
 		Validate.notNull(rootModel);

Modified: artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/dialect/DefaultSqlEmitter.java
===================================================================
--- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/dialect/DefaultSqlEmitter.java	2009-03-17 11:56:32 UTC (rev 2895)
+++ artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/dialect/DefaultSqlEmitter.java	2009-03-17 12:36:50 UTC (rev 2896)
@@ -19,9 +19,11 @@
 package org.jiemamy.dialect;
 
 import java.util.List;
+import java.util.Map.Entry;
 
 import org.apache.commons.lang.StringUtils;
 
+import org.jiemamy.JiemamyFactory;
 import org.jiemamy.ReferenceResolver;
 import org.jiemamy.model.RootModel;
 import org.jiemamy.model.attribute.AttributeModel;
@@ -36,10 +38,13 @@
 import org.jiemamy.model.attribute.constraint.PrimaryKey;
 import org.jiemamy.model.attribute.constraint.TableCheckConstraint;
 import org.jiemamy.model.attribute.constraint.UniqueKey;
+import org.jiemamy.model.dataset.DataSetModel;
+import org.jiemamy.model.dataset.RecordModel;
 import org.jiemamy.model.datatype.BuiltinDataType;
 import org.jiemamy.model.datatype.LiteralType;
 import org.jiemamy.model.entity.EntityModel;
 import org.jiemamy.model.entity.TableModel;
+import org.jiemamy.model.entity.TableRef;
 import org.jiemamy.model.entity.ViewModel;
 import org.jiemamy.model.sql.Identifier;
 import org.jiemamy.model.sql.Keyword;
@@ -52,6 +57,7 @@
 import org.jiemamy.utils.EntityDependencyCalculator;
 import org.jiemamy.utils.model.DataTypeUtil;
 import org.jiemamy.utils.model.ForeignKeyUtil;
+import org.jiemamy.utils.model.TableUtil;
 
 /**
  * {@link SqlEmitter}の標準実装クラス。
@@ -99,6 +105,20 @@
 			}
 		}
 		
+		int dataSetIndex = config.getDataSetIndex();
+		if (dataSetIndex >= 0 && dataSetIndex < rootModel.getDataSets().size()) {
+			ReferenceResolver referenceResolver = rootModel.getJiemamy().getReferenceResolver();
+			DataSetModel dataSetModel = rootModel.getDataSets().get(dataSetIndex);
+			for (Entry<TableRef, List<RecordModel>> entry : dataSetModel.getRecords().entrySet()) {
+				TableModel tableModel = referenceResolver.resolve(entry.getKey());
+				List<RecordModel> records = entry.getValue();
+				
+				for (RecordModel recordModel : records) {
+					result.add(emitDml(tableModel, recordModel));
+				}
+			}
+		}
+		
 		if (StringUtils.isEmpty(rootModel.getEndScript()) == false) {
 			result.add(new SqlStatementImpl(Literal.of(rootModel.getEndScript(), LiteralType.FRAGMENT)));
 		}
@@ -132,6 +152,48 @@
 		return new SqlStatementImpl(tokens);
 	}
 	
+	/**
+	 * TODO for daisuke
+	 * 
+	 * @param tableModel
+	 * @param records
+	 * @return
+	 */
+	private SqlStatement emitDml(TableModel tableModel, RecordModel records) {
+		JiemamyFactory factory = tableModel.getJiemamy().getFactory();
+		List<Token> tokens = CollectionsUtil.newArrayList();
+		tokens.add(Keyword.INSERT);
+		tokens.add(Keyword.INTO);
+		tokens.add(Identifier.of(tableModel.getName()));
+		
+		List<Token> columnList = CollectionsUtil.newArrayList();
+		List<Token> dataList = CollectionsUtil.newArrayList();
+		columnList.add(Separator.LEFT_PAREN);
+		dataList.add(Separator.LEFT_PAREN);
+		
+		List<ColumnModel> columns = TableUtil.getColumns(tableModel);
+		for (ColumnModel columnModel : columns) {
+			columnList.add(Identifier.of(columnModel.getName()));
+			columnList.add(Separator.COMMA);
+			
+			String string = records.getValues().get(factory.newReference(columnModel));
+			// FIXME Stringとは限らない。
+			dataList.add(Literal.of(string));
+			dataList.add(Separator.COMMA);
+		}
+		columnList.remove(columnList.size() - 1);
+		dataList.remove(dataList.size() - 1);
+		columnList.add(Separator.RIGHT_PAREN);
+		dataList.add(Separator.RIGHT_PAREN);
+		
+		tokens.addAll(columnList);
+		tokens.add(Keyword.VALUES);
+		tokens.addAll(dataList);
+		tokens.add(Separator.SEMICOLON);
+		
+		return new SqlStatementImpl(tokens);
+	}
+	
 
 	/**
 	 * 属性の出力戦略。

Modified: eros/trunk/maven-jiemamy-plugin/src/main/java/org/jiemamy/maven/JiemamyMojo.java
===================================================================
--- eros/trunk/maven-jiemamy-plugin/src/main/java/org/jiemamy/maven/JiemamyMojo.java	2009-03-17 11:56:32 UTC (rev 2895)
+++ eros/trunk/maven-jiemamy-plugin/src/main/java/org/jiemamy/maven/JiemamyMojo.java	2009-03-17 12:36:50 UTC (rev 2896)
@@ -76,6 +76,7 @@
 			DefaultSqlExportConfig config = new DefaultSqlExportConfig();
 			config.setOutputFile((File) parameter.get(SqlExporter.OUTPUT_FILE));
 			config.setOverwrite((Boolean) parameter.get(SqlExporter.OVERWRITE));
+			config.setDataSetIndex((Integer) parameter.get(SqlExporter.DATA_SET_INDEX));
 			
 			getLog().info("Executing Exporter...");
 			

Modified: vesta/trunk/org.jiemamy.eclipse.ui/src/main/java/org/jiemamy/eclipse/composer/SqlExportWizard.java
===================================================================
--- vesta/trunk/org.jiemamy.eclipse.ui/src/main/java/org/jiemamy/eclipse/composer/SqlExportWizard.java	2009-03-17 11:56:32 UTC (rev 2895)
+++ vesta/trunk/org.jiemamy.eclipse.ui/src/main/java/org/jiemamy/eclipse/composer/SqlExportWizard.java	2009-03-17 12:36:50 UTC (rev 2896)
@@ -19,10 +19,12 @@
 package org.jiemamy.eclipse.composer;
 
 import java.io.File;
+import java.util.List;
 
 import org.eclipse.jface.dialogs.IDialogSettings;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.wizard.Wizard;
+import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.IFileEditorInput;
 import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.IWorkbenchWizard;
@@ -31,7 +33,12 @@
 import org.jiemamy.composer.exporter.SqlExportConfig;
 import org.jiemamy.composer.exporter.SqlExporter;
 import org.jiemamy.eclipse.JiemamyUIPlugin;
+import org.jiemamy.eclipse.editor.JiemamyEditor;
 import org.jiemamy.eclipse.ui.ExporterWizard;
+import org.jiemamy.eclipse.utils.EditorUtil;
+import org.jiemamy.model.RootModel;
+import org.jiemamy.model.dataset.DataSetModel;
+import org.jiemamy.utils.CollectionsUtil;
 
 /**
  * {@link SqlExporter}の設定取得ウィザード。
@@ -42,8 +49,10 @@
 	
 	private IFileEditorInput input;
 	
-	private FileSelectWizardPage page;
+	private FileSelectWizardPage page1;
 	
+	private SqlExportWizardPage page2;
+	
 	private DefaultSqlExportConfig config;
 	
 
@@ -61,9 +70,21 @@
 	
 	@Override
 	public void addPages() {
+		IEditorPart activeEditor = EditorUtil.getActiveEditor();
+		List<String> dataSetNames = CollectionsUtil.newArrayList();
+		if (activeEditor instanceof JiemamyEditor) {
+			JiemamyEditor jiemamyEditor = (JiemamyEditor) activeEditor;
+			RootModel rootModel = jiemamyEditor.getRootModel();
+			List<DataSetModel> dataSets = rootModel.getDataSets();
+			for (DataSetModel dataSetModel : dataSets) {
+				dataSetNames.add(dataSetModel.getName());
+			}
+		}
 		super.addPages();
-		page = new FileSelectWizardPage();
-		addPage(page);
+		page1 = new FileSelectWizardPage();
+		addPage(page1);
+		page2 = new SqlExportWizardPage(dataSetNames);
+		addPage(page2);
 	}
 	
 	public SqlExportConfig getConfig() {
@@ -79,8 +100,10 @@
 	@Override
 	public boolean performFinish() {
 		config = new DefaultSqlExportConfig();
-		config.setOutputFile(new File(page.getPath()));
-		config.setOverwrite(page.getOverwrite());
+		config.setOutputFile(new File(page1.getPath()));
+		config.setOverwrite(page1.getOverwrite());
+		config.setDataSetIndex(page2.getDataSetIndex());
+		config.setEmitDropStatements(page2.getEmitDropStatements());
 		return true;
 	}
 	

Added: vesta/trunk/org.jiemamy.eclipse.ui/src/main/java/org/jiemamy/eclipse/composer/SqlExportWizardPage.java
===================================================================
--- vesta/trunk/org.jiemamy.eclipse.ui/src/main/java/org/jiemamy/eclipse/composer/SqlExportWizardPage.java	                        (rev 0)
+++ vesta/trunk/org.jiemamy.eclipse.ui/src/main/java/org/jiemamy/eclipse/composer/SqlExportWizardPage.java	2009-03-17 12:36:50 UTC (rev 2896)
@@ -0,0 +1,99 @@
+/*
+ * Copyright 2007-2009 Jiemamy Project and the Others.
+ * Created on 2009/03/17
+ *
+ * This file is part of Jiemamy.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.jiemamy.eclipse.composer;
+
+import java.util.List;
+
+import org.apache.commons.lang.StringUtils;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.wizard.WizardPage;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Combo;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Label;
+
+/**
+ * ファイルを選択するウィザードページ。
+ * 
+ * @author daisuke
+ */
+class SqlExportWizardPage extends WizardPage {
+	
+	private Button chkDropStatements;
+	
+	private Combo cmbDataSets;
+	
+	private final List<String> dataSetNames;
+	
+
+	/**
+	 * インスタンスを生成する。
+	 * 
+	 * @param dataSetNames
+	 */
+	SqlExportWizardPage(List<String> dataSetNames) {
+		super("SQL出力設定", "SQL出力設定", (ImageDescriptor) null);
+		this.dataSetNames = dataSetNames;
+	}
+	
+	public void createControl(final Composite parent) {
+		Composite composite = new Composite(parent, SWT.NULL);
+		composite.setLayout(new GridLayout(2, false));
+		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
+		
+		chkDropStatements = new Button(composite, SWT.CHECK);
+		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
+		gd.horizontalSpan = 3;
+		chkDropStatements.setLayoutData(gd);
+		chkDropStatements.setText("DROP文を出力する"); // RESOURCE
+		
+		Label label = new Label(composite, SWT.NONE);
+		label.setText("出力するデータセット:"); // RESOURCE
+		
+		cmbDataSets = new Combo(composite, SWT.READ_ONLY);
+		cmbDataSets.add(StringUtils.EMPTY);
+		for (String dataSetName : dataSetNames) {
+			cmbDataSets.add(dataSetName);
+		}
+		
+		setControl(composite);
+	}
+	
+	/**
+	 * TODO for daisuke
+	 * 
+	 * @return
+	 */
+	public int getDataSetIndex() {
+		return cmbDataSets.getSelectionIndex() - 1;
+	}
+	
+	/**
+	 * TODO for daisuke
+	 * 
+	 * @return
+	 */
+	public boolean getEmitDropStatements() {
+		return chkDropStatements.getSelection();
+	}
+	
+}


Property changes on: vesta/trunk/org.jiemamy.eclipse.ui/src/main/java/org/jiemamy/eclipse/composer/SqlExportWizardPage.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Modified: zeus/trunk/jiemamy-spec-core/src/main/java/org/jiemamy/dialect/EmitConfig.java
===================================================================
--- zeus/trunk/jiemamy-spec-core/src/main/java/org/jiemamy/dialect/EmitConfig.java	2009-03-17 11:56:32 UTC (rev 2895)
+++ zeus/trunk/jiemamy-spec-core/src/main/java/org/jiemamy/dialect/EmitConfig.java	2009-03-17 12:36:50 UTC (rev 2896)
@@ -35,11 +35,11 @@
 	boolean emitDropStatements();
 	
 	/**
-	 * INSERT文を出力するかどうかを取得する。
+	 * 出力するデータセットのインデックスを取得する。
 	 * 
-	 * @return 出力する場合は{@code true}、そうでない場合は{@code false}
+	 * @return 出力するデータセットのインデックス. 出力しない場合は負数
 	 * @since 0.2
 	 */
-	boolean emitInsertStatements();
+	int getDataSetIndex();
 	
 }



Jiemamy-notify メーリングリストの案内
Back to archive index