001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: /*
021: *
022: * Copyright 2005 Sun Microsystems, Inc.
023: *
024: * Licensed under the Apache License, Version 2.0 (the "License");
025: * you may not use this file except in compliance with the License.
026: * You may obtain a copy of the License at
027: *
028: * http://www.apache.org/licenses/LICENSE-2.0
029: *
030: * Unless required by applicable law or agreed to in writing, software
031: * distributed under the License is distributed on an "AS IS" BASIS,
032: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
033: * See the License for the specific language governing permissions and
034: * limitations under the License.
035: *
036: */
037: package org.netbeans.modules.jdbcwizard.builder.model;
038:
039: import org.netbeans.modules.jdbcwizard.builder.model.DBQueryModel;
040: import org.netbeans.modules.jdbcwizard.builder.util.XMLCharUtil;
041: import org.netbeans.modules.jdbcwizard.builder.dbmodel.DBTable;
042: import org.netbeans.modules.jdbcwizard.builder.dbmodel.DBColumn;
043: import java.util.logging.Logger;
044: import java.util.logging.Level;
045:
046: import java.util.List;
047: import java.util.ArrayList;
048:
049: /**
050: * @author Venkat P
051: */
052: public class DerbyQueryGenerator implements DBQueryModel {
053:
054: private String mtabName = null;
055:
056: private List mcolumns = null;
057:
058: private List mInsertColumns = null;
059:
060: private List mUpdateColumns = null;
061:
062: private List mChooseColumns = null;
063:
064: private List mPollColumns = null;
065:
066: private String mSchemaName = null;
067:
068: private static final String INSERT_QUERY = "insertQuery";
069:
070: private static final String UPDATE_QUERY = "updateQuery";
071:
072: private static final Logger mLog = Logger
073: .getLogger(DerbyQueryGenerator.class.getName());
074:
075: private DerbyQueryGenerator() {
076:
077: }
078:
079: private static final DerbyQueryGenerator instance = new DerbyQueryGenerator();
080:
081: public static final DerbyQueryGenerator getInstance() {
082: if (instance != null)
083: return instance;
084: else
085: return new DerbyQueryGenerator();
086: }
087:
088: /*
089: * (non-Javadoc)
090: *
091: * @see com.sun.jbi.ui.devtool.jdbc.builder.model.DBQueryModel#init(com.sun.jbi.ui.devtool.jdbc.builder.dbmodel.DBTable)
092: */
093: public void init(final DBTable souTable) {
094: this .mtabName = souTable.getName();
095: this .mcolumns = souTable.getColumnList();
096: this .mInsertColumns = new ArrayList();
097: this .mUpdateColumns = new ArrayList();
098: this .mChooseColumns = new ArrayList();
099: this .mPollColumns = new ArrayList();
100:
101: for (int cnt = 0; cnt < this .mcolumns.size(); cnt++) {
102: final DBColumn temp = (DBColumn) this .mcolumns.get(cnt);
103: if (temp.isInsertSelected()) {
104: this .mInsertColumns.add(temp);
105: }
106: if (temp.isUpdateSelected()) {
107: this .mUpdateColumns.add(temp);
108: }
109: if (temp.isChooseSelected()) {
110: this .mChooseColumns.add(temp);
111: }
112: if (temp.isPollSelected()) {
113: this .mPollColumns.add(temp);
114: }
115: }
116: this .mSchemaName = souTable.getSchema();
117: }
118:
119: /**
120: * @return String
121: * @throws Exception
122: */
123: public String createInsertQuery() throws Exception {
124: final StringBuffer sb = new StringBuffer();
125: sb.append("insert into");
126: sb.append(" ");
127: sb.append("\"" + this .mSchemaName + "\"");
128: sb.append(".");
129: sb.append("\"" + this .mtabName + "\"");
130: sb.append(" ");
131: sb.append("(");
132: for (int i = 0; i < this .mInsertColumns.size(); i++) {
133: sb.append("\""
134: + ((DBColumn) this .mInsertColumns.get(i)).getName()
135: + "\"");
136: if (i == this .mInsertColumns.size() - 1) {
137: sb.append(")");
138: } else {
139: sb.append(",");
140: }
141: }
142: sb.append(" ");
143: sb.append("values (");
144: for (int i = 0; i < this .mInsertColumns.size(); i++) {
145: sb.append("?");
146: if (i == this .mInsertColumns.size() - 1) {
147: sb.append(")");
148: } else {
149: sb.append(",");
150: }
151: }
152: DerbyQueryGenerator.mLog.log(Level.INFO,
153: "Generated Insert Query " + sb.toString());
154: return sb.toString();
155: }
156:
157: /**
158: * @return String
159: * @throws Exception
160: */
161: public String createUpdateQuery() throws Exception {
162: final StringBuffer sb = new StringBuffer();
163: sb.append("update");
164: sb.append(" ");
165: sb.append("\"" + this .mSchemaName + "\"");
166: sb.append(".");
167: sb.append("\"" + this .mtabName + "\"");
168: sb.append(" ");
169: sb.append("set ");
170: for (int i = 0; i < this .mUpdateColumns.size(); i++) {
171: sb.append("\"" + this .mtabName + "\"");
172: sb.append(".");
173: sb.append("\""
174: + ((DBColumn) this .mUpdateColumns.get(i)).getName()
175: + "\"");
176: sb.append(" ");
177: sb.append("=");
178: sb.append(" ?");
179: if (i != this .mUpdateColumns.size() - 1) {
180: sb.append(",");
181: }
182: }
183: DerbyQueryGenerator.mLog.log(Level.INFO,
184: "Generated Update Query " + sb.toString());
185: return sb.toString();
186: }
187:
188: /**
189: * @return String
190: * @throws Exception
191: */
192: public String createDeleteQuery() throws Exception {
193: final StringBuffer sb = new StringBuffer();
194: sb.append("delete");
195: sb.append(" ");
196: sb.append("from");
197: sb.append(" ");
198: sb.append("\"" + this .mSchemaName + "\"");
199: sb.append(".");
200: sb.append("\"" + this .mtabName + "\"");
201: DerbyQueryGenerator.mLog.log(Level.INFO,
202: "Generated Delete Query " + sb.toString());
203: return sb.toString();
204: }
205:
206: /**
207: * @return String
208: * @throws Exception
209: */
210: public String createFindQuery() throws Exception {
211: final StringBuffer sb = new StringBuffer();
212: sb.append("select");
213: sb.append(" ");
214:
215: for (int i = 0; i < this .mChooseColumns.size(); i++) {
216: sb.append("\""
217: + ((DBColumn) this .mChooseColumns.get(i)).getName()
218: + "\"");
219: if (i == this .mChooseColumns.size() - 1) {
220: sb.append(" ");
221: } else {
222: sb.append(",");
223: }
224: }
225: // selected columns
226:
227: sb.append("from");
228: sb.append(" ");
229: sb.append("\"" + this .mSchemaName + "\"");
230: sb.append(".");
231: sb.append("\"" + this .mtabName + "\"");
232:
233: DerbyQueryGenerator.mLog.log(Level.INFO,
234: "Generated Find Query " + sb.toString());
235: return sb.toString();
236: }
237:
238: /**
239: * @return String
240: * @throws Exception
241: */
242: public String createPoolQuery() throws Exception {
243: final StringBuffer sb = new StringBuffer();
244: sb.append("select");
245: sb.append(" ");
246: for (int i = 0; i < this .mPollColumns.size(); i++) {
247: sb.append("\""
248: + ((DBColumn) this .mPollColumns.get(i)).getName()
249: + "\"");
250: if (i == this .mPollColumns.size() - 1) {
251: sb.append(" ");
252: } else {
253: sb.append(",");
254: }
255: }
256: // selected columns
257: sb.append("from");
258: sb.append(" ");
259: sb.append("\"" + this .mSchemaName + "\"");
260: sb.append(".");
261: sb.append("\"" + this .mtabName + "\"");
262: DerbyQueryGenerator.mLog.log(Level.INFO,
263: "Generated Pool Query " + sb.toString());
264: return sb.toString();
265: }
266:
267: /**
268: * @return String
269: * @throws Exception
270: */
271: public String getParamOrder(final String queryType)
272: throws Exception {
273: final StringBuffer sb = new StringBuffer();
274: List tempList = null;
275: if (DerbyQueryGenerator.INSERT_QUERY.equals(queryType)) {
276: tempList = this .mInsertColumns;
277: }
278: if (DerbyQueryGenerator.UPDATE_QUERY.equals(queryType)) {
279: tempList = this .mUpdateColumns;
280: }
281: for (int i = 0; i < tempList.size(); i++) {
282: String ncName = XMLCharUtil
283: .makeValidNCName(((DBColumn) tempList.get(i))
284: .getName());
285: sb.append(ncName);
286: if (i == tempList.size() - 1) {
287: sb.append("");
288: } else {
289: sb.append(",");
290: }
291: }
292: return sb.toString();
293: }
294:
295: /**
296: * @return String
297: * @throws Exception
298: */
299: public String getPrimaryKey() throws Exception {
300: String pKey = null;
301: final java.util.Iterator pkIter = this .mcolumns.iterator();
302: while (pkIter.hasNext()) {
303: final DBColumn tc = (DBColumn) pkIter.next();
304: if (tc.isPrimaryKey()) {
305: pKey = tc.getName();
306: break;
307: }
308: }
309: return pKey;
310: }
311: }
|