001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.db.explorer.actions;
043:
044: import java.sql.DatabaseMetaData;
045: import java.sql.SQLException;
046: import java.text.MessageFormat;
047: import org.netbeans.api.db.sql.support.SQLIdentifiers;
048:
049: import org.openide.nodes.Node;
050: import org.openide.DialogDisplayer;
051: import org.openide.NotifyDescriptor;
052:
053: import org.netbeans.modules.db.explorer.infos.ColumnNodeInfo;
054: import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo;
055: import org.netbeans.modules.db.explorer.infos.TableNodeInfo;
056: import org.netbeans.modules.db.explorer.infos.ViewColumnNodeInfo;
057: import org.netbeans.modules.db.explorer.infos.ViewNodeInfo;
058:
059: /**
060: *
061: * @author Jim Davidson
062: */
063:
064: public class QueryAction extends DatabaseAction {
065:
066: private String quoteStr;
067:
068: protected boolean hasColumnsSelected(Node[] activatedNodes) {
069: if (activatedNodes != null)
070: if (activatedNodes.length == 1)
071: return true;
072: else if (activatedNodes.length > 0) {
073: int t = 0;
074: int v = 0;
075: for (int i = 0; i < activatedNodes.length; i++) {
076: if (activatedNodes[i]
077: .getCookie(ColumnNodeInfo.class) != null) {
078: t++;
079: continue;
080: }
081: if (activatedNodes[i]
082: .getCookie(ViewColumnNodeInfo.class) != null)
083: v++;
084: }
085: if (t != activatedNodes.length
086: && v != activatedNodes.length)
087: return false;
088: else
089: return true;
090: } else
091: return false;
092: else
093: return false;
094: }
095:
096: protected String getDefaultQuery(Node[] activatedNodes) {
097:
098: org.openide.nodes.Node node = activatedNodes[0];
099: DatabaseNodeInfo info = (DatabaseNodeInfo) node
100: .getCookie(DatabaseNodeInfo.class);
101: SQLIdentifiers.Quoter quoter;
102:
103: try {
104: DatabaseMetaData dmd = info.getConnection().getMetaData();
105: quoter = SQLIdentifiers.createQuoter(dmd);
106: } catch (SQLException ex) {
107: String message = MessageFormat.format(bundle().getString(
108: "ShowDataError"), new String[] { ex.getMessage() }); // NOI18N
109: DialogDisplayer.getDefault().notify(
110: new NotifyDescriptor.Message(message,
111: NotifyDescriptor.ERROR_MESSAGE));
112: return "";
113: }
114:
115: java.lang.StringBuffer cols = new java.lang.StringBuffer();
116:
117: java.lang.String schema = info.getSchema();
118: if (schema == null) {
119: schema = "";
120: } else {
121: schema = schema.trim();
122: }
123:
124: java.lang.String onome;
125: if (info instanceof TableNodeInfo
126: || info instanceof ViewNodeInfo) {
127: onome = quoter.quoteIfNeeded(info.getName());
128: if (!schema.equals("")) {
129: onome = quoter.quoteIfNeeded(schema) + "." + onome;
130: }
131: return "select * from " + onome;
132: } else if (info instanceof ColumnNodeInfo
133: || info instanceof ViewColumnNodeInfo) {
134: onome = quoter
135: .quoteIfNeeded((info instanceof ViewColumnNodeInfo) ? info
136: .getView()
137: : info.getTable());
138: if (!schema.equals("")) {
139: onome = quoter.quoteIfNeeded(schema) + "." + onome;
140: }
141: for (int i = 0; i < activatedNodes.length; i++) {
142: node = activatedNodes[i];
143: info = (org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo) node
144: .getCookie(org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo.class);
145: if (info instanceof org.netbeans.modules.db.explorer.infos.ColumnNodeInfo
146: || info instanceof org.netbeans.modules.db.explorer.infos.ViewColumnNodeInfo) {
147: if (cols.length() > 0) {
148: cols.append(", ");
149: }
150: cols.append(quoter.quoteIfNeeded(info.getName()));
151: }
152: }
153: return "select " + cols.toString() + " from " + onome;
154: }
155: return "";
156: }
157: }
|