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