MCPcopy Create free account
hub / github.com/documentdb/documentdb / command_list_databases

Function command_list_databases

pg_documentdb/src/commands/db_stats.c:139–230  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

137
138
139Datum
140command_list_databases(PG_FUNCTION_ARGS)
141{
142 ReportFeatureUsage(FEATURE_COMMAND_LIST_DATABASES);
143
144 pgbson *spec = PG_GETARG_PGBSON(0);
145
146 bool nameOnly = false;
147 pgbson *filter = NULL;
148
149 bson_iter_t specIter;
150 PgbsonInitIterator(spec, &specIter);
151 while (bson_iter_next(&specIter))
152 {
153 const char *key = bson_iter_key(&specIter);
154 if (strcmp(key, "nameOnly") == 0)
155 {
156 EnsureTopLevelFieldIsBooleanLike("nameOnly", &specIter);
157 nameOnly = BsonValueAsBool(bson_iter_value(&specIter));
158 }
159 else if (strcmp(key, "filter") == 0)
160 {
161 EnsureTopLevelFieldType("filter", &specIter, BSON_TYPE_DOCUMENT);
162 filter = PgbsonInitFromDocumentBsonValue(bson_iter_value(&specIter));
163 }
164 else if (strcmp(key, "listDatabases") == 0)
165 {
166 /* ignore */
167 }
168 else if (!IsCommonSpecIgnoredField(key))
169 {
170 ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_BADVALUE),
171 errmsg("%s is an unrecognized field name", key)));
172 }
173 }
174
175 const char *sizeOnDiskSelector = "";
176 const char *totalSizeSelector = "";
177 const char *filterString = "";
178
179 if (!nameOnly)
180 {
181 sizeOnDiskSelector = ", 0::int8 AS \"sizeOnDisk\", false AS empty";
182 totalSizeSelector =
183 "pg_catalog.pg_database_size(pg_catalog.current_database())::int8 AS \"totalSize\", ";
184 }
185
186 if (filter != NULL)
187 {
188 filterString = FormatSqlQuery("WHERE document OPERATOR(%s.@@) $1",
189 ApiCatalogSchemaName);
190 }
191
192 /* We can definitely do better here. TODO: Improve size tracking etc. */
193 const char *cmdStr = FormatSqlQuery(
194 "WITH r1 AS (SELECT DISTINCT database_name AS name %s FROM %s.collections),"
195 "r2 AS (SELECT %s.row_get_bson(r1) AS document FROM r1),"
196 "r3 AS (SELECT document FROM r2 %s),"

Callers

nothing calls this directly

Calls 11

ReportFeatureUsageFunction · 0.85
PgbsonInitIteratorFunction · 0.85
BsonValueAsBoolFunction · 0.85
EnsureTopLevelFieldTypeFunction · 0.85
IsCommonSpecIgnoredFieldFunction · 0.85
FormatSqlQueryFunction · 0.85
BsonTypeIdFunction · 0.85

Tested by

no test coverage detected