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 narayanaa
052: */
053: public class MySQLQueryGenerator 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(MySQLQueryGenerator.class.getName());
071:
072: private static final String INSERT_QUERY = "insertQuery";
073:
074: private static final String UPDATE_QUERY = "updateQuery";
075:
076: private MySQLQueryGenerator() {
077:
078: }
079:
080: private static final MySQLQueryGenerator instance = new MySQLQueryGenerator();
081:
082: public static final MySQLQueryGenerator getInstance() {
083: if (instance != null)
084: return instance;
085: else
086: return new MySQLQueryGenerator();
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
133: .append(((DBColumn) this .mInsertColumns.get(i))
134: .getName());
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: MySQLQueryGenerator.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
173: .append(((DBColumn) this .mUpdateColumns.get(i))
174: .getName());
175: sb.append(" ");
176: sb.append("=");
177: sb.append(" ?");
178: if (i != this .mUpdateColumns.size() - 1) {
179: sb.append(",");
180: }
181: }
182: MySQLQueryGenerator.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: MySQLQueryGenerator.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
215: .append(((DBColumn) this .mChooseColumns.get(i))
216: .getName());
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: MySQLQueryGenerator.mLog.log(Level.INFO,
232: "Generated Find Query " + 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(((DBColumn) this .mPollColumns.get(i)).getName());
246: if (i == this .mPollColumns.size() - 1) {
247: sb.append(" ");
248: } else {
249: sb.append(",");
250: }
251: }
252: // selected columns
253: sb.append("from");
254: sb.append(" ");
255: //sb.append("\"" + this.mSchemaName + "\"");
256: //sb.append(".");
257: sb.append(this .mtabName);
258: MySQLQueryGenerator.mLog.log(Level.INFO,
259: "Generated Pool Query " + sb.toString());
260: return sb.toString();
261: }
262:
263: /**
264: * @return String
265: * @throws Exception
266: */
267: public String getParamOrder(final String queryType)
268: throws Exception {
269: final StringBuffer sb = new StringBuffer();
270: List tempList = null;
271: if (MySQLQueryGenerator.INSERT_QUERY.equals(queryType)) {
272: tempList = this .mInsertColumns;
273: }
274: if (MySQLQueryGenerator.UPDATE_QUERY.equals(queryType)) {
275: tempList = this .mUpdateColumns;
276: }
277: for (int i = 0; i < tempList.size(); i++) {
278: String ncName = XMLCharUtil
279: .makeValidNCName(((DBColumn) tempList.get(i))
280: .getName());
281: sb.append(ncName);
282: if (i == tempList.size() - 1) {
283: sb.append("");
284: } else {
285: sb.append(",");
286: }
287: }
288: return sb.toString();
289: }
290:
291: /**
292: * @return String
293: * @throws Exception
294: */
295: public String getPrimaryKey() throws Exception {
296: String pKey = null;
297: final java.util.Iterator pkIter = this .mcolumns.iterator();
298: while (pkIter.hasNext()) {
299: final DBColumn tc = (DBColumn) pkIter.next();
300: if (tc.isPrimaryKey()) {
301: pKey = tc.getName();
302: break;
303: }
304: }
305: return pKey;
306: }
307: }
|