MCPcopy Create free account
hub / github.com/duckdb/duckdb / DisplayTables

Method DisplayTables

tools/shell/shell.cpp:2134–2214  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2132}
2133
2134MetadataResult ShellState::DisplayTables(const vector<string> &args) {
2135 if (args.size() > 2) {
2136 return MetadataResult::PRINT_USAGE;
2137 }
2138 // FIXME: copy pasted from below
2139 // Parse the filter pattern to check for schema qualification
2140 string filter_pattern = args.size() > 1 ? args[1] : string();
2141 string schema_filter = "";
2142 string table_filter = "%" + filter_pattern + "%";
2143
2144 // Parse the filter pattern to check for schema qualification
2145 try {
2146 auto components = duckdb::QualifiedName::ParseComponents(filter_pattern);
2147 if (components.size() >= 2) {
2148 // e.g : "schema.table" or "schema.%"
2149 schema_filter = "%" + components[0] + "%";
2150 table_filter = "%" + components[1] + "%";
2151 }
2152 } catch (const duckdb::ParserException &) {
2153 // If parsing fails, treat as a simple table pattern
2154 }
2155 string schema_filter_str;
2156 string name_filter;
2157 if (!table_filter.empty()) {
2158 name_filter = StringUtil::Format(" AND columns.table_name ILIKE %s", SQLString(table_filter));
2159 }
2160 if (!schema_filter.empty()) {
2161 schema_filter_str = StringUtil::Format(" AND columns.schema_name ILIKE %s", SQLString(schema_filter));
2162 }
2163 auto query = StringUtil::Format(R"(
2164SELECT columns.database_name, columns.schema_name, columns.table_name, list(
2165 struct_pack(column_name, data_type, is_primary_key := c.column_index IS NOT NULL) order by column_index),
2166 t.estimated_size AS estimated_size, t.table_oid AS table_oid
2167FROM duckdb_columns() columns
2168LEFT JOIN duckdb_tables() t USING (table_oid)
2169LEFT JOIN (
2170 SELECT table_oid, UNNEST(constraint_column_indexes)+1 column_index
2171 FROM duckdb_constraints()
2172 WHERE constraint_type='PRIMARY KEY') c
2173USING (table_oid, column_index)
2174WHERE NOT columns.internal%s%s
2175GROUP BY ALL;
2176)",
2177 schema_filter_str, name_filter);
2178
2179 auto &con = *conn;
2180 auto query_result = con.Query(query);
2181 if (query_result->HasError()) {
2182 PrintDatabaseError(query_result->GetError());
2183 return MetadataResult::FAIL;
2184 }
2185 vector<ShellTableInfo> result;
2186 for (auto &row : *query_result) {
2187 ShellTableInfo table;
2188 table.database_name = row.GetValue<string>(0);
2189 table.schema_name = row.GetValue<string>(1);
2190 table.table_name = row.GetValue<string>(2);
2191

Callers 1

ShowTablesFunction · 0.80

Calls 9

SQLStringClass · 0.85
GetBaseValueMethod · 0.80
sizeMethod · 0.45
emptyMethod · 0.45
QueryMethod · 0.45
HasErrorMethod · 0.45
GetErrorMethod · 0.45
push_backMethod · 0.45
IsNullMethod · 0.45

Tested by

no test coverage detected