001: /* ====================================================================
002: * The QueryForm License, Version 1.1
003: *
004: * Copyright (c) 1998 - 2004 David F. Glasser. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by
022: * David F. Glasser."
023: * Alternately, this acknowledgment may appear in the software itself,
024: * if and wherever such third-party acknowledgments normally appear.
025: *
026: * 4. The names "QueryForm" and "David F. Glasser" must
027: * not be used to endorse or promote products derived from this
028: * software without prior written permission. For written
029: * permission, please contact dglasser@pobox.com.
030: *
031: * 5. Products derived from this software may not be called "QueryForm",
032: * nor may "QueryForm" appear in their name, without prior written
033: * permission of David F. Glasser.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL DAVID F. GLASSER, THE APACHE SOFTWARE
039: * FOUNDATION OR ITS CONTRIBUTORS, OR ANY AUTHORS OR DISTRIBUTORS
040: * OF THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This product includes software developed by the
051: * Apache Software Foundation (http://www.apache.org/).
052: *
053: * ====================================================================
054: *
055: * $Source: /cvsroot/qform/qform/src/org/glasser/qform/DDLPanel.java,v $
056: * $Revision: 1.2 $
057: * $Author: dglasser $
058: * $Date: 2005/04/30 16:25:11 $
059: *
060: * --------------------------------------------------------------------
061: */
062:
063: package org.glasser.qform;
064:
065: import java.sql.*;
066: import org.glasser.sql.*;
067: import org.glasser.swing.*;
068: import javax.swing.*;
069: import javax.swing.border.*;
070: import java.awt.*;
071: import java.awt.event.*;
072: import java.util.*;
073:
074: /**
075: * This panel displays the CREATE TABLE statement that can be used to create
076: * a table, or a table based on the selected view.
077: */
078: public class DDLPanel extends JPanel implements ActionListener {
079:
080: JTextArea textArea = new JTextArea();
081:
082: ConfigPanel configPanel = new ConfigPanel();
083:
084: TableInfo tableInfo = null;
085:
086: public DDLPanel(TableInfo tableInfo) {
087: this .tableInfo = tableInfo;
088: setBorder(new EmptyBorder(10, 10, 10, 10));
089: setLayout(new BorderLayout());
090: textArea.setEditable(false);
091: add(new JScrollPane(textArea), BorderLayout.CENTER);
092: add(configPanel, BorderLayout.EAST);
093: updateSQL();
094: textArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
095: }
096:
097: void updateSQL() {
098:
099: textArea
100: .setText(buildSQL(tableInfo, true, '"', '"',
101: configPanel.getUpperCaseKeyword(), configPanel
102: .getTableNameCase(), configPanel
103: .getColumnCase(), configPanel
104: .getDataTypeCase()));
105:
106: textArea.setCaretPosition(0);
107: }
108:
109: public void actionPerformed(ActionEvent e) {
110: updateSQL();
111: }
112:
113: static String ENDL = "\n"; //System.getProperty("line.separator");
114:
115: public static String buildSQL(TableInfo tableInfo,
116: boolean qualifyTableName, char openQuote, char closeQuote,
117: boolean keywordCase, int tableNameCase, int columnNameCase,
118: int dataTypeCase) {
119:
120: StringBuffer buffer = new StringBuffer(1024);
121: buffer.append(shift("CREATE TABLE ", keywordCase));
122: if (qualifyTableName) {
123: buffer.append(shift(tableInfo.getQualifiedTableName(
124: openQuote, closeQuote), tableNameCase));
125: } else {
126: buffer.append(shift(tableInfo.maybeQuoteTableName(
127: openQuote, closeQuote), tableNameCase));
128: }
129: buffer.append(" (").append(ENDL);
130:
131: Column[] columns = tableInfo.getColumns();
132: ArrayList pks = new ArrayList();
133: for (int j = 0; columns != null && j < columns.length; j++) {
134: Column col = columns[j];
135: boolean isPK = col.getPkComponent();
136: if (isPK)
137: pks.add(shift(col.maybeQuoteColumnName(openQuote,
138: closeQuote), columnNameCase));
139: buffer.append(" ");
140: buffer.append(
141: shift(col.maybeQuoteColumnName(openQuote,
142: closeQuote), columnNameCase)).append(" ");
143: String colType = shift(col.getTypeName(), dataTypeCase);
144: buffer.append(colType);
145: int sqlType = col.getDataType();
146: if (DBUtil.isCharType(sqlType)) {
147: buffer.append("(").append(col.getColumnSize()).append(
148: ")");
149: } else if (DBUtil.isNumericType(sqlType)) {
150: if (sqlType == Types.NUMERIC
151: || sqlType == Types.DECIMAL) {
152: buffer.append("(").append(col.getColumnSize())
153: .append(", ");
154: buffer.append(col.getDecimalDigits()).append(")");
155: }
156: }
157:
158: if (col.getNullable() == false) {
159: buffer.append(shift(" NOT NULL", keywordCase));
160: }
161:
162: String def = col.getColumnDefaultValue();
163: if (def != null && (def = def.trim()).length() > 0) {
164: buffer.append(shift(" DEFAULT ", keywordCase));
165: if (DBUtil.isCharType(sqlType)) {
166: buffer.append("'");
167: buffer.append(DBUtil.escape(def));
168: buffer.append("'");
169: } else {
170: buffer.append(def);
171: }
172: }
173:
174: if (j == columns.length - 1) { // last column
175: if (pks.size() > 0) {
176: buffer.append(",");
177: }
178: } else {
179: buffer.append(",");
180: }
181:
182: buffer.append(ENDL);
183: }
184:
185: if (pks.size() > 0) {
186: buffer.append(shift(" PRIMARY KEY (", keywordCase));
187: for (int j = 0; j < pks.size(); j++) {
188: if (j > 0) {
189: buffer.append(", ");
190: }
191: buffer.append(pks.get(j));
192: }
193: buffer.append(")").append(ENDL);
194: }
195:
196: buffer.append(");");
197:
198: return buffer.toString();
199: }
200:
201: static String shift(String s, boolean b) {
202: if (s == null) {
203: return s;
204: } else if (b) {
205: return s.toUpperCase();
206: } else {
207: return s.toLowerCase();
208: }
209: }
210:
211: static String shift(String s, int n) {
212: if (s == null || n == 0) {
213: return s;
214: } else if (n > 0) {
215: return s.toUpperCase();
216: } else {
217: return s.toLowerCase();
218: }
219: }
220:
221: class ConfigPanel extends JPanel {
222:
223: String[] caseChoices = { "Preserve", "Force to lower",
224: "Force to UPPER" };
225: int[] caseStrategies = { 0, -1, 1 };
226:
227: JComboBox cmbKeywordCase = new JComboBox(new String[] {
228: "UPPER", "lower" });
229: JComboBox cmbColumnCase = new JComboBox(caseChoices);
230: JComboBox cmbTableNameCase = new JComboBox(caseChoices);
231: JComboBox cmbDataTypeCase = new JComboBox(caseChoices);
232:
233: boolean getUpperCaseKeyword() {
234: return cmbKeywordCase.getSelectedIndex() == 0;
235: }
236:
237: int getColumnCase() {
238: return caseStrategies[cmbColumnCase.getSelectedIndex()];
239: }
240:
241: int getTableNameCase() {
242: return caseStrategies[cmbTableNameCase.getSelectedIndex()];
243: }
244:
245: int getDataTypeCase() {
246: return caseStrategies[cmbDataTypeCase.getSelectedIndex()];
247: }
248:
249: Object[][] config = {
250: {
251: cmbKeywordCase,
252: "Keyword case",
253: "The case (upper or lower) to be used for SQL keywords.",
254: "KEYWORD_CASE" },
255: { cmbTableNameCase, "Table name case",
256: "The case to be used for table names.",
257: "TABLE_NAME_CASE" },
258: { cmbColumnCase, "Column name case",
259: "The case to be used for column names.",
260: "COLUMN_NAME_CASE" },
261: { cmbDataTypeCase, "Data type case",
262: "The case to be used for data type names.",
263: "DATA_TYPE_CASE" }
264:
265: };
266:
267: public ConfigPanel() {
268: setBorder(new EmptyBorder(0, 10, 0, 0));
269: GUIHelper.buildFormPanel(this , config, 50, 5, null, null,
270: false, -1);
271:
272: for (int j = 0; j < config.length; j++) {
273: JComboBox cmb = (JComboBox) config[j][0];
274: cmb.setActionCommand((String) config[j][3]);
275: cmb.addActionListener(DDLPanel.this );
276: }
277: }
278:
279: public String toString() {
280: return "keyword=" + this .cmbKeywordCase.getSelectedItem()
281: + ", column="
282: + this .cmbColumnCase.getSelectedItem()
283: + ", tableName="
284: + this.cmbTableNameCase.getSelectedItem();
285: }
286:
287: }
288:
289: }
|