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:
038: package org.netbeans.modules.jdbcwizard.builder.model;
039:
040: import org.netbeans.modules.jdbcwizard.builder.model.DBQueryModel;
041: import org.netbeans.modules.jdbcwizard.builder.util.XMLCharUtil;
042: import org.netbeans.modules.jdbcwizard.builder.dbmodel.DBColumn;
043: import org.netbeans.modules.jdbcwizard.builder.dbmodel.DBTable;
044:
045: import java.util.ArrayList;
046: import java.util.List;
047: import java.util.logging.Logger;
048: import java.util.logging.Level;
049:
050: /**
051: * @author Venkat P
052: */
053: public class JdbcQueryGenerator implements DBQueryModel {
054:
055: private String mtabName = null;
056:
057: private List mcolumns = null;
058:
059: private List mInsertColumns = null;
060:
061: private List mUpdateColumns = null;
062:
063: private List mChooseColumns = null;
064:
065: private List mPollColumns = null;
066:
067: private String mSchemaName = null;
068:
069: private static final Logger mLog = Logger
070: .getLogger(JdbcQueryGenerator.class.getName());
071:
072: private static final String INSERT_QUERY = "insertQuery";
073:
074: private static final String UPDATE_QUERY = "updateQuery";
075:
076: private JdbcQueryGenerator() {
077:
078: }
079:
080: private static final JdbcQueryGenerator instance = new JdbcQueryGenerator();
081:
082: public static final JdbcQueryGenerator getInstance() {
083: if (instance != null)
084: return instance;
085: else
086: return new JdbcQueryGenerator();
087: }
088:
089: /**
090: * @param souTable
091: */
092: public void init(final DBTable souTable) {
093: this .mtabName = souTable.getName();
094: this .mcolumns = souTable.getColumnList();
095: this .mInsertColumns = new ArrayList();
096: this .mUpdateColumns = new ArrayList();
097: this .mChooseColumns = new ArrayList();
098: this .mPollColumns = new ArrayList();
099:
100: for (int cnt = 0; cnt < this .mcolumns.size(); cnt++) {
101: final DBColumn temp = (DBColumn) this .mcolumns.get(cnt);
102: if (temp.isInsertSelected()) {
103: this .mInsertColumns.add(temp);
104: }
105: if (temp.isUpdateSelected()) {
106: this .mUpdateColumns.add(temp);
107: }
108: if (temp.isChooseSelected()) {
109: this .mChooseColumns.add(temp);
110: }
111: if (temp.isPollSelected()) {
112: this .mPollColumns.add(temp);
113: }
114: }
115: this .mSchemaName = souTable.getSchema();
116: }
117:
118: /**
119: * @return String
120: * @throws Exception
121: */
122: public String createInsertQuery() throws Exception {
123: final StringBuffer sb = new StringBuffer();
124: sb.append("insert into");
125: sb.append(" ");
126: sb.append("\"" + this .mSchemaName + "\"");
127: sb.append(".");
128: sb.append("\"" + this .mtabName + "\"");
129: sb.append(" ");
130: sb.append("(");
131: for (int i = 0; i < this .mInsertColumns.size(); i++) {
132: sb.append("\""
133: + ((DBColumn) this .mInsertColumns.get(i)).getName()
134: + "\"");
135: if (i == this .mInsertColumns.size() - 1) {
136: sb.append(")");
137: } else {
138: sb.append(",");
139: }
140: }
141: sb.append(" ");
142: sb.append("values (");
143: for (int i = 0; i < this .mInsertColumns.size(); i++) {
144: sb.append("?");
145: if (i == this .mInsertColumns.size() - 1) {
146: sb.append(")");
147: } else {
148: sb.append(",");
149: }
150: }
151: JdbcQueryGenerator.mLog.log(Level.INFO,
152: "Generated Insert Query " + sb.toString());
153: return sb.toString();
154: }
155:
156: /**
157: * @return String
158: * @throws Exception
159: */
160: public String createUpdateQuery() throws Exception {
161: final StringBuffer sb = new StringBuffer();
162: sb.append("update");
163: sb.append(" ");
164: sb.append("\"" + this .mSchemaName + "\"");
165: sb.append(".");
166: sb.append("\"" + this .mtabName + "\"");
167: sb.append(" ");
168: sb.append("set ");
169: for (int i = 0; i < this .mUpdateColumns.size(); i++) {
170: sb.append("\"" + this .mtabName + "\"");
171: sb.append(".");
172: sb.append("\""
173: + ((DBColumn) this .mUpdateColumns.get(i)).getName()
174: + "\"");
175: sb.append(" ");
176: sb.append("=");
177: sb.append(" ?");
178: if (i != this .mUpdateColumns.size() - 1) {
179: sb.append(",");
180: }
181: }
182: JdbcQueryGenerator.mLog.log(Level.INFO,
183: "Generated Update Query " + sb.toString());
184: return sb.toString();
185: }
186:
187: /**
188: * @return String
189: * @throws Exception
190: */
191: public String createDeleteQuery() throws Exception {
192: final StringBuffer sb = new StringBuffer();
193: sb.append("delete");
194: sb.append(" ");
195: sb.append("from");
196: sb.append(" ");
197: sb.append("\"" + this .mSchemaName + "\"");
198: sb.append(".");
199: sb.append("\"" + this .mtabName + "\"");
200: JdbcQueryGenerator.mLog.log(Level.INFO,
201: "Generated Delete Query " + sb.toString());
202: return sb.toString();
203: }
204:
205: /**
206: * @return String
207: * @throws Exception
208: */
209: public String createFindQuery() throws Exception {
210: final StringBuffer sb = new StringBuffer();
211: sb.append("select");
212: sb.append(" ");
213: for (int i = 0; i < this .mChooseColumns.size(); i++) {
214: sb.append("\""
215: + ((DBColumn) this .mChooseColumns.get(i)).getName()
216: + "\"");
217: if (i == this .mChooseColumns.size() - 1) {
218: sb.append(" ");
219: } else {
220: sb.append(",");
221: }
222: }
223: // selected columns
224:
225: sb.append("from");
226: sb.append(" ");
227: sb.append("\"" + this .mSchemaName + "\"");
228: sb.append(".");
229: sb.append("\"" + this .mtabName + "\"");
230:
231: JdbcQueryGenerator.mLog.log(Level.INFO, "Generated Find Query "
232: + sb.toString());
233: return sb.toString();
234: }
235:
236: /**
237: * @return String
238: * @throws Exception
239: */
240: public String createPoolQuery() throws Exception {
241: final StringBuffer sb = new StringBuffer();
242: sb.append("select");
243: sb.append(" ");
244: for (int i = 0; i < this .mPollColumns.size(); i++) {
245: sb.append("\""
246: + ((DBColumn) this .mPollColumns.get(i)).getName()
247: + "\"");
248: if (i == this .mPollColumns.size() - 1) {
249: sb.append(" ");
250: } else {
251: sb.append(",");
252: }
253: }
254: // selected columns
255: sb.append("from");
256: sb.append(" ");
257: sb.append("\"" + this .mSchemaName + "\"");
258: sb.append(".");
259: sb.append("\"" + this .mtabName + "\"");
260: JdbcQueryGenerator.mLog.log(Level.INFO, "Generated Pool Query "
261: + sb.toString());
262: return sb.toString();
263: }
264:
265: /**
266: * @return String
267: * @throws Exception
268: */
269: public String getParamOrder(final String queryType)
270: throws Exception {
271: final StringBuffer sb = new StringBuffer();
272: List tempList = null;
273: if (JdbcQueryGenerator.INSERT_QUERY.equals(queryType)) {
274: tempList = this .mInsertColumns;
275: }
276: if (JdbcQueryGenerator.UPDATE_QUERY.equals(queryType)) {
277: tempList = this .mUpdateColumns;
278: }
279: for (int i = 0; i < tempList.size(); i++) {
280: String ncName = XMLCharUtil
281: .makeValidNCName(((DBColumn) tempList.get(i))
282: .getName());
283: sb.append(ncName);
284: if (i == tempList.size() - 1) {
285: sb.append("");
286: } else {
287: sb.append(",");
288: }
289: }
290: return sb.toString();
291: }
292:
293: /**
294: * @return String
295: * @throws Exception
296: */
297: public String getPrimaryKey() throws Exception {
298: String pKey = null;
299: final java.util.Iterator pkIter = this .mcolumns.iterator();
300: while (pkIter.hasNext()) {
301: final DBColumn tc = (DBColumn) pkIter.next();
302: if (tc.isPrimaryKey()) {
303: pKey = tc.getName();
304: break;
305: }
306: }
307: return pKey;
308: }
309: }
|