This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 166549
Collapse All | Expand All

(-)a/db/src/org/netbeans/modules/db/explorer/action/QueryAction.java (-43 / +100 lines)
Lines 41-47 Link Here
41
41
42
import java.sql.DatabaseMetaData;
42
import java.sql.DatabaseMetaData;
43
import java.sql.SQLException;
43
import java.sql.SQLException;
44
import java.text.MessageFormat;
45
import org.netbeans.api.db.sql.support.SQLIdentifiers;
44
import org.netbeans.api.db.sql.support.SQLIdentifiers;
46
import org.netbeans.modules.db.explorer.DatabaseConnection;
45
import org.netbeans.modules.db.explorer.DatabaseConnection;
47
import org.netbeans.modules.db.explorer.node.ColumnNode;
46
import org.netbeans.modules.db.explorer.node.ColumnNode;
Lines 83-142 Link Here
83
        return result;
82
        return result;
84
    }
83
    }
85
84
85
    /**
86
     * Used by the {@code getQualifiedTableName} method to append a catalog or a
87
     * schema into a {@code StringBuilder} that will contain the qualified table
88
     * name.  This method is run for the name of the table's catalog first and
89
     * then the table's schema.  The builder is appended if the catalog/schema
90
     * name is different from that which is the default for the database
91
     * connection.  The name part is always added if the table has already been
92
     * qualified (as defined by the {@code tableIsQualified} parameter.  This
93
     * ensures that the schema name is always appended if the catalog is
94
     * appended (when the schema is not null).  Some databases, notably MySQL,
95
     * define catalogs and not schemas.  In this case the schema name is null
96
     * and is never appended onto the table name buffer.
97
     * @param tableNameBuilder the buffer that will ultimately contain the table
98
     *                          name that is sufficiently qualified to be accessed
99
     *                          over a given database connection
100
     * @param quoter puts SQL identifiers is quotes when needed.
101
     * @param name of the catalog/schema that the table is defined under
102
     * @param authenticatedName the corresponding catalog/schema that is the
103
     *                          default for this database connection
104
     * @param tableIsQualified true when the table name has already been appended
105
     *                          and therefore should be appended to by this call
106
     *                          (assuming that {@code name} is not null).
107
     * @return true if table name buffer has been appended to or {@code tableIsQualified} is true
108
     */
109
    private boolean appendQualifiedName(
110
            StringBuilder tableNameBuilder,
111
            SQLIdentifiers.Quoter quoter,
112
            String name,
113
            String authenticatedName,
114
            boolean tableIsQualified) {
115
        if ( name!=null && (tableIsQualified ||! name.equals(authenticatedName)) ) {
116
            tableNameBuilder.append(quoter.quoteIfNeeded(name));
117
            tableNameBuilder.append('.');
118
            return true;
119
        } else {
120
            return tableIsQualified;
121
        }
122
    }
123
124
    /**
125
     * Get the table that is sufficiently specified to enable it to be used with
126
     * the given database connection.  Database connections are initialised with
127
     * catalog and a schema.  If the table is for the default schema for this
128
     * database connection then the simple table name does not need to be
129
     * qualified and {@code simpleTableName} is returned.  If the table is in a
130
     * different schema but the same catalog then the table name is qualified
131
     * with the schema and if the table's schema is in a different catalog from
132
     * the database connection's default then the table name is qualified with
133
     * the catalog and the schema names.
134
     * <p/>
135
     * The parts of a qualified table name are separated by periods (full-stops).
136
     * For example, the following selects from the city table of the sakila
137
     * schema {@code select * sakila.city}.
138
     * @param simpleTableName unqualified table name
139
     * @param connection valid database connection that SQL will be run under using the given table.
140
     * @param provider gives the catalog and the schema names that the given table name is referenced under.
141
     * @param quoter puts SQL identifiers is quotes when needed.
142
     * @return table name that is sufficiently qualified to execute against the given catalog and schema.
143
     * @throws SQLException failed to identify the default catalog for this database connection
144
     */
145
    private String getQualifiedTableName(String simpleTableName, DatabaseConnection connection, SchemaNameProvider provider, SQLIdentifiers.Quoter quoter) throws SQLException {
146
        final String schemaName = provider.getSchemaName();
147
        final String catName = provider.getCatalogName();
148
149
        StringBuilder fullTableName = new StringBuilder();
150
        boolean tableIsQualified = false;
151
152
        tableIsQualified = appendQualifiedName(fullTableName,quoter,catName,connection.getConnection().getCatalog(),tableIsQualified);
153
        tableIsQualified = appendQualifiedName(fullTableName,quoter,schemaName,connection.getSchema(),tableIsQualified);
154
        fullTableName.append(simpleTableName);
155
156
        return fullTableName.toString();
157
    }
158
86
    protected String getDefaultQuery(Node[] activatedNodes) {
159
    protected String getDefaultQuery(Node[] activatedNodes) {
87
160
88
        DatabaseConnection connection = activatedNodes[0].getLookup().lookup(DatabaseConnection.class);
161
        DatabaseConnection connection = activatedNodes[0].getLookup().lookup(DatabaseConnection.class);
89
162
90
        //org.openide.nodes.Node node = activatedNodes[0];
91
        //DatabaseNodeInfo info = (DatabaseNodeInfo) node.getCookie(DatabaseNodeInfo.class);
92
        SQLIdentifiers.Quoter quoter;
163
        SQLIdentifiers.Quoter quoter;
93
164
94
        try {
165
        try {
95
            DatabaseMetaData dmd = connection.getConnection().getMetaData();
166
            DatabaseMetaData dmd = connection.getConnection().getMetaData();
96
            quoter = SQLIdentifiers.createQuoter(dmd);
167
            quoter = SQLIdentifiers.createQuoter(dmd);
168
169
            SchemaNameProvider provider = activatedNodes[0].getLookup().lookup(SchemaNameProvider.class);
170
171
            boolean isColumn = activatedNodes[0].getLookup().lookup(ColumnNode.class) != null;
172
173
            String onome;
174
            if (!isColumn) {
175
                onome = getQualifiedTableName(activatedNodes[0].getName(),connection,provider,quoter);
176
177
                return "select * from " + onome;
178
            } else {
179
                String parentName = activatedNodes[0].getLookup().lookup(ColumnNameProvider.class).getParentName();
180
                onome = getQualifiedTableName(parentName,connection,provider,quoter);
181
182
                StringBuilder cols = new StringBuilder();
183
                for (Node node : activatedNodes) {
184
                    if (cols.length() > 0) {
185
                        cols.append(", ");
186
                    }
187
188
                    cols.append(quoter.quoteIfNeeded(node.getName()));
189
                }
190
191
                return "select " + cols.toString() + " from " + onome;
192
            }
97
        } catch (SQLException ex) {
193
        } catch (SQLException ex) {
98
            String message = NbBundle.getMessage (QueryAction.class, "ShowDataError", ex.getMessage()); // NOI18N
194
            String message = NbBundle.getMessage (QueryAction.class, "ShowDataError", ex.getMessage()); // NOI18N
99
            DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE));
195
            DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE));
100
            return "";
196
            return "";
101
        }
197
        }
102
103
        StringBuffer cols = new StringBuffer();
104
105
        SchemaNameProvider provider = activatedNodes[0].getLookup().lookup(SchemaNameProvider.class);
106
107
        String schemaName = provider.getSchemaName();
108
        String catName = provider.getCatalogName();
109
        if (schemaName == null) {
110
            schemaName = catName;
111
        }
112
113
        boolean isColumn = activatedNodes[0].getLookup().lookup(ColumnNode.class) != null;
114
115
        java.lang.String onome;
116
        if (!isColumn) {
117
            onome = quoter.quoteIfNeeded(activatedNodes[0].getName());
118
            if (!schemaName.equals("")) {
119
                onome = quoter.quoteIfNeeded(schemaName) + "." + onome;
120
            }
121
122
            return "select * from " + onome;
123
        } else {
124
            String parentName = activatedNodes[0].getLookup().lookup(ColumnNameProvider.class).getParentName();
125
            onome = quoter.quoteIfNeeded(parentName);
126
127
            if (!schemaName.equals("")) {
128
                onome = quoter.quoteIfNeeded(schemaName) + "." + onome;
129
            }
130
131
            for (Node node : activatedNodes) {
132
                if (cols.length() > 0) {
133
                    cols.append(", ");
134
                }
135
136
                cols.append(quoter.quoteIfNeeded(node.getName()));
137
            }
138
139
            return "select " + cols.toString() + " from " + onome;
140
        }
141
    }
198
    }
142
}
199
}

Return to bug 166549