| 2214 | } |
| 2215 | |
| 2216 | MetadataResult ShellState::DisplayEntries(const vector<string> &args, char type) { |
| 2217 | string s; |
| 2218 | |
| 2219 | if (args.size() > 2) { |
| 2220 | return MetadataResult::PRINT_USAGE; |
| 2221 | } |
| 2222 | |
| 2223 | // Parse the filter pattern to check for schema qualification |
| 2224 | string filter_pattern = args.size() > 1 ? args[1] : "%"; |
| 2225 | string schema_filter = ""; |
| 2226 | string table_filter = filter_pattern; |
| 2227 | |
| 2228 | // Parse the filter pattern to check for schema qualification |
| 2229 | try { |
| 2230 | auto components = duckdb::QualifiedName::ParseComponents(filter_pattern); |
| 2231 | if (components.size() >= 2) { |
| 2232 | // e.g : "schema.table" or "schema.%" |
| 2233 | schema_filter = components[0]; |
| 2234 | table_filter = components[1]; |
| 2235 | // e.g : "schema." |
| 2236 | if (table_filter.empty()) { |
| 2237 | table_filter = "%"; |
| 2238 | } |
| 2239 | } |
| 2240 | } catch (const duckdb::ParserException &) { |
| 2241 | // If parsing fails, treat as a simple table pattern |
| 2242 | schema_filter = ""; |
| 2243 | table_filter = filter_pattern; |
| 2244 | } |
| 2245 | |
| 2246 | // Use DuckDB's system tables instead of SQLite's sqlite_schema |
| 2247 | if (type == 't') { |
| 2248 | string schema_filter_str; |
| 2249 | string name_filter = "WHERE ao.name LIKE ?1"; |
| 2250 | if (!schema_filter.empty()) { |
| 2251 | schema_filter_str = "\n WHERE schema_name LIKE ?1"; |
| 2252 | name_filter = "WHERE ao.name LIKE ?2"; |
| 2253 | } |
| 2254 | s = StringUtil::Format(R"( |
| 2255 | WITH all_objects AS ( |
| 2256 | SELECT schema_name, table_name as name FROM duckdb_tables%s |
| 2257 | UNION ALL |
| 2258 | SELECT schema_name, view_name as name FROM duckdb_views%s |
| 2259 | ), |
| 2260 | name_counts AS ( |
| 2261 | SELECT name, COUNT(*) as count FROM all_objects |
| 2262 | GROUP BY name |
| 2263 | ), |
| 2264 | disambiguated AS ( |
| 2265 | SELECT |
| 2266 | CASE |
| 2267 | WHEN nc.count > 1 THEN ao.schema_name || '.' || ao.name |
| 2268 | ELSE ao.name |
| 2269 | END as display_name |
| 2270 | FROM all_objects ao |
| 2271 | JOIN name_counts nc ON ao.name = nc.name |
| 2272 | %s |
| 2273 | ) |
no test coverage detected